· 9 years ago · Nov 16, 2016, 03:30 PM
1import sys
2import string
3import json
4import datetime
5import schedule
6import time
7
8from twython import Twython,TwythonRateLimitError, TwythonError
9
10
11#Connecting to API, AUTH token and secret unnecessary
12APP_KEY = KEY1
13APP_SECRET = KEY2
14OAUTH_TOKEN = KEY3
15OAUTH_SECRET = KEY4
16
17#Verify twitter credentials
18twitter = Twython(APP_KEY,APP_SECRET, OAUTH_TOKEN, OAUTH_SECRET)
19twitter.verify_credentials()
20
21#Get username to monitor followers list
22amountOfUsers = 0
23arrayOfIds = []
24
25def chunks(l, n):
26 for i in range(0, len(l), n):
27 yield l[i:i + n]
28
29with open("twitter_ids_list.txt") as f:
30 content = f.read().splitlines()
31 arrayOfIds.append(content)
32
33users = []
34try:
35 #Gathering full USER JSON
36 print("Now writing user data for each ID!")
37 for chunk in chunks(arraysOfIds, 99):
38 users = twitter.lookup_user(user_id = arrayOfIds)
39 amountOfUsers += 1
40 print("%i users written so far!" % (amountOfUsers))
41 time.sleep(15*60)
42 print("Full data for %i users has been saved!" % (amountOfUsers))
43except TwythonRateLimitError as error:
44 print("[Exception Raised] Rate limit exceeded")
45 reset = int(twitter.get_lastfunction_header('x-rate-limit-reset'))
46 wait = max(reset - time.time(), 0) + 10 # plus 10 seconds to be safe
47 print("Waiting %i seconds before continuing" % (wait))
48 time.sleep(wait)
49
50with open("twitter_ids_json.txt", "a") as f2:
51 json.dump(users, f2)