diff --git a/HISTORY.rst b/HISTORY.rst index 79d3658..94734a5 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,9 +1,26 @@ History ======= -0.2.4 (2016-01-01) +0.2.4.imio2 (unreleased) +------------------------ + +- Nothing changed yet. + + +0.2.4 (2020-12-04) ------------------ +* Quickfix: Always try to parse JSON (as default) from response + Youtube stopped sending correct Content-Type header: text/html instead of JSON + [laulaz] + +* Add timeout + Give the possibility to add a urllib timeout for a OEmbedEndpoint + [jfroche] + +* support non-standard text/javascript response + [sk1p, abarmat] + * Fix packaging por pypi 0.2.3 (2015-04-02) diff --git a/oembed/__init__.py b/oembed/__init__.py index 4096e9d..e504290 100644 --- a/oembed/__init__.py +++ b/oembed/__init__.py @@ -64,7 +64,7 @@ import urllib2 # Python 2 import re - +NO_TIMEOUT=object() # json module is in the standard library as of python 2.6; fall back to # simplejson if present for older versions. try: @@ -214,7 +214,7 @@ class OEmbedEndpoint(object): This class handles a number of URL schemes and manage resource retrieval. ''' - def __init__(self, url, urlSchemes=None): + def __init__(self, url, urlSchemes=None, timeout=NO_TIMEOUT): ''' Create a new OEmbedEndpoint object. @@ -226,6 +226,7 @@ def __init__(self, url, urlSchemes=None): self._urlSchemes = {} self._initRequestHeaders() self._urllib = urllib2 + self._timeout = timeout if urlSchemes is not None: for urlScheme in urlSchemes: @@ -348,7 +349,11 @@ def fetch(self, url): ''' opener = self._urllib.build_opener() opener.addheaders = self._requestHeaders.items() - response = opener.open(url) + if self._timeout != NO_TIMEOUT: + response = opener.open(url, timeout=self._timeout) + else: + response = opener.open(url) + headers = response.info() raw = response.read() raw = raw.decode('utf8') @@ -364,7 +369,10 @@ def fetch(self, url): headers['Content-Type'].find('text/json') != -1: response = OEmbedResponse.newFromJSON(raw) else: - raise OEmbedError('Invalid mime-type in response - %s' % headers['Content-Type']) + try: + response = OEmbedResponse.newFromJSON(raw) + except: + raise OEmbedError('Invalid mime-type in response - %s' % headers['Content-Type']) return response diff --git a/setup.py b/setup.py index 22472cf..4535a7f 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ def publish(): publish() sys.exit() -version = '0.2.4' +version = '0.2.4.imio2.dev0' setup( name='python-oembed',