/
gettwfriends.py
162 lines (145 loc) · 5.96 KB
/
gettwfriends.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import urllib.request
import urllib.parse
import urllib.error
import twurl
import json
import sqlite3
import ssl
import os
def gettwfriends(acct, count):
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
count = int(count)
TWITTER_URL = 'https://api.twitter.com/1.1/friends/list.json'
USER_URL = 'https://api.twitter.com/1.1/users/lookup.json'
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
conn = sqlite3.connect(APP_ROOT+'/friends.sqlite')
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS People
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE,
data TEXT DEFAULT NULL,
retrieved INTEGER DEFAULT (0) CHECK (retrieved in (0,1) ) )''')
cur.execute('''CREATE TABLE IF NOT EXISTS Follows
(from_id INTEGER,
to_id INTEGER,
UNIQUE (from_id, to_id))''')
conn.commit()
while True:
# acct = input('Enter a Twitter account, or quit: ')
# if (acct == 'quit'):
# break
count = count - 1
if count < 0:
break
if (len(acct) < 1):
cur.execute(
'SELECT id, name FROM People WHERE retrieved=0 LIMIT 1')
try:
(id, acct) = cur.fetchone()
except:
print('No unretrieved Twitter accounts found')
# continue
else:
cur.execute(
'SELECT id FROM People WHERE name = ? LIMIT 1', (acct, ))
try:
id = cur.fetchone()[0]
except:
cur.execute('''INSERT OR IGNORE INTO People
(name, retrieved) VALUES (?, 0)''', (acct, ))
conn.commit()
if cur.rowcount != 1:
print('Error inserting account:', acct)
continue
id = cur.lastrowid
try:
url2 = twurl.augment(USER_URL, {'screen_name': acct})
try:
connection2 = urllib.request.urlopen(url2, context=ctx)
except Exception as err2:
print('Failed to Retrieve #USER', err2)
break
data2 = connection2.read().decode()
headers2 = dict(connection2.getheaders())
print('Remaining user_lookup',
headers2['x-rate-limit-remaining'])
try:
js2 = json.loads(data2)[0]
save = json.dumps(js2).encode()
except:
print('Unable to parse json')
print(data2)
break
cur.execute('UPDATE People SET data=? WHERE name=?',
(memoryview(save), acct, ))
conn.commit()
except:
print('Error retrieving User data')
continue
if count > 5:
racct = count
else:
racct = 200
url = twurl.augment(TWITTER_URL, {'screen_name': acct, 'count': racct})
print('Retrieving account', acct)
try:
connection = urllib.request.urlopen(url, context=ctx)
except Exception as err:
print('Failed to Retrieve', err)
break
data = connection.read().decode()
headers = dict(connection.getheaders())
# print(json.dumps(json.loads(data), indent=4))
# print(connection.getheaders())
print(json.dumps(dict(connection.getheaders()) , indent=4))
# print('Remaining', headers['X-Rate-Limit-Remaining'])
try:
js = json.loads(data)
except:
print('Unable to parse json')
print(data)
break
if 'users' not in js:
print('Incorrect JSON received')
print(json.dumps(js, indent=4))
continue
cur.execute('UPDATE People SET retrieved=1 WHERE name = ?', (acct, ))
countnew = 0
countold = 0
for u in js['users']:
friend = u['screen_name']
print(friend)
cur.execute('SELECT id FROM People WHERE name = ? LIMIT 1',
(friend, ))
try:
friend_id = cur.fetchone()[0]
countold = countold + 1
except:
cur.execute('''INSERT OR IGNORE INTO People (name, data,retrieved)
VALUES (?,?, 0)''', (friend, memoryview(json.dumps(u).encode()), ))
conn.commit()
if cur.rowcount != 1:
print('Error inserting account:', friend)
continue
friend_id = cur.lastrowid
countnew = countnew + 1
cur.execute('''INSERT OR IGNORE INTO Follows (from_id, to_id)
VALUES (?, ?)''', (id, friend_id))
cur.execute('''INSERT OR IGNORE INTO Follows (from_id, to_id)
VALUES (?, ?)''', (id, id))
print('New accounts=', countnew, ' revisited=', countold)
conn.commit()
print('Remaining', headers['x-rate-limit-remaining'])
acct = ""
cur.close()
def clear():
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
conn = sqlite3.connect(APP_ROOT+'/friends.sqlite')
cur = conn.cursor()
cur.execute('''DROP TABLE IF EXISTS People''')
cur.execute('''DROP TABLE IF EXISTS Follows''')
conn.commit()
cur.close()