implement po_token extractor args

This commit is contained in:
Simon
2025-01-11 16:39:50 +07:00
parent 2474a6a96b
commit 457f6a0b87
9 changed files with 168 additions and 7 deletions

View File

@@ -40,6 +40,7 @@ class DownloadsConfigType(TypedDict):
comment_max: str | bool
comment_sort: Literal["top", "new"]
cookie_import: bool
potoken: bool
throttledratelimit: int
extractor_lang: str | bool
integrate_ryd: bool
@@ -85,6 +86,7 @@ class AppConfig:
"comment_max": False,
"comment_sort": "top",
"cookie_import": False,
"potoken": False,
"throttledratelimit": False,
"extractor_lang": False,
"integrate_ryd": False,

View File

@@ -34,6 +34,11 @@ urlpatterns = [
views.CookieView.as_view(),
name="api-cookie",
),
path(
"potoken/",
views.POTokenView.as_view(),
name="api-potoken",
),
path(
"token/",
views.TokenView.as_view(),

View File

@@ -5,7 +5,7 @@ from appsettings.src.config import AppConfig
from appsettings.src.snapshot import ElasticSnapshot
from common.src.ta_redis import RedisArchivist
from common.views_base import AdminOnly, ApiBaseView
from download.src.yt_dlp_base import CookieHandler
from download.src.yt_dlp_base import CookieHandler, POTokenHandler
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from task.src.task_manager import TaskCommand
@@ -252,6 +252,36 @@ class CookieView(ApiBaseView):
return validation
class POTokenView(ApiBaseView):
"""handle PO token"""
permission_classes = [AdminOnly]
def get(self, request):
"""get token"""
config = AppConfig().config
potoken = POTokenHandler(config).get()
return Response({"potoken": potoken})
def post(self, request):
"""post token"""
config = AppConfig().config
new_token = request.data.get("potoken")
if not new_token:
message = "missing potoken key in request data"
print(message)
return Response({"message": message}, status=400)
POTokenHandler(config).set_token(new_token)
return Response({"potoken": new_token})
def delete(self, request):
"""delete token"""
config = AppConfig().config
POTokenHandler(config).revoke_token()
return Response({"potoken": None})
class TokenView(ApiBaseView):
"""resolves to /api/appsettings/token/
DELETE: revoke the token