Skip to content

Commit

Permalink
Send a specific user-agent to tiny.cc as they seem to block urllib's …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
eht16 committed Feb 1, 2014
1 parent b2d577a commit 39f13cb
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions scripts/git2irc/git2irc.py
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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']
Expand Down

0 comments on commit 39f13cb

Please sign in to comment.