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

#python からどんな #Twitter #API でも叩けるようにしておきたい、ごく簡単な汎用インターフェイス的スクリプトの例 #1104

Open
YumaInaura opened this issue Apr 7, 2019 · 0 comments

Comments

@YumaInaura
Copy link
Owner

  • get やら post やら叩けるけるようにしておく
  • put やらは考えていない
  • 第一引数にAPI の url を与え、第二引数にパラメータの JSON を渡す

config.py

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

script

#!/usr/bin/env python3

import json, config, os, re, sys
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

twitter = OAuth1Session(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api_url = sys.argv[1]
params = json.loads(sys.argv[2])

if os.environ.get('POST'):
  response = twitter.post(api_url, params=params)
else:
  response = twitter.get(api_url, params=params)

result = response.json()

print(json.dumps(result))

users show API を叩く例

$ ./common.py 'https://api.twitter.com/1.1/users/show.json' '{"screen_name":"YumaInaura"}'

{"id": 473780756, "id_str": "473780756", "name": "Rails\u6b744\u5e74\u30a8\u30f3\u30b8\u30cb\u30a2\u3092\u304a\u63a2\u3057\u306e\u65b9\u306f\u3053\u3061\u3089\u307e\u3067@\u7a32\u6d66\u60a0\u99ac ver 0.9", "screen_name": "YumaInaura", "location": "", "profile_location": null, "description": "\u7a32\u6d66\u60a0\u99ac / \u3044\u306a\u3046\u3089\u3086\u3046\u307e / Rails\u696d\u52d9\u7d04\u56db\u5e74 / \u6b21\u306e\u304a\u4ed5\u4e8b\u63a2\u3057\u4e2d / Ruby / Rails / Perl / Shell / BigQuery / MySQL / Linux / Apache / ansible / Jenkins etc .. / \u8a73\u7d30\u306fWantedly\u3067 / Twitter DM\u53ef", "url": "https://t.co/hp6dkSV15H", "entities": {"url": {"urls": [{"url": "https://t.co/hp6dkSV15H", "expanded_url": "https://www.wantedly.com/users/93140896", "display_url": "wantedly.com/users/93140896", "indices": [0, 23]}]}, "description": {"urls": []}}, "protected": false, "followers_count": 558, "friends_count": 0, "listed_count": 28, "created_at": "Wed Jan 25 09:59:57 +0000 2012", "favourites_count": 26004, "utc_offset": null, "time_zone": null, "geo_enabled": true, "verified": false, "statuses_count": 38991, "lang": "en", "status": {"created_at": "Sun Apr 07 00:28:24 +0000 2019", "id": 1114686324135432192, "id_str": "1114686324135432192", "text": "@np_misaki yeah", "truncated": false, "entities": {"hashtags": [], "symbols": [], "user_mentions": [{"screen_name": "np_misaki", "name": "\u306e\u3055\u3063\u3077\u307f\u3055\u304d", "id": 1046341165547569152, "id_str": "1046341165547569152", "indices": [0, 10]}], "urls": []}, "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>", "in_reply_to_status_id": 1114686136503259136, "in_reply_to_status_id_str": "1114686136503259136", "in_reply_to_user_id": 1046341165547569152, "in_reply_to_user_id_str": "1046341165547569152", "in_reply_to_screen_name": "np_misaki", "geo": null, "coordinates": null, "place": null, "contributors": null, "is_quote_status": false, "retweet_count": 0, "favorite_count": 0, "favorited": false, "retweeted": false, "lang": "en"}, "contributors_enabled": false, "is_translator": false, "is_translation_enabled": false, "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile": false, "profile_image_url": "http://pbs.twimg.com/profile_images/1113620035036008448/p-X7tciz_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1113620035036008448/p-X7tciz_normal.jpg", "profile_banner_url": "https://pbs.twimg.com/profile_banners/473780756/1552991452", "profile_link_color": "1B95E0", "profile_sidebar_border_color": "000000", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "profile_use_background_image": false, "has_extended_profile": false, "default_profile": false, "default_profile_image": false, "following": false, "follow_request_sent": false, "notifications": false, "translator_type": "none"}

lookup API を叩く例

$ ./common.py 'https://api.twitter.com/1.1/users/lookup.json' '{"user_id":"473780756"}'
[{"id": 473780756, "id_str": "473780756", "name": "Rails\u6b744\u5e74\u30a8\u30f3\u30b8\u30cb\u30a2\u3092\u304a\u63a2\u3057\u306e\u65b9\u306f\u3053\u3061\u3089\u307e\u3067@\u7a32\u6d66\u60a0\u99ac ver 0.9", "screen_name": "YumaInaura", "location": "", "description": "\u7a32\u6d66\u60a0\u99ac / \u3044\u306a\u3046\u3089\u3086\u3046\u307e / Rails\u696d\u52d9\u7d04\u56db\u5e74 / \u6b21\u306e\u304a\u4ed5\u4e8b\u63a2\u3057\u4e2d / Ruby / Rails / Perl / Shell / BigQuery / MySQL / Linux / Apache / ansible / Jenkins etc .. / \u8a73\u7d30\u306fWantedly\u3067 / Twitter DM\u53ef", "url": "https://t.co/hp6dkSV15H", "entities": {"url": {"urls": [{"url": "https://t.co/hp6dkSV15H", "expanded_url": "https://www.wantedly.com/users/93140896", "display_url": "wantedly.com/users/93140896", "indices": [0, 23]}]}, "description": {"urls": []}}, "protected": false, "followers_count": 558, "friends_count": 0, "listed_count": 28, "created_at": "Wed Jan 25 09:59:57 +0000 2012", "favourites_count": 26004, "utc_offset": null, "time_zone": null, "geo_enabled": true, "verified": false, "statuses_count": 38991, "lang": "en", "status": {"created_at": "Sun Apr 07 00:28:24 +0000 2019", "id": 1114686324135432192, "id_str": "1114686324135432192", "text": "@np_misaki yeah", "truncated": false, "entities": {"hashtags": [], "symbols": [], "user_mentions": [{"screen_name": "np_misaki", "name": "\u306e\u3055\u3063\u3077\u307f\u3055\u304d", "id": 1046341165547569152, "id_str": "1046341165547569152", "indices": [0, 10]}], "urls": []}, "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>", "in_reply_to_status_id": 1114686136503259136, "in_reply_to_status_id_str": "1114686136503259136", "in_reply_to_user_id": 1046341165547569152, "in_reply_to_user_id_str": "1046341165547569152", "in_reply_to_screen_name": "np_misaki", "geo": null, "coordinates": null, "place": null, "contributors": null, "is_quote_status": false, "retweet_count": 0, "favorite_count": 0, "favorited": false, "retweeted": false, "lang": "en"}, "contributors_enabled": false, "is_translator": false, "is_translation_enabled": false, "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile": false, "profile_image_url": "http://pbs.twimg.com/profile_images/1113620035036008448/p-X7tciz_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1113620035036008448/p-X7tciz_normal.jpg", "profile_banner_url": "https://pbs.twimg.com/profile_banners/473780756/1552991452", "profile_link_color": "1B95E0", "profile_sidebar_border_color": "000000", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "profile_use_background_image": false, "has_extended_profile": false, "default_profile": false, "default_profile_image": false, "following": false, "follow_request_sent": false, "notifications": false, "translator_type": "none"}]

POST でツイートを作成する例

$ POST=1 ./common.py 'https://api.twitter.com/1.1/statuses/update.json' '{"status":"hello api test this is"}'

{"created_at": "Sun Apr 07 00:39:05 +0000 2019", "id": 1114689015469330432, "id_str": "1114689015469330432", "text": "hello api test this is", "truncated": false, "entities": {"hashtags": [], "symbols": [], "user_mentions": [], "urls": []}, "source": "<a href=\"http://yumainaura.net/\" rel=\"nofollow\">yumainaura</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": 473780756, "id_str": "473780756", "name": "Rails\u6b744\u5e74\u30a8\u30f3\u30b8\u30cb\u30a2\u3092\u304a\u63a2\u3057\u306e\u65b9\u306f\u3053\u3061\u3089\u307e\u3067@\u7a32\u6d66\u60a0\u99ac ver 0.9", "screen_name": "YumaInaura", "location": "", "description": "\u7a32\u6d66\u60a0\u99ac / \u3044\u306a\u3046\u3089\u3086\u3046\u307e / Rails\u696d\u52d9\u7d04\u56db\u5e74 / \u6b21\u306e\u304a\u4ed5\u4e8b\u63a2\u3057\u4e2d / Ruby / Rails / Perl / Shell / BigQuery / MySQL / Linux / Apache / ansible / Jenkins etc .. / \u8a73\u7d30\u306fWantedly\u3067 / Twitter DM\u53ef", "url": "https://t.co/hp6dkSV15H", "entities": {"url": {"urls": [{"url": "https://t.co/hp6dkSV15H", "expanded_url": "https://www.wantedly.com/users/93140896", "display_url": "wantedly.com/users/93140896", "indices": [0, 23]}]}, "description": {"urls": []}}, "protected": false, "followers_count": 558, "friends_count": 0, "listed_count": 28, "created_at": "Wed Jan 25 09:59:57 +0000 2012", "favourites_count": 26006, "utc_offset": null, "time_zone": null, "geo_enabled": true, "verified": false, "statuses_count": 38997, "lang": "en", "contributors_enabled": false, "is_translator": false, "is_translation_enabled": false, "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile": false, "profile_image_url": "http://pbs.twimg.com/profile_images/1113620035036008448/p-X7tciz_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1113620035036008448/p-X7tciz_normal.jpg", "profile_banner_url": "https://pbs.twimg.com/profile_banners/473780756/1552991452", "profile_link_color": "1B95E0", "profile_sidebar_border_color": "000000", "profile_sidebar_fill_color": "000000", "profile_text_color": "000000", "profile_use_background_image": false, "has_extended_profile": false, "default_profile": false, "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"}
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