Skip to content

Commit f05a327

Browse files
committed
use requests
1 parent 39aa4cd commit f05a327

File tree

2 files changed

+44
-84
lines changed

2 files changed

+44
-84
lines changed

setup.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@
33

44
IS_PY2 = sys.version_info[0] == 2
55

6-
_requirements = ['requests', ]
7-
if not IS_PY2:
8-
_requirements.append('requests_cache')
9-
10-
# 'requests' is installed as requirement by requests-cache,
11-
# commented out because it triggers a bug in setuptool:
12-
# https://bitbucket.org/pypa/setuptools/issue/196/tests_require-pytest-pytest-cov-breaks
13-
14-
6+
_requirements = ['requests', 'requests_cache']
157
_modules = ['tvdb_api', 'tvdb_ui', 'tvdb_exceptions']
8+
169
if IS_PY2:
1710
_modules.append('tvdb_cache')
1811

tvdb_api.py

Lines changed: 42 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
# project:tvdb_api
55
# repository:http://github.com/dbr/tvdb_api
66
# license:unlicense (http://unlicense.org/)
7+
import sys
8+
import os
9+
import time
10+
import requests
11+
import requests_cache
12+
import getpass
13+
import tempfile
14+
import warnings
15+
import logging
16+
import datetime
17+
18+
from urllib import quote as url_quote
19+
from tvdb_ui import BaseUI, ConsoleUI
20+
from tvdb_exceptions import (
21+
tvdb_error, tvdb_shownotfound,
22+
tvdb_seasonnotfound, tvdb_episodenotfound, tvdb_attributenotfound
23+
)
724

825
"""Simple-to-use Python interface to The TVDB's API (thetvdb.com)
926
@@ -17,31 +34,9 @@
1734
__author__ = "dbr/Ben"
1835
__version__ = "1.10"
1936

20-
import sys
2137

2238
IS_PY2 = sys.version_info[0] == 2
2339

24-
import os
25-
import time
26-
if IS_PY2:
27-
import requests
28-
import urllib2
29-
from tvdb_cache import CacheHandler
30-
from urllib import quote as url_quote
31-
else:
32-
import requests
33-
from urllib.parse import quote as url_quote
34-
import getpass
35-
import tempfile
36-
import warnings
37-
import logging
38-
import datetime
39-
40-
from tvdb_ui import BaseUI, ConsoleUI
41-
from tvdb_exceptions import (
42-
tvdb_error, tvdb_shownotfound,
43-
tvdb_seasonnotfound, tvdb_episodenotfound, tvdb_attributenotfound
44-
)
4540

4641
if IS_PY2:
4742
int_types = (int, long)
@@ -427,59 +422,31 @@ def __init__(self,
427422

428423
self.config['dvdorder'] = dvdorder
429424

430-
if not IS_PY2: # FIXME: Allow using requests in Python 2?
431-
import requests_cache
432-
if cache is True:
433-
self.session = requests_cache.CachedSession(
434-
expire_after=21600, # 6 hours
435-
backend='sqlite',
436-
cache_name=self._getTempDir(),
437-
)
438-
self.config['cache_enabled'] = True
439-
elif cache is False:
440-
self.session = requests.Session()
441-
self.config['cache_enabled'] = False
442-
elif isinstance(cache, text_type):
443-
# Specified cache path
444-
self.session = requests_cache.CachedSession(
445-
expire_after=21600, # 6 hours
446-
backend='sqlite',
447-
cache_name=os.path.join(cache, "tvdb_api"),
448-
)
449-
else:
450-
self.session = cache
451-
try:
452-
self.session.get
453-
except AttributeError:
454-
raise ValueError("cache argument must be True/False, string as cache path or requests.Session-type object (e.g from requests_cache.CachedSession)")
455-
else:
456-
# For backwards compatibility in Python 2.x
457-
if cache is True:
458-
self.config['cache_enabled'] = True
459-
self.config['cache_location'] = self._getTempDir()
460-
self.urlopener = urllib2.build_opener(
461-
CacheHandler(self.config['cache_location'])
425+
if cache is True:
426+
self.session = requests_cache.CachedSession(
427+
expire_after=21600, # 6 hours
428+
backend='sqlite',
429+
cache_name=self._getTempDir(),
430+
include_get_headers=True
462431
)
463-
464-
elif cache is False:
465-
self.config['cache_enabled'] = False
466-
self.urlopener = urllib2.build_opener() # default opener with no caching
467-
468-
elif isinstance(cache, basestring):
469-
self.config['cache_enabled'] = True
470-
self.config['cache_location'] = cache
471-
self.urlopener = urllib2.build_opener(
472-
CacheHandler(self.config['cache_location'])
432+
self.config['cache_enabled'] = True
433+
elif cache is False:
434+
self.session = requests.Session()
435+
self.config['cache_enabled'] = False
436+
elif isinstance(cache, (str, unicode)):
437+
# Specified cache path
438+
self.session = requests_cache.CachedSession(
439+
expire_after=21600, # 6 hours
440+
backend='sqlite',
441+
cache_name=os.path.join(cache, "tvdb_api"),
442+
include_get_headers=True
473443
)
474-
475-
elif isinstance(cache, urllib2.OpenerDirector):
476-
# If passed something from urllib2.build_opener, use that
477-
log().debug("Using %r as urlopener" % cache)
478-
self.config['cache_enabled'] = True
479-
self.urlopener = cache
480-
481-
else:
482-
raise ValueError("Invalid value for Cache %r (type was %s)" % (cache, type(cache)))
444+
else:
445+
self.session = cache
446+
try:
447+
self.session.get
448+
except AttributeError:
449+
raise ValueError("cache argument must be True/False, string as cache path or requests.Session-type object (e.g from requests_cache.CachedSession)")
483450

484451
self.config['banners_enabled'] = banners
485452
self.config['actors_enabled'] = actors
@@ -563,7 +530,7 @@ def _loadUrl(self, url, data=None, recache=False, language=None):
563530
if not self.__authorized:
564531
self.authorize()
565532

566-
r = requests.get(url, headers=self.headers).json()
533+
r = self.session.get(url, headers=self.headers).json()
567534
r_data = r.get('data')
568535
links = r.get('links')
569536

@@ -580,7 +547,7 @@ def _loadUrl(self, url, data=None, recache=False, language=None):
580547
return data
581548

582549
def authorize(self):
583-
r = requests.post('https://api.thetvdb.com/login', json=self.config['auth_payload'], headers=self.headers)
550+
r = self.session.post('https://api.thetvdb.com/login', json=self.config['auth_payload'], headers=self.headers)
584551
token = r.json().get('token')
585552
self.headers['Authorization'] = "Bearer %s" % token.encode('utf8')
586553
self.__authorized = True

0 commit comments

Comments
 (0)