Mercurial > repos > other > WhoTweets
annotate whotweets.py @ 1:2634c968e4ac default tip
Add a bit more context to the README
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 02 Oct 2017 19:09:19 +0100 |
parents | b2ff4e4d4085 |
children |
rev | line source |
---|---|
0
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 import configparser |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 from collections import defaultdict |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 import twitter |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 TWEET_COUNT = 200 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 BAR_SIZE = 50 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 def __build_api(): |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 config = configparser.ConfigParser() |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 config.read('settings.ini') |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 app_config = config['DEFAULT'] |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 return twitter.Api(consumer_key=app_config['consumer_key'], |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 consumer_secret=app_config['consumer_secret'], |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 access_token_key=app_config['access_token'], |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 access_token_secret=app_config['access_token_secret']) |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 def __fetch_tweets(twitter_api, start=None): |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 return twitter_api.GetHomeTimeline(count=TWEET_COUNT, max_id=start) |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 def _main(): |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 api = __build_api() |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 new_tweets = __fetch_tweets(api) |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 tweets = new_tweets |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 i = 0 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 while new_tweets and i < 5: |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 oldest_tweet_id = new_tweets[-1].id |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 new_tweets = __fetch_tweets(api, oldest_tweet_id) |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 tweets += new_tweets |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 i = i + 1 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 print(len(tweets)) |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 authors = defaultdict(int) |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 for tweet in tweets: |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 authors[tweet.user.screen_name] += 1 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 sorted_authors = sorted(authors.items(), key=lambda x: x[1], reverse=True) |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 max_count = sorted_authors[0][1] |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 #scale = BAR_SIZE * 1.0 / max_count |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 for author in sorted_authors: |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 name = '@{}'.format(author[0]) |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 author_tweets = author[1] |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 author_bar_size = round((author_tweets / max_count) * BAR_SIZE) |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 print("{:>20.20} - {:3d}: {}".format(name, author_tweets, '#' * author_bar_size)) |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 if __name__ == '__main__': |
b2ff4e4d4085
Initial (only?) commit of WhoTweets script
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 _main() |