mirror of
https://git.vectorsigma.ru/public/tubearchivist.git
synced 2026-08-08 23:09:33 +00:00
implement po_token extractor args
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -251,6 +251,7 @@ class PendingList(PendingIndex):
|
||||
self._notify_add(idx, total)
|
||||
video_details = self.get_youtube_details(youtube_id, vid_type)
|
||||
if not video_details:
|
||||
rand_sleep(self.config)
|
||||
continue
|
||||
|
||||
video_details.update(
|
||||
|
||||
@@ -11,6 +11,7 @@ from io import StringIO
|
||||
import yt_dlp
|
||||
from appsettings.src.config import AppConfig
|
||||
from common.src.ta_redis import RedisArchivist
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class YtWrap:
|
||||
@@ -36,6 +37,10 @@ class YtWrap:
|
||||
self.obs.update(self.obs_request)
|
||||
if self.config:
|
||||
self.add_cookie()
|
||||
self.add_potoken()
|
||||
|
||||
if getattr(settings, "DEBUG", False):
|
||||
print(self.obs)
|
||||
|
||||
def add_cookie(self):
|
||||
"""add cookie if enabled"""
|
||||
@@ -43,6 +48,21 @@ class YtWrap:
|
||||
cookie_io = CookieHandler(self.config).get()
|
||||
self.obs["cookiefile"] = cookie_io
|
||||
|
||||
def add_potoken(self):
|
||||
"""add potoken if enabled"""
|
||||
if self.config["downloads"].get("potoken"):
|
||||
potoken = POTokenHandler(self.config).get()
|
||||
self.obs.update(
|
||||
{
|
||||
"extractor_args": {
|
||||
"youtube": {
|
||||
"po_token": [potoken],
|
||||
"player-client": ["web", "default"],
|
||||
},
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
def download(self, url):
|
||||
"""make download request"""
|
||||
with yt_dlp.YoutubeDL(self.obs) as ydl:
|
||||
@@ -148,3 +168,27 @@ class CookieHandler:
|
||||
"validated_str": now.strftime("%Y-%m-%d %H:%M"),
|
||||
}
|
||||
RedisArchivist().set_message("cookie:valid", message)
|
||||
|
||||
|
||||
class POTokenHandler:
|
||||
"""handle po token"""
|
||||
|
||||
REDIS_KEY = "potoken"
|
||||
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
|
||||
def get(self) -> str | None:
|
||||
"""get PO token"""
|
||||
potoken = RedisArchivist().get_message_str(self.REDIS_KEY)
|
||||
return potoken
|
||||
|
||||
def set_token(self, new_token: str) -> None:
|
||||
"""set new PO token"""
|
||||
RedisArchivist().set_message(self.REDIS_KEY, new_token)
|
||||
AppConfig().update_config({"downloads.potoken": True})
|
||||
|
||||
def revoke_token(self) -> None:
|
||||
"""revoke token"""
|
||||
RedisArchivist().del_message(self.REDIS_KEY)
|
||||
AppConfig().update_config({"downloads.potoken": False})
|
||||
|
||||
Reference in New Issue
Block a user