introduce a bare-bones invidious API translation layer
also rewritten ythdd_api.py a tiny bit
This commit is contained in:
37
ythdd_api.py
37
ythdd_api.py
@@ -2,7 +2,8 @@
|
||||
from flask import Response, request
|
||||
from markupsafe import escape
|
||||
import requests, time, json
|
||||
import ythdd_api_v1, ythdd_globals
|
||||
import ythdd_globals
|
||||
import ythdd_api_v1, ythdd_inv_tl
|
||||
|
||||
def api_greeting():
|
||||
string = {'status': 200, 'msg': f"ok (ythdd {ythdd_globals.version})", 'latest_api': f"v{ythdd_globals.apiVersion}"}
|
||||
@@ -18,18 +19,40 @@ def api_global_catchall(received_request):
|
||||
api_version = request_list[0]
|
||||
if request_list[0] == 'v1':
|
||||
# use v1 api
|
||||
del request_list[0] # v1
|
||||
# if list is empty, aka /api/v1/
|
||||
if request_list == ['']:
|
||||
return api_greeting()
|
||||
del request_list[0]
|
||||
# if list is empty, aka /api/v1/, or /api/v1
|
||||
if request_list == [''] or request_list == []:
|
||||
#return api_greeting()
|
||||
resp = api_greeting()
|
||||
try:
|
||||
status, received, data = ythdd_api_v1.lookup(request_list)
|
||||
except Exception as e:
|
||||
ythdd_globals.apiFailedRequests += 1
|
||||
status, received, data = 500, f"internal server error: call ended in failure: {e}", []
|
||||
resp = Response(json.dumps({'status': status, 'msg': received, 'data': data}), mimetype='application/json', status=status)
|
||||
elif request_list[0] == 'invidious':
|
||||
# drop 'invidious' from the list
|
||||
del request_list[0]
|
||||
# for /api/invidious/ and /api/invidious
|
||||
# show greeting from Invidious TL
|
||||
#print(request_list) # for debugging purposes
|
||||
if request_list == [''] or request_list == []:
|
||||
#resp = ythdd_inv_tl.greeting()
|
||||
status, response = ythdd_inv_tl.greeting()
|
||||
return Response(response, status=status)
|
||||
# if a path has been supplied try to get appropriate data
|
||||
try:
|
||||
# lookup and construct a response
|
||||
resp = ythdd_inv_tl.lookup(request_list)
|
||||
#print(resp) # for debugging purposes
|
||||
# unless an error occurs
|
||||
except Exception as e:
|
||||
ythdd_globals.apiFailedRequests += 1
|
||||
status, received, data = 500, f"internal server error: invidious translation call ended in failure: {e}", []
|
||||
resp = Response(json.dumps({'status': status, 'msg': received, 'data': data}), mimetype='application/json', status=status)
|
||||
else:
|
||||
ythdd_globals.apiFailedRequests += 1
|
||||
status, received, data = 405, f'error: unsupported api version: "{request_list[0]}". try: "v{ythdd_globals.apiVersion}".', []
|
||||
resp = Response(json.dumps({'status': status, 'msg': received, 'data': data}), mimetype='application/json', status=status)
|
||||
|
||||
response = {'status': status, 'msg': received, 'data': data}
|
||||
return Response(json.dumps(response), mimetype='application/json', status=status)
|
||||
return resp
|
||||
Reference in New Issue
Block a user