Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f63a25b9a8
Fetching contributors…

Cannot retrieve contributors at this time

50 lines (40 sloc) 1.568 kb
"""
bandeco.utils
Package with various helper functions used throughout the whole
application.
"""
import logging
import urllib
def canfail(f):
"""Decorator to allow a function to throw any Exception.
In case an Exception is raised, the decorated function returns
an empty string.
"""
def decorate(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception, e:
fname = f.__name__ if hasattr(f, '__name__') else '(unknown)'
logging.error("Exception on %s: %s" % (fname, str(e)))
return ''
return decorate
def fetch_url(url):
"""Fetches a given URL and returns it's content.
This functions fakes a Chromium-based browser by sending some
extra http headers besides the traditional User-Agent.
:param url: URL to fetch.
"""
browser = urllib.FancyURLopener()
browser.version = ('Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US) '
'AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.198.0'
' Safari/532.0')
browser.addheader('CONNECTION', 'keep-alive')
browser.addheader('ACCEPT', 'application/xml,application/xhtml'
'+xml,text/html;q=0.9,text/plain;q=0.8,image/'
'png,*/*;q=0.5')
browser.addheader('ACCEPT_CHARSET',
'ISO-8859-1,utf-8;q=0.7,*;q=0.3')
browser.addheader('ACCEPT_ENCODING', 'gzip,deflate,sdch')
browser.addheader('ACCEPT_LANGUAGE', 'en-US,en;q=0.8')
fd = browser.open(url)
return fd.read()
Jump to Line
Something went wrong with that request. Please try again.