view 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
line wrap: on
line source

import configparser
from collections import defaultdict
import twitter

TWEET_COUNT = 200
BAR_SIZE = 50

def __build_api():
    config = configparser.ConfigParser()
    config.read('settings.ini')
    app_config = config['DEFAULT']

    return twitter.Api(consumer_key=app_config['consumer_key'],
                       consumer_secret=app_config['consumer_secret'],
                       access_token_key=app_config['access_token'],
                       access_token_secret=app_config['access_token_secret'])

def __fetch_tweets(twitter_api, start=None):
    return twitter_api.GetHomeTimeline(count=TWEET_COUNT, max_id=start)

def _main():
    api = __build_api()
    new_tweets = __fetch_tweets(api)
    tweets = new_tweets
    i = 0
    while new_tweets and i < 5:
        oldest_tweet_id = new_tweets[-1].id
        new_tweets = __fetch_tweets(api, oldest_tweet_id)
        tweets += new_tweets
        i = i + 1

    print(len(tweets))
    authors = defaultdict(int)
    for tweet in tweets:
        authors[tweet.user.screen_name] += 1

    sorted_authors = sorted(authors.items(), key=lambda x: x[1], reverse=True)
    max_count = sorted_authors[0][1]
    #scale = BAR_SIZE * 1.0 / max_count
    for author in sorted_authors:
        name = '@{}'.format(author[0])
        author_tweets = author[1]
        author_bar_size = round((author_tweets / max_count) * BAR_SIZE)
        print("{:>20.20} - {:3d}: {}".format(name, author_tweets, '#' * author_bar_size))

if __name__ == '__main__':
    _main()