-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfollowerFetcher.py
72 lines (61 loc) · 2.41 KB
/
followerFetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'''
Skript för att hämta en eller flera användares följare.
idn på konton läses från stdin och följarnas idn skrivs ut till stdout.
'''
import ConnectionList as CL
import json
from twython import Twython,TwythonRateLimitError,TwythonError,TwythonAuthError
import sys
import datetime
import time
def main():
conn = CL.ConnectionList(filepath="config/access.conf")
#Läs userid från stdin
userId = input()
cursor = -1
timeout = 1
while True:
try:
#Hämta respons
response = conn.connection().get_followers_ids(user_id = userId,cursor = cursor)
#Skriv ut info till stderr
print("userId="+str(userId)+", cursor="+str(cursor), file=sys.stderr)
#Skriv ut alla id till stdout
for followerId in response['ids']:
print(followerId)
#Ställ in cursorn
cursor = response['next_cursor']
#När det inte finns mer att hämta.
while response['next_cursor'] == 0:
#Läs
userId = input()
#Nollställ cursor
cursor = -1
#hämta nästa respons
response = conn.connection().get_followers_ids(user_id = userId,cursor = cursor)
except TwythonAuthError:
print("Private account: userId", file=sys.stderr)
#Add to blacklist
break
except TwythonRateLimitError as err:
timeout += 1
print("access "+str(conn.position())+" timed out.", file=sys.stderr)
if timeout > conn.size():
timeout = 1
print(":(", file=sys.stderr)
print(err, file=sys.stderr)
print(datetime.datetime.now(), file=sys.stderr)
time.sleep(60*15+60) #In sec. 60*15 = 15 min + 1min
print(":)", file=sys.stderr)
except TwythonError as err: #Handel timeouts
print("Error:", file=sys.stderr)
print(err, file=sys.stderr)
#När End Of File: bryt ur while-loopen och avsluta.
except EOFError as eof:
print(eof, file=sys.stderr)
break
#Skriv ut andra fel till terminalen
except Exception as other:
print("Error.. userId="+userid+", cursor="+cursor, file=sys.stderr)
print(other, file=sys.stderr)
main()