This repository was archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·408 lines (343 loc) · 12.9 KB
/
main.py
File metadata and controls
executable file
·408 lines (343 loc) · 12.9 KB
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
#!/usr/bin/env python
import coloredlogs, logging, logging.config
import twitter
import click
from os.path import split
from time import sleep
from twitter_cli.config import *
from twitter_cli.models import *
from twitter_cli.downloader import *
from twitter_cli.counter import *
from requests.exceptions import SSLError
# Refer to
# 1. https://stackoverflow.com/a/7507842/1677041
# 2. https://stackoverflow.com/a/49400680/1677041
# 3. https://docs.python.org/2/library/logging.config.html
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S',
},
'colored': {
'()': 'coloredlogs.ColoredFormatter',
'format': "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
'datefmt': '%H:%M:%S',
}
},
'handlers': {
'default': {
'level': 'DEBUG' if __debug__ else 'INFO',
'formatter': 'standard',
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout', # Default is stderr
},
'console': {
'level': 'DEBUG' if __debug__ else 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'colored',
'stream': 'ext://sys.stdout'
},
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': 'main.log',
'maxBytes': 1024 * 1024,
'backupCount': 10
},
},
'loggers': {
'': { # root logger
'handlers': ['console', 'file'],
'level': 'DEBUG',
'propagate': False
},
'__main__': { # if __name__ == '__main__'
'handlers': ['console', 'file'],
'level': 'DEBUG',
'propagate': False
},
'twitter_cli.downloader': {
'handlers': ['console', 'file'],
'level': 'DEBUG',
'propagate': False
},
'twitter': {
'handlers': ['console', 'file'],
'level': 'DEBUG',
'propagate': False
},
'twitter_cli': {
'handlers': ['console', 'file'],
'level': 'DEBUG',
'propagate': False
},
}
}
logging.config.dictConfig(LOGGING_CONFIG)
logger = logging.getLogger(__name__)
api = twitter.Api(proxies=get_proxy(), **get_keys())
PAGE_SIZE = 100
MYSELF = 'me'
def print_sample_entity(entities, prefix=''):
if not entities:
entities = 'Empty'
if type(entities) in (type, list):
entity = entities[0] if len(entities) > 0 else ''
logger.info('\nCount of %s: %d' % (prefix, len(entities) if entities else 0))
else:
entity = entities
if isinstance(entity, twitter.models.TwitterModel):
content = pretty_json_string(entity.AsDict())
elif isinstance(entity, dict):
content = pretty_json_string(entity)
else:
content = str(entity)
logger.info(prefix + content)
def destroy_favorited_status(status, retry=10):
if not status or not status.id:
return False
try:
logger.info('Destroying the favorited status %s' % status.id)
result = api.DestroyFavorite(status=status)
logger.info('Destroy finished!')
return result != None
except twitter.TwitterError as e:
logger.exception(e)
if e.args[0][0]['code'] == 144:
# TwitterError: No status found with that ID
# Let's just pretend it as suceed.
return True
except SSLError as e:
if retry > 0:
return destroy_favorited_status(status, retry=retry-1)
else:
raise e
return False
def save_status(status, destroy=False, timeline=None):
if isinstance(status, TwitterModel):
s = Status.initialize(status)
elif isinstance(status, PeeweeModel):
s = status
else:
assert('Unexpected status type! %s' % status)
logger.debug('Status: %s\n%s:[\n%s\n]' % (s.id, s.user.name, s.text))
should_destory = False
video = s.video
if video:
logger.debug('Found video: %s' % video)
path = get_video_storage_path(is_favorite=s.favorited, timeline=timeline)
extension = split(video.content_type)[-1]
filepath = download_video(video.video_url, path, prefix=str(status.id), extension=extension)
if filepath:
video.downloaded_path = filepath
video.save()
should_destory = True
Counter.success()
else:
logger.error('Download video failed?')
Counter.failure()
if s.is_photo:
for p in s.photos:
logger.debug('Found photo: %s' % p)
path = get_photo_storage_path(is_favorite=s.favorited, timeline=timeline)
filepath = download_photo(p.media_url_https or p.media_url, path, prefix=str(status.id))
if filepath:
p.downloaded_path = filepath
p.save()
should_destory = True
Counter.success()
else:
logger.error('Download photo failed?')
Counter.failure()
if s.quoted_status:
logger.debug('Found quoted status: %s\n%s:[\n%s\n]' % (s.quoted_status.id, s.quoted_status.user.name, s.quoted_status.text))
save_status(s.quoted_status)
should_destory = True
if s.retweeted_status:
logger.debug('Found retweeted status: %s\n%s:[\n%s\n]' % (s.retweeted_status.id, s.retweeted_status.user.name, s.retweeted_status.text))
save_status(s.retweeted_status)
should_destory = True
if destroy:
if not should_destory:
logger.warning('Destroying this status even no media found!%s', print_sample_entity(s, 'Details:\n'))
destroy_favorited_status(status)
def fetch_iteriable_statuses(pfunc):
max_id = None
statuses = None
while True:
try:
logger.info('Request starts with %s' % (max_id or 'latest'))
statuses = pfunc(max_id)
logger.info('Request completed with %d status(es)', len(statuses) if statuses else 0)
except twitter.TwitterError as e:
logger.exception(e)
statuses = None
code = e.args[0][0]['code']
if code == 88:
# TwitterError: Rate limit exceeded.
# https://developer.twitter.com/en/docs/basics/rate-limiting
# Let's try it in next hour later.
logger.info('Start sleeping because rate limit exceeded')
sleep(2 * 60 * 60)
logger.info('End sleeping')
continue
elif code == 34:
# "Sorry, that page does not exist."
break
except Exception as e:
logger.exception(e)
statuses = None
if statuses and len(statuses) > 0:
for status in statuses:
yield status
new_max_id = statuses[-1].id
if new_max_id == max_id:
logger.info('This should be the normal ending.')
break
max_id = new_max_id
else:
break
@click.group()
@click.option('--debug/--no-debug', default=False, help='Enable logger level to DEBUG')
@click.pass_context
def cli(ctx, debug):
logger.setLevel(logging.DEBUG if debug else logging.INFO)
debug and click.echo('Debug mode is on')
@cli.command()
@click.pass_context
def configure(ctx):
'''
Show the current configurations.
'''
logger.info('\n%s' % get_raw_config())
@cli.command()
@click.pass_context
def credential(ctx):
'''
Verify the current user credential.
'''
try:
userinfo = api.VerifyCredentials()
except twitter.TwitterError as e:
logger.exception(e)
logger.error('Make sure you\'ve configured the twitter keys right in file %s.' % CONFIG_FILE)
if userinfo:
print_sample_entity(userinfo, 'User info:')
logger.info('Verification pass!')
else:
logger.error('Verification Failed')
@cli.command()
@click.argument('usernames', nargs=-1, type=click.STRING)
@click.option('--pinned/--no-pinned', default=False, help='Append the pinned users to current usernames')
@click.option('--download-media/--no-download-media', default=True, help='Download status\'s media files or not')
@click.option('--schedule', type=click.INT, default=0, help='Run as scheduler with specified hours')
@click.pass_context
def timeline(ctx, usernames, pinned, download_media, schedule):
'''
Fetch the specified users' timeline.
'''
# Convert the tuple to list
usernames = list(usernames)
if pinned:
usernames.extend(get_pinned_users())
if len(usernames) <= 0:
usernames = [MYSELF]
def job():
generators = {}
for username in usernames:
if username == MYSELF:
pfunc = lambda max_id: api.GetHomeTimeline(count=PAGE_SIZE, max_id=max_id)
elif len(username) > 0:
pfunc = lambda max_id: api.GetUserTimeline(screen_name=username, count=PAGE_SIZE, max_id=max_id)
else:
pfunc = None
if pfunc:
generators[username] = fetch_iteriable_statuses(pfunc)
for username, generator in generators.items():
logger.info('Fetching timeline of pinned user [%s]' % username)
for status in generator:
shall_download = status.favorite_count * 1.0 / status.user.followers_count > 0.05
shall_download |= status.user.retweet_count * 1.0 / status.user.followers_count > 0.01
if shall_download or download_media:
save_status(status, timeline=username)
else:
print_sample_entity(status, prefix='Info:')
if schedule <= 0:
job()
else:
while True:
try:
job()
except (KeyboardInterrupt, SystemExit):
logger.warning('Interrupt by keyboard, stopping')
break
except Exception as e:
logger.exception(e)
logger.info('Start sleeping, waiting for next schedule...')
sleep(schedule * 60 * 60)
logger.info('End sleeping')
logger.info('Done!')
@cli.command()
@click.option('--from-latest/--from-last', default=False, help='Fetch statuses from latest or last saved one')
@click.option('--download-media/--no-download-media', default=True, help='Download status\'s media files or not')
@click.option('--destroy/--no-destroy', default=False, help='Destroy the favorite statuses')
@click.option('--schedule', type=click.INT, default=0, help='Run as scheduler with specified hours')
@click.pass_context
def favorites(ctx, from_latest, download_media, destroy, schedule):
'''
Fetch the user's favorite statuses.
'''
def job():
if from_latest:
max_id = None
else:
status = Status.select().order_by(Status.id).first()
max_id = status.id if status else None
for status in fetch_iteriable_statuses(lambda max_id: api.GetFavorites(count=PAGE_SIZE, max_id=max_id)):
if download_media:
save_status(status, destroy=destroy)
else:
print_sample_entity(status, prefix='Info: ')
if schedule <= 0:
job()
else:
while True:
try:
job()
except (KeyboardInterrupt, SystemExit):
logger.warning('Interrupt by keyboard, stopping')
break
except Exception as e:
logger.exception(e)
logger.info('Start sleeping, waiting for next schedule...')
sleep(schedule * 60 * 60)
logger.info('End sleeping')
wait_for_download_completed()
logger.info('Done!')
@cli.command()
@click.argument('names', nargs=-1, type=click.STRING)
@click.option('--username', type=click.STRING, default=None, help='List owner\'s screen name')
@click.option('--download-media/--no-download-media', default=True, help='Download status\'s media files or not')
@click.pass_context
def list(ctx, names, username, download_media,):
'''
Fetch the statuses of specified list.
'''
tweet_lists = api.GetLists(screen_name=username)
if len(names) > 0:
tweet_lists = tweet_lists.filter(lambda x: x.name in names)
if len(tweet_lists) <= 0:
logger.warn("Not found expected list!")
return
for tweet_list in tweet_lists:
for status in fetch_iteriable_statuses(lambda max_id: api.GetListTimeline(list_id=tweet_list.id, count=PAGE_SIZE, max_id=max_id)):
if download_media:
save_status(status, destroy=False)
else:
print_sample_entity(status, prefix='Info: ')
if __name__ == '__main__':
cli()