You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
450 lines
16 KiB
450 lines
16 KiB
6 years ago
|
# -*- coding: utf-8 -*-
|
||
5 years ago
|
from __future__ import absolute_import
|
||
2 years ago
|
import base64
|
||
6 years ago
|
import io
|
||
|
import logging
|
||
|
import os
|
||
|
import zipfile
|
||
5 years ago
|
import re
|
||
|
import copy
|
||
2 years ago
|
from PIL import Image
|
||
5 years ago
|
|
||
|
try:
|
||
|
from urlparse import urljoin
|
||
|
except ImportError:
|
||
|
from urllib.parse import urljoin
|
||
6 years ago
|
|
||
|
import rarfile
|
||
4 years ago
|
from babelfish import language_converters
|
||
6 years ago
|
from subzero.language import Language
|
||
|
from guessit import guessit
|
||
|
from requests import Session
|
||
|
from six import text_type
|
||
11 months ago
|
from random import randint, randrange
|
||
6 years ago
|
|
||
2 years ago
|
from python_anticaptcha import AnticaptchaClient, ImageToTextTask
|
||
4 years ago
|
from subliminal.providers import ParserBeautifulSoup
|
||
|
from subliminal_patch.providers import Provider
|
||
5 years ago
|
from subliminal.subtitle import (
|
||
|
SUBTITLE_EXTENSIONS,
|
||
4 years ago
|
fix_line_ending
|
||
2 years ago
|
)
|
||
4 years ago
|
from subliminal_patch.subtitle import (
|
||
5 years ago
|
Subtitle,
|
||
4 years ago
|
guess_matches
|
||
5 years ago
|
)
|
||
3 years ago
|
from .utils import FIRST_THOUSAND_OR_SO_USER_AGENTS as AGENT_LIST
|
||
6 years ago
|
from subliminal.video import Episode, Movie
|
||
|
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
4 years ago
|
language_converters.register('zimuku = subliminal_patch.converters.zimuku:zimukuConverter')
|
||
|
|
||
|
supported_languages = list(language_converters['zimuku'].to_zimuku.keys())
|
||
6 years ago
|
|
||
2 years ago
|
|
||
6 years ago
|
class ZimukuSubtitle(Subtitle):
|
||
|
"""Zimuku Subtitle."""
|
||
|
|
||
5 years ago
|
provider_name = "zimuku"
|
||
|
|
||
4 years ago
|
def __init__(self, language, page_link, version, session, year):
|
||
6 years ago
|
super(ZimukuSubtitle, self).__init__(language, page_link=page_link)
|
||
|
self.version = version
|
||
4 years ago
|
self.release_info = version
|
||
5 years ago
|
self.hearing_impaired = False
|
||
|
self.encoding = "utf-8"
|
||
|
self.session = session
|
||
4 years ago
|
self.year = year
|
||
6 years ago
|
|
||
|
@property
|
||
|
def id(self):
|
||
4 years ago
|
return self.page_link
|
||
6 years ago
|
|
||
|
def get_matches(self, video):
|
||
|
matches = set()
|
||
|
|
||
4 years ago
|
if video.year == self.year:
|
||
|
matches.add('year')
|
||
|
|
||
6 years ago
|
# episode
|
||
|
if isinstance(video, Episode):
|
||
5 years ago
|
info = guessit(self.version, {"type": "episode"})
|
||
6 years ago
|
# other properties
|
||
3 years ago
|
matches |= guess_matches(video, info)
|
||
4 years ago
|
|
||
|
# add year to matches if video doesn't have a year but series, season and episode are matched
|
||
|
if not video.year and all(item in matches for item in ['series', 'season', 'episode']):
|
||
|
matches |= {'year'}
|
||
6 years ago
|
# movie
|
||
|
elif isinstance(video, Movie):
|
||
|
# other properties
|
||
3 years ago
|
matches |= guess_matches(video, guessit(self.version, {"type": "movie"}))
|
||
6 years ago
|
|
||
|
return matches
|
||
|
|
||
|
|
||
2 years ago
|
def string_to_hex(s):
|
||
|
val = ""
|
||
|
for i in s:
|
||
|
val += hex(ord(i))[2:]
|
||
|
return val
|
||
|
|
||
|
|
||
6 years ago
|
class ZimukuProvider(Provider):
|
||
|
"""Zimuku Provider."""
|
||
|
|
||
4 years ago
|
languages = {Language(*l) for l in supported_languages}
|
||
3 years ago
|
video_types = (Episode, Movie)
|
||
4 years ago
|
logger.info(str(supported_languages))
|
||
5 years ago
|
|
||
11 months ago
|
server_url = "https://srtku.com"
|
||
2 years ago
|
search_url = "/search?q={}"
|
||
5 years ago
|
|
||
6 years ago
|
subtitle_class = ZimukuSubtitle
|
||
|
|
||
|
def __init__(self):
|
||
|
self.session = None
|
||
|
|
||
2 years ago
|
verify_token = ""
|
||
|
code = ""
|
||
2 years ago
|
location_re = re.compile(
|
||
11 months ago
|
r'self\.location = "(.*)" \+ stringToHex\(')
|
||
2 years ago
|
verification_image_re = re.compile(r'<img.*?src="data:image/bmp;base64,(.*?)".*?>')
|
||
2 years ago
|
|
||
|
def yunsuo_bypass(self, url, *args, **kwargs):
|
||
2 years ago
|
def parse_verification_image(image_content: str):
|
||
|
|
||
|
def bmp_to_image(base64_str, img_type='png'):
|
||
|
img_data = base64.b64decode(base64_str)
|
||
|
img = Image.open(io.BytesIO(img_data))
|
||
|
img = img.convert("RGB")
|
||
|
img_fp = io.BytesIO()
|
||
|
img.save(img_fp, img_type)
|
||
|
img_fp.seek(0)
|
||
|
return img_fp
|
||
|
|
||
|
fp = bmp_to_image(image_content)
|
||
|
task = ImageToTextTask(fp)
|
||
|
client = AnticaptchaClient(os.environ.get('ANTICAPTCHA_ACCOUNT_KEY'))
|
||
|
job = client.createTask(task)
|
||
|
job.join()
|
||
|
return job.get_captcha_text()
|
||
|
|
||
2 years ago
|
i = -1
|
||
|
while True:
|
||
|
i += 1
|
||
|
r = self.session.get(url, *args, **kwargs)
|
||
2 years ago
|
if r.status_code == 404:
|
||
|
# mock js script logic
|
||
2 years ago
|
tr = self.location_re.findall(r.text)
|
||
2 years ago
|
verification_image = self.verification_image_re.findall(r.text)
|
||
11 months ago
|
if len(verification_image):
|
||
|
self.code = parse_verification_image(verification_image[0])
|
||
|
else:
|
||
|
self.code = f"{randrange(800, 1920)},{randrange(600, 1080)}"
|
||
2 years ago
|
self.session.cookies.set("srcurl", string_to_hex(r.url))
|
||
|
if tr:
|
||
2 years ago
|
verify_resp = self.session.get(
|
||
2 years ago
|
urljoin(self.server_url, tr[0] + string_to_hex(self.code)), allow_redirects=False)
|
||
2 years ago
|
if verify_resp.status_code == 302 \
|
||
|
and self.session.cookies.get("security_session_verify") is not None:
|
||
2 years ago
|
pass
|
||
|
continue
|
||
|
if len(self.location_re.findall(r.text)) == 0:
|
||
2 years ago
|
self.verify_token = string_to_hex(self.code)
|
||
2 years ago
|
return r
|
||
|
|
||
6 years ago
|
def initialize(self):
|
||
|
self.session = Session()
|
||
3 years ago
|
self.session.headers["User-Agent"] = AGENT_LIST[randint(0, len(AGENT_LIST) - 1)]
|
||
6 years ago
|
|
||
|
def terminate(self):
|
||
|
self.session.close()
|
||
|
|
||
4 years ago
|
def _parse_episode_page(self, link, year):
|
||
2 years ago
|
r = self.yunsuo_bypass(link)
|
||
5 years ago
|
bs_obj = ParserBeautifulSoup(
|
||
|
r.content.decode("utf-8", "ignore"), ["html.parser"]
|
||
|
)
|
||
2 years ago
|
subs_body = bs_obj.find("tbody")
|
||
5 years ago
|
subs = []
|
||
5 years ago
|
for sub in subs_body.find_all("tr"):
|
||
|
a = sub.find("a")
|
||
|
name = _extract_name(a.text)
|
||
|
name = os.path.splitext(name)[
|
||
|
0
|
||
|
] # remove ext because it can be an archive type
|
||
|
|
||
5 years ago
|
language = Language("eng")
|
||
5 years ago
|
for img in sub.find("td", class_="tac lang").find_all("img"):
|
||
5 years ago
|
if (
|
||
2 years ago
|
"china" in img.attrs["src"]
|
||
|
and "hongkong" in img.attrs["src"]
|
||
2 years ago
|
):
|
||
4 years ago
|
language = Language("zho").add(Language('zho', 'TW', None))
|
||
2 years ago
|
logger.debug("language:" + str(language))
|
||
2 years ago
|
elif (
|
||
2 years ago
|
"china" in img.attrs["src"]
|
||
|
or "jollyroger" in img.attrs["src"]
|
||
5 years ago
|
):
|
||
|
language = Language("zho")
|
||
4 years ago
|
elif "hongkong" in img.attrs["src"]:
|
||
2 years ago
|
language = Language('zho', 'TW', None)
|
||
5 years ago
|
break
|
||
5 years ago
|
sub_page_link = urljoin(self.server_url, a.attrs["href"])
|
||
|
backup_session = copy.deepcopy(self.session)
|
||
|
backup_session.headers["Referer"] = link
|
||
|
|
||
|
subs.append(
|
||
4 years ago
|
self.subtitle_class(language, sub_page_link, name, backup_session, year)
|
||
5 years ago
|
)
|
||
|
|
||
|
return subs
|
||
|
|
||
6 years ago
|
def query(self, keyword, season=None, episode=None, year=None):
|
||
|
params = keyword
|
||
5 years ago
|
if season:
|
||
|
params += ".S{season:02d}".format(season=season)
|
||
6 years ago
|
elif year:
|
||
5 years ago
|
params += " {:4d}".format(year)
|
||
6 years ago
|
|
||
5 years ago
|
logger.debug("Searching subtitles %r", params)
|
||
6 years ago
|
subtitles = []
|
||
2 years ago
|
search_link = urljoin(self.server_url, text_type(self.search_url).format(params))
|
||
2 years ago
|
|
||
2 years ago
|
r = self.yunsuo_bypass(search_link, timeout=30)
|
||
6 years ago
|
r.raise_for_status()
|
||
|
|
||
|
if not r.content:
|
||
5 years ago
|
logger.debug("No data returned from provider")
|
||
6 years ago
|
return []
|
||
|
|
||
4 years ago
|
html = r.content.decode("utf-8", "ignore")
|
||
|
# parse window location
|
||
|
pattern = r"url\s*=\s*'([^']*)'\s*\+\s*url"
|
||
|
parts = re.findall(pattern, html)
|
||
|
redirect_url = search_link
|
||
|
while parts:
|
||
|
parts.reverse()
|
||
|
redirect_url = urljoin(self.server_url, "".join(parts))
|
||
2 years ago
|
r = self.session.get(redirect_url, timeout=30)
|
||
4 years ago
|
html = r.content.decode("utf-8", "ignore")
|
||
|
parts = re.findall(pattern, html)
|
||
|
logger.debug("search url located: " + redirect_url)
|
||
|
|
||
5 years ago
|
soup = ParserBeautifulSoup(
|
||
|
r.content.decode("utf-8", "ignore"), ["lxml", "html.parser"]
|
||
|
)
|
||
|
|
||
|
# non-shooter result page
|
||
|
if soup.find("div", {"class": "item"}):
|
||
|
logger.debug("enter a non-shooter page")
|
||
|
for item in soup.find_all("div", {"class": "item"}):
|
||
|
title_a = item.find("p", class_="tt clearfix").find("a")
|
||
4 years ago
|
subs_year = year
|
||
5 years ago
|
if season:
|
||
4 years ago
|
# episode year in zimuku is the season's year not show's year
|
||
|
actual_subs_year = re.findall(r"\d{4}", title_a.text) or None
|
||
|
if actual_subs_year:
|
||
|
subs_year = int(actual_subs_year[0]) - season + 1
|
||
5 years ago
|
title = title_a.text
|
||
|
season_cn1 = re.search("第(.*)季", title)
|
||
|
if not season_cn1:
|
||
|
season_cn1 = "一"
|
||
|
else:
|
||
|
season_cn1 = season_cn1.group(1).strip()
|
||
|
season_cn2 = num_to_cn(str(season))
|
||
|
if season_cn1 != season_cn2:
|
||
|
continue
|
||
2 years ago
|
episode_link = urljoin(self.server_url, title_a.attrs["href"])
|
||
4 years ago
|
new_subs = self._parse_episode_page(episode_link, subs_year)
|
||
5 years ago
|
subtitles += new_subs
|
||
|
|
||
4 years ago
|
# NOTE: shooter result pages are ignored due to the existence of zimuku provider
|
||
6 years ago
|
|
||
|
return subtitles
|
||
|
|
||
|
def list_subtitles(self, video, languages):
|
||
|
if isinstance(video, Episode):
|
||
|
titles = [video.series] + video.alternative_series
|
||
|
elif isinstance(video, Movie):
|
||
|
titles = [video.title] + video.alternative_titles
|
||
|
else:
|
||
|
titles = []
|
||
|
|
||
|
subtitles = []
|
||
|
# query for subtitles with the show_id
|
||
|
for title in titles:
|
||
|
if isinstance(video, Episode):
|
||
5 years ago
|
subtitles += [
|
||
|
s
|
||
|
for s in self.query(
|
||
|
title,
|
||
|
season=video.season,
|
||
|
episode=video.episode,
|
||
|
year=video.year,
|
||
|
)
|
||
|
if s.language in languages
|
||
|
]
|
||
6 years ago
|
elif isinstance(video, Movie):
|
||
5 years ago
|
subtitles += [
|
||
|
s
|
||
|
for s in self.query(title, year=video.year)
|
||
|
if s.language in languages
|
||
|
]
|
||
6 years ago
|
|
||
|
return subtitles
|
||
|
|
||
|
def download_subtitle(self, subtitle):
|
||
2 years ago
|
def _get_archive_download_link(yunsuopass, sub_page_link):
|
||
|
res = yunsuopass(sub_page_link)
|
||
5 years ago
|
bs_obj = ParserBeautifulSoup(
|
||
2 years ago
|
res.content.decode("utf-8", "ignore"), ["html.parser"]
|
||
5 years ago
|
)
|
||
|
down_page_link = bs_obj.find("a", {"id": "down1"}).attrs["href"]
|
||
|
down_page_link = urljoin(sub_page_link, down_page_link)
|
||
2 years ago
|
res = yunsuopass(down_page_link)
|
||
5 years ago
|
bs_obj = ParserBeautifulSoup(
|
||
2 years ago
|
res.content.decode("utf-8", "ignore"), ["html.parser"]
|
||
5 years ago
|
)
|
||
2 years ago
|
return urljoin(sub_page_link, bs_obj.find("a", {"rel": "nofollow"}).attrs["href"])
|
||
5 years ago
|
|
||
|
# download the subtitle
|
||
|
logger.info("Downloading subtitle %r", subtitle)
|
||
2 years ago
|
download_link = _get_archive_download_link(self.yunsuo_bypass, subtitle.page_link)
|
||
2 years ago
|
r = self.yunsuo_bypass(download_link, headers={'Referer': subtitle.page_link}, timeout=30)
|
||
5 years ago
|
r.raise_for_status()
|
||
3 years ago
|
try:
|
||
|
filename = r.headers["Content-Disposition"]
|
||
|
except KeyError:
|
||
|
logger.debug("Unable to parse subtitles filename. Dropping this subtitles.")
|
||
|
return
|
||
6 years ago
|
|
||
5 years ago
|
if not r.content:
|
||
|
logger.debug("Unable to download subtitle. No data returned from provider")
|
||
|
return
|
||
|
|
||
|
archive_stream = io.BytesIO(r.content)
|
||
|
archive = None
|
||
|
if rarfile.is_rarfile(archive_stream):
|
||
|
logger.debug("Identified rar archive")
|
||
|
if ".rar" not in filename:
|
||
|
logger.debug(
|
||
|
".rar should be in the downloaded file name: {}".format(filename)
|
||
|
)
|
||
|
return
|
||
|
archive = rarfile.RarFile(archive_stream)
|
||
|
subtitle_content = _get_subtitle_from_archive(archive)
|
||
|
elif zipfile.is_zipfile(archive_stream):
|
||
|
logger.debug("Identified zip archive")
|
||
|
if ".zip" not in filename:
|
||
|
logger.debug(
|
||
|
".zip should be in the downloaded file name: {}".format(filename)
|
||
|
)
|
||
|
return
|
||
|
archive = zipfile.ZipFile(archive_stream)
|
||
|
subtitle_content = _get_subtitle_from_archive(archive)
|
||
|
else:
|
||
|
is_sub = ""
|
||
|
for sub_ext in SUBTITLE_EXTENSIONS:
|
||
|
if sub_ext in filename:
|
||
|
is_sub = sub_ext
|
||
6 years ago
|
break
|
||
5 years ago
|
if not is_sub:
|
||
|
logger.debug(
|
||
|
"unknown subtitle ext int downloaded file name: {}".format(filename)
|
||
|
)
|
||
|
return
|
||
|
logger.debug("Identified {} file".format(is_sub))
|
||
|
subtitle_content = r.content
|
||
6 years ago
|
|
||
5 years ago
|
if subtitle_content:
|
||
|
subtitle.content = fix_line_ending(subtitle_content)
|
||
|
else:
|
||
|
logger.debug("Could not extract subtitle from %r", archive)
|
||
6 years ago
|
|
||
|
|
||
|
def _get_subtitle_from_archive(archive):
|
||
5 years ago
|
extract_subname, max_score = "", -1
|
||
|
|
||
|
for subname in archive.namelist():
|
||
6 years ago
|
# discard hidden files
|
||
5 years ago
|
if os.path.split(subname)[-1].startswith("."):
|
||
6 years ago
|
continue
|
||
|
|
||
|
# discard non-subtitle files
|
||
5 years ago
|
if not subname.lower().endswith(SUBTITLE_EXTENSIONS):
|
||
6 years ago
|
continue
|
||
|
|
||
4 years ago
|
# prefer ass/ssa/srt subtitles with double languages or simplified/traditional chinese
|
||
|
score = ("ass" in subname or "ssa" in subname or "srt" in subname) * 1
|
||
5 years ago
|
if "简体" in subname or "chs" in subname or ".gb." in subname:
|
||
|
score += 2
|
||
|
if "繁体" in subname or "cht" in subname or ".big5." in subname:
|
||
|
score += 2
|
||
4 years ago
|
if "chs.eng" in subname or "chs&eng" in subname or "cht.eng" in subname or "cht&eng" in subname:
|
||
|
score += 2
|
||
|
if "中英" in subname or "简英" in subname or "繁英" in subname or "双语" in subname or "简体&英文" in subname or "繁体&英文" in subname:
|
||
5 years ago
|
score += 4
|
||
|
logger.debug("subtitle {}, score: {}".format(subname, score))
|
||
|
if score > max_score:
|
||
|
max_score = score
|
||
|
extract_subname = subname
|
||
|
|
||
|
return archive.read(extract_subname) if max_score != -1 else None
|
||
|
|
||
|
|
||
|
def _extract_name(name):
|
||
|
""" filter out Chinese characters from subtitle names """
|
||
|
name, suffix = os.path.splitext(name)
|
||
|
c_pattern = "[\u4e00-\u9fff]"
|
||
|
e_pattern = "[a-zA-Z]"
|
||
|
c_indices = [m.start(0) for m in re.finditer(c_pattern, name)]
|
||
|
e_indices = [m.start(0) for m in re.finditer(e_pattern, name)]
|
||
|
|
||
|
target, discard = e_indices, c_indices
|
||
|
|
||
|
if len(target) == 0:
|
||
|
return ""
|
||
|
|
||
|
first_target, last_target = target[0], target[-1]
|
||
|
first_discard = discard[0] if discard else -1
|
||
|
last_discard = discard[-1] if discard else -1
|
||
|
if last_discard < first_target:
|
||
|
new_name = name[first_target:]
|
||
|
elif last_target < first_discard:
|
||
|
new_name = name[:first_discard]
|
||
|
else:
|
||
|
# try to find maximum continous part
|
||
|
result, start, end = [0, 1], -1, 0
|
||
|
while end < len(name):
|
||
|
while end not in e_indices and end < len(name):
|
||
|
end += 1
|
||
|
if end == len(name):
|
||
|
break
|
||
|
start = end
|
||
|
while end not in c_indices and end < len(name):
|
||
|
end += 1
|
||
|
if end - start > result[1] - result[0]:
|
||
|
result = [start, end]
|
||
|
start = end
|
||
|
end += 1
|
||
2 years ago
|
new_name = name[result[0]: result[1]]
|
||
5 years ago
|
new_name = new_name.strip() + suffix
|
||
|
return new_name
|
||
|
|
||
|
|
||
|
def num_to_cn(number):
|
||
|
""" convert numbers(1-99) to Chinese """
|
||
|
assert number.isdigit() and 1 <= int(number) <= 99
|
||
|
|
||
2 years ago
|
trans_map = {n: c for n, c in zip("123456789", "一二三四五六七八九")}
|
||
5 years ago
|
|
||
|
if len(number) == 1:
|
||
|
return trans_map[number]
|
||
|
else:
|
||
|
part1 = "十" if number[0] == "1" else trans_map[number[0]] + "十"
|
||
|
part2 = trans_map[number[1]] if number[1] != "0" else ""
|
||
3 years ago
|
return part1 + part2
|