LoginSignup
0

More than 1 year has passed since last update.

#python で #Twitter API を叩いてツイートを削除するスクリプト例 (複数ツイート対応)

Last updated at Posted at 2019-03-24

config.py を用意しておく

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

ツイートのIDを改行区切りで、標準出力で渡す

echo "[TweetID]\n[TweetID]" | python3 example.py 

Script

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

Original by Github issue

チャットメンバー募集

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

Twitter

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