Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#API を叩くには #python と #shell と #jq コマンドを組み合わせて、標準入出力でパイプするのが良さげじゃない? #904

Open
YumaInaura opened this issue Mar 23, 2019 · 0 comments

Comments

@YumaInaura
Copy link
Owner

Twitterでツイートして、すぐにそれを削除する例

  • シェルで Hello Mr.Twitter を標準出力する
  • python が標準入力を受け取ってツイートして、API から受け取った json を標準出力する
  • jq コマンドでツイートの id を標準出力する
  • また別の python スクリプトが ID を標準入力で受け取って、削除して、APIから受け取った json を標準出力する
  • jq コマンドで json の内容をフォーマットして標準出力する
echo "Hello Mr.Twitter " | python twitter/create.py | jq --raw-output '.["id_str"]' | python twitter/destroy.py | jq .

イメージ

image

script - config.py

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''

script - create.py

# https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update

import json, config, os, re, urllib.parse, unicodedata, sys
from requests_oauthlib import OAuth1Session
 
CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK, CS, AT, ATS)

message = ''

for line in sys.stdin:
  message += re.sub(r'^\s+|\s+$', '', line)

formatted_message = re.sub(r'\\n', "\n", message)

formatted_message = formatted_message[:140]

encoded_message = urllib.parse.quote_plus(formatted_message)

tweet = {
  "status" : encoded_message,
  "in_reply_to_status_id" : os.environ.get('IN_REPLY_TO_STATUS_ID') if os.environ.get('IN_REPLY_TO_STATUS_ID') else '',
  "auto_populate_reply_metadata" : 'true',
}

url = "https://api.twitter.com//1.1/statuses/update.json?status={status}&in_reply_to_status_id={in_reply_to_status_id}&auto_populate_reply_metadata={auto_populate_reply_metadata}".format(**tweet)

if os.environ.get("DEBUG"):
  print(url)
else:
  res = twitter.post(url)
  print(json.dumps(res.json()))

script - destroy.py

# https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-destroy-id.html

import json, config, re, sys, os
from requests_oauthlib import OAuth1Session
 
CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK, CS, AT, ATS)

for line in sys.stdin:
  id = line.strip()
  api_url = 'https://api.twitter.com/1.1/statuses/destroy/{id}.json'.format(**{ "id" : id })

  res = twitter.post(api_url)
  print(json.dumps(res.json()))

ひとこと

ツールボックスに道具を揃えて、使い分けられるエンジニアになりたい。

エンジニア勉強会に参加しないエンジニア。俺です。だけど一番行って良かったと思ったのはRubyKaigi大阪01だ。@yuki24 さんの発表は本当に頭に残ってる。ツールボックスを使い分ける。ちなみに僕もLT枠で登壇した。

— 0 (@YumaInaura) February 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant