LoginSignup
7
4

More than 5 years have passed since last update.

GitHubのClone数をAPIで取得する

Posted at

GitHubは、Clone数を「Graphs」>「Traffic」から確認できます。

Traffic_·_naitwo2_mysettings.jpg

APIでも取得できるようになった(前からできた?)ので、取得方法をメモします。

トークンの取得

まず、APIで使用するトークンを取得します。
手順は以下の記事がとてもわかり易いです。

GitHub「Personal access tokens」の設定方法 - Qiita

Clone数を取得する

以下はcurlコマンドでAPIを叩くときの記述方法です。

コマンド
curl -H "Authorization: token 【取得したToken】 " https://api.github.com/repos/【owner】/【repository】/traffic/clones
実行結果
{
  "count": 10,
  "uniques": 7,
  "clones": [
    {
      "timestamp": "2017-02-06T00:00:00Z",
      "count": 2,
      "uniques": 2
    },
    {
      "timestamp": "2017-02-07T00:00:00Z",
      "count": 3,
      "uniques": 2
    },
    {
      "timestamp": "2017-02-08T00:00:00Z",
      "count": 1,
      "uniques": 1
    },
    {
      "timestamp": "2017-02-09T00:00:00Z",
      "count": 2,
      "uniques": 1
    },
    {
      "timestamp": "2017-02-13T00:00:00Z",
      "count": 1,
      "uniques": 1
    },
    {
      "timestamp": "2017-02-15T00:00:00Z",
      "count": 1,
      "uniques": 1
    }
  ]
}



デフォルトは日単位の情報が取得できます。
URL末尾に以下の「per=week」を付加することによって、週単位に変更できます。

**traffic/clones?per=week

実行結果(Week)
{
  "count": 10,
  "uniques": 7,
  "clones": [
    {
      "timestamp": "2017-02-06T00:00:00Z",
      "count": 8,
      "uniques": 5
    },
    {
      "timestamp": "2017-02-13T00:00:00Z",
      "count": 2,
      "uniques": 2
    }
  ]
}

WebUI、APIともに14日間の情報しか保存されないので、今度グラフ化して履歴を追えるようにしたいと思います。

Releaseのダウンロード数を取得する(おまけ)

Releaseのダウンロード数等を取得するには以下の記述になります。
※ダウンロード数以外の情報も表示されます

コマンド
curl https://api.github.com/repos/【owner】/【repository】/releases
7
4
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
7
4