(original) (raw)

changeset: 201:1488e1f55f61 user: Alexandre Vassalotti alexandre@peadrop.com date: Sat Apr 20 12:59:46 2013 -0700 files: perf.py description: Issue #17785: Use a faster URL shortener for perf.py diff -r 134941cc41c0 -r 1488e1f55f61 perf.py --- a/perf.py Thu Apr 18 03:10:32 2013 -0700 +++ b/perf.py Sat Apr 20 12:59:46 2013 -0700 @@ -54,6 +54,7 @@ import csv import contextlib +import json import logging import math import optparse @@ -68,10 +69,9 @@ import time import threading try: - from urllib.request import urlopen - from urllib.error import URLError -except ImportError: - from urllib2 import urlopen, URLError + import http.client as httpclient +except: + import httplib as httpclient try: import multiprocessing except ImportError: @@ -784,21 +784,25 @@ def ShortenUrl(url): - """Shorten a given URL using tinyurl.com. + """Shorten a given URL using Google URL shortener. Args: url: url to shorten. Returns: - Shorter url. If tinyurl.com is not available, returns the original - url unaltered. + Shorter url. If the service is not available, returns None """ - tinyurl_api = "http://tinyurl.com/api-create.php?url=" try: - url = urlopen(tinyurl_api + url).read() - except URLError: - info("failed to call out to tinyurl.com") - return url + conn = httpclient.HTTPSConnection("www.googleapis.com") + body = json.dumps({"longUrl": url}) + headers = {"Content-Type": "application/json"} + conn.request("POST", "/urlshortener/v1/url", body, headers) + response = conn.getresponse() + if response.status == httpclient.OK: + return json.loads(response.read().decode("utf-8"))["id"] + except IOError: + info("failed to call out to URL shortening service") + return None def SummarizeData(data, points=100, summary_func=max): /alexandre@peadrop.com