-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
205 lines (171 loc) · 5.66 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from aiohttp import web
from loguru import logger
import os
import json
import base64
import time
from cryptography import fernet
from aiohttp_session import setup, get_session, session_middleware
from aiohttp_session.cookie_storage import EncryptedCookieStorage
from bilibili_api import hot, homepage, rank, search
from config import put_config_by_key, get_config_by_key, get_all_config_safe
from functools import wraps
import asyncio
import ffvideo
routes = web.RouteTableDef()
def json_ok(data):
return web.json_response({
'status': 'ok',
'data': data,
})
def json_fail(status='fail'):
return web.json_response({
'status': status,
'data': None
})
def login_check(req_handler):
@wraps(req_handler)
async def wrapTheFunction(request):
session = await get_session(request)
if 'last_visit' not in session:
logger.error('need login')
return json_fail('need_login')
data = await req_handler(request)
return data
return wrapTheFunction
@routes.post('/api/login')
async def login(request):
data = await request.json()
logger.info(f'post data:{data}')
password = data['password']
if get_config_by_key('password') == password:
session = await get_session(request)
session['last_visit'] = int(time.time())
return json_ok({})
return json_fail()
@routes.get('/api/logout')
async def logout(request):
session = await get_session(request)
if 'last_visit' in session:
del session['last_visit']
return json_ok({})
@login_check
@routes.get('/api/ws/ctl')
async def video_control_ws(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == web.WSMsgType.TEXT:
logger.info(f'ws receive msg:{msg.data}')
payload = json.loads(msg.data)
type = payload['type']
args = payload['args']
if type == 'init':
ffvideo.set_video_config(args)
await ws.send_str('init_ok')
elif type == 'pause':
ffvideo.set_play_state('pause')
elif type == 'play':
ffvideo.set_play_state('playing')
else:
raise Exception('not support')
if msg.type == web.WSMsgType.ERROR:
logger.error('ws connection closed with exception %s' % ws.exception())
logger.info('websocket connection closed')
return ws
@login_check
@routes.get('/api/ws')
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
# 启动,后台线程处理
task = asyncio.create_task(ffvideo.sync_to_ws(ws))
logger.info(f'start sync_to_ws task')
async for msg in ws:
if msg.type == web.WSMsgType.ERROR:
logger.error('ws connection closed with exception %s' % ws.exception())
logger.info('websocket connection closed')
task.cancel()
logger.info(f'sync_to_ws task cancel')
return ws
@routes.get('/api/config')
@login_check
async def get_config(request):
return json_ok(get_all_config_safe())
@routes.get('/api/video/list')
@login_check
async def video_list(request):
prefix = get_config_by_key('video_path')
path = request.query["path"]
abs_path = f'{prefix}/{path}'
files = os.listdir(abs_path)
ret = []
files.sort()
for f in files:
abs_f = f'{abs_path}/{f}'
if os.path.isdir(abs_f):
ret.append({
'fileType': 'DIR',
'fileName': f,
})
else:
if f.endswith('.mp4'):
ret.append({
'fileType': 'FILE',
'fileName': f,
'url': os.path.normpath(f'/api/video/files/{path}/{f}')
})
return json_ok(ret)
@routes.get('/api/video/files/{path:[^{}]+}')
@login_check
async def video_files(request):
prefix = get_config_by_key('video_path')
path = request.match_info['path']
abs_path = os.path.normpath(f'{prefix}/{path}')
logger.info(f'play path: {abs_path}')
return web.FileResponse(path = abs_path)
@routes.get(r'/')
async def home(request):
return web.HTTPFound("/index.html")
@routes.get(r'/{name:(?!api).+}')
async def static_web(request):
name = request.match_info['name']
logger.info(f'static file request:{name}')
fpath = f'./web/dist/{name}'
if os.path.exists(fpath):
return web.FileResponse(path=fpath)
else:
# return web.HTTPFound("/index.html")
return web.HTTPNotFound()
@routes.get('/api/bilibili/hot')
@login_check
async def bl_hot(request):
resp = await hot.get_hot_videos()
return json_ok(resp)
@routes.get('/api/bilibili/search')
@login_check
async def bl_search(request):
keyword = request.query["keyword"]
page_no = request.query["pageNo"]
resp = await search.search(keyword, page_no)
return json_ok(resp)
@routes.get('/api/bilibili/rank')
@login_check
async def bl_rank(request):
type = request.query["type"]
logger.info(f'rank type:{type}')
resp = await rank.get_rank(rank.RankType[type])
return json_ok(resp)
@routes.get('/api/bilibili/home')
@login_check
async def bl_home(request):
resp = await homepage.get_videos()
return json_ok(resp)
if __name__ == '__main__':
app = web.Application()
# cookie session加密
secret_key = get_config_by_key('secret_key', default_value=fernet.Fernet.generate_key().decode('utf-8'))
put_config_by_key('secret_key', secret_key)
setup(app, EncryptedCookieStorage(base64.urlsafe_b64decode(secret_key.encode('utf-8'))))
app.add_routes(routes)
web.run_app(app)