4
4
# project:tvdb_api
5
5
# repository:http://github.com/dbr/tvdb_api
6
6
# 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
+ )
7
24
8
25
"""Simple-to-use Python interface to The TVDB's API (thetvdb.com)
9
26
17
34
__author__ = "dbr/Ben"
18
35
__version__ = "1.10"
19
36
20
- import sys
21
37
22
38
IS_PY2 = sys .version_info [0 ] == 2
23
39
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
- )
45
40
46
41
if IS_PY2 :
47
42
int_types = (int , long )
@@ -427,59 +422,31 @@ def __init__(self,
427
422
428
423
self .config ['dvdorder' ] = dvdorder
429
424
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
462
431
)
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
473
443
)
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)" )
483
450
484
451
self .config ['banners_enabled' ] = banners
485
452
self .config ['actors_enabled' ] = actors
@@ -563,7 +530,7 @@ def _loadUrl(self, url, data=None, recache=False, language=None):
563
530
if not self .__authorized :
564
531
self .authorize ()
565
532
566
- r = requests .get (url , headers = self .headers ).json ()
533
+ r = self . session .get (url , headers = self .headers ).json ()
567
534
r_data = r .get ('data' )
568
535
links = r .get ('links' )
569
536
@@ -580,7 +547,7 @@ def _loadUrl(self, url, data=None, recache=False, language=None):
580
547
return data
581
548
582
549
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 )
584
551
token = r .json ().get ('token' )
585
552
self .headers ['Authorization' ] = "Bearer %s" % token .encode ('utf8' )
586
553
self .__authorized = True
0 commit comments