msdd’s blog

deep learning勉強中。プログラム関連のこと書きます。

Githubのトレンドを取得するGithub Unofficial Trending API

はじめに

f:id:msdd:20200423224651p:plain

GithubTrendingをコードから取得したいと思いAPIがないか探した。 Github API v4のドキュメントでtrendingがつくものを探していたが見つからなかった。 他でも探していると、 stackoverflowでの回答や、 Githubのコミュニティフォーラム に公式のAPIはないようなことが書かれていた。 そこで、非公式のもので、Trendingを取得できるものを見つけたので、それを使ってみようと思う。

Github Unofficial Trending API

GithubAPIで不足しているTrendingを取得できる非公式API

ブラウザで試してみる

試しに、ブラウザにhttps://ghapi.huchen.dev/repositories?language=javascript&since=weeklyを入力すると、結果が表示される。 これは、言語がjavascriptで期間がweeklyつまり一週間のトレンドを取得するものである。

f:id:msdd:20200423215802p:plain
ブラウザで試した画面

json形式の結果が得られる。下が結果の一部で、リポジトリ名を確認してみると 1番目は、project_corona_trackerで、 2番目がdecentralized-video-chartということがわかる。

[
  {
    "author": "adrianhajdin",
    "name": "project_corona_tracker",
    "avatar": "https://github.com/adrianhajdin.png",
    "url": "https://github.com/adrianhajdin/project_corona_tracker",
    "description": "This is a code repository for the corresponding YouTube video. In this tutorial we are going to build and deploy a corona tracker application. Covered topics: React.js, Chart.js, Material UI and much more.",
    "language": "JavaScript",
    "languageColor": "#f1e05a",
    "stars": 341,
    "forks": 89,
    "currentPeriodStars": 178,
    "builtBy": [
      {
        "username": "adrianhajdin",
        "href": "https://github.com/adrianhajdin",
        "avatar": "https://avatars3.githubusercontent.com/u/24898559"
      }
    ]
  },
  {
    "author": "ianramzy",
    "name": "decentralized-video-chat",
    "avatar": "https://github.com/ianramzy.png",
    "url": "https://github.com/ianramzy/decentralized-video-chat",
    "description": "🚀 Zipcall.io 🚀 Peer to peer browser video calling platform with unmatched video quality and latency.",
    "language": "JavaScript",
    "languageColor": "#f1e05a",
    "stars": 1945,
    "forks": 183,

これをGithubのTrendingのページで確認してみる。上の結果と同じで、 きちんと、言語がjavascript、期間がweeklyのtrendingを取得できていることがわかる。

f:id:msdd:20200423215701p:plain

使い方

Trendingは

がある。

リポジトリのトレンドを取得

ベースとなるurl

https://ghapi.huchen.dev/repositories

にパラメータを足していく。パラメータを足すときは、urlの後ろに?をつけて、parameter名=値という形で追加していく。複数あるときは、&でつなぐ。

パラメータ名 説明
language プログラミング言語(e.g. javascript,python,c%23(c#) など)
since 期間 (daily,weekly,monthlyから選ぶ。)
spoken_language_code 話す言語(ja,enなど)

language : https://github.com/huchenme/github-trending-api/blob/master/src/languages.json
spoken_language_code : https://github.com/huchenme/github-trending-api/blob/master/src/spoken-languages.json
にあるurlParamの値。

https://ghapi.huchen.dev/repositories?since=daily&spoken_language_code=ja

結果はリスト形式で得られる。そのリストの個別のオブジェクトは下のようになっている。

{
    "author": "リポジトリのオーナ",
    "name": "リポジトリ名",
    "avatar": "アバターのurl",
    "url": "リポジトリurl",
    "description": "リポジトリ説明",
    "language": "プログラミング言語",
    "languageColor": "githubのプログラミング言語の色",
    "stars": スター数,
    "forks": フォーク数,
    "currentPeriodStars": 期間内についたスター数,
    "builtBy": [
        {
            "username": "ユーザー名",
            "href": "ユーザーurl",
            "avatar": "アバターurl"
        },
        {
            "username": "ユーザー名",
            "href": "ユーザーurl",
            "avatar": "アバターurl"
        }
    ]
}

開発者のトレンドを取得

ベースとなるurl

https://ghapi.huchen.dev/developers
パラメータ名 説明
language プログラミング言語(e.g. javascript,python,c%23(c#) など)
since 期間 (daily,weekly,monthlyから選ぶ。)

https://ghapi.huchen.dev/developers?language=javascript&since=daily

結果はリスト形式で得られる。そのリストの個別のオブジェクトは下のようになっている。

{
    "username": "ユーザー名",
    "name": "変更できる名前",
    "url": "ユーザーurl",
    "avatar": "アバターurl",
    "repo": {
        "name": "人気のリポジトリ名",
        "description": "リポジトリ説明",
        "url": "リポジトリのurl"
    }
}

pythonで実行

import urllib.request
import json


endpoint_repo="https://ghapi.huchen.dev/repositories"
endpoint_developer="https://ghapi.huchen.dev/developers"


def get_trending_repositories(language=None,\
since="daily",spoken_language_code=None):
    params=""
    params+="?since="+since

    if language is not None:
        params+=("&language="+language)

    if spoken_language_code is not None:
        params+=("&spoken_language_code="+spoken_language_code)

    url=endpoint_repo+params

    with urllib.request.urlopen(url) as res:
        body=res.read()
        j=json.loads(body)

        return j


def get_trending_developers(language=None,since="daily"):
    params=""
    params+="?since="+since

    if language is not None:
        params+=("&language="+language)

    url=endpoint_developer+params

    with urllib.request.urlopen(url) as res:
        body=res.read()
        j=json.loads(body)

        return j

レポジトリのtrendingを取得してみる。期間:今日、話し言語は:日本語、プログラミング言語:pythonで実行してみた。

def show_trending_repos(since="daily",language=None,spoken_language_code=None):
    result=get_trending_repositories(since=since,language=language,\
        spoken_language_code=spoken_language_code)
    for i,repo in enumerate(result):
        print(i,"番目")
        print("リポジトリ名:",repo["name"])
        print("url:",repo["url"])
        print()

        with open("test.json","w",encoding="utf-8") as f:
            json.dump(repo,f,indent=4)

if __name__=="__main__":
    show_trending_repos(since="monthly",language="python",\
        spoken_language_code="ja")

結果は下のようになった。trendingと一致している。trendingの数は、25個などの時もあれば、0個や1個の時もある。

0 番目
リポジトリ名: deep-learning-from-scratch
url: https://github.com/oreilly-japan/deep-learning-from-scratch

開発者のtrendingも試した。

def show_trending_developers(since="daily",language=None):
    result=get_trending_developers(since=since,language=language)
    for i,developer in enumerate(result):
        print(i,"番目")
        print("ユーザー名:",developer["username"])
        print()

        with open("test.json","w",encoding="utf-8") as f:
            json.dump(developer,f,indent=4)


if __name__=="__main__":
    show_trending_developers(since="daily",language="javascript")

結果は25個あったので省略。 きちんと現在のtrendingと同じであった。

まとめ

Github の公式APIには、Trendingを取得するものがなかった。 そこで、非公式のTrendingを取得するAPIGithub Unofficial Trending APIを使ってTrendingを取得してみた。 使い方も簡単で、きちんと動作していた。