Created
March 23, 2010 15:51
-
-
Save philippbosch/341320 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# encoding: utf-8 | |
""" | |
twittertrend.py | |
Created by Philipp Bosch on 2010-03-23. | |
Copyleft (ɔ) 2010 Philipp Bosch. No rights reserved. | |
""" | |
import sys | |
import getopt | |
import urllib2 | |
import re | |
help_message = ''' | |
I want to be called like this: | |
twittertrend.py <year> <month> <day> <tag_1> ... <tag_n> | |
''' | |
class Usage(Exception): | |
def __init__(self, msg): | |
self.msg = msg | |
def main(argv=None): | |
if argv is None: | |
argv = sys.argv | |
try: | |
try: | |
opts, args = getopt.getopt(argv[1:], "ho:v", ["help", "output="]) | |
except getopt.error, msg: | |
raise Usage(msg) | |
# option processing | |
for option, value in opts: | |
if option == "-v": | |
verbose = True | |
if option in ("-h", "--help"): | |
raise Usage(help_message) | |
if option in ("-o", "--output"): | |
output = value | |
if len(argv) <= 4: | |
raise Usage('arguments missing') | |
except Usage, err: | |
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) | |
print >> sys.stderr, "\t for help use --help" | |
return 2 | |
tags = args[3:] | |
for tag in tags: | |
req = urllib2.Request('http://trendistic.com/%(tag)s/_since-%(year)s-%(month)s-%(day)s-00h-utc/_until-%(year)s-%(month)s-%(day)s-23h-utc' % {'tag': tag, 'year': args[0], 'month': args[1], 'day': args[2]}, headers={'Cookie': 'local_tz=1'}) | |
try: | |
url = urllib2.urlopen(req) | |
except urllib2.HTTPError, msg: | |
print >> sys.stderr, "HTTP error occured: %s" % msg | |
return 2 | |
html = url.read() | |
matches = re.search('var plot = \'([^\']+)\'', html) | |
data = matches.group(1).split('\\n') | |
total = 0 | |
for datum in data: | |
try: | |
datetime, foo, value = datum.split(',') | |
except ValueError: | |
print >> sys.stderr, "not enough data for '%s'" % tag | |
return 2 | |
total += int(value) | |
avg = float(total)/len(data) | |
print "%s: %s" % (tag, avg) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment