Skip to content

Instantly share code, notes, and snippets.

@illixion
Created July 14, 2020 22:10
Show Gist options
  • Save illixion/0a91cf0905e0a36d053d17c565f434f6 to your computer and use it in GitHub Desktop.
Save illixion/0a91cf0905e0a36d053d17c565f434f6 to your computer and use it in GitHub Desktop.
# unlike.py
# Delete all likes from your Twitter account
# Requires a list of tweet IDs that you should be able to get from your Twitter data backup
from time import sleep
from timeit import default_timer as timer
import requests_oauthlib # install through pip
import pdb
import logging
import json
logging.basicConfig(filename='unlike.log', format='%(asctime)s:%(levelname)s:%(message)s', level=logging.INFO)
# Get API keys from https://developer.twitter.com/en/apps
# Requires some sort of verification, I've got a few grandfathered apps so your mileage may vary
session = requests_oauthlib.OAuth1Session(
"api key",
"api secret",
"access token",
"access token secret",
)
ids = open("ids.txt", "r")
last_post_request = timer()
for id in ids:
current_id = id.rstrip()
while True:
print(current_id, end="... ")
tweet = session.get(f"https://api.twitter.com/1.1/statuses/show.json?id={current_id}&include_entities=false&trim_user=true")
try:
error_code = json.loads(tweet.text)['errors'][0]['code']
if error_code == 179 or error_code == 63 or error_code == 34 or error_code == 144:
print("private")
logging.info(f"{current_id}: private")
sleep(1.3)
break
except KeyError:
pass
if tweet.json()['favorited'] is False:
print("not favorited")
logging.info(f"{current_id}: not favorited")
sleep(1.3)
break
while timer() - last_post_request < 90:
sleep(1)
r = session.post(
f"https://api.twitter.com/1.1/favorites/destroy.json?id={current_id}&include_entities=false",
)
last_post_request = timer()
if r.status_code == 200 or r.status_code == 304 or r.status_code == 404:
print(r.reason)
logging.info(f"{current_id}: {r.reason}")
break
else:
if r.status_code == 420:
logging.warning(f"{current_id}: enhancing calm")
sleep(300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment