From 39f13cb3b6a47e535a04385a0d2207bcec9fce87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Tr=C3=B6ger?= Date: Sat, 1 Feb 2014 14:11:25 +0100 Subject: [PATCH] Send a specific user-agent to tiny.cc as they seem to block urllib's default For some reason, tiny.cc started to block requests with the user-agent "Python-urllib/2.7" which is the default of the urllib module. Using any other user-agent works so we send our own including a link to the script's source code. --- scripts/git2irc/git2irc.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/git2irc/git2irc.py b/scripts/git2irc/git2irc.py index c47f309..f8653ca 100644 --- a/scripts/git2irc/git2irc.py +++ b/scripts/git2irc/git2irc.py @@ -46,7 +46,7 @@ from cgi import FieldStorage from json import loads -from urllib2 import urlopen +from urllib2 import urlopen, Request from urllib import urlencode import logging import logging.handlers @@ -63,6 +63,9 @@ # extend on demand LOG_EMAIL_ADDRESSES = ['enrico@geany.org'] +# user-agent to be used for requests to tiny.cc +USER_AGENT = u'git2irc.py - https://raw.github.com/geany/infrastructure/master/scripts/git2irc/git2irc.py' + # global and cuts across concerns, assumed to be properly initialized later logger = None # see init_logging() config = {'git': {}, 'irc': {}, 'shortener': {}} # see init_config() @@ -135,8 +138,9 @@ def shorten_url(long_url): req_enc = urlencode(req) req_url = '%s?%s' % (config['shortener']['url'], req_enc) short_url = long_url # default is to return same URL (ie. in case of error) + request = Request(req_url, headers={"User-Agent": USER_AGENT}) try: - resp_file = urlopen(req_url) + resp_file = urlopen(request) resp_dict = loads(resp_file.read()) if int(resp_dict['errorCode']) == 0: short_url = resp_dict['results']['short_url']