1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

旬のGitHubリポジトリをnushellで収集および記録する

1
Posted at
  • GitHubには膨大なリポジトリが日々公開されており、各言語のトレンドを把握するのに GitHub Search API が便利です。
  • 「最近作成された」かつ「スター数が多い」リポジトリを取得すれば、現在注目されている技術やプロジェクトをざっくりと把握できます。
  • 今回は、データをそのまま構造体として扱いフィルタ・加工・整形が得意な nushell を使って、トレンドリポジトリ情報をTSVファイルに保存する方法を紹介します。

環境

  • macOS 13.7.8 Ventura
  • nushell 0.110.0

手順

ファイル作成

$ mkdir gh-trend
$ cd gh-trend
$ touch main.nu

コード記述

main.nu に以下のコードを記述します。

# 検索期間: 現在から4週間前の日付
let since = (date now) - 4wk | format date "%Y-%m-%d"

# 出力ファイル名: 実行日付_gh_trend.tsv
let filename = (date now | format date "%Y%m%d") + "_gh_trend.tsv"

# 対象言語
let languages = ["javascript", "typescript"]

$languages | par-each { |lang|
    # 言語ごとの保存ディレクトリを作成
    mkdir $"data/($lang)"

    # GitHub Search API: 指定言語・期間内に作成されたリポジトリをスター数降順で取得
    let url = $"https://api.github.com/search/repositories?q=created:%3E($since)+language:($lang)&sort=stars&order=desc&per_page=10"

    http get $url
    | get items                                    # リポジトリ一覧を抽出
    | select name stargazers_count html_url        # 必要なカラム(リポジトリ名, スター数, URL)のみ選択
    | to tsv                                       # TSV形式に変換
    | save -f $"data/($lang)/($filename)"          # ファイルに保存(上書き)

} | ignore

実行

  • 以下のコマンドで実行します。
$ nu main.nu
  • 実行後、カレントディレクトリに data ディレクトリが生成されます。以下のような構造・内容になっていることを確認してください。
$ ls

data	main.nu

$ ls data

javascript	typescript

$ ls data/javascript
20260221_gh_trend.tsv

$ cat data/javascript/20260221_gh_trend.tsv
name	stargazers_count	html_url
XXXX	1000	https://github.com/xxxx/xxxx
XXXX	999	https://github.com/xxxx/xxxx
XXXX	999	https://github.com/xxxx/xxxx
XXXX	998	https://github.com/xxxx/xxxx
XXXX	997	https://github.com/xxxx/xxxx

まとめ

  • GitHub Search API を利用することで、膨大なリポジトリの中から注目度の高いプロジェクトを手軽に収集できます。
  • nushell と組み合わせることで、APIレスポンスをそのまま構造体として扱いつつ、少ないコード量でデータの取得・整形・保存まで行うことができます。
  • このデータを定期的に記録しておけば、トレンドの変化を継続的に追えるだけでなく、スター数の多いリポジトリのコードを読む習慣にもつながり、学習にも役立てられると感じています。
  • GitHub Actions や cron などを利用してこのスクリプトを週1回程度自動実行するようにしておくと、手動で実行する手間なくトレンドデータを蓄積し続けられるのでおすすめです。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?