From 5a582875dda81026b1403d83dbddf1fbb4d8630a Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 27 Nov 2024 00:07:49 +0100 Subject: [PATCH] Refactor page naming. --- src/sync.py | 6 ++-- src/templates/_macros.tmpl | 6 ++-- src/templates/playlist.tmpl | 4 +-- src/templates/queries.tmpl | 6 ++-- src/templates/results.tmpl | 4 +-- src/templates/video.tmpl | 6 ++-- src/templates/videos.tmpl | 4 +-- src/templates/yt_video.tmpl | 6 ++-- src/ytplom/misc.py | 55 +++++++++++++++++++++++++------------ 9 files changed, 59 insertions(+), 38 deletions(-) diff --git a/src/sync.py b/src/sync.py index 1c7e522..7bba29e 100755 --- a/src/sync.py +++ b/src/sync.py @@ -11,7 +11,7 @@ from urllib.request import urlopen from paramiko import SSHClient # type: ignore from scp import SCPClient # type: ignore from ytplom.misc import ( - PATH_DB, PATH_DOWNLOADS, PATH_TEMP, + PAGE_NAMES, PATH_DB, PATH_DOWNLOADS, PATH_TEMP, DatabaseConnection, PathStr, QuotaLog, VideoFile, YoutubeQuery, YoutubeVideo) @@ -21,7 +21,6 @@ YTPLOM_REMOTE = environ.get('YTPLOM_REMOTE') YTPLOM_PORT = environ.get('YTPLOM_PORT') PATH_DB_REMOTE = PathStr(path_join(PATH_TEMP, 'remote_db.sql')) -MISSING_API = PathStr('/missing.json') ATTR_NAME_LAST_UPDATE = 'last_update' @@ -95,7 +94,8 @@ def main(): os_remove(PATH_DB_REMOTE) for host, direction, mover in ((YTPLOM_REMOTE, 'local->remote', scp.put), ('localhost', 'remote->local', scp.get)): - with urlopen(f'http://{host}:{YTPLOM_PORT}{MISSING_API}') as response: + url_missing = f'http://{host}:{YTPLOM_PORT}/{PAGE_NAMES["missing"]}' + with urlopen(url_missing) as response: missing = json_loads(response.read()) for path in (path_join(PATH_DOWNLOADS, path) for path in missing): print(f'SYNC: sending {direction} file {path}') diff --git a/src/templates/_macros.tmpl b/src/templates/_macros.tmpl index 12b28df..e11bc86 100644 --- a/src/templates/_macros.tmpl +++ b/src/templates/_macros.tmpl @@ -3,11 +3,11 @@ {% macro nav_head(selected="") %}

-{{ _link_if("playlist" != selected, "playlist") }} +{{ _link_if("playlist" != selected, page_names.playlist) }} · -{{ _link_if("videos" != selected, "videos") }} +{{ _link_if("files" != selected, page_names.files) }} · -{{ _link_if("queries" != selected, "queries") }} +{{ _link_if("yt_queries" != selected, page_names.yt_queries) }}


{% endmacro %} diff --git a/src/templates/playlist.tmpl b/src/templates/playlist.tmpl index 58c2b5b..7625714 100644 --- a/src/templates/playlist.tmpl +++ b/src/templates/playlist.tmpl @@ -3,7 +3,7 @@ {% block script %} const RELOAD_INTERVAL_S = 10; -const PATH_LAST_UPDATE = '/_last_playlist_update.json'; +const PATH_LAST_UPDATE = '{{page_names.last_update}}'; const MSG_SERVER_DOWN = 'Server seems to be unavailable.'; const MSG_ERR_UNKNOWN = 'Unknown error checking ' + PATH_LAST_UPDATE; const last_update = '{{last_update}}'; @@ -41,7 +41,7 @@ td.history { width: 50%; } {% if running %}{% if pause %}PAUSED{% else %}PLAYING{% endif %}{% else %}STOPPED{% endif %}:
{{ current_title }}
-
+ diff --git a/src/templates/queries.tmpl b/src/templates/queries.tmpl index 457f80e..8dae26d 100644 --- a/src/templates/queries.tmpl +++ b/src/templates/queries.tmpl @@ -2,9 +2,9 @@ {% block body %} -{{ macros.nav_head("queries") }} +{{ macros.nav_head("yt_queries") }}

