LoginSignup
0
4

More than 3 years have passed since last update.

ソフトウェアアップデート通知をTeamsで -Apple編-

Last updated at Posted at 2019-10-14

何がしたいか

Xcode、iOS、MacOSなど、開発業務に関係するソフトウェアアップデートのリリースを自動で通知してくれる仕組みを作りたい。
目的としては、毎朝リリースページをチェックしている特定のメンバーの負荷軽減、クラウドプラットフォームで提供されているサーバレス実装の理解を深めること。

どうやるか

1.リリースページを定期的にスクレイピングし、製品名と更新日を取得する。
2.製品ごとにTeamsの通知専用チャンネルにPostする。

何を使うか

  • GCP Cloud Functions - pythonで対象ページのスクレイピング・TeamsへPostするFunctionを作成
  • GCP Compute Engine - Cronで上記のFunctionをリクエストする
  • Teams Incoming Webhook - 通知専用チャンネルへの投稿

GCP Cloud Functions

「scrape_apple」という名前で関数を作成。
スクレイピングにrequests・BeautifulSoup4、Teamsへの投稿にpymsteamsライブラリを使用する。これらをrequirements.txtに記述しておく。

requirements.txt

# Function dependencies, for example:
# package>=version

requests==2.19.1
bs4==0.0.1
pymsteams==0.1.12

処理の流れ

作成したスクリプト → Github
1.リリースページのHtmlを取得。製品名と更新日(とURL)を格納したdictを作る。

scrape_apple.py
def scrape():
    pattern = '\(.+\)'

    url = 'https://developer.apple.com/news/releases/'
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    articles = soup.findAll('article')

    articles_dict = {}
    for article in articles:
        product_name = article.find("a").find("h2").getText()
        product_date = article.find(class_="article-date").getText()
        articles_dict[re.sub(pattern, '', product_name).strip()] = {'product_name': product_name, 'product_date': product_date, 'url': url}

    return articles_dict

2.1で作成したdictから、product_dateが現在の日付でないものを取り除く。

scrape_apple.py
def analyze_json(articles_dict):
    copy_dict = articles_dict.copy()
    for article in articles_dict:
        product_name = article
        product_date = articles_dict[product_name]['product_date']
        product_date_formatted = datetime.datetime.strptime(product_date, '%B %d, %Y')

        now = datetime.datetime.now()
        dt = now - product_date_formatted
        if dt >= datetime.timedelta(hours=24):
            del copy_dict[product_name]

    return copy_dict

3.dictの各Valueをメッセージに含め、Teamsへ送信。

scrape_apple.py
def post_teams(articles_dict):
    url = 'Your Incoming Webhook URL'

    for product in articles_dict:
        myTeamsMessage = pymsteams.connectorcard(url)
        myTeamsMessage.title("【Apple】" + product)

        message = "Product: {0}<br>Updated At: {1}<br>[Web]({2})".format(articles_dict[product]['product_name'], articles_dict[product]['product_date'], articles_dict[product]['url'])
        myTeamsMessage.text(message)
        myTeamsMessage.send()

GCP Compute Engine

上記で作成したFunctionをcurlコマンドでリクエストするcronを組むだけなので割愛。

追記(2019/10/16)

Google Chromeのアップデートリリースを通知するスクリプトをGithubに追加しました。

0
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
0
4