-
Notifications
You must be signed in to change notification settings - Fork 20
/
cli.py
457 lines (424 loc) · 14.4 KB
/
cli.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import click
import datetime
import os
import sqlite_utils
import time
import json
from twitter_to_sqlite import utils
def add_identifier_options(subcommand):
for decorator in reversed(
(
click.argument("identifiers", type=str, nargs=-1),
click.option(
"--attach",
type=click.Path(
file_okay=True, dir_okay=False, allow_dash=False, exists=True
),
multiple=True,
help="Additional database file to attach",
),
click.option("--sql", help="SQL query to fetch identifiers to use"),
)
):
subcommand = decorator(subcommand)
return subcommand
@click.group()
@click.version_option()
def cli():
"Save data from Twitter to a SQLite database"
@cli.command()
@click.argument("url")
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
def fetch(url, auth):
"Make an authenticated request to the Twitter API"
auth = json.load(open(auth))
session = utils.session_for_auth(auth)
click.echo(json.dumps(session.get(url).json(), indent=4))
@cli.command()
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
default="auth.json",
help="Path to save tokens to, defaults to auth.json",
)
def auth(auth):
"Save authentication credentials to a JSON file"
click.echo("Create an app here: https://developer.twitter.com/en/apps")
click.echo("Then navigate to 'Keys and tokens' and paste in the following:")
click.echo()
api_key = click.prompt("API key")
api_secret_key = click.prompt("API secret key")
access_token = click.prompt("Access token")
access_token_secret = click.prompt("Access token secret")
open(auth, "w").write(
json.dumps(
{
"api_key": api_key,
"api_secret_key": api_secret_key,
"access_token": access_token,
"access_token_secret": access_token_secret,
},
indent=4,
)
+ "\n"
)
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option("--user_id", help="Numeric user ID")
@click.option("--screen_name", help="Screen name")
@click.option("--silent", is_flag=True, help="Disable progress bar")
def followers(db_path, auth, user_id, screen_name, silent):
"Save followers for specified user (defaults to authenticated user)"
auth = json.load(open(auth))
session = utils.session_for_auth(auth)
db = sqlite_utils.Database(db_path)
fetched = []
# Get the follower count, so we can have a progress bar
count = 0
profile = utils.get_profile(session, user_id, screen_name)
screen_name = profile["screen_name"]
user_id = profile["id"]
def go(update):
utils.save_users(db, [profile])
for followers_chunk in utils.fetch_follower_chunks(
session, user_id, screen_name
):
fetched.extend(followers_chunk)
utils.save_users(db, followers_chunk, followed_id=user_id)
update(len(followers_chunk))
if not silent:
count = profile["followers_count"]
with click.progressbar(
length=count,
label="Importing {:,} followers for @{}".format(count, screen_name),
) as bar:
go(bar.update)
else:
go(lambda x: None)
# open("/tmp/all.json", "w").write(json.dumps(fetched, indent=4))
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option("--user_id", help="Numeric user ID")
@click.option("--screen_name", help="Screen name")
def favorites(db_path, auth, user_id, screen_name):
"Save tweets favorited by specified user"
auth = json.load(open(auth))
session = utils.session_for_auth(auth)
db = sqlite_utils.Database(db_path)
with click.progressbar(
utils.fetch_favorites(session, user_id, screen_name),
label="Importing favorites",
show_pos=True,
) as bar:
utils.save_tweets(db, bar)
@cli.command(name="user-timeline")
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option("--stop_after", type=int, help="Only pull this number of recent tweets")
@click.option("--user_id", help="Numeric user ID")
@click.option("--screen_name", help="Screen name")
def user_timeline(db_path, auth, stop_after, user_id, screen_name):
"Save tweets posted by specified user"
auth = json.load(open(auth))
session = utils.session_for_auth(auth)
profile = utils.get_profile(session, user_id, screen_name)
db = sqlite_utils.Database(db_path)
with click.progressbar(
utils.fetch_user_timeline(session, user_id, screen_name, stop_after),
length=profile["statuses_count"],
label="Importing tweets",
show_pos=True,
) as bar:
# Save them 100 at a time
chunk = []
for tweet in bar:
chunk.append(tweet)
if len(chunk) >= 100:
utils.save_tweets(db, chunk)
chunk = []
if chunk:
utils.save_tweets(db, chunk)
@cli.command(name="users-lookup")
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@add_identifier_options
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option("--ids", is_flag=True, help="Treat input as user IDs, not screen names")
def users_lookup(db_path, identifiers, attach, sql, auth, ids):
"Fetch user accounts"
auth = json.load(open(auth))
session = utils.session_for_auth(auth)
db = sqlite_utils.Database(db_path)
identifiers = utils.resolve_identifiers(db, identifiers, attach, sql)
for batch in utils.fetch_user_batches(session, identifiers, ids):
utils.save_users(db, batch)
@cli.command(name="statuses-lookup")
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@add_identifier_options
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"--skip-existing", is_flag=True, help="Skip tweets that are already in the DB"
)
@click.option("--silent", is_flag=True, help="Disable progress bar")
def statuses_lookup(db_path, identifiers, attach, sql, auth, skip_existing, silent):
"Fetch tweets by their IDs"
auth = json.load(open(auth))
session = utils.session_for_auth(auth)
db = sqlite_utils.Database(db_path)
identifiers = utils.resolve_identifiers(db, identifiers, attach, sql)
if skip_existing:
existing_ids = set(
r[0] for r in db.conn.execute("select id from tweets").fetchall()
)
identifiers = [i for i in identifiers if int(i) not in existing_ids]
if silent:
for batch in utils.fetch_status_batches(session, identifiers):
utils.save_tweets(db, batch)
else:
# Do it with a progress bar
count = len(identifiers)
with click.progressbar(
length=count,
label="Importing {:,} tweet{}".format(count, "" if count == 1 else "s"),
) as bar:
for batch in utils.fetch_status_batches(session, identifiers):
utils.save_tweets(db, batch)
bar.update(len(batch))
@cli.command(name="list-members")
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("identifiers", type=str, nargs=-1)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"--ids", is_flag=True, help="Treat input as list IDs, not user/slug strings"
)
def list_members(db_path, identifiers, auth, ids):
"Fetch lists - accepts one or more screen_name/list_slug identifiers"
auth = json.load(open(auth))
session = utils.session_for_auth(auth)
db = sqlite_utils.Database(db_path)
for identifier in identifiers:
utils.fetch_and_save_list(db, session, identifier, ids)
@cli.command(name="followers-ids")
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@add_identifier_options
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"--ids", is_flag=True, help="Treat input as list IDs, not user/slug strings"
)
@click.option(
"--sleep", type=int, default=61, help="Seconds to sleep between API calls"
)
def followers_ids(db_path, identifiers, attach, sql, auth, ids, sleep):
"Populate followers table with IDs of account followers"
_shared_friends_ids_followers_ids(
db_path,
identifiers,
attach,
sql,
auth,
ids,
sleep,
api_url="https://api.twitter.com/1.1/followers/ids.json",
first_key="followed_id",
second_key="follower_id",
)
@cli.command(name="friends-ids")
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@add_identifier_options
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"--ids", is_flag=True, help="Treat input as list IDs, not user/slug strings"
)
@click.option(
"--sleep", type=int, default=61, help="Seconds to sleep between API calls"
)
def friends_ids(db_path, identifiers, attach, sql, auth, ids, sleep):
"Populate followers table with IDs of account friends"
_shared_friends_ids_followers_ids(
db_path,
identifiers,
attach,
sql,
auth,
ids,
sleep,
api_url="https://api.twitter.com/1.1/friends/ids.json",
first_key="follower_id",
second_key="followed_id",
)
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("track", type=str, required=True, nargs=-1)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option("--verbose", is_flag=True, help="Verbose mode: display every tweet")
def track(db_path, track, auth, verbose):
"Experimental: Save tweets matching these keywords in real-time"
auth = json.load(open(auth))
session = utils.session_for_auth(auth)
db = sqlite_utils.Database(db_path)
for tweet in utils.stream_filter(session, track=track):
if verbose:
print(json.dumps(tweet, indent=2))
with db.conn:
utils.save_tweets(db, [tweet])
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@add_identifier_options
@click.option("--ids", is_flag=True, help="Treat input as user IDs, not screen names")
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option("--verbose", is_flag=True, help="Verbose mode: display every tweet")
def follow(db_path, identifiers, attach, sql, ids, auth, verbose):
"Experimental: Follow these Twitter users and save tweets in real-time"
auth = json.load(open(auth))
session = utils.session_for_auth(auth)
db = sqlite_utils.Database(db_path)
identifiers = utils.resolve_identifiers(db, identifiers, attach, sql)
# Make sure we have saved these users to the database
for batch in utils.fetch_user_batches(session, identifiers, ids):
utils.save_users(db, batch)
# Ensure we have user IDs, not screen names
if ids:
follow = identifiers
else:
follow = utils.user_ids_for_screen_names(db, identifiers)
# Start streaming:
for tweet in utils.stream_filter(session, follow=follow):
if verbose:
print(json.dumps(tweet, indent=2))
with db.conn:
utils.save_tweets(db, [tweet])
def _shared_friends_ids_followers_ids(
db_path, identifiers, attach, sql, auth, ids, sleep, api_url, first_key, second_key
):
auth = json.load(open(auth))
session = utils.session_for_auth(auth)
db = sqlite_utils.Database(db_path)
identifiers = utils.resolve_identifiers(db, identifiers, attach, sql)
for identifier in identifiers:
# Make sure this user is saved
arg_user_id = identifier if ids else None
arg_screen_name = None if ids else identifier
profile = utils.get_profile(session, arg_user_id, arg_screen_name)
user_id = profile["id"]
utils.save_users(db, [profile])
args = {("user_id" if ids else "screen_name"): identifier}
for id_batch in utils.cursor_paginate(
session, api_url, args, "ids", 5000, sleep
):
first_seen = datetime.datetime.utcnow().isoformat()
db["following"].insert_all(
(
{first_key: user_id, second_key: other_id, "first_seen": first_seen}
for other_id in id_batch
),
ignore=True,
)
time.sleep(sleep)