Skip to content

Instantly share code, notes, and snippets.

@agrif
Created July 4, 2012 01:18
Show Gist options
  • Save agrif/3044558 to your computer and use it in GitHub Desktop.
Save agrif/3044558 to your computer and use it in GitHub Desktop.
import sys
import urllib
import urllib2
import json
def short_url(url):
if not url:
return None
apiurl = 'https://www.googleapis.com/urlshortener/v1/url'
data = json.dumps({'longUrl' : url})
headers = {'Content-Type' : 'application/json'}
r = urllib2.Request(apiurl, data, headers)
try:
retdata = urllib2.urlopen(r).read()
retdata = json.loads(retdata)
return retdata.get('id', url)
except urllib2.URLError:
return url
except ValueError:
return url
fname = sys.argv[1]
commits = []
with open(fname, 'r') as f:
for line in f.readlines():
co, data = line.split(' ', 1)
data = eval(data)
commits.append((co, data))
base_url = "https://chart.googleapis.com/chart"
GET = {}
minimums = map(lambda el: el[1]['minimum'], commits)
stddev_down = map(lambda el: el[1]['average'] - el[1]['standard deviation'], commits)
stddev_up = map(lambda el: el[1]['average'] + el[1]['standard deviation'], commits)
maximums = map(lambda el: el[1]['maximum'], commits)
averages = map(lambda el: el[1]['average'], commits)
data_series = [minimums, stddev_down, stddev_up, maximums, averages]
data_types = ['F,000000,0,1:-1,40', 'H,000000,0,1:-1,1:20', 'H,000000,3,1:-1,1:20', 'H,000000,4,1:-1,1:40']
data_series = map(lambda dat: '-1,' + ','.join(map(str, dat)) + ',-1', data_series)
GET['chd'] = 't0:' + '|'.join(data_series)
GET['chm'] = '|'.join(data_types)
GET['chs'] = '%ix225' % (len(commits) * 80 + 100,)
GET['cht'] = 'ls'
GET['chxt'] = 'y,x'
GET['chds'] = '%i,%i' % (min(minimums), max(maximums))
GET['chxr'] = '0,%i,%i' % (min(minimums), max(maximums))
GET['chxl'] = '1:||' + '|'.join(map(lambda el: el[0], commits)) + '|'
final_url = base_url + '?' + urllib.urlencode(GET)
print short_url(final_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment