Skip to content

Instantly share code, notes, and snippets.

@csinchok
Created February 11, 2015 19:14
Show Gist options
  • Select an option

  • Save csinchok/ce1690f2aaccd14fbf50 to your computer and use it in GitHub Desktop.

Select an option

Save csinchok/ce1690f2aaccd14fbf50 to your computer and use it in GitHub Desktop.
Fav Bombin'
import requests
from requests_oauthlib import OAuth1
import time
APP_API_KEY = "CONSUMER_KEY"
APP_API_SECRET = "CONSUMER_SECRET"
USER_ACCESS_TOKEN = "USER_ACCESS_TOKEN"
USER_ACCESS_TOKEN_SECRET = "USER_ACCESS_TOKEN_SECRET"
AUTH = OAuth1(
APP_API_KEY, APP_API_SECRET,
USER_ACCESS_TOKEN, USER_ACCESS_TOKEN_SECRET)
SEARCH_ENDPOINT = "https://api.twitter.com/1.1/statuses/user_timeline.json"
FAV_ENDPOINT = "https://api.twitter.com/1.1/favorites/create.json"
UNFAV_ENDPOINT = "https://api.twitter.com/1.1/favorites/destroy.json"
def fav_toggle(id_str):
response = requests.post(FAV_ENDPOINT, params={"id": id_str}, auth=AUTH)
response.raise_for_status()
time.sleep(2)
response = requests.post(UNFAV_ENDPOINT, params={"id": id_str}, auth=AUTH)
response.raise_for_status()
print(id_str)
def scroll(max_id=None):
params = {
"screen_name": "chrissinchok",
"trim_user": "true",
"exclude_replies": "false",
"include_rts": "false",
"count": 100
}
if max_id:
params["max_id"] = max_id
response = requests.get(SEARCH_ENDPOINT, params=params, auth=AUTH)
response.raise_for_status()
for status in response.json():
max_id = status["id_str"]
if len(status.get("entities", {}).get("user_mentions", [])) > 0:
continue
if not status["favorited"]:
fav_toggle(status["id_str"])
time.sleep(2)
scroll(max_id=int(max_id) - 1)
if __name__ == "__main__":
scroll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment