LoginSignup
3
1

More than 3 years have passed since last update.

Githubのtrendリポジトリをslackに自動通知してみた

Last updated at Posted at 2020-12-15

経緯

エンジニアとして、今世界ではどんなテクノロジーが話題になっていることを知っていることは重要です。でも、それって何を見れば良いのでしょうか?自分に合った情報源を今試行錯誤しながら作っています。情報源の1つとして、Githubのtrendリポジトリを見つけました。今回は、Githubのtrendリポジトリの情報をslackに自動通知したときのやり方を共有します。基本的には、自分が前に書いたこちらの記事でやってることと同じです。

trendリポジトリとは

trendリポジトリとは、Github上のリポジトリの中で特定の期間でのスター数が多いリポジトリです。下の写真のような感じです。
Screenshot from 2020-12-12 15-33-10.png
つまり、世界中のエンジニアがこれ面白いとか、めっちゃいいねって思うリポジトリが集結しています。指定のプログラミング言語で絞ることができます。自分は、pythonとC++を普段使うのでこの2つの情報を毎日定期的にslackに通知するようにしていきます。

手順

手順を簡単にまとめるとこんな感じです。

  • trendリポジトリのページからリポジトリのタイトル、リンク、説明をスクレイピング
  • incoming webhookを使ってslackに通知
  • 上で作成したソースコードをcloud functionsにデプロイ
  • cloud schedulerで定期通知

詳細は自分が前に書いた記事で紹介しているので、今回はソースコードのみ紹介します。

main.py
import requests
import slackweb
import time
from bs4 import BeautifulSoup



def main():
    urlName = "https://github.com/trending/python?since=daily"
    url = requests.get(urlName)
    soup = BeautifulSoup(url.content, "html.parser")
    py_groups = soup.find_all('article', class_='Box-row')

    for py_group in py_groups:
        tag_size = len(py_group)
        link_part = py_group.h1.a.get("href")
        title = link_part[1:]
        link_full = "https://github.com" + link_part
        attachments_all = []
        main_msg = {"title": title, "text": link_full}
        attachments_all.append(main_msg)
        #incoming webhookのurlを入れてください
        slack = slackweb.Slack(url="")
        slack.notify(attachments=attachments_all)
        if tag_size == 9:
            abst = py_group.p.text.lstrip()
            attachments_abst = []
            abst_msg = {"text": abst}
            attachments_abst.append(abst_msg)
            slack.notify(attachments=attachments_abst)
        time.sleep(1)

    urlName = "https://github.com/trending/c++?since=daily"
    url = requests.get(urlName)
    soup = BeautifulSoup(url.content, "html.parser")
    cpp_groups = soup.find_all('article', class_='Box-row')

    for cpp_group in cpp_groups:
        tag_size = len(cpp_group)
        link_part = cpp_group.h1.a.get("href")
        title = link_part[1:]
        link_full = "https://github.com" + link_part
        attachments_all = []
        main_msg = {"title": title, "text": link_full}
        attachments_all.append(main_msg)
        # incoming webhookのurlを入れてください
        slack = slackweb.Slack(url="")
        slack.notify(attachments=attachments_all)
        if tag_size == 9:
            abst = cpp_group.p.text.lstrip()
            attachments_abst = []
            abst_msg = {"text": abst}
            attachments_abst.append(abst_msg)
            slack.notify(attachments=attachments_abst)
        time.sleep(1)


if __name__ == '__main__':
    main()

今回はpythonとC++のtrendリポジトリを通知していますが、違う言語のときは、urlNameに指定の言語のページのurlを入れてください。完成するとこんな感じになります。
Screenshot from 2020-12-12 16-04-10.png

まとめ

Githubのtrendを見てると、当たり前ですが技術のトレンドがわかります。それ面白そうっていうリポジトリも結構あります。あとは、次の日にはランキングが結構変わっているので、世界の技術の進歩がかなり速いことを実感します。定期通知する方法として、こちらの記事の方法が気になるので、時間ある時にちゃんと見てみようと思います。

間違いや質問、ご意見等ありましたらお気軽にコメントください。頑張って答えますので(笑)。

3
1
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
3
1