Skip to content

Commit

Permalink
Merge pull request #7 from SaneBow/main
Browse files Browse the repository at this point in the history
@SaneBow 贡献的应付「验证码」的方法(感觉是故意留的后门?)
  • Loading branch information
pizzamx authored May 25, 2022
2 parents a1545b4 + 479cee7 commit 8e1f2af
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
6 changes: 5 additions & 1 deletion addon.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.subtitles.zimukux" name="ZiMuKuX" version="0.1.5" provider-name="pizzamx">
<addon id="script.subtitles.zimukux" name="ZiMuKuX" version="0.1.6" provider-name="pizzamx">
<requires>
<import addon="xbmc.python" version="3.0.0"/>
<import addon="script.module.beautifulsoup4" version="4.6.2"/>
Expand All @@ -20,6 +20,10 @@
<fanart>resources/fanart.png</fanart>
</assets>
<news>
v0.1.6(2022/5/25)
- Adapt to Zimuku's new protection mechanism in search
- Optimization to reduce the number of requests

v0.1.5(2022/4/22)
- Adaption made for Zimuku's new anti-spoofing mechanism
- Misc. bug fix and code tweaks
Expand Down
47 changes: 35 additions & 12 deletions resources/lib/zimuku_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

from ast import expr_context
import os
import sys
import time
Expand All @@ -30,21 +31,23 @@ class Zimuku_Agent:
def __init__(self, base_url, dl_location, logger, unpacker, settings):
self.ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'
self.ZIMUKU_BASE = base_url
self.ZIMUKU_API = '%s/search?q=%%s' % base_url
self.ZIMUKU_API = '%s/search?q=%%s&vertoken=%%s' % base_url
self.DOWNLOAD_LOCATION = dl_location
self.FILE_MIN_SIZE = 1024

self.logger = logger
self.unpacker = unpacker
self.plugin_settings = settings
self.session = requests.Session()
self.vertoken = ''

def set_setting(self, settings):
# for unittestting purpose
self.plugin_settings = settings

def get_page(self, url, **kwargs):
"""
Get page with requests.
Get page with requests session.
Parameters:
url Target URL.
Expand All @@ -56,26 +59,24 @@ def get_page(self, url, **kwargs):
"""
headers = None
http_body = None
s = self.session
try:
request_headers = {'User-Agent': self.ua}
if kwargs:
for key, value in list(kwargs.items()):
request_headers[key.replace('_', '-')] = value

s = requests.Session()
a = requests.adapters.HTTPAdapter(max_retries=3)
s.mount('http://', a)
# self.logger.log(sys._getframe().f_code.co_name, 'requests GET [%s]' % (url))
self.logger.log(sys._getframe().f_code.co_name, 'requests GET [%s]' % (url))

url += '&' if '?' in url else '?'
url += 'security_verify_data=313932302c31303830'

# https://github.com/pizzamx/zimuku_for_kodi/pull/5/commits/5d4ed2fbd87dc08682884c874018ac0f9f35b25c
url1 = url + '&security_verify_data=313932302c31303830'
url2 = url + '?security_verify_data=313932302c31303830'
s.get(url, headers=request_headers)
s.get(url1, headers=request_headers)
http_response = s.get(url, headers=request_headers)
if http_response.status_code != 200:
s.get(url2, headers=request_headers)
http_response = s.get(url2, headers=request_headers)
s.get(url, headers=request_headers)
http_response = s.get(url, headers=request_headers)

headers = http_response.headers
http_body = http_response.content
Expand Down Expand Up @@ -175,6 +176,23 @@ def extract_sub_info(self, sub, lang_info_mode):
"lang": langs
}

def get_vertoken(self):
# get vertoken from home page and cache it for the session
if self.vertoken:
return self.vertoken
else:
self.logger.log(sys._getframe().f_code.co_name, "Fetching new vertoken form home page")
try:
headers, data = self.get_page(self.ZIMUKU_BASE+'/')
hsoup = BeautifulSoup(data, 'html.parser')
vertoken = hsoup.find('input', attrs={'name': 'vertoken'}).attrs.get('value', '')
self.vertoken = vertoken
return vertoken
except Exception as e:
self.logger.log(sys._getframe().f_code.co_name, 'ERROR GETTING vertoken, E=(%s: %s)' %
(Exception, e), level=3)
return ''

def search(self, title, item):
"""
搜索字幕
Expand All @@ -195,7 +213,9 @@ def search(self, title, item):
"""
subtitle_list = []

url = self.ZIMUKU_API % (urllib.parse.quote(title))
vertoken = self.get_vertoken()

url = self.ZIMUKU_API % (urllib.parse.quote(title), vertoken)
self.logger.log(sys._getframe().f_code.co_name,
"Search API url: %s" % (url))
try:
Expand Down Expand Up @@ -538,3 +558,6 @@ def download_links(self, links, referer):
(referer),
level=2)
return '', ''

def close(self):
self.session.close()
6 changes: 3 additions & 3 deletions tests/test_zimuku.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ def setUp(self):
from lib import zimuku_agent as zmkagnt

global tmp_folder
tmp_folder = tempfile.TemporaryFile().name
tmp_folder = tempfile.TemporaryDirectory().name
os.mkdir(tmp_folder)

return super().setUp()

def get_agent(self, settings):
return zmkagnt.Zimuku_Agent(
'https://zimuku.org', tmp_folder, Logger(),
'http://zimuku.org', tmp_folder, Logger(),
Unpacker(tmp_folder),
settings)

Expand Down

0 comments on commit 8e1f2af

Please sign in to comment.