3232 logger .debug ('use "spawn" for multi-process' )
3333
3434
35- @app .listener (' before_server_start' )
35+ @app .listener (" before_server_start" )
3636async def connect_db (app , loop ):
3737 logger .debug ("open sqlite Connection" )
3838 sqlite_client .connect ()
3939 sqlite_client .create_tables ([Video , VideoChapter , Chat ])
4040
4141
42- @app .listener (' after_server_stop' )
42+ @app .listener (" after_server_stop" )
4343async def close_db (app , loop ):
4444 if not sqlite_client .is_closed ():
4545 logger .debug ("close sqlite" )
@@ -49,13 +49,7 @@ async def close_db(app, loop):
4949@app .exception (Exception )
5050async def handle_exception (request : Request , exception : Exception ):
5151 logger .error (exception , exc_info = True )
52- return json (
53- {
54- "status_code" : 500 ,
55- "message" : str (exception )
56- },
57- status = 500
58- )
52+ return json ({"status_code" : 500 , "message" : str (exception )}, status = 500 )
5953
6054
6155@app .exception (LogicError )
@@ -66,7 +60,7 @@ async def handle_exception(request: Request, exception: LogicError):
6660 "status_code" : 400 ,
6761 "message" : str (exception ),
6862 },
69- status = 500
63+ status = 500 ,
7064 )
7165
7266
@@ -77,38 +71,40 @@ async def health(request: Request):
7771
7872@app .post ("/api/youtube/prefetch" )
7973async def fetch_youtube_video_info (request : Request ):
80- url = request .json [' url' ]
74+ url = request .json [" url" ]
8175 youtube_service = YoutubeService (url )
8276 data = youtube_service .fetch_basic_info ()
83- return json ({
84- "status_code" : 200 ,
85- "message" : "Successfully fetch video info" ,
86- "payload" : data
87- })
77+ return json (
78+ {
79+ "status_code" : 200 ,
80+ "message" : "Successfully fetch video info" ,
81+ "payload" : data ,
82+ }
83+ )
8884
8985
9086@app .post ("/api/youtube/process" )
9187async def process_youtube_video (request : Request ):
92- url = request .json [' url' ]
93- provider = request .json [' provider' ]
88+ url = request .json [" url" ]
89+ provider = request .json [" provider" ]
9490 youtube_service = YoutubeService (url )
9591 data = await asyncio .create_task (youtube_service .fetch_video_data (provider ))
96- return json ({
97- "status_code" : 200 ,
98- "message" : "Successfully fetch video data, analyze video in processing." ,
99- "payload" : data
100- })
92+ return json (
93+ {
94+ "status_code" : 200 ,
95+ "message" : "Successfully fetch video data, analyze video in processing." ,
96+ "payload" : data ,
97+ }
98+ )
10199
102100
103101@app .post ("/api/video/analysis" )
104102async def analysis_youtube_video (request : Request ):
105- video_id : int = int (request .json [' video_id' ])
103+ video_id : int = int (request .json [" video_id" ])
106104 data = await asyncio .create_task (VideoService .analysis_video (video_id ))
107- return json ({
108- "status_code" : 200 ,
109- "message" : "Analyze video in processing." ,
110- "payload" : data
111- })
105+ return json (
106+ {"status_code" : 200 , "message" : "Analyze video in processing." , "payload" : data }
107+ )
112108
113109
114110@app .options ("/api/video/detail/<video_id>" )
@@ -119,72 +115,66 @@ async def opts_detail(request: Request, video_id: int):
119115@app .get ("/api/video/detail/<video_id>" )
120116async def get_video_detail (request : Request , video_id : int ):
121117 data = VideoService .get_video_detail (video_id )
122- return json ({
123- "status_code" : 200 ,
124- "message" : "Successfully get video detail" ,
125- "payload" : data
126- })
118+ return json (
119+ {
120+ "status_code" : 200 ,
121+ "message" : "Successfully get video detail" ,
122+ "payload" : data ,
123+ }
124+ )
127125
128126
129127@app .post ("/api/video/summary" )
130128async def summary (request : Request ):
131- vid = request .json [' video_id' ]
132- lang_code = request .json [' lang_code' ]
133- provider = request .json [' provider' ]
129+ vid = request .json [" video_id" ]
130+ lang_code = request .json [" lang_code" ]
131+ provider = request .json [" provider" ]
134132 model = request .json .get ("model" , None )
135- data = await asyncio .create_task (VideoService .summary_video (vid , lang_code , provider , model ))
133+ data = await asyncio .create_task (
134+ VideoService .summary_video (vid , lang_code , provider , model )
135+ )
136136 asyncio .create_task (VideoService .analysis_summary_video (vid , model , provider ))
137- return json ({
138- "status_code" : 200 ,
139- "message" : "Successfully summary video" ,
140- "payload" : data
141- })
137+ return json (
138+ {"status_code" : 200 , "message" : "Successfully summary video" , "payload" : data }
139+ )
142140
143141
144142@app .delete ("/api/video/<video_id>" )
145143async def delete_video (request : Request , video_id : int ):
146144 VideoService .delete (video_id )
147- return json ({
148- "status_code" : 200 ,
149- "message" : f"Successfully delete video { video_id } "
150- })
145+ return json (
146+ {"status_code" : 200 , "message" : f"Successfully delete video { video_id } " }
147+ )
151148
152149
153150@app .get ("/api/videos/<page>" )
154151async def list_videos (request : Request , page : int ):
155152 total , data = VideoService .get_videos_with_paging (page )
156- return json ({
157- "status_code" : 200 ,
158- "message" : "Successfully" ,
159- "payload" : {
160- "total" : total ,
161- "videos" : data
153+ return json (
154+ {
155+ "status_code" : 200 ,
156+ "message" : "Successfully" ,
157+ "payload" : {"total" : total , "videos" : data },
162158 }
163- } )
159+ )
164160
165161
166162@app .post ("/api/chat" )
167163async def chat (request : Request ):
168- vid = request .json [' video_id' ]
169- question = request .json [' question' ]
170- provider = request .json [' provider' ]
164+ vid = request .json [" video_id" ]
165+ question = request .json [" question" ]
166+ provider = request .json [" provider" ]
171167 model = request .json .get ("model" , None )
172168 data = await asyncio .create_task (ChatService .ask (question , vid , provider , model ))
173- return json ({
174- "status_code" : 200 ,
175- "message" : "Successfully" ,
176- "payload" : data
177- })
169+ return json ({"status_code" : 200 , "message" : "Successfully" , "payload" : data })
178170
179171
180172@app .get ("/api/chat/history/<video_id>" )
181173async def chat_history (request : Request , video_id : int ):
182174 chat_histories = ChatService .get_chat_histories (video_id = video_id )
183- return json ({
184- "status_code" : 200 ,
185- "message" : "Successfully" ,
186- "payload" : chat_histories
187- })
175+ return json (
176+ {"status_code" : 200 , "message" : "Successfully" , "payload" : chat_histories }
177+ )
188178
189179
190180@app .delete ("/api/chat/clear/<video_id>" )
@@ -193,7 +183,14 @@ async def clear_chat(request: Request, video_id: int):
193183 return response .HTTPResponse (status = 200 )
194184
195185
196- if __name__ == ' __main__' :
186+ if __name__ == " __main__" :
197187 setup_log ()
198188 app_debug_mode = True if env .DEBUG_MODE in ["on" , "yes" , "enabled" ] else False
199- app .run (host = "0.0.0.0" , port = 8000 , access_log = True , dev = app_debug_mode , debug = app_debug_mode , workers = 10 )
189+ app .run (
190+ host = "0.0.0.0" ,
191+ port = 8000 ,
192+ access_log = True ,
193+ dev = app_debug_mode ,
194+ debug = app_debug_mode ,
195+ workers = 10 ,
196+ )
0 commit comments