[Python-checkins] benchmarks: Issue #17785: Use a faster URL shortener for perf.py
alexandre.vassalotti
python-checkins at python.org
Sat Apr 20 22:00:06 CEST 2013
http://hg.python.org/benchmarks/rev/1488e1f55f61
changeset: 201:1488e1f55f61
user: Alexandre Vassalotti <alexandre at peadrop.com>
date: Sat Apr 20 12:59:46 2013 -0700
summary:
Issue #17785: Use a faster URL shortener for perf.py
files:
perf.py | 28 ++++++++++++++++------------
1 files changed, 16 insertions(+), 12 deletions(-)
diff --git a/perf.py b/perf.py
--- a/perf.py
+++ b/perf.py
@@ -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):
--
Repository URL: http://hg.python.org/benchmarks
More information about the Python-checkins
mailing list