-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserDB.py
68 lines (60 loc) · 2.69 KB
/
UserDB.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
import MySQLdb
import json
import time
import os
import tweepy
import logging
import datetime
import sys
USER_DIR = 'twitter-following'
def store_friends(api, centre, following_names):
while True:
try:
logging.info("The user currently being added/checked is %s" % centre)
# trying to open a DB connection
try:
conn = MySQLdb.connect("localhost", "root", "1234", "twitter_graph", charset='utf8')
cursor = conn.cursor()
except:
print(sys.exc_info())
logging.error(sys.exc_info())
return
userfname = os.path.join(USER_DIR, str(centre) + '.json')
if not os.path.exists(userfname):
user = api.get_user(centre)
time.sleep(60)
sql_stmt = 'INSERT IGNORE INTO politicians VALUES("%s","%s","%s",%d,"UNKNOWN")' % (
str(user.screen_name), str(user.name), str(user.location), user.followers_count)
try:
cursor.execute(sql_stmt)
conn.commit()
except MySQLdb.ProgrammingError as error:
logging.debug(str(error))
conn.rollback()
conn.close()
d = {'name': user.name,
'screen_name': user.screen_name,
'id': user.id,
'friends_count': user.friends_count,
'followers_count': user.followers_count,
'following_names': following_names}
with open(userfname, 'w') as outf:
outf.write(json.dumps(d, indent=1))
outf.close()
break
except tweepy.TweepError as error:
print(str(error))
if str(error) == 'Not authorized.':
print('Can''t access user data - not authorized.')
break
if str(error) == 'User has been suspended.':
print('User suspended.')
break
if str(error) == "[{'message': 'Rate limit exceeded', 'code': 88}]" or str(error) == "[{u'message': " \
"u'Rate limit " \
"exceeded', " \
"u'code': 88}]":
print('Rate limited. Sleeping for 15 minutes.')
logging.info('Rate limit exceeded at %s' % str(datetime.now()))
time.sleep(15 * 60 + 15)
continue