IFTTTを使って、Twitterにハッシュタグつきでツイートしたら、はてなブログに自動投稿する例。画像表示もインライン対応。
IFTTTを使って、Twitterにハッシュタグつきでツイートしたら、はてなブログに自動投稿する例。画像表示もインライン対応。
はてなのメール投稿用のアドレスをゲット
メルアドは 設定 > 詳細から取得可能
こんなURL
https://blog.hatena.ne.jp/yumainaura/yumainaura.hateblo.jp/config/detail
はてなの設定を、見たままモードにしておく
Twitter埋め込みのHTMLが、そのまま使えるようにしておく
IFTTTでハッシュタグつきツイートをトリガーにする<<
Gmail 送信をアクションにする
ToAdress に はてなブログの投稿用メールアドレスを入れる
Subject とか Text とか調整する
<br> from Twitter https://twitter.com/<br> <br> <br> via <a href="https://ifttt.com/?ref=da&site=gmail">IFTTT</a>
結果の例
ツイートをリプライで連鎖させていると、ひとつ親のツイートまで表示してくれるっぽい。
http://yumainaura.hateblo.jp/entry/2019/02/02/205113
はてな編集画面の例
はてなに投稿されたHTMLの例
NOTE
Gmail送信のAttachmentにTwitterの First Link を指定しても、うまく動かなかった。Twitterはそもそも画像URLの扱いが厳しそうな。
メールでMarkdownを強制指定できるかどうかは、はてなに問い合わせてみた。結果待ち。
Markdown or はてな記法をデフォルト設定にしていて、変えたくない場合は、とりあえず、Twitter連携用の別ブログを作っておいても良いかもしれない。
@null #はてな ツイートテスト
@null#はてな
— いなうらゆうま🤖マインドフルネス (@YumaInaura) February 2, 2019
ツイートテスト pic.twitter.com/UG31pIAHnN
from Twitter https://twitter.com/YumaInaura
February 02, 2019 at 08:50PM
via IFTTT http://twitter.com/YumaInaura/status/1091665255690727424
Twitterでつぶやくだけで、はてなブログに投稿してみるテスト。 #はてな
Twitterでつぶやくだけで、はてなブログに投稿してみるテスト。 #はてな
— いなうらゆうま🤖マインドフルネス (@YumaInaura) February 2, 2019
from Twitter https://twitter.com/YumaInaura
February 02, 2019 at 08:13PM via IFTTT
Zappier で Github Issue 作成をトリガーにして、pythonスクリプトからGoogle翻訳APIを叩き、翻訳済みの新しいGithub Issue を作成する例。日本語から英語へバージョン。
Zappier で Github Issue 作成をトリガーにして、pythonスクリプトからGoogle翻訳APIを叩き、翻訳済みの新しいGithub Issue を作成する例。日本語から英語へバージョン。
補足
Google のトークンの有効期限などは確認していない。とりあえず動いた。
Google翻訳API用に GCP の認証トークンを取得しておく
Issue 作成をトリガーにする
Code By Zapier
Github の title と description を 入力リソースに指定する
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 をアクションにする
結果
https://github.com/YumaInaura/issue/issues/63
画像もバッチリ
おまけ。必要なら Zapier の Detect Language / Only Continue if
で、日本語判定された場合にだけ、アクションが走るようにしておく。
Google翻訳APIをpythonスクリプトで叩いて英語を日本語に翻訳する。
Google翻訳APIをpythonスクリプトで叩いて英語を日本語に翻訳する。
script
import requests import os data = { 'q': 'The Grate Great Pyramid of Giza (also known as the Pyramid of Khufu or the Pyramid of Cheops) is the oldest and largest of the three pyramids in the Giza pyramid complex.', 'source': 'en', 'target': 'ja', 'format': 'text' } url = 'https://translation.googleapis.com/language/translate/v2' token = os.environ['TOKEN'] headers = { 'Authorization': 'Bearer {}'.format(token), 'Content-Type': 'application/json', } res = requests.post(url, headers=headers, json=data) print(res.json())
実行例
TOKEN=xxxxxxxxxxxxx python example.py
結果
{'data': {'translations': [{'translatedText': 'ギザのグレート大ピラミッド(クフのピラミッドまたはチープのピラミッドとしても知られています)は、ギザピラミッド複合体の3つのピラミッドの中で最も古く、最も大きいピラミッドです。'}]}}
Ref
Google翻訳APIをシェルスクリプトで叩いて英語を日本語に翻訳する。ほとんど公式チュートリアルのまま。
Google翻訳APIをシェルスクリプトで叩いて英語を日本語に翻訳する。ほとんど公式チュートリアルのまま。
Google Translate API を有効化しておく
チュートリアルのとおりに。
https://cloud.google.com/translate/docs/quickstart
トークンの取得
Script
key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx curl "https://translation.googleapis.com/language/translate/v2" \ -H "Authorization: Bearer $key" \ -s -X POST -H "Content-Type: application/json" \ --data "{ 'q': 'The Great Pyramid of Giza (also known as the Pyramid of Khufu or the Pyramid of Cheops) is the oldest and largest of the three pyramids in the Giza pyramid complex.', 'source': 'en', 'target': 'ja', 'format': 'text' }"
結果
$ sh example.sh
{ "data": { "translations": [ { "translatedText": "ギザの大ピラミッド(クフのピラミッドまたはチープのピラミッドとしても知られています)は、ギザピラミッド複合体の3つのピラミッドの中で最も古く、最も大きいピラミッドです。" } ] } }
Ref
Cloud Translation API ドキュメント | Cloud Translation API | Google Cloud
Google Cloud の認証トークンを gcloud コマンドで取得する。環境変数でサービスアカウントファイルを指定する例。
Google Cloud の認証トークンを gcloud コマンドで取得する。環境変数でサービスアカウントファイルを指定する例。
つまづき
この手順でつまづいた。
クイックスタート | Cloud Translation API | Google Cloud
Command
gcloud auth application-default print-access-token
Error
なんだよこれふざけんな‥! うう
Yumas-MacBook-Air:tmp yumainaura$ gcloud auth application-default print-access-token ERROR: (gcloud.auth.application-default.print-access-token) The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
Credential
https://console.cloud.google.com/apis/credentials/serviceaccountkey
ここではテストで何やら強い権限を与えてしまうが、テストが終わったら一瞬で消したほうが良いかも。
再度チャレンジ
上で作ったファイルを環境変数で指定する。 example.json
的な。
$ GOOGLE_APPLICATION_CREDENTIALS=example.json gcloud auth application-default print-access-token トークン!トークン!トークン!トークン!トークン!生成!
うまくいった。
喜びのポーズ!!!
Ref
The Application Default Credentials are not available · Issue #175 · googleapis/gax-dotnet