quota: {{quota_count}}/100000

- +
@@ -15,7 +15,7 @@ {% for query in queries %} - + {% endfor %}
{{query.retrieved_at[:19]}}{{query.text}}{{query.text}}
diff --git a/src/templates/results.tmpl b/src/templates/results.tmpl index 131d14e..1998f7f 100644 --- a/src/templates/results.tmpl +++ b/src/templates/results.tmpl @@ -8,14 +8,14 @@ {% for video in videos %} - + {{video.definition}}
{{video.duration}} -{{video.title}} · {{video.description}} +{{video.title}} · {{video.description}} {% endfor %} diff --git a/src/templates/video.tmpl b/src/templates/video.tmpl index 032a9b9..7de9b06 100644 --- a/src/templates/video.tmpl +++ b/src/templates/video.tmpl @@ -5,10 +5,10 @@ {{ macros.nav_head() }} - - + +
path:{{file.rel_path}}
YouTube ID:{{file.yt_id}}
present:{% if file.present %}yes{% else %}no{% endif %}
YouTube ID:{{file.yt_id}}
present:{% if file.present %}yes{% else %}no{% endif %}
-
+ {% for flag_name in flag_names %} {{ flag_name }}:
{% endfor %} diff --git a/src/templates/videos.tmpl b/src/templates/videos.tmpl index 5cbf47f..c49cf18 100644 --- a/src/templates/videos.tmpl +++ b/src/templates/videos.tmpl @@ -2,11 +2,11 @@ {% block body %} -{{ macros.nav_head("videos") }} +{{ macros.nav_head("files") }}

downloaded videos:

