-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterstats.py
executable file
·86 lines (64 loc) · 2.64 KB
/
twitterstats.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
from collections import defaultdict
from datetime import datetime, timedelta
import logging
import click
import tabulate
from twitter import Twitter
import twitterdb
# if the most recent tweet by a user is less than this old,
# don't bother pulling new tweets
# this is an optimisation so we don't waste api calls.
user_refresh = timedelta(hours=1)
logger = logging.getLogger('twitter')
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
@click.group(chain=False)
def cli():
pass
@cli.command(name='show-stats')
@click.argument('handle')
def show_stats(handle):
logger.info('generating stats report for {0}'.format(handle))
tdb = twitterdb.TwitterDB('sqlite:///{0}.db'
.format(handle), echo=False)
dates = [(datetime.today() - timedelta(days=i)).date()
for i in range(1, 8)]
headers = ['0. user', '1. today (so far)'] + \
[date.strftime('{0}. %a %x'.format(idx + 2))
for idx, date in enumerate(dates)]
today_counts = tdb.get_tweet_counts_for_date(datetime.now().date())
columns = defaultdict(list)
for count in today_counts:
columns[headers[0]].append(count[0])
columns[headers[1]].append(count[1])
for idx, date in enumerate(dates):
counts = tdb.get_tweet_counts_for_date(date)
for count in counts:
columns[headers[idx + 2]].append(count[1])
# I'm sure there's a more sensible way of doing this
# But tabulate doesn't want to sort the columns into a sensible order.
rows = zip(*([key] + value for key, value in sorted(columns.items())))
logger.info(tabulate.tabulate(rows, headers='firstrow'))
@cli.command(name='get-tweets')
@click.argument('handle')
def update_tweets(handle):
logger.info('updating tweets for users followed by {0}'
.format(handle))
tdb = twitterdb.TwitterDB('sqlite:///{0}.db'
.format(handle), echo=False)
t = Twitter(tdb)
ids = t.get_followed_ids(handle)
t.save_unknown_users(ids)
one_week_ago = datetime.now() - timedelta(days=7)
for id in ids:
user = tdb.get_user_by_id(id)
logger.info('Getting tweets for {0}:{1}'.format(user.user_name,
user.user_id))
t.get_tweets_until(id, one_week_ago, user_refresh)
logger.info('Done saving all the followed tweets I can!')
if __name__ == '__main__':
cli()