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

#Twitter #API を叩いて 10秒で消えるツイートを作成する例 ( #python #shell #jq ) #1009

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

Comments

@YumaInaura
Copy link
Owner

YumaInaura commented Mar 31, 2019

作成してすぐに削除する。

config.py

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, config, os, re, sys
from requests_oauthlib import OAuth1Session
import time
import datetime
from datetime import timedelta
 
CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK, CS, AT, ATS)

message = sys.stdin.read()

status = {
  "message" : message
}

api_url = 'https://api.twitter.com/1.1/statuses/update.json?status={message}'.format(**status)

res = twitter.post(api_url)

print(json.dumps(res.json()))

delete.py

#!/usr/bin/env python3

# 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()))

e.g

$ echo "hello twiter api test\n test" | python3 ./create.py | tee create.log && sleep 10 && cat create.log | jq --raw-output .id_str | python3

{"created_at": "Sun Mar 31 01:08:12 +0000 2019", "id": 1112159627213332482, "id_str": "1112159627213332482", "text": "hello twiter api test\n test", "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": "0", "screen_name": "YumaInaura", "location": "", "description": "", "url": "https://t.co/8PZWB3UTuq", "entities": {"url": {"urls": [{"url": "https://t.co/8PZWB3UTuq", "expanded_url": "https://www.instagram.com/yumainaura/", "display_url": "instagram.com/yumainaura/", "indices": [0, 23]}]}, "description": {"urls": []}}, "protected": false, "followers_count": 529, "friends_count": 0, "listed_count": 28, "created_at": "Wed Jan 25 09:59:57 +0000 2012", "favourites_count": 24817, "utc_offset": null, "time_zone": null, "geo_enabled": true, "verified": false, "statuses_count": 37137, "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/1110446096596127744/wI7-uC6I_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1110446096596127744/wI7-uC6I_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": "et"}
{"created_at": "Sun Mar 31 01:08:12 +0000 2019", "id": 1112159627213332482, "id_str": "1112159627213332482", "text": "hello twiter api test\n test", "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": "0", "screen_name": "YumaInaura", "location": "", "description": "", "url": "https://t.co/8PZWB3UTuq", "entities": {"url": {"urls": [{"url": "https://t.co/8PZWB3UTuq", "expanded_url": "https://www.instagram.com/yumainaura/", "display_url": "instagram.com/yumainaura/", "indices": [0, 23]}]}, "description": {"urls": []}}, "protected": false, "followers_count": 529, "friends_count": 0, "listed_count": 28, "created_at": "Wed Jan 25 09:59:57 +0000 2012", "favourites_count": 24817, "utc_offset": null, "time_zone": null, "geo_enabled": true, "verified": false, "statuses_count": 37137, "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/1110446096596127744/wI7-uC6I_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1110446096596127744/wI7-uC6I_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": "et"}
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