1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PyGithubを使ってジョンズホプキンス大学がGitHub上に公開している新型コロナウイルス感染状況のファイルを取得するサンプル

Last updated at Posted at 2020-04-11

PyGithubを使って、GitHub上のファイルを取得するサンプル。

はじめに

世界的に関心の高い新型コロナウイルスの感染状況のデータをジョンズホプキンス大学がGitHub↓に公開しているので

から日々のデータを取得してみる。

なお既に存在するファイルの場合は取得しない。
取得してわかったけどヘッダ情報も集計データも途中で変わっちゃうんだよね。
この後のデータ項目の名寄せ処理がめんどくさそう・・・

【前提】

Python3環境が構築済であること。
開発環境構築時に以下のコマンドで利用モジュールの取得が必要。

pip install PyGithub

また、Githubにアカウントを持っていて個人用アクセスtorken を取得しておくことも必要。
取得方法は下記参照
https://help.github.com/ja/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
セキュリティ上の理由から、 GitHub は過去 1 年間使用されていない個人アクセストークンを自動的に削除しますので注意してくださいね。

以下ソースです

pythonの処理内容はコメントみればわかると思いますので特に解説不要でしょうか?

get_csse_covid_19_daily_reports.py
# -*- coding: utf-8 -*-
# 必要モジュールのインポート
import os.path
import base64
from github import Github
#  アクセストークンの作成方法は下記参照
# https://help.github.com/ja/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
token = "******your_token******"

repository = "CSSEGISandData/COVID-19"
dir_path = "csse_covid_19_data/csse_covid_19_daily_reports"

#################################################################
# 指定したディレクトリのファイルを取得
#################################################################
def get_file(dir_path):

    g = Github(token)

    repo = g.get_repo(repository)

    contents = repo.get_contents(dir_path)

    #コンテンツがなくなるまで処理
    while contents:
        file_content = contents.pop(0)

        # 指定したディレクトリの下にディレクトリが無いか念のため確認
        if file_content.type == "dir":
            contents.extend(repo.get_contents(file_content.path))
            print(file_content.path)

        else:
            # 最後の4文字が .csv のファイルのみ取得
            if file_content.path[-4:] == ".csv":

                # ローカルにファイルが存在しない場合のみ追加
                if os.path.exists(file_content.path) == False:
                    print ("ファイル追加 : " + file_content.path)
                    file_contents = repo.get_contents(file_content.path)

                    # コンテンツの中身はBase64でデコードする。理由は↓参照
                    # https://developer.github.com/v3/repos/contents/
                    content = base64.b64decode(file_contents.content)

                    # ローカルのファイルを追加
                    with open(file_content.path, mode="wb") as f:
                        f.write(content)

    return("キタ―――(゚∀゚)―――― !!")

def main():
        result = get_file(dir_path)
        print(result)

if __name__ == "__main__":
    main()

https://github.com/goodboymax/covid19 にも格納しています。

【参考】

https://haibara-works.hatenablog.com/entry/2019/10/01/132916
https://help.github.com/ja/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
https://developer.github.com/v3/repos/contents/

1
1
3

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?