-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
188 lines (150 loc) · 6.32 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from datetime import datetime
import json, os, re
from flask import Flask, request
from flask_cors import CORS
from flask_compress import Compress
import requests
GAME_API = {
'genshin':
'https://api-takumi-static.mihoyo.com/content_v2_user/app/16471662a82d418a/getContentList?iAppId=43&iChanId=719&iPageSize={pageSize}&iPage={pageNum}&sLangKey=zh-cn',
'starrail':
'https://api-takumi-static.mihoyo.com/content_v2_user/app/1963de8dc19e461c/getContentList?iPage={pageNum}&iPageSize={pageSize}&sLangKey=zh-cn&isPreview=0&iChanId=255',
'honkai3':
'https://api-takumi-static.mihoyo.com/content_v2_user/app/b9d5f96cd69047eb/getContentList?iPageSize={pageSize}&iPage={pageNum}&sLangKey=zh-cn&iChanId=693&isPreview=0',
'zzz':
"https://api-takumi-static.mihoyo.com/content_v2_user/app/706fd13a87294881/getContentList?iPageSize={pageSize}&iPage={pageNum}&sLangKey=zh-cn&iChanId=273",
'mihoyo':
"https://api-takumi-static.mihoyo.com/content_v2_user/app/537c17c553834ed6/getContentList?iPageSize={pageSize}&iPage={pageNum}&sLangKey=zh-cn&iChanId=107",
'genshin_os':
"https://sg-public-api-static.hoyoverse.com/content_v2_user/app/a1b1f9d3315447cc/getContentList?iAppId=32&iChanId=395&iPageSize={pageSize}&iPage={pageNum}&sLangKey=zh-tw",
'starrail_os':
"https://sg-public-api-static.hoyoverse.com/content_v2_user/app/113fe6d3b4514cdd/getContentList?iPage={pageNum}&iPageSize={pageSize}&sLangKey=zh-cn&isPreview=0&iChanId=248",
'honkai3_os':
"https://sg-public-api-static.hoyoverse.com/content_v2_user/app/5fcd2aa439ca4aea/getContentList?iPageSize={pageSize}&iPage={pageNum}&sLangKey=zh-cn&iChanId=514&isPreview=0",
'zzz_os':
"https://sg-public-api-static.hoyoverse.com/content_v2_user/app/3e9196a4b9274bd7/getContentList?iPageSize={pageSize}&iPage={pageNum}&iChanId=288&sLangKey=zh-cn",
'hoyoverse':
"https://sg-public-api-static.hoyoverse.com/content_v2_user/app/ea15ca8a7a654b5e/getContentList?isPreview=0&iPage={pageNum}&iPageSize={pageSize}&iChanId=206&sLangKey=zh-cn",
}
if os.environ.get('SUPPORT_GAMES'):
SUPPORT_GAMES = os.environ.get('SUPPORT_GAMES').split(',')
else:
SUPPORT_GAMES = list(GAME_API.keys())
PAGE_SIZE = 100
CACHE_TIME = 3600
CACHE_PATH = './data'
VIDEO_PATTERN = r'https?://[^ ]+\.(mp4|mov)'
app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
compress = Compress(app)
def get_news(api_url, pageSize, pageNum=1):
res = requests.get(api_url.format(pageSize=pageSize, pageNum=pageNum))
if not res.ok:
return None
ret_data = res.json()
if ret_data['retcode'] != 0:
return None
return [transform_news(news) for news in ret_data['data']['list']]
def get_total(api_url):
res = requests.get(api_url.format(pageSize=5, pageNum=1))
if not res.ok:
return None
ret_data = res.json()
if ret_data['retcode'] != 0:
return None
return ret_data['data']['iTotal']
def transform_news(news_raw):
ret = {
'id': news_raw['iInfoId'],
'title': news_raw['sTitle'],
'startTime': news_raw['dtStartTime'],
'createTime': news_raw['dtCreateTime'],
'banner': None,
'video': None,
}
ext_data = json.loads(news_raw['sExt'])
for ext in ext_data:
datalist = ext_data[ext]
if not isinstance(datalist, list):
continue
for data in datalist:
if data.get('url'):
if data['url'].startswith('http'):
ret['banner'] = data['url']
break
if ret['banner']:
break
if video := re.search(VIDEO_PATTERN, news_raw['sContent']):
ret['video'] = video.group(0)
return ret
def read_cache(cache_name):
filename = os.path.join(CACHE_PATH, cache_name + '_cache.json')
if os.path.exists(filename):
return json.load(open(filename, mode='r', encoding='utf-8'))
else:
return None
def write_cache(cache_name, data):
if not os.path.exists(CACHE_PATH):
os.makedirs(CACHE_PATH)
filename = os.path.join(CACHE_PATH, cache_name + '_cache.json')
json.dump(data, open(filename, 'w', encoding='utf-8'), ensure_ascii=False)
def get_ts():
return int(datetime.now().timestamp())
def get_news_data(game: str):
api_url = GAME_API[game]
news_total = get_total(api_url)
print(f"game: {game}")
print(f"news: {news_total}")
news_list = []
current_page = 1
while (current_page - 1) * PAGE_SIZE < news_total:
print(f'page: {current_page}')
news_list.extend(get_news(api_url, PAGE_SIZE, current_page))
current_page += 1
return {
'updateTime': get_ts(),
'newsCount': news_total,
'newsData': news_list
}
def patch_news_list(news_list: list, patch: list, total: int):
if total <= len(patch):
return patch
return patch + news_list[-(total - len(patch)):]
with app.app_context():
print('support games: ' + ','.join(SUPPORT_GAMES))
for game in SUPPORT_GAMES:
if cache := read_cache(game):
total = get_total(GAME_API[game])
if total - len(cache['newsData']) < PAGE_SIZE:
continue
write_cache(game, get_news_data(game))
@app.route('/<game>', methods=['GET'])
def get_game_news(game: str):
if game not in SUPPORT_GAMES:
return {'code': 1, 'msg': '配置不存在'}
query = request.args
force_refresh = query.get('force_refresh', '0') == '1'
data = read_cache(game)
try:
if data is None:
data = get_news_data(game)
if data:
write_cache(game, data)
if data is None:
return {'code': 1, 'msg': '获取数据失败'}
cache_expired = data['updateTime'] + CACHE_TIME < get_ts()
if force_refresh or cache_expired:
total = get_total(GAME_API[game])
data['updateTime'] = get_ts()
if total != data['newsCount']:
data['newsCount'] = total
data['newsData'] = patch_news_list(
data['newsData'], get_news(GAME_API[game], PAGE_SIZE, 1),
total)
write_cache(game, data)
return {'code': 0, **data}
except Exception as e:
print(e)
return {'code': 1, 'msg': '服务器错误,请稍后再试'}
if __name__ == '__main__':
app.run('0.0.0.0', port=3000, debug=True)