Zappier で Github Issue 作成をトリガーにして、pythonスクリプトからGoogle翻訳APIを叩き、翻訳済みの新しいGithub Issue を作成する例。日本語から英語へバージョン。

Zappier で Github Issue 作成をトリガーにして、pythonスクリプトからGoogle翻訳APIを叩き、翻訳済みの新しいGithub Issue を作成する例。日本語から英語へバージョン。

補足

Googleトークンの有効期限などは確認していない。とりあえず動いた。

Google翻訳API用に GCP の認証トークンを取得しておく

Google Cloud の認証トークンを gcloud コマンドで取得する。環境変数でサービスアカウントファイルを指定する例。 · Issue #545 · YumaInaura/YumaInaura

Issue 作成をトリガーにする

image

Code By Zapier

Github の title と description を 入力リソースに指定する

image

Script

先程取得したトークンをスクリプト内に埋め込む Google 翻訳で Markdownが崩れるので、pythonで適当に整形。

import requests
import re

data = {
  'q': [
        input["title"],
        input["body"],
    ],
  'source': 'ja',
  'target': 'en',
  'format': 'text'
}

url = 'https://translation.googleapis.com/language/translate/v2'
token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

headers = {
 'Authorization': 'Bearer {}'.format(token),
 'Content-Type': 'application/json',
}

res = requests.post(url, headers=headers, json=data)

# Title
output_title = res.json()['data']['translations'][0]['translatedText']
# For twitter in English 240 upper limit characters
output_title = output_title[:200]

# Fix Body: Image Markdown tag spaces
output_body = res.json()['data']['translations'][1]['translatedText']
output_body = re.sub(r'! \[image\] ', "![image]", output_body)


output = [
  {
    "title": output_title,
    "body": output_body
  }
]

Find or Create Issue をアクションにする

  • いろいろ記入する
  • Github ISsueを探して、なければ作成することも可能」という謎の仕様なので、検索対象として、絶対に入力しないような Search value を指定しておく。

image

image

結果

https://github.com/YumaInaura/issue/issues/63

image

画像もバッチリ

image

おまけ。必要なら Zapier の Detect Language / Only Continue if

で、日本語判定された場合にだけ、アクションが走るようにしておく。

image

https://github.com/YumaInaura/YumaInaura/issues/548