-
Notifications
You must be signed in to change notification settings - Fork 0
/
zhibo8_v4.py
73 lines (58 loc) · 2.9 KB
/
zhibo8_v4.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
import os
root = os.path.dirname(os.path.relpath(__file__))
from jinja2 import Environment, FileSystemLoader
import re, requests, time, eel
templates_dir = os.path.join(root, "web")
env = Environment(loader=FileSystemLoader(templates_dir))
template = env.get_template('main_template.html')
def getHtml(url="https://www.zhibo8.cc/"):
req = requests.get(url)
if req.encoding == 'ISO-8859-1':
encodings = requests.utils.get_encodings_from_content(req.text)
if encodings:
encoding = encodings[0]
else:
encoding = req.apparent_encoding
encode_content = req.content.decode(encoding, 'replace') # 如果设置为replace,则会用?取代非法字符;
return encode_content
else:
return req.text
def reform(result): # 整理比赛数据格式
sort = [result[1], result[0], result[2]]
timeLeft = time.mktime(time.strptime(sort[0], '%Y-%m-%d %H:%M')) - time.time()
if timeLeft < 82800 and sort[0][-5:] >= "00:00" and sort[0][-5:] <= "05:00": # 判断时间是否需要替换为汉字
sort[0] = "今夜 " + sort[0][-5:]
elif timeLeft < 169200: # ‘明晚’方法待考证
sort[0] = "明天 " + sort[0][-5:]
return sort
def splitTeamInfo(gameInfoList):
nonTeam = ['欧联杯', '足球', '篮球', 'NBA', 'CBA', '英超', '西甲', '荷甲', '待定', '中超', '亚冠', '欧冠',
'中甲', '足协杯']
giveup = ['篮球', '足球', 'F1', '其他']
gameInfo = gameInfoList.split(',')
temp1 = [i for i in gameInfo if i not in nonTeam and i not in giveup]
temp2 = [i for i in gameInfo if i in nonTeam and i not in giveup]
gameInfoListSorted = [temp1] + [temp2]
return gameInfoListSorted
def showTeam(*args):
showList = showListReady = []
targetRE = '<li label="(.*?)" id="saishi.*?data-time="(.*?)".*?">(.*?)</a>'
results = re.findall(targetRE, getHtml(), re.S)
for result in results:
resultReform = reform(result)
for team in args:
if team in resultReform[1] and resultReform not in showList:
showList.append(resultReform)
for game in range(len(showList)): # 整理成分组的list,[第一组时间][第二组比赛信息][第三组转播信息]
showListReady[game] = [showList[game][0].split()] + [splitTeamInfo(showList[game][1])] + \
[showList[game][2].split()]
return showListReady
if __name__ == '__main__':
showListReady = showTeam('国安', '利物浦', '阿森纳', '热刺', '勇士', 'F1', '皇家马德里')
listfix = [[item for subsublist in sublist for item in subsublist] for sublist in showListReady]
filename = os.path.join(root, 'web', 'index.html')
with open(filename, 'w', encoding='UTF-8') as fh:
output = template.render(showListReady=showListReady)
fh.write(output)
eel.init('web')
eel.start('index.html', size=(760, 700))