home · contact · privacy
Refactor page naming.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 26 Nov 2024 23:07:49 +0000 (00:07 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 26 Nov 2024 23:07:49 +0000 (00:07 +0100)
src/sync.py
src/templates/_macros.tmpl
src/templates/playlist.tmpl
src/templates/queries.tmpl
src/templates/results.tmpl
src/templates/video.tmpl
src/templates/videos.tmpl
src/templates/yt_video.tmpl
src/ytplom/misc.py

index 1c7e5225e7dce800f185759d0764d45c506d374c..7bba29ea154c936a45ba2efd0d48562771bbed9c 100755 (executable)
@@ -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}')
index 12b28df410b8fad27381dba13e0ef1705bab7580..e11bc864a59a4cc9edfecf069b803c66f5e6e97b 100644 (file)
@@ -3,11 +3,11 @@
 
 {% macro nav_head(selected="") %}
 <p>
-{{ _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) }}
 </p>
 <hr />
 {% endmacro %}
index 58c2b5b0cf81672e28794e61c6bdbceb0c91a0e7..7625714ace16b8e080bf28bffd99cd1102612b53 100644 (file)
@@ -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%; }
 <tr><td id="status" colspan=2>
 {% if running %}{% if pause %}PAUSED{% else %}PLAYING{% endif %}{% else %}STOPPED{% endif %}:<br />
 {{ current_title }}<br />
-<form action="/playlist" method="POST">
+<form action="/{{page_names.playlist}}" method="POST">
 <input type="submit" name="pause" autofocus value="{% if paused %}resume{% else %}pause{% endif %}">
 <input type="submit" name="prev" value="prev">
 <input type="submit" name="next" value="next">
index 457f80e2d34d9ef1452835fe10071babab96cc63..8dae26dcac85c7d4f918ff0bc00cfdf148ae175d 100644 (file)
@@ -2,9 +2,9 @@
 
 
 {% block body %}
-{{ macros.nav_head("queries") }}
+{{ macros.nav_head("yt_queries") }}
 <p>quota: {{quota_count}}/100000</p>
-<form action="/queries" method="POST" />
+<form action="/{{page_names.yt_queries}}" method="POST" />
 <input name="query" />
 </form>
 <table>
@@ -15,7 +15,7 @@
 {% for query in queries %}
 <tr>
 <td>{{query.retrieved_at[:19]}}</td>
-<td><a href="/query/{{query.id_}}">{{query.text}}</a></td>
+<td><a href="/{{page_names.yt_query}}/{{query.id_}}">{{query.text}}</a></td>
 </tr>
 {% endfor %}
 </table>
index 131d14e438c33ab111b9794d9100db71e0d94d7d..1998f7f146da7d9f7c0bc9510a906f99bcd915a8 100644 (file)
@@ -8,14 +8,14 @@
 {% for video in videos %}
 <tr>
 <td>
-<a href="/yt_video/{{video.id_}}"><img src="/thumbnails/{{video.id_}}.jpg" /></a>
+<a href="/{{page_names.yt_result}}/{{video.id_}}"><img src="/{page_names.thumbnails}/{{video.id_}}.jpg" /></a>
 </td>
 <td>
 {{video.definition}}<br />
 {{video.duration}}
 </td>
 <td>
-<b><a href="/yt_video/{{video.id_}}">{{video.title}}</a></b> · {{video.description}}
+<b><a href="/{{page_names.yt_result}}/{{video.id_}}">{{video.title}}</a></b> · {{video.description}}
 </td>
 </tr>
 {% endfor %}
index 032a9b9e447f611ad54c278b3c1f466d7e9ba7d1..7de9b067c6d3c3e43f598b1e41eb56cfa9067b27 100644 (file)
@@ -5,10 +5,10 @@
 {{ macros.nav_head() }}
 <table>
 <tr><th>path:</th><td>{{file.rel_path}}</td></tr>
-<tr><th>YouTube ID:</th><td><a href="/yt_video/{{file.yt_id}}">{{file.yt_id}}</a></tr>
-<tr><th>present:</th><td>{% if file.present %}<a href="/dl/{{file.yt_id}}">yes</a>{% else %}no{% endif %}</td></tr>
+<tr><th>YouTube ID:</th><td><a href="/{{page_names.yt_result}}/{{file.yt_id}}">{{file.yt_id}}</a></tr>
+<tr><th>present:</th><td>{% if file.present %}<a href="/{{page_names.download}}/{{file.yt_id}}">yes</a>{% else %}no{% endif %}</td></tr>
 </table>
-<form action="/video/{{file.yt_id}}" method="POST" />
+<form action="/{{page_names.file}}/{{file.yt_id}}" method="POST" />
 {% for flag_name in flag_names %}
 {{ flag_name }}: <input type="checkbox" name="{{flag_name}}" {% if file.is_flag_set(flag_name) %}checked {% endif %} /><br />
 {% endfor %}
index 5cbf47f05fb8fdf879ed39984c2b5153b8e2480a..c49cf18e134c20a245818057bf2c5d830623c981 100644 (file)
@@ -2,11 +2,11 @@
 
 
 {% block body %}
-{{ macros.nav_head("videos") }}
+{{ macros.nav_head("files") }}
 <p>downloaded videos:</p>
 <ul>
 {% for video_id, path in videos %}
-<li><a href="/video/{{video_id}}">{{ path }}</a>
+<li><a href="/{{page_names.file}}/{{video_id}}">{{ path }}</a>
 {% endfor %}
 </ul>
 {% endblock %}
index 1ecb2581591472159f50ee4d33143094738f636f..32477417ec51579bb227197e556d93333801ca51 100644 (file)
@@ -5,17 +5,17 @@
 {{ macros.nav_head() }}
 <table>
 <tr><th>title:</th><td>{{video_data.title}}</td></tr>
-<tr><th>thumbnail:</th><td><img src="/thumbnails/{{video_data.id_}}.jpg" /></td></tr>
+<tr><th>thumbnail:</th><td><img src="/{{page_names.thumbnails}}/{{video_data.id_}}.jpg" /></td></tr>
 <tr><th>description:</th><td>{{video_data.description}}</td></tr>
 <tr><th>duration:</th><td>{{video_data.duration}}</td></tr>
 <tr><th>definition:</th><td>{{video_data.definition}}</td></tr>
 <tr><th>YouTube ID:</th><td>{{video_data.id_}} (<a href="{{youtube_prefix}}{{video_data.id_}}">watch</a>)</td></tr>
-<tr><th>download:</th><td>{% if is_temp %}working on it{% else %}<a href="/dl/{{video_data.id_}}">{{ file_path if file_path else "please do" }}</a>{% endif %}</td></tr>
+<tr><th>download:</th><td>{% if is_temp %}working on it{% else %}<a href="/{{page_names.download}}/{{video_data.id_}}">{{ file_path if file_path else "please do" }}</a>{% endif %}</td></tr>
 <tr>
 <th>linked queries:</th>
 <td>
 <ul>
-{% for query in queries %}<li><a href="/query/{{query.id_}}">{{query.text}}</a>{% endfor %}
+{% for query in queries %}<li><a href="/{{page_names.yt_query}}/{{query.id_}}">{{query.text}}</a>{% endfor %}
 </ul>
 </td>
 </tr>
index f65351c110fa34ae8ecd957a75424ed748b8efbe..48f475bdf103512b5a6846a45e5d2deffa2ef1de 100644 (file)
@@ -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: