Skip to content

Commit

Permalink
offline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Jan 21, 2021
1 parent b67c6f2 commit e5ceaa1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
42 changes: 28 additions & 14 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
from tgbot_ping import get_runtime
from apscheduler.schedulers.background import BackgroundScheduler

from html_request import get_search_html, analyse_search_html, get_detail_page
from utils import (save_error_dump, save_to_cache, get_from_cache, get_error_dump,
reset_request, today_request, show_usage)
from config import PROXY, TOKEN, SEARCH_URL, MAINTAINER, REPORT
from html_request import get_search_html, analyse_search_html, get_detail_page, offline_search, offline_link
from utils import (save_error_dump, save_to_cache, yyets_get_from_cache, get_error_dump,
reset_request, today_request, show_usage
)
from config import PROXY, TOKEN, SEARCH_URL, MAINTAINER, REPORT, WORKERS, OFFLINE

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(levelname)s]: %(message)s')
if PROXY:
Expand Down Expand Up @@ -119,23 +120,29 @@ def send_search(message):
today_request("answer")
send_my_response(message)
return
bot.send_chat_action(message.chat.id, 'record_video')

bot.send_chat_action(message.chat.id, 'record_video')
name = message.text
logging.info('Receiving message: %s from user %s(%s)', name, message.chat.username, message.chat.id)
if name is None:
today_request("invalid")
with open('assets/warning.webp', 'rb') as sti:
bot.send_message(message.chat.id, "不要调戏我!我会报警的")
bot.send_sticker(message.chat.id, sti)
return

logging.info('Receiving message about %s from user %s(%s)', name, message.chat.username, message.chat.id)
html = get_search_html(name)
result = analyse_search_html(html)
if OFFLINE:
logging.warning("☢️ Going offline mode!!!")
bot.send_message(message.chat.id, "人人影视官网不可用,目前在使用离线模式,可能没有最新的剧集。")
html = ""
result = offline_search(name)
else:
html = get_search_html(name)
result = analyse_search_html(html)

markup = types.InlineKeyboardMarkup()
for url, detail in result.items():
btn = types.InlineKeyboardButton(detail['name'], callback_data="choose%s" % url)
btn = types.InlineKeyboardButton(detail, callback_data="choose%s" % url)
markup.add(btn)

if result:
Expand All @@ -151,7 +158,7 @@ def send_search(message):
bot.send_message(message.chat.id, f"没有找到你想要的信息,是不是你打了错别字,或者搜索了一些国产影视剧。🤪\n"
f"还是你想调戏我哦🙅‍️\n\n"
f"可以看看这个链接,看看有没有结果。 {SEARCH_URL.format(kw=encoded)} \n\n"
"⚠️如果确定要我背锅,那么请使用 /help ", disable_web_page_preview=True)
"⚠️如果确定要我背锅,那么请使用 /help 来提交错误", disable_web_page_preview=True)
if REPORT:
btn = types.InlineKeyboardButton("快来修复啦", callback_data="fix")
markup.add(btn)
Expand All @@ -174,13 +181,20 @@ def choose_link(call):
bot.send_chat_action(call.message.chat.id, 'typing')
# call.data is url, http://www.rrys2020.com/resource/36588
resource_url = re.findall(r"choose(\S*)", call.data)[0]
markup = types.InlineKeyboardMarkup()

link = get_from_cache(resource_url)
if OFFLINE:
worker_page = offline_link(resource_url)
btn1 = types.InlineKeyboardButton("打开网页", url=worker_page)
markup.add(btn1)
bot.send_message(call.message.chat.id, "离线模式,点击按钮打开网页获取结果", reply_markup=markup)
return

link = yyets_get_from_cache(resource_url)
if not link:
link = get_detail_page(resource_url)
save_to_cache(resource_url, link)

markup = types.InlineKeyboardMarkup()
btn1 = types.InlineKeyboardButton("分享页面", callback_data="share%s" % resource_url)
btn2 = types.InlineKeyboardButton("我全都要", callback_data="all%s" % resource_url)
markup.add(btn1, btn2)
Expand All @@ -194,7 +208,7 @@ def choose_link(call):
def share_page(call):
bot.send_chat_action(call.message.chat.id, 'typing')
resource_url = re.findall(r"share(\S*)", call.data)[0]
result = get_from_cache(resource_url)
result = yyets_get_from_cache(resource_url)
bot.send_message(call.message.chat.id, result['share'])


Expand All @@ -203,7 +217,7 @@ def all_episode(call):
# just send a file
bot.send_chat_action(call.message.chat.id, 'typing')
resource_url = re.findall(r"all(\S*)", call.data)[0]
result = get_from_cache(resource_url)
result = yyets_get_from_cache(resource_url)

