Last active
August 11, 2018 00:38
-
-
Save h4k1m0u/5055f4b5baa3292312a0eaea0731afc0 to your computer and use it in GitHub Desktop.
Number of tweets per month
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import tweepy | |
import matplotlib.pyplot as plt | |
from collections import OrderedDict | |
# app credentials | |
CONSUMER_KEY = '' | |
CONSUMER_SECRET = '' | |
ACCESS_TOKEN = '' | |
ACCESS_TOKEN_SECRET = '' | |
# authenticate app | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) | |
# api | |
api = tweepy.API(auth, wait_on_rate_limit=True) | |
# count tweets per month (last 3 years) | |
counts_month = {} | |
for year in reversed(range(2016, 2019)): | |
for month in reversed(range(1, 13)): | |
counts_month[str(year) + str(month).zfill(2)] = 0 | |
for tweet in tweepy.Cursor(api.user_timeline).items(): | |
year = tweet.created_at.year | |
month = tweet.created_at.month | |
counts_month[str(year) + str(month).zfill(2)] += 1 | |
# plot bar diagram | |
counts_month = OrderedDict(sorted(counts_month.items())) | |
months = counts_month.keys() | |
months_labels = [k[4:6] + '/' + k[:4] for k in months] | |
counts = counts_month.values() | |
plt.bar(months, counts) | |
plt.xticks(range(len(months)), months_labels, rotation=60) | |
plt.title('Number of tweets per month') | |
plt.ylabel('Number of tweets') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment