🖥

#python スクリプトで #Twitter #API を叩いて複数のツイートを作成する例。標準入力に #JSON を与える場合。

2019/04/09に公開

config.py

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

twitterauth.py

#!/usr/bin/env python3

import os, config
from requests_oauthlib import OAuth1Session

if os.environ.get('TWITTER_CONSUMER_KEY'):
  CONSUMER_KEY = os.environ.get('TWITTER_CONSUMER_KEY')
  CONSUMER_SECRET = os.environ.get('TWITTER_CONSUMER_SECRET')
  ACCESS_TOKEN = os.environ.get('TWITTER_ACCESS_TOKEN')
  ACCESS_TOKEN_SECRET = os.environ.get('TWITTER_ACCESS_TOKEN_SECRET')
else:
  CONSUMER_KEY = config.CONSUMER_KEY
  CONSUMER_SECRET = config.CONSUMER_SECRET
  ACCESS_TOKEN = config.ACCESS_TOKEN
  ACCESS_TOKEN_SECRET = config.ACCESS_TOKEN_SECRET

def twitter():
  return  OAuth1Session(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

create.py

#!/usr/bin/env python3

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

import json, os, sys, twitterauth

twitter = twitterauth.twitter()

input_datas = json.loads(sys.stdin.read())

api_url = 'https://api.twitter.com/1.1/statuses/update.json'

json_key = os.environ.get('JSON_KEY') if os.environ.get('JSON_KEY') else 'text'
results = []

for input_data in input_datas:
  params = {
    "status" : input_data[json_key]
  }

  res = twitter.post(api_url, params=params)
  results.append(res.json())

print(json.dumps(results))

exe

$ echo '[{ "text": "hello\\nworld" }, { "text" : "yes you are daddy" }]' | ./create.py
[{"created_at": "Tue Apr 09 06:23:23 +0000 2019", "id": 1115500435551342592, "id_str": "1115500435551342592", "text": "hello\nworld", "truncated": false, "entities": {"hashtags": [], "symbols": [], "user_mentions": [], "urls": []}, "source": "<a href=\"https://mb2.jp/\" rel=\"nofollow\">YumaInaura2nd</a>", "in_reply_to_status_id": null, "in_reply_to_status_id_str": null, "in_reply_to_user_id": null, "in_reply_to_user_id_str": null, "in_reply_to_screen_name": null, "user": {"id": 1095662627890425861, "id_str": "1095662627890425861", "name": "YumaInaura Second", "screen_name": "YumaInaura2nd", "location": "", "description": "@yumainaura Translation", "url": null, "entities": {"description": {"urls": []}}, "protected": false, "followers_count": 1, "friends_count": 0, "listed_count": 0, "created_at": "Wed Feb 13 12:35:01 +0000 2019", "favourites_count": 0, "utc_offset": null, "time_zone": null, "geo_enabled": false, "verified": false, "statuses_count": 1993, "lang": "en", "contributors_enabled": false, "is_translator": false, "is_translation_enabled": false, "profile_background_color": "F5F8FA", "profile_background_image_url": null, "profile_background_image_url_https": null, "profile_background_tile": false, "profile_image_url": "http://pbs.twimg.com/profile_images/1095662853997060102/W8itdZuR_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1095662853997060102/W8itdZuR_normal.jpg", "profile_link_color": "1DA1F2", "profile_sidebar_border_color": "C0DEED", "profile_sidebar_fill_color": "DDEEF6", "profile_text_color": "333333", "profile_use_background_image": true, "has_extended_profile": false, "default_profile": true, "default_profile_image": false, "following": false, "follow_request_sent": false, "notifications": false, "translator_type": "none"}, "geo": null, "coordinates": null, "place": null, "contributors": null, "is_quote_status": false, "retweet_count": 0, "favorite_count": 0, "favorited": false, "retweeted": false, "lang": "en"}, {"created_at": "Tue Apr 09 06:23:23 +0000 2019", "id": 1115500436461477888, "id_str": "1115500436461477888", "text": "yes you are daddy", "truncated": false, "entities": {"hashtags": [], "symbols": [], "user_mentions": [], "urls": []}, "source": "<a href=\"https://mb2.jp/\" rel=\"nofollow\">YumaInaura2nd</a>", "in_reply_to_status_id": null, "in_reply_to_status_id_str": null, "in_reply_to_user_id": null, "in_reply_to_user_id_str": null, "in_reply_to_screen_name": null, "user": {"id": 1095662627890425861, "id_str": "1095662627890425861", "name": "YumaInaura Second", "screen_name": "YumaInaura2nd", "location": "", "description": "@yumainaura Translation", "url": null, "entities": {"description": {"urls": []}}, "protected": false, "followers_count": 1, "friends_count": 0, "listed_count": 0, "created_at": "Wed Feb 13 12:35:01 +0000 2019", "favourites_count": 0, "utc_offset": null, "time_zone": null, "geo_enabled": false, "verified": false, "statuses_count": 1994, "lang": "en", "contributors_enabled": false, "is_translator": false, "is_translation_enabled": false, "profile_background_color": "F5F8FA", "profile_background_image_url": null, "profile_background_image_url_https": null, "profile_background_tile": false, "profile_image_url": "http://pbs.twimg.com/profile_images/1095662853997060102/W8itdZuR_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1095662853997060102/W8itdZuR_normal.jpg", "profile_link_color": "1DA1F2", "profile_sidebar_border_color": "C0DEED", "profile_sidebar_fill_color": "DDEEF6", "profile_text_color": "333333", "profile_use_background_image": true, "has_extended_profile": false, "default_profile": true, "default_profile_image": false, "following": false, "follow_request_sent": false, "notifications": false, "translator_type": "none"}, "geo": null, "coordinates": null, "place": null, "contributors": null, "is_quote_status": false, "retweet_count": 0, "favorite_count": 0, "favorited": false, "retweeted": false, "lang": "en"}]

image

image

Original by Github issue

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

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-09

Discussion