11import gzip
2+ import socket
23import random
34from time import sleep
4- from StringIO import StringIO
55from xml .etree import ElementTree
6+ from urllib .request import Request
67
7- from utils import movie_size_and_hash , get_session , log
8-
9- # s1-9, s101-109
10- SUB_DOMAINS = ['s1' , 's2' , 's3' , 's4' , 's5' , 's6' , 's7' , 's8' , 's9' ,
11- 's101' , 's102' , 's103' , 's104' , 's105' , 's106' , 's107' , 's108' , 's109' ]
12- API_URL_TEMPLATE = "http://{sub_domain}.api.bsplayer-subtitles.com/v1.php"
13-
14-
15- def get_sub_domain ():
16- sub_domains_end = len (SUB_DOMAINS ) - 1
17- return API_URL_TEMPLATE .format (sub_domain = SUB_DOMAINS [random .randint (0 , sub_domains_end )])
8+ from .utils import movie_size_and_hash , get_session , log
189
1910
2011class BSPlayer (object ):
12+ VERSION = "2.67"
13+ DOMAIN = "api.bsplayer-subtitles.com"
14+ SUB_DOMAINS = [
15+ 's1' , 's2' , 's3' , 's4' , 's5' , 's6' , 's7' , 's8' ,
16+ 's101' , 's102' , 's103' , 's104' , 's105' , 's106' , 's107' , 's108' , 's109'
17+ ]
18+
2119 def __init__ (self , search_url = None , proxies = None ):
2220 self .session = get_session (proxies = proxies )
23- self .search_url = search_url or get_sub_domain ()
21+ self .search_url = search_url or self . get_sub_domain ()
2422 self .token = None
2523
2624 def __enter__ (self ):
@@ -30,33 +28,43 @@ def __enter__(self):
3028 def __exit__ (self , exc_type , exc_val , exc_tb ):
3129 return self .logout ()
3230
31+ def get_sub_domain (self , tries = 5 ):
32+ for t in range (tries ):
33+ domain = f"{ random .choice (self .SUB_DOMAINS )} .{ self .DOMAIN } "
34+ try :
35+ socket .gethostbyname (domain )
36+ return f"http://{ domain } /v1.php"
37+ except socket .gaierror :
38+ continue
39+ raise Exception ("API Domain not found" )
40+
3341 def api_request (self , func_name = 'logIn' , params = '' , tries = 5 ):
3442 headers = {
3543 'User-Agent' : 'BSPlayer/2.x (1022.12360)' ,
3644 'Content-Type' : 'text/xml; charset=utf-8' ,
3745 'Connection' : 'close' ,
38- 'SOAPAction' : '"http://api.bsplayer-subtitles.com /v1.php#{func_name}"' . format ( func_name = func_name )
46+ 'SOAPAction' : f '"http://{ self . DOMAIN } /v1.php#{ func_name } "'
3947 }
4048 data = (
4149 '<?xml version="1.0" encoding="UTF-8"?>\n '
4250 '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
4351 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
4452 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
45- 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="{search_url}">'
53+ f 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="{ self . search_url } ">'
4654 '<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
47- '<ns1:{func_name}>{params}</ns1:{func_name}></SOAP-ENV:Body></SOAP-ENV:Envelope>'
48- ). format ( search_url = self . search_url , func_name = func_name , params = params )
55+ f '<ns1:{ func_name } >{ params } </ns1:{ func_name } ></SOAP-ENV:Body></SOAP-ENV:Envelope>'
56+ )
4957
5058 log ('BSPlayer.api_request' , 'Sending request: %s.' % func_name )
51- for i in xrange (tries ):
59+ for i in range (tries ):
5260 try :
53- self .session . addheaders . extend ( headers . items () )
54- res = self .session .open (self . search_url , data )
61+ req = Request ( self .search_url , data = data . encode (), headers = headers , method = "POST" )
62+ res = self .session .open (req )
5563 return ElementTree .fromstring (res .read ())
56- except Exception , ex :
64+ except Exception as ex :
5765 log ("BSPlayer.api_request" , "ERROR: %s." % ex )
5866 if func_name == 'logIn' :
59- self .search_url = get_sub_domain ()
67+ self .search_url = self . get_sub_domain ()
6068 sleep (1 )
6169 log ('BSPlayer.api_request' , 'ERROR: Too many tries (%d)...' % tries )
6270 raise Exception ('Too many tries...' )
@@ -68,9 +76,11 @@ def login(self):
6876
6977 root = self .api_request (
7078 func_name = 'logIn' ,
71- params = ('<username></username>'
72- '<password></password>'
73- '<AppID>BSPlayer v2.67</AppID>' )
79+ params = (
80+ '<username></username>'
81+ '<password></password>'
82+ f'<AppID>BSPlayer v{ self .VERSION } </AppID>'
83+ )
7484 )
7585 res = root .find ('.//return' )
7686 if res .find ('status' ).text == 'OK' :
@@ -86,7 +96,7 @@ def logout(self):
8696
8797 root = self .api_request (
8898 func_name = 'logOut' ,
89- params = '<handle>{token}</handle>' . format ( token = self . token )
99+ params = f '<handle>{ self . token } </handle>'
90100 )
91101 res = root .find ('.//return' )
92102 self .token = None
@@ -102,18 +112,21 @@ def search_subtitles(self, movie_path, language_ids='heb,eng', logout=False):
102112 if isinstance (language_ids , (tuple , list , set )):
103113 language_ids = "," .join (language_ids )
104114
105- movie_size , movie_hash = movie_size_and_hash (movie_path )
106- log ('BSPlayer.search_subtitles' , 'Movie Size: %s, Movie Hash: %s.' % (movie_size , movie_hash ))
115+ try :
116+ movie_size , movie_hash = movie_size_and_hash (movie_path )
117+ except Exception as ex :
118+ print (ex )
119+ exit (1 )
120+ log ('BSPlayer.search_subtitles' , f'Movie Size: { movie_size } , Movie Hash: { movie_hash } .' )
107121 root = self .api_request (
108122 func_name = 'searchSubtitles' ,
109123 params = (
110- '<handle>{token}</handle>'
111- '<movieHash>{movie_hash}</movieHash>'
112- '<movieSize>{movie_size}</movieSize>'
113- '<languageId>{language_ids}</languageId>'
124+ f '<handle>{ self . token } </handle>'
125+ f '<movieHash>{ movie_hash } </movieHash>'
126+ f '<movieSize>{ movie_size } </movieSize>'
127+ f '<languageId>{ language_ids } </languageId>'
114128 '<imdbId>*</imdbId>'
115- ).format (token = self .token , movie_hash = movie_hash ,
116- movie_size = movie_size , language_ids = language_ids )
129+ )
117130 )
118131 res = root .find ('.//return/result' )
119132 if res .find ('status' ).text != 'OK' :
@@ -129,7 +142,8 @@ def search_subtitles(self, movie_path, language_ids='heb,eng', logout=False):
129142 subDownloadLink = item .find ('subDownloadLink' ).text ,
130143 subLang = item .find ('subLang' ).text ,
131144 subName = item .find ('subName' ).text ,
132- subFormat = item .find ('subFormat' ).text
145+ subFormat = item .find ('subFormat' ).text ,
146+ subRating = item .find ('subRating' ).text or '0'
133147 ))
134148
135149 if logout :
@@ -141,10 +155,10 @@ def search_subtitles(self, movie_path, language_ids='heb,eng', logout=False):
141155 def download_subtitles (download_url , dest_path , proxies = None ):
142156 session = get_session (proxies = proxies , http_10 = True )
143157 session .addheaders = [('User-Agent' , 'Mozilla/4.0 (compatible; Synapse)' ),
144- ('Content-Length' , 0 )]
158+ ('Content-Length' , 0 )]
145159 res = session .open (download_url )
146160 if res :
147- gf = gzip .GzipFile (fileobj = StringIO ( res . read ()) )
161+ gf = gzip .GzipFile (fileobj = res )
148162 with open (dest_path , 'wb' ) as f :
149163 f .write (gf .read ())
150164 f .flush ()
0 commit comments