2021-01-18 22:26:02 +08:00
|
|
|
# coding: utf-8
|
|
|
|
|
# YYeTsBot - fansub.py
|
|
|
|
|
# 2019/8/15 18:30
|
|
|
|
|
|
|
|
|
|
__author__ = 'Benny <benny.think@gmail.com>'
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import logging
|
|
|
|
|
import pickle
|
|
|
|
|
import sys
|
|
|
|
|
import json
|
2021-01-20 21:51:03 +08:00
|
|
|
import hashlib
|
2021-06-23 18:50:32 +08:00
|
|
|
import contextlib
|
2021-01-22 20:44:59 +08:00
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
import pymongo
|
2021-01-18 22:26:02 +08:00
|
|
|
|
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
2021-01-20 21:51:03 +08:00
|
|
|
from config import (YYETS_SEARCH_URL, GET_USER, BASE_URL, SHARE_WEB,
|
2021-01-18 22:26:02 +08:00
|
|
|
SHARE_URL, WORKERS, SHARE_API, USERNAME, PASSWORD,
|
2021-06-23 18:50:32 +08:00
|
|
|
AJAX_LOGIN, REDIS, FANSUB_ORDER, FIX_SEARCH, MONGO,
|
|
|
|
|
ZHUIXINFAN_SEARCH, ZHUIXINFAN_RESOURCE)
|
2021-01-18 22:26:02 +08:00
|
|
|
import redis
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(levelname)s]: %(message)s')
|
|
|
|
|
|
|
|
|
|
session = requests.Session()
|
|
|
|
|
ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
|
|
|
|
|
session.headers.update({"User-Agent": ua})
|
|
|
|
|
|
2021-01-24 21:39:25 +08:00
|
|
|
this_module = sys.modules[__name__]
|
|
|
|
|
|
2021-01-18 22:26:02 +08:00
|
|
|
|
|
|
|
|
class BaseFansub:
|
|
|
|
|
"""
|
|
|
|
|
all the subclass should implement three kinds of methods:
|
|
|
|
|
1. online search, contains preview for bot and complete result
|
2021-01-21 19:32:55 +08:00
|
|
|
2. login and check (set pass if not applicable)
|
|
|
|
|
3. search_result as this is critical for bot to draw markup
|
2021-01-18 22:26:02 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
label = None
|
|
|
|
|
cookie_file = None
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.data = None
|
|
|
|
|
self.url = None
|
|
|
|
|
self.redis = redis.StrictRedis(host=REDIS, decode_responses=True)
|
2021-01-21 19:32:55 +08:00
|
|
|
# how to store data in redis:
|
|
|
|
|
# either online or offline, only these two type of data will be stored
|
|
|
|
|
# http://z.cn --> <html><head>.... valid for 12 hours, cache purpose
|
|
|
|
|
# 1273hda_hash_of_url - {url:http://z.cn,class:xxclass} forever
|
2021-01-18 22:26:02 +08:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def id(self):
|
|
|
|
|
# implement how to get the unique id for this resource
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def __get_search_html__(self, kw: str) -> str:
|
|
|
|
|
# return html text of search page
|
|
|
|
|
pass
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def search_preview(self, search_text: str) -> dict:
|
2021-01-18 22:26:02 +08:00
|
|
|
# try to retrieve critical information from html
|
|
|
|
|
# this result must return to bot for manual selection
|
2021-01-21 19:32:55 +08:00
|
|
|
# {"url1": "name1", "url2": "name2", "source":"yyets"}
|
2021-01-18 22:26:02 +08:00
|
|
|
pass
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def search_result(self, resource_url: str) -> dict:
|
2021-01-18 22:26:02 +08:00
|
|
|
"""
|
|
|
|
|
This will happen when user click one of the button, only by then we can know the resource link
|
|
|
|
|
From the information above, try to get a detail dict structure.
|
|
|
|
|
This method should check cache first if applicable
|
|
|
|
|
This method should set self.link and self.data
|
|
|
|
|
This method should call __execute_online_search
|
2021-01-21 19:32:55 +08:00
|
|
|
:param resource_url: in entry method, this variable is hash. Otherwise it's plain url
|
|
|
|
|
:return: {"all": dict_result, "share": share_link, "cnname": cnname}
|
2021-01-18 22:26:02 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def __execute_search_result__(self) -> dict:
|
2021-01-18 22:26:02 +08:00
|
|
|
"""
|
|
|
|
|
Do the real search job, without any cache mechanism
|
|
|
|
|
:return: {"all": rss_result, "share": share_link, "cnname": cnname}
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def __login_check(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def __manual_login(self):
|
|
|
|
|
pass
|
|
|
|
|
|
2021-01-19 19:42:08 +08:00
|
|
|
def __save_cookies__(self, requests_cookiejar):
|
2021-01-18 22:26:02 +08:00
|
|
|
with open(self.cookie_file, 'wb') as f:
|
|
|
|
|
pickle.dump(requests_cookiejar, f)
|
|
|
|
|
|
2021-01-19 19:42:08 +08:00
|
|
|
def __load_cookies__(self):
|
2021-01-18 22:26:02 +08:00
|
|
|
with open(self.cookie_file, 'rb') as f:
|
|
|
|
|
return pickle.load(f)
|
|
|
|
|
|
2021-01-19 19:42:08 +08:00
|
|
|
def __get_from_cache__(self, url: str, method_name: str) -> dict:
|
2021-01-20 21:51:03 +08:00
|
|
|
logging.info("[%s] Reading data from cache %s", self.label, url)
|
2021-01-18 22:26:02 +08:00
|
|
|
data = self.redis.get(url)
|
|
|
|
|
if data:
|
2021-01-21 19:32:55 +08:00
|
|
|
logging.info("😄 Cache hit")
|
2021-01-18 22:26:02 +08:00
|
|
|
return json.loads(data)
|
|
|
|
|
else:
|
2021-01-21 19:32:55 +08:00
|
|
|
logging.info("😱 Cache miss")
|
2021-01-18 22:26:02 +08:00
|
|
|
result_method = getattr(self, method_name)
|
2021-01-19 23:27:11 +08:00
|
|
|
self.__save_to_cache__(url, result_method())
|
2021-01-19 19:42:08 +08:00
|
|
|
return self.__get_from_cache__(url, method_name)
|
2021-01-18 22:26:02 +08:00
|
|
|
|
2021-01-19 23:27:11 +08:00
|
|
|
def __save_to_cache__(self, url: str, value: dict, ex=3600 * 12) -> None:
|
2021-01-18 22:26:02 +08:00
|
|
|
data = json.dumps(value, ensure_ascii=False)
|
|
|
|
|
self.redis.set(url, data, ex=ex)
|
|
|
|
|
|
|
|
|
|
|
2021-01-21 19:32:55 +08:00
|
|
|
class YYeTsBase(BaseFansub):
|
2021-01-18 22:26:02 +08:00
|
|
|
@property
|
|
|
|
|
def id(self):
|
|
|
|
|
# implement how to get the unique id for this resource
|
|
|
|
|
rid = self.url.split('/')[-1]
|
|
|
|
|
return rid
|
|
|
|
|
|
2021-01-21 19:32:55 +08:00
|
|
|
|
|
|
|
|
class YYeTsOnline(YYeTsBase):
|
|
|
|
|
label = "yyets online"
|
|
|
|
|
cookie_file = os.path.join("data", "cookies.dump")
|
|
|
|
|
|
2021-01-18 22:26:02 +08:00
|
|
|
def __get_search_html__(self, kw: str) -> str:
|
2021-01-19 19:42:08 +08:00
|
|
|
# don't have to login here
|
2021-01-20 21:51:03 +08:00
|
|
|
logging.info("[%s] Searching for %s", self.label, kw)
|
|
|
|
|
r = session.get(YYETS_SEARCH_URL.format(kw=kw))
|
2021-01-18 22:26:02 +08:00
|
|
|
r.close()
|
|
|
|
|
return r.text
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def search_preview(self, search_text: str) -> dict:
|
2021-01-21 19:32:55 +08:00
|
|
|
# yyets online
|
2021-01-18 22:26:02 +08:00
|
|
|
html_text = self.__get_search_html__(search_text)
|
2021-01-20 21:51:03 +08:00
|
|
|
logging.info('[%s] Parsing html...', self.label)
|
2021-01-21 12:20:43 +08:00
|
|
|
soup = BeautifulSoup(html_text, 'html.parser')
|
2021-01-18 22:26:02 +08:00
|
|
|
link_list = soup.find_all("div", class_="clearfix search-item")
|
|
|
|
|
dict_result = {}
|
|
|
|
|
for block in link_list:
|
|
|
|
|
name = block.find_all('a')[-1].text
|
|
|
|
|
url = BASE_URL + block.find_all('a')[-1].attrs['href']
|
2021-01-21 19:32:55 +08:00
|
|
|
url_hash = hashlib.sha1(url.encode('u8')).hexdigest()
|
|
|
|
|
dict_result[url_hash] = name
|
|
|
|
|
self.redis.hset(url_hash, mapping={"class": self.__class__.__name__, "url": url})
|
2021-01-20 22:43:02 +08:00
|
|
|
dict_result["source"] = self.label
|
2021-01-18 22:26:02 +08:00
|
|
|
return dict_result
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def search_result(self, resource_url: str) -> dict:
|
2021-01-21 19:32:55 +08:00
|
|
|
# yyets online
|
2021-01-18 22:26:02 +08:00
|
|
|
self.url = resource_url
|
2021-01-21 19:25:18 +08:00
|
|
|
self.data = self.__get_from_cache__(self.url, self.__execute_search_result__.__name__)
|
2021-01-18 22:26:02 +08:00
|
|
|
return self.data
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def __execute_search_result__(self) -> dict:
|
2021-01-20 21:51:03 +08:00
|
|
|
logging.info("[%s] Loading detail page %s", self.label, self.url)
|
2021-01-18 22:26:02 +08:00
|
|
|
share_link, api_res = self.__get_share_page()
|
|
|
|
|
cnname = api_res["data"]["info"]["cnname"]
|
|
|
|
|
self.data = {"all": api_res, "share": share_link, "cnname": cnname}
|
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
|
|
def __login_check(self):
|
2021-01-20 21:51:03 +08:00
|
|
|
logging.debug("[%s] Checking login status...", self.label)
|
2021-01-18 22:26:02 +08:00
|
|
|
if not os.path.exists(self.cookie_file):
|
2021-01-20 21:51:03 +08:00
|
|
|
logging.warning("[%s] Cookie file not found", self.label)
|
2021-01-18 22:26:02 +08:00
|
|
|
self.__manual_login()
|
|
|
|
|
|
2021-01-19 19:42:08 +08:00
|
|
|
r = session.get(GET_USER, cookies=self.__load_cookies__())
|
2021-01-18 22:26:02 +08:00
|
|
|
if not r.json()['status'] == 1:
|
|
|
|
|
self.__manual_login()
|
|
|
|
|
|
|
|
|
|
def __manual_login(self):
|
|
|
|
|
data = {"account": USERNAME, "password": PASSWORD, "remember": 1}
|
2021-01-20 21:51:03 +08:00
|
|
|
logging.info("[%s] Login in as %s", self.label, data)
|
2021-01-18 22:26:02 +08:00
|
|
|
r = requests.post(AJAX_LOGIN, data=data)
|
|
|
|
|
resp = r.json()
|
|
|
|
|
if resp.get('status') == 1:
|
2021-01-20 21:51:03 +08:00
|
|
|
logging.debug("Login success! %s", r.cookies)
|
2021-01-19 19:42:08 +08:00
|
|
|
self.__save_cookies__(r.cookies)
|
2021-01-18 22:26:02 +08:00
|
|
|
else:
|
|
|
|
|
logging.error("Login failed! %s", resp)
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
r.close()
|
|
|
|
|
|
|
|
|
|
def __get_share_page(self):
|
2021-01-19 19:42:08 +08:00
|
|
|
self.__login_check()
|
2021-01-18 22:26:02 +08:00
|
|
|
rid = self.id
|
|
|
|
|
|
2021-01-19 19:42:08 +08:00
|
|
|
res = session.post(SHARE_URL, data={"rid": rid}, cookies=self.__load_cookies__()).json()
|
2021-01-18 22:26:02 +08:00
|
|
|
share_code = res['data'].split('/')[-1]
|
|
|
|
|
share_url = SHARE_WEB.format(code=share_code)
|
2021-01-20 21:51:03 +08:00
|
|
|
logging.info("[%s] Share url is %s", self.label, share_url)
|
2021-01-18 22:26:02 +08:00
|
|
|
|
|
|
|
|
# get api response
|
|
|
|
|
api_response = session.get(SHARE_API.format(code=share_code)).json()
|
|
|
|
|
return share_url, api_response
|
|
|
|
|
|
2021-01-20 21:51:03 +08:00
|
|
|
|
2021-01-21 19:32:55 +08:00
|
|
|
class YYeTsOffline(YYeTsBase):
|
2021-01-21 19:25:18 +08:00
|
|
|
label = "yyets offline"
|
|
|
|
|
|
2021-02-07 16:40:27 +08:00
|
|
|
def __init__(self, db="zimuzu", col="yyets"):
|
2021-01-22 20:44:59 +08:00
|
|
|
super().__init__()
|
2021-01-22 22:21:39 +08:00
|
|
|
self.mongo = pymongo.MongoClient(host=MONGO)
|
2021-01-31 14:26:53 +08:00
|
|
|
self.collection = self.mongo[db][col]
|
2021-01-22 20:44:59 +08:00
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def search_preview(self, search_text: str) -> dict:
|
2021-01-22 20:44:59 +08:00
|
|
|
logging.info("[%s] Loading offline data from MongoDB...", self.label)
|
2021-01-21 19:25:18 +08:00
|
|
|
|
2021-02-07 16:40:27 +08:00
|
|
|
projection = {'_id': False, 'data.info': True}
|
|
|
|
|
data = self.collection.find({
|
|
|
|
|
"$or": [
|
2021-04-24 20:56:54 +08:00
|
|
|
{"data.info.cnname": {"$regex": f".*{search_text}.*", "$options": "-i"}},
|
|
|
|
|
{"data.info.enname": {"$regex": f".*{search_text}.*", "$options": "-i"}},
|
|
|
|
|
{"data.info.aliasname": {"$regex": f".*{search_text}.*", "$options": "-i"}},
|
2021-02-07 16:40:27 +08:00
|
|
|
]},
|
|
|
|
|
projection
|
|
|
|
|
)
|
2021-01-21 19:25:18 +08:00
|
|
|
results = {}
|
2021-01-22 20:44:59 +08:00
|
|
|
for item in data:
|
2021-02-07 16:40:27 +08:00
|
|
|
info = item["data"]["info"]
|
|
|
|
|
fake_url = "http://www.rrys2020.com/resource/{}".format(info["id"])
|
2021-01-22 20:44:59 +08:00
|
|
|
url_hash = hashlib.sha1(fake_url.encode('u8')).hexdigest()
|
2021-02-07 16:40:27 +08:00
|
|
|
results[url_hash] = info["cnname"] + info["enname"] + info["aliasname"]
|
2021-01-22 20:44:59 +08:00
|
|
|
self.redis.hset(url_hash, mapping={"class": self.__class__.__name__, "url": fake_url})
|
2021-01-21 19:32:55 +08:00
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
logging.info("[%s] Offline search complete", self.label)
|
|
|
|
|
results["source"] = self.label
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
def search_result(self, resource_url) -> dict:
|
2021-01-21 19:32:55 +08:00
|
|
|
# yyets offline
|
2021-01-21 19:25:18 +08:00
|
|
|
self.url = resource_url
|
2021-02-07 16:40:27 +08:00
|
|
|
# http://www.rrys2020.com/resource/10017
|
|
|
|
|
rid = self.url.split("/resource/")[1]
|
|
|
|
|
data: dict = self.collection.find_one({"data.info.id": int(rid)}, {'_id': False})
|
|
|
|
|
name = data["data"]["info"]["cnname"]
|
2021-01-22 20:44:59 +08:00
|
|
|
self.data = {"all": data, "share": WORKERS.format(id=rid), "cnname": name}
|
2021-01-21 19:25:18 +08:00
|
|
|
return self.data
|
|
|
|
|
|
2021-01-22 20:44:59 +08:00
|
|
|
def __del__(self):
|
|
|
|
|
self.mongo.close()
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
|
|
|
|
|
class ZimuxiaOnline(BaseFansub):
|
|
|
|
|
label = "zimuxia online"
|
2021-01-20 21:51:03 +08:00
|
|
|
|
|
|
|
|
def __get_search_html__(self, kw: str) -> str:
|
|
|
|
|
# don't have to login here
|
|
|
|
|
logging.info("[%s] Searching for %s", self.label, kw)
|
|
|
|
|
r = session.get(FIX_SEARCH.format(kw=kw))
|
|
|
|
|
r.close()
|
|
|
|
|
return r.text
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def search_preview(self, search_text: str) -> dict:
|
2021-01-21 19:32:55 +08:00
|
|
|
# zimuxia online
|
2021-01-20 21:51:03 +08:00
|
|
|
html_text = self.__get_search_html__(search_text)
|
|
|
|
|
logging.info('[%s] Parsing html...', self.label)
|
2021-01-21 12:20:43 +08:00
|
|
|
soup = BeautifulSoup(html_text, 'html.parser')
|
2021-01-20 21:51:03 +08:00
|
|
|
link_list = soup.find_all("h2", class_="post-title")
|
|
|
|
|
|
|
|
|
|
dict_result = {}
|
|
|
|
|
for link in link_list:
|
2021-01-21 19:32:55 +08:00
|
|
|
# TODO wordpress search content and title, some cases it would be troublesome
|
2021-01-20 21:51:03 +08:00
|
|
|
url = link.a['href']
|
|
|
|
|
url_hash = hashlib.sha1(url.encode('u8')).hexdigest()
|
|
|
|
|
name = link.a.text
|
|
|
|
|
dict_result[url_hash] = name
|
2021-01-21 19:32:55 +08:00
|
|
|
self.redis.hset(url_hash, mapping={"class": self.__class__.__name__, "url": url})
|
2021-01-20 22:43:02 +08:00
|
|
|
dict_result["source"] = self.label
|
2021-01-20 21:51:03 +08:00
|
|
|
return dict_result
|
|
|
|
|
|
2021-01-21 19:32:55 +08:00
|
|
|
def search_result(self, resource_url: str) -> dict:
|
|
|
|
|
# zimuxia online
|
|
|
|
|
self.url = resource_url
|
2021-01-21 19:25:18 +08:00
|
|
|
self.data = self.__get_from_cache__(self.url, self.__execute_search_result__.__name__)
|
2021-01-20 21:51:03 +08:00
|
|
|
return self.data
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def __execute_search_result__(self) -> dict:
|
2021-01-20 21:51:03 +08:00
|
|
|
logging.info("[%s] Loading detail page %s", self.label, self.url)
|
|
|
|
|
cnname, html_text = self.obtain_all_response()
|
|
|
|
|
self.data = {"all": html_text, "share": self.url, "cnname": cnname}
|
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
|
|
def obtain_all_response(self) -> (str, str):
|
|
|
|
|
r = session.get(self.url)
|
2021-01-21 12:20:43 +08:00
|
|
|
soup = BeautifulSoup(r.text, 'html.parser')
|
2021-01-20 21:51:03 +08:00
|
|
|
cnname = soup.title.text.split("|")[0]
|
|
|
|
|
return cnname, dict(html=r.text)
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
|
|
|
|
|
class ZimuxiaOffline(BaseFansub):
|
|
|
|
|
label = "zimuxia offline"
|
|
|
|
|
|
|
|
|
|
def search_preview(self, search_text: str) -> dict:
|
2021-01-23 16:21:40 +08:00
|
|
|
pass
|
2021-01-20 21:51:03 +08:00
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def search_result(self, resource_url) -> dict:
|
2021-01-23 16:21:40 +08:00
|
|
|
pass
|
2021-01-20 22:43:02 +08:00
|
|
|
|
|
|
|
|
|
2021-06-23 18:50:32 +08:00
|
|
|
class ZhuixinfanOnline(BaseFansub):
|
|
|
|
|
label = "zhuixinfan online"
|
|
|
|
|
|
|
|
|
|
def __get_search_html__(self, kw: str) -> str:
|
|
|
|
|
logging.info("[%s] Searching for %s", self.label, kw)
|
|
|
|
|
r = session.get(ZHUIXINFAN_SEARCH.format(kw))
|
|
|
|
|
r.close()
|
|
|
|
|
return r.text
|
|
|
|
|
|
|
|
|
|
def search_preview(self, search_text: str) -> dict:
|
|
|
|
|
# zhuixinfan online
|
|
|
|
|
html_text = self.__get_search_html__(search_text)
|
|
|
|
|
logging.info('[%s] Parsing html...', self.label)
|
|
|
|
|
soup = BeautifulSoup(html_text, 'html.parser')
|
|
|
|
|
link_list = soup.find_all("ul", class_="resource_list")
|
|
|
|
|
|
|
|
|
|
dict_result = {}
|
|
|
|
|
for li in link_list:
|
|
|
|
|
for link in li:
|
|
|
|
|
with contextlib.suppress(AttributeError):
|
|
|
|
|
name = link.dd.text
|
|
|
|
|
url = ZHUIXINFAN_RESOURCE.format(link.dd.a["href"])
|
|
|
|
|
url_hash = hashlib.sha1(url.encode('u8')).hexdigest()
|
|
|
|
|
dict_result[url_hash] = name
|
|
|
|
|
self.redis.hset(url_hash, mapping={"class": self.__class__.__name__, "url": url, "name": name})
|
|
|
|
|
|
|
|
|
|
dict_result["source"] = self.label
|
|
|
|
|
return dict_result
|
|
|
|
|
|
|
|
|
|
def search_result(self, resource_url: str) -> dict:
|
|
|
|
|
# zhuixinfan online
|
|
|
|
|
self.url = resource_url
|
|
|
|
|
self.data = self.__execute_search_result__()
|
|
|
|
|
return self.data
|
|
|
|
|
# {"all": dict_result, "share": share_link, "cnname": cnname}
|
|
|
|
|
|
|
|
|
|
def __execute_search_result__(self) -> dict:
|
|
|
|
|
logging.info("[%s] Loading detail page %s", self.label, self.url)
|
|
|
|
|
url_hash = hashlib.sha1(self.url.encode('u8')).hexdigest()
|
|
|
|
|
cnname = self.redis.hget(url_hash, "name")
|
|
|
|
|
# TODO
|
|
|
|
|
self.data = {"all": "不好意思,还没做呢……", "share": self.url, "cnname": cnname}
|
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
|
|
|
2021-01-20 22:43:02 +08:00
|
|
|
class FansubEntrance(BaseFansub):
|
2021-01-21 19:25:18 +08:00
|
|
|
order = FANSUB_ORDER.split(",")
|
2021-01-20 23:04:23 +08:00
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
def search_preview(self, search_text: str) -> dict:
|
|
|
|
|
source = "聪明机智温柔可爱善良的Benny"
|
2021-01-21 19:32:55 +08:00
|
|
|
for sub_str in self.order:
|
|
|
|
|
logging.info("Looping from %s", sub_str)
|
|
|
|
|
fc = globals().get(sub_str)
|
|
|
|
|
result = fc().search_preview(search_text)
|
2021-01-20 23:04:23 +08:00
|
|
|
# this result contains source:sub, so we'll pop and add it
|
|
|
|
|
source = result.pop("source")
|
|
|
|
|
if result:
|
2021-01-21 19:32:55 +08:00
|
|
|
logging.info("Result hit in %s %s", sub_str, fc)
|
|
|
|
|
FansubEntrance.fansub_class = fc
|
2021-01-20 23:04:23 +08:00
|
|
|
result["source"] = source
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
return dict(source=source)
|
|
|
|
|
|
2021-01-21 19:32:55 +08:00
|
|
|
def search_result(self, resource_url_hash: str) -> dict:
|
|
|
|
|
# entrance
|
|
|
|
|
cache_data = self.redis.hgetall(resource_url_hash)
|
|
|
|
|
resource_url = cache_data["url"]
|
|
|
|
|
class_name = cache_data["class"]
|
|
|
|
|
fc = globals().get(class_name)
|
|
|
|
|
return fc().search_result(resource_url)
|
2021-01-20 23:04:23 +08:00
|
|
|
|
|
|
|
|
|
2021-01-21 19:25:18 +08:00
|
|
|
# we'll check if FANSUB_ORDER is correct. Must put it here, not before.
|
|
|
|
|
for fs in FANSUB_ORDER.split(","):
|
|
|
|
|
if globals().get(fs) is None:
|
|
|
|
|
raise NameError(f"FANSUB_ORDER is incorrect! {fs}")
|
2021-01-24 21:39:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Commands can use latin letters, numbers and underscores. yyets_offline
|
|
|
|
|
def class_to_tg(sub_class: str):
|
|
|
|
|
trans = {"Online": "_online", "Offline": "_offline"}
|
|
|
|
|
|
|
|
|
|
for upper, lower in trans.items():
|
|
|
|
|
sub_class = sub_class.replace(upper, lower)
|
|
|
|
|
|
|
|
|
|
return sub_class.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for sub_name in globals().copy():
|
|
|
|
|
if sub_name.endswith("Offline") or sub_name.endswith("Online"):
|
|
|
|
|
cmd_name = class_to_tg(sub_name)
|
|
|
|
|
m = getattr(this_module, sub_name)
|
|
|
|
|
logging.info("Mapping %s to %s", cmd_name, m)
|
|
|
|
|
vars()[cmd_name] = m
|
2021-02-07 16:40:27 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-06-23 18:50:32 +08:00
|
|
|
a = ZimuxiaOnline()
|
|
|
|
|
v = a.search_preview("女人为何")
|
2021-02-07 16:40:27 +08:00
|
|
|
print(v)
|