with tempfile.NamedTemporaryFile(mode='wb+', prefix=result["cnname"], suffix=".txt") as tmp:
bytes_data = json.dumps(result["all"], ensure_ascii=False, indent=4).encode('u8')
Expand Down
4 changes: 3 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
GET_USER = "http://www.rrys2020.com/user/login/getCurUserTopInfo"
# rss is unavailable as of 2021.01.10
RSS_URL = "http://rss.rrys.tv/rss/feed/{id}"
RESOURCE_SCORE = "http://www.rrys2020.com/resource/getScore" # post rid=38000
SEARCH_URL = "http://www.rrys2020.com/search?keyword={kw}&type=resource"
AJAX_LOGIN = "http://www.rrys2020.com/User/Login/ajaxLogin"
SHARE_URL = "http://www.rrys2020.com/resource/ushare"
SHARE_WEB = "http://got002.com/resource.html?code={code}"
# http://got002.com/api/v1/static/resource/detail?code=9YxN91
SHARE_API = "http://got002.com/api/v1/static/resource/detail?code={code}"

WORKERS = "https://yyets.yyetsdb.workers.dev/?id={id}"

TOKEN = os.environ.get("TOKEN") or "TOKEN"
USERNAME = os.environ.get("USERNAME") or "USERNAME"
PASSWORD = os.environ.get("PASSWORD") or "password"
PROXY = os.environ.get("PROXY")
MAINTAINER = os.environ.get("MAINTAINER")
REDIS = os.environ.get("REDIS") or "redis"
REPORT = os.environ.get("REPORT") or False
OFFLINE = os.environ.get("OFFLINE") or False
43 changes: 29 additions & 14 deletions html_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import feedparser
from bs4 import BeautifulSoup

from config import SEARCH_URL, GET_USER, RSS_URL, BASE_URL, SHARE_WEB, SHARE_URL, RESOURCE_SCORE, SHARE_API
from config import SEARCH_URL, GET_USER, RSS_URL, BASE_URL, SHARE_WEB, SHARE_URL, WORKERS, SHARE_API
from utils import load_cookies, cookie_file, login

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(levelname)s]: %(message)s')
Expand Down Expand Up @@ -43,7 +43,7 @@ def get_detail_page(url: str) -> dict:
rss_url = RSS_URL.format(id=url.split("/")[-1])
rss_result = analyse_rss(rss_url)

# get name from here...
# get search_content from here...
if not rss_result:
rss_result = api_res

Expand All @@ -58,7 +58,7 @@ def analyse_search_html(html: str) -> dict:
for block in link_list:
name = block.find_all('a')[-1].text
url = BASE_URL + block.find_all('a')[-1].attrs['href']
list_result[url] = {"name": name, "score": get_score(url.split('/')[-1])}
list_result[url] = name

return list_result

Expand Down Expand Up @@ -92,21 +92,36 @@ def analysis_share_page(detail_url: str) -> (str, dict):
return share_url, api_response


def get_score(rid: str) -> float:
# there is actually no meaning in getting score for now
return 10.0
# return s.post(RESOURCE_SCORE, data={"rid": rid}).json()['score']


def is_cookie_valid() -> bool:
cookie = load_cookies()
r = s.get(GET_USER, cookies=cookie)
return r.json()['status'] == 1


def offline_search(search_content):
# from cloudflare workers
# no redis cache for now
logging.info("Loading data from cfkv...")
index = WORKERS.format(id="index")
data: dict = requests.get(index).json()
logging.info("Loading complete, searching now...")

results = {}
for name, rid in data.items():
if search_content in name:
fake_url = f"http://www.rrys2020.com/resource/{rid}"
results[fake_url] = name.replace("\n", " ")
logging.info("Search complete")
return results


def offline_link(resource_url) -> str:
rid = resource_url.split("/")[-1]
query_url = WORKERS.format(id=rid)
# TODO: too lazy to optimize cloudflare worker page.
return query_url


if __name__ == '__main__':
__search = get_search_html('轮到你了')
__search_result = analyse_search_html(__search)
__chose = "http://www.rrys2020.com/resource/38000"
__link = get_detail_page(__chose)
print(__link)
a = offline_search("越狱")
print(a)
4 changes: 2 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def save_to_cache(url: str, value: dict) -> None:
r.set(url, data, ex=3600 * 12)


def get_from_cache(url: str) -> dict:
def yyets_get_from_cache(url: str) -> dict:
logging.info("Reading data from cache %s", url)
from html_request import get_detail_page

Expand All @@ -35,7 +35,7 @@ def get_from_cache(url: str) -> dict:
else:
logging.info("Cache miss")
save_to_cache(url, get_detail_page(url))
return get_from_cache(url)
return yyets_get_from_cache(url)


def save_error_dump(uid, err: str):
Expand Down

0 comments on commit e5ceaa1

Please sign in to comment.