IFTTTを使って、Twitterにハッシュタグつきでツイートしたら、はてなブログに自動投稿する例。画像表示もインライン対応。

IFTTTを使って、Twitterハッシュタグつきでツイートしたら、はてなブログに自動投稿する例。画像表示もインライン対応。

はてなのメール投稿用のアドレスをゲット

メルアドは 設定 > 詳細から取得可能

こんなURL

https://blog.hatena.ne.jp/yumainaura/yumainaura.hateblo.jp/config/detail

メールで記事を投稿する - はてなブログ ヘルプ

はてなの設定を、見たままモードにしておく

Twitter埋め込みのHTMLが、そのまま使えるようにしておく

image

IFTTTでハッシュタグつきツイートをトリガーにする<<

image image

Gmail 送信をアクションにする

image

ToAdress に はてなブログの投稿用メールアドレスを入れる

Subject とか Text とか調整する






<br>
from Twitter https://twitter.com/<br>
<br>
<br>
via <a href="https://ifttt.com/?ref=da&site=gmail">IFTTT</a>

結果の例

ツイートをリプライで連鎖させていると、ひとつ親のツイートまで表示してくれるっぽい。

image image

http://yumainaura.hateblo.jp/entry/2019/02/02/205113

はてな編集画面の例

image

はてなに投稿されたHTMLの例

image

NOTE

Gmail送信のAttachmentにTwitterの First Link を指定しても、うまく動かなかった。Twitterはそもそも画像URLの扱いが厳しそうな。

メールでMarkdownを強制指定できるかどうかは、はてなに問い合わせてみた。結果待ち。

image

Markdown or はてな記法をデフォルト設定にしていて、変えたくない場合は、とりあえず、Twitter連携用の別ブログを作っておいても良いかもしれない。

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

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

Google翻訳APIをpythonスクリプトで叩いて英語を日本語に翻訳する。

Google翻訳APIpythonスクリプトで叩いて英語を日本語に翻訳する。

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

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

Google翻訳APIをシェルスクリプトで叩いて英語を日本語に翻訳する。ほとんど公式チュートリアルのまま。

Google翻訳APIシェルスクリプトで叩いて英語を日本語に翻訳する。ほとんど公式チュートリアルのまま。

Google Translate API を有効化しておく

チュートリアルのとおりに。

https://cloud.google.com/translate/docs/quickstart

トークンの取得

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

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

クイックスタート  |  Cloud Translation API  |  Google Cloud

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

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

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

つまづき

この手順でつまづいた。

クイックスタート  |  Cloud Translation API  |  Google Cloud

Command

gcloud auth application-default print-access-token

Error

なんだよこれふざけんな‥! うう [emoji:33A]

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

ここではテストで何やら強い権限を与えてしまうが、テストが終わったら一瞬で消したほうが良いかも。

image

image

再度チャレンジ

上で作ったファイルを環境変数で指定する。 example.json 的な。

$ GOOGLE_APPLICATION_CREDENTIALS=example.json gcloud auth
application-default print-access-token
トークン!トークン!トークン!トークン!トークン!生成!

うまくいった。

喜びのポーズ!!!

image

Ref

The Application Default Credentials are not available · Issue #175 · googleapis/gax-dotnet

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