-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
88 lines (74 loc) · 3.37 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
from datetime import datetime
from bs4 import BeautifulSoup
from flask import Flask, jsonify, request, render_template
from flask import current_app
from api import API
app = Flask(__name__, static_folder='static')
app.config['JSON_AS_ASCII'] = False
app.logger.setLevel(logging.DEBUG)
dmhyAPI = API()
unknown_subgroup_id = -1
unknown_subgroup_name = "未知字幕组"
parser_html = 'html.parser'
@app.route("/")
def read_root():
return render_template('index.html')
@app.route("/subgroup")
def subgroup():
_, data = dmhyAPI.get_types_and_subgroups()
soup = BeautifulSoup(data, parser_html)
options = soup.find(id='AdvSearchTeam')
subgroups = [{'Id': int(option.get('value')), 'Name': option.text} for option in options.contents]
subgroups.append({'Id': unknown_subgroup_id, 'Name': unknown_subgroup_name})
return jsonify(Subgroups=subgroups)
@app.route("/type")
def type():
_, data = dmhyAPI.get_types_and_subgroups()
soup = BeautifulSoup(data, parser_html)
options = soup.find(id='AdvSearchSort')
types = [{'Id': int(option.get('value')), 'Name': option.text} for option in options.contents]
return jsonify(Types=types)
@app.route("/list")
def list():
keyword = request.args.get('keyword')
sort_id = request.args.get('sort_id')
team_id = request.args.get('team_id')
sort_id = int(sort_id) if sort_id else 0
team_id = int(team_id) if team_id else 0
_, data = dmhyAPI.search(keyword=keyword, sort_id=sort_id, team_id=team_id)
has_more = False
resources = []
try:
soup = BeautifulSoup(data, parser_html)
trs = soup.find(id='topic_list').find_all('tr')[1:]
has_more = True if any(['下一頁' in getattr(div.find('a'), 'text', '') for div in
soup.find_all('div', class_='nav_title')]) else False
for tr in trs:
has_subgroup_info = (len(tr.find_all('td')[2].find_all('a')) == 2)
try:
resource = {
'Title': tr.find_all('td')[2].find_all('a')[-1].text.strip(),
'TypeId': int(tr.find_all('td')[1].a.get('href').split('/')[-1]),
'TypeName': tr.find_all('td')[1].a.text.strip(),
'SubgroupId': int(tr.find_all('td')[2].find_all('a')[0].get('href').split('/')[
-1]) if has_subgroup_info else unknown_subgroup_id,
'SubgroupName': tr.find_all('td')[2].find_all('a')[
0].text.strip() if has_subgroup_info else unknown_subgroup_name,
'Magnet': tr.find_all('td')[3].a.get('href'),
'PageUrl': dmhyAPI.base_uri + tr.find_all('td')[2].find_all('a')[-1].get('href'),
'FileSize': tr.find_all('td')[4].text.strip(),
'PublishDate': datetime.strptime(
tr.find_all('td')[0].span.text.strip(), '%Y/%m/%d %H:%M'
).strftime('%Y-%m-%d %H:%M:%S')
}
resources.append(resource)
except ValueError:
pass
except Exception as e:
current_app.logger.error(e)
current_app.logger.error(tr.pretty())
except Exception as e:
current_app.logger.error(e)
current_app.logger.error("无法解析结果")
return jsonify(HasMore=has_more, Resources=resources)