Skip to content

Commit

Permalink
allow unit of measurement to be changed: second, minute, hour, day, year
Browse files Browse the repository at this point in the history
  • Loading branch information
edsu committed Mar 2, 2018
1 parent e7cafd8 commit bcc820e
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions utils/tweetometer.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,56 @@
#!/usr/bin/env python3

"""
Reads tweet or Twitter user JSON and outputs a CSV of when the user account was
created, how many tweets they have sent to date, and their average tweets per
hour. The unit of measurement can be changed to second, minute, day and year
with the --unit option.
"""

import json
import optparse
import fileinput
import dateutil.parser
from datetime import datetime, timezone


op = optparse.OptionParser()
op.add_option('--unit', choices=['second', 'minute', 'hour', 'day', 'year'], default='hour')

opts, args = op.parse_args()

if opts.unit == 'second':
div = 1
elif opts.unit == 'minute':
div = 60
elif opts.unit == 'hour':
div = 60 * 60
elif opts.unit == 'day':
div = 60 * 60 * 24
elif opts.unit == 'year':
div = 60 * 60 * 24 * 365

now = datetime.now(timezone.utc)

for line in fileinput.input():
u = json.loads(line)
print('scree_name,tweets per %s' % opts.unit)

for line in fileinput.input(args):
t = json.loads(line)
if 'user' in t:
u = t['user']
elif 'screen_name' in t:
u = t
else:
raise Exception("not a tweet or user JSON object")

created_at = dateutil.parser.parse(u['created_at'])
age = now - created_at
days = age.total_seconds() / 60.0 / 60.0 / 24.0
unit = age.total_seconds() / float(div)
total = u['statuses_count']
tweets_per_day = total / days
tweets_per_unit = total / unit
print('%s,%s,%s,%0.2f' % (
u['screen_name'],
total,
created_at,
tweets_per_day
tweets_per_unit
))

0 comments on commit bcc820e

Please sign in to comment.