LoginSignup
0
0

More than 1 year has passed since last update.

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

Last updated at Posted at 2019-03-31

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

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"}

Original by Github issue

チャットメンバー募集

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

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0