{% endblock %} diff --git a/src/templates/yt_video.tmpl b/src/templates/yt_video.tmpl index 1ecb258..3247741 100644 --- a/src/templates/yt_video.tmpl +++ b/src/templates/yt_video.tmpl @@ -5,17 +5,17 @@ {{ macros.nav_head() }} - + - + diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index f65351c..48f475b 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -39,11 +39,12 @@ FlagName = NewType('FlagName', str) FlagsInt = NewType('FlagsInt', int) AmountDownloads = NewType('AmountDownloads', int) PlayerUpdateId = NewType('PlayerUpdateId', str) +PageNames: TypeAlias = dict[str, PathStr] DownloadsIndex: TypeAlias = dict[YoutubeId, PathStr] TemplateContext: TypeAlias = dict[ str, None | bool | PlayerUpdateId | Optional[PathStr] | YoutubeId | QueryText | QuotaCost | list[FlagName] | 'VideoFile' | 'YoutubeVideo' - | list['YoutubeVideo'] | list['YoutubeQuery'] + | PageNames | list['YoutubeVideo'] | list['YoutubeQuery'] | list[tuple[YoutubeId, PathStr]] | list[tuple[PathStr, PathStr]]] # major expected directories @@ -66,6 +67,20 @@ NAME_TEMPLATE_VIDEO = PathStr('video.tmpl') NAME_TEMPLATE_YT_VIDEO = PathStr('yt_video.tmpl') NAME_TEMPLATE_PLAYLIST = PathStr('playlist.tmpl') +# page names +PAGE_NAMES: PageNames = { + 'download': PathStr('dll'), + 'file': PathStr('fille'), + 'files': PathStr('filles'), + 'last_update': PathStr('last_playlist_update.json'), + 'missing': PathStr('missing.jsoner'), + 'playlist': PathStr('playlisttt'), + 'thumbnails': PathStr('thumbnailz'), + 'yt_result': PathStr('ytt_video'), + 'yt_query': PathStr('ytt_query'), + 'yt_queries': PathStr('ytt_queries') +} + # yt_dlp config YT_DOWNLOAD_FORMAT = 'bestvideo[height<=1080][width<=1920]+bestaudio'\ '/best[height<=1080][width<=1920]' @@ -648,12 +663,12 @@ class TaskHandler(BaseHTTPRequestHandler): page_name = toks_url[1] body_length = int(self.headers['content-length']) postvars = parse_qs(self.rfile.read(body_length).decode()) - if 'playlist' == page_name: + if PAGE_NAMES['playlist'] == page_name: self._post_player_command(list(postvars.keys())[0]) - elif 'video' == page_name: + elif PAGE_NAMES['file'] == page_name: self._post_video_flag(YoutubeId(toks_url[2]), [FlagName(k) for k in postvars]) - elif 'queries' == page_name: + elif PAGE_NAMES['yt_queries'] == page_name: self._post_query(QueryText(postvars['query'][0])) def _post_player_command(self, command: str) -> None: @@ -683,7 +698,9 @@ class TaskHandler(BaseHTTPRequestHandler): file.save(conn) conn.commit_close() file.ensure_absence_if_deleted() - self._send_http(headers=[('Location', f'/video/{yt_id}')], code=302) + self._send_http(headers=[('Location', + f'/{PAGE_NAMES["file"]}/{yt_id}')], + code=302) def _post_query(self, query_txt: QueryText) -> None: conn = DatabaseConnection() @@ -735,8 +752,10 @@ class TaskHandler(BaseHTTPRequestHandler): result.save(conn) result.save_to_query(conn, query_data.id_) conn.commit_close() - self._send_http(headers=[('Location', f'/query/{query_data.id_}')], - code=302) + self._send_http( + headers=[('Location', + f'/{PAGE_NAMES["yt_query"]}/{query_data.id_}')], + code=302) def do_GET(self) -> None: # pylint:disable=invalid-name """Map GET requests to handlers for various paths.""" @@ -744,23 +763,23 @@ class TaskHandler(BaseHTTPRequestHandler): toks_url: list[str] = url.path.split('/') page_name = toks_url[1] try: - if 'thumbnails' == page_name: + if PAGE_NAMES['thumbnails'] == page_name: self._send_thumbnail(PathStr(toks_url[2])) - elif 'dl' == page_name: + elif PAGE_NAMES['download'] == page_name: self._send_or_download_video(YoutubeId(toks_url[2])) - elif 'videos' == page_name: + elif PAGE_NAMES['files'] == page_name: self._send_videos_index() - elif 'video' == page_name: + elif PAGE_NAMES['file'] == page_name: self._send_video_data(YoutubeId(toks_url[2])) - elif 'yt_video' == page_name: + elif PAGE_NAMES['yt_result'] == page_name: self._send_yt_video_data(YoutubeId(toks_url[2])) - elif 'missing.json' == page_name: + elif PAGE_NAMES['missing'] == page_name: self._send_missing_json() - elif 'query' == page_name: + elif PAGE_NAMES['yt_query'] == page_name: self._send_query_page(QueryId(toks_url[2])) - elif 'queries' == page_name: + elif PAGE_NAMES['yt_queries'] == page_name: self._send_queries_index_and_search() - elif '_last_playlist_update.json' == page_name: + elif PAGE_NAMES['last_update'] == page_name: self._send_last_playlist_update() else: # e.g. for / self._send_playlist() @@ -772,6 +791,7 @@ class TaskHandler(BaseHTTPRequestHandler): tmpl_ctx: TemplateContext ) -> None: tmpl = self.server.jinja.get_template(tmpl_name) + tmpl_ctx['page_names'] = PAGE_NAMES html = tmpl.render(**tmpl_ctx) self._send_http(bytes(html, 'utf8')) @@ -799,7 +819,8 @@ class TaskHandler(BaseHTTPRequestHandler): self._send_http(content=video) return self.server.downloads.queue_download(video_id) - self._send_http(headers=[('Location', f'/yt_video/{video_id}')], + self._send_http(headers=[('Location', + f'/{PAGE_NAMES["yt_result"]}/{video_id}')], code=302) def _send_query_page(self, query_id: QueryId) -> None: -- 2.30.2
title:{{video_data.title}}
thumbnail:
thumbnail:
description:{{video_data.description}}
duration:{{video_data.duration}}
definition:{{video_data.definition}}
YouTube ID:{{video_data.id_}} (watch)
download:{% if is_temp %}working on it{% else %}{{ file_path if file_path else "please do" }}{% endif %}
download:{% if is_temp %}working on it{% else %}{{ file_path if file_path else "please do" }}{% endif %}
linked queries: