changeset 0:b2ff4e4d4085

Initial (only?) commit of WhoTweets script
author IBBoard <dev@ibboard.co.uk>
date Mon, 02 Oct 2017 19:02:47 +0100
parents
children 2634c968e4ac
files .hgignore README.md settings.example.ini whotweets.py
diffstat 4 files changed, 101 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Mon Oct 02 19:02:47 2017 +0100
@@ -0,0 +1,2 @@
+.vscode
+settings.ini
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Mon Oct 02 19:02:47 2017 +0100
@@ -0,0 +1,47 @@
+PyWhoTweets
+===========
+
+About
+-----
+
+PyWhoTweets is a small Python command-line script that fetches a set of tweets from your timeline
+and tallies up the authors to work out who tweets most in your timeline.
+
+Getting Started
+---------------
+
+PyWhoTweets is written in Python 3 and uses `python-twitter` for API access.
+
+Before using PyWhoTweets, you need to create a set of secrets. To do this,
+go to https://apps.twitter.com/ and create a new application for PyWhoTweets.
+Once you have created the new app, put the keys in `settings.ini` using
+the example file as a template.
+
+
+Using PyWhoTweets
+-----------------
+
+To use PyWhoTweets, just configure the keys and run `python3 whotweets.py`.
+The script will then make a series of requests to Twitter for tweets before
+counting them up and displaying an ASCII graph of tweet counts.
+
+
+License
+-------
+
+PyWhoTweets is mainly API calls, but is released under the Apache License, Version 2.0.
+
+
+Copyright 2017 IBBoard
+
+Licensed under the Apache License, Version 2.0 (the 'License');
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an 'AS IS' BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/settings.example.ini	Mon Oct 02 19:02:47 2017 +0100
@@ -0,0 +1,5 @@
+[DEFAULT]
+consumer_key=ABC
+consumer_secret=DEFHIJ
+access_token=KLMNOP
+access_token_secret=QRSTUV
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/whotweets.py	Mon Oct 02 19:02:47 2017 +0100
@@ -0,0 +1,47 @@
+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()