From: Christian Heller Date: Wed, 27 Nov 2024 04:53:51 +0000 (+0100) Subject: Some variable renamings for better clarity. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%22https:/validator.w3.org/%7Broute%7D?a=commitdiff_plain;h=4baea47f1176166fe46f1ffc1ce1e85ee950d760;p=ytplom Some variable renamings for better clarity. --- diff --git a/src/sync.py b/src/sync.py index fdeced8..98bdfcb 100755 --- a/src/sync.py +++ b/src/sync.py @@ -12,7 +12,7 @@ from paramiko import SSHClient # type: ignore from scp import SCPClient # type: ignore from ytplom.misc import ( PAGE_NAMES, PATH_DB, PATH_DOWNLOADS, PATH_TEMP, - Config, DatabaseConnection, PathStr, QuotaLog, VideoFile, + Config, DbConnection, PathStr, QuotaLog, VideoFile, YoutubeQuery, YoutubeVideo) @@ -21,7 +21,7 @@ ATTR_NAME_LAST_UPDATE = 'last_update' def back_and_forth(sync_func: Callable, - dbs: tuple[DatabaseConnection, DatabaseConnection], + dbs: tuple[DbConnection, DbConnection], shared: YoutubeVideo | tuple[Any, str] ) -> None: """Run sync_func on arg pairs + shared, and again with pairs switched.""" @@ -31,7 +31,7 @@ def back_and_forth(sync_func: Callable, def sync_objects(host_names: tuple[str, str], - dbs: tuple[DatabaseConnection, DatabaseConnection], + dbs: tuple[DbConnection, DbConnection], shared: tuple[Any, str] ) -> None: """Equalize both DB's object collections; prefer newer states to older.""" @@ -58,7 +58,7 @@ def sync_objects(host_names: tuple[str, str], def sync_relations(host_names: tuple[str, str], - dbs: tuple[DatabaseConnection, DatabaseConnection], + dbs: tuple[DbConnection, DbConnection], yt_result: YoutubeVideo ) -> None: """To dbs[1] add YT yt_video->yt_q_colls[0] mapping not in yt_q_colls[1]""" @@ -78,8 +78,7 @@ def main(): ssh.connect(config.remote) scp = SCPClient(ssh.get_transport()) scp.get(PATH_DB, PATH_DB_REMOTE) - local_db = DatabaseConnection(PATH_DB) - remote_db = DatabaseConnection(PATH_DB_REMOTE) + local_db, remote_db = DbConnection(PATH_DB), DbConnection(PATH_DB_REMOTE) for cls in (QuotaLog, YoutubeQuery, YoutubeVideo, VideoFile): back_and_forth(sync_objects, (local_db, remote_db), (cls, 'rel_path' if cls is VideoFile else 'id_')) diff --git a/src/templates/file_data.tmpl b/src/templates/file_data.tmpl new file mode 100644 index 0000000..7ea70f9 --- /dev/null +++ b/src/templates/file_data.tmpl @@ -0,0 +1,17 @@ +{% extends '_base.tmpl' %} + + +{% block body %} +{{ macros.nav_head(page_names) }} + + + + +
path:{{file.rel_path}}
YouTube ID:{{file.yt_id}}
present:{% if file.present %}yes{% else %}no{% endif %}
+
+{% for flag_name in flag_names %} +{{ flag_name }}:
+{% endfor %} + +
+{% endblock %} diff --git a/src/templates/files.tmpl b/src/templates/files.tmpl new file mode 100644 index 0000000..69543c8 --- /dev/null +++ b/src/templates/files.tmpl @@ -0,0 +1,12 @@ +{% extends '_base.tmpl' %} + + +{% block body %} +{{ macros.nav_head(page_names, "files") }} +

downloaded videos:

+ +{% endblock %} diff --git a/src/templates/queries.tmpl b/src/templates/queries.tmpl deleted file mode 100644 index 9ef271d..0000000 --- a/src/templates/queries.tmpl +++ /dev/null @@ -1,22 +0,0 @@ -{% extends '_base.tmpl' %} - - -{% block body %} -{{ macros.nav_head(page_names, "yt_queries") }} -

quota: {{quota_count}}/100000

-
- -
- - - - - -{% for query in queries %} - - - - -{% endfor %} -
retrieved atquery
{{query.retrieved_at[:19]}}{{query.text}}
-{% endblock %} diff --git a/src/templates/results.tmpl b/src/templates/results.tmpl deleted file mode 100644 index 872d461..0000000 --- a/src/templates/results.tmpl +++ /dev/null @@ -1,23 +0,0 @@ -{% extends '_base.tmpl' %} - - -{% block body %} -{{ macros.nav_head(page_names) }} -

query: {{query}}

- -{% for video in videos %} - - - - - -{% endfor %} -
- - -{{video.definition}}
-{{video.duration}} -
-{{video.title}} · {{video.description}} -
-{% endblock %} diff --git a/src/templates/video.tmpl b/src/templates/video.tmpl deleted file mode 100644 index 7ea70f9..0000000 --- a/src/templates/video.tmpl +++ /dev/null @@ -1,17 +0,0 @@ -{% extends '_base.tmpl' %} - - -{% block body %} -{{ macros.nav_head(page_names) }} - - - - -
path:{{file.rel_path}}
YouTube ID:{{file.yt_id}}
present:{% if file.present %}yes{% else %}no{% endif %}
-
-{% for flag_name in flag_names %} -{{ flag_name }}:
-{% endfor %} - -
-{% endblock %} diff --git a/src/templates/videos.tmpl b/src/templates/videos.tmpl deleted file mode 100644 index 69543c8..0000000 --- a/src/templates/videos.tmpl +++ /dev/null @@ -1,12 +0,0 @@ -{% extends '_base.tmpl' %} - - -{% block body %} -{{ macros.nav_head(page_names, "files") }} -

downloaded videos:

- -{% endblock %} diff --git a/src/templates/yt_queries.tmpl b/src/templates/yt_queries.tmpl new file mode 100644 index 0000000..9ef271d --- /dev/null +++ b/src/templates/yt_queries.tmpl @@ -0,0 +1,22 @@ +{% extends '_base.tmpl' %} + + +{% block body %} +{{ macros.nav_head(page_names, "yt_queries") }} +

quota: {{quota_count}}/100000

+
+ +
+ + + + + +{% for query in queries %} + + + + +{% endfor %} +
retrieved atquery
{{query.retrieved_at[:19]}}{{query.text}}
+{% endblock %} diff --git a/src/templates/yt_result.tmpl b/src/templates/yt_result.tmpl new file mode 100644 index 0000000..73ffbab --- /dev/null +++ b/src/templates/yt_result.tmpl @@ -0,0 +1,23 @@ +{% extends '_base.tmpl' %} + + +{% block body %} +{{ macros.nav_head(page_names) }} + + + + + + + + + + + + +
title:{{video_data.title}}
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 %}
linked queries: + +
+{% endblock %} diff --git a/src/templates/yt_results.tmpl b/src/templates/yt_results.tmpl new file mode 100644 index 0000000..872d461 --- /dev/null +++ b/src/templates/yt_results.tmpl @@ -0,0 +1,23 @@ +{% extends '_base.tmpl' %} + + +{% block body %} +{{ macros.nav_head(page_names) }} +

query: {{query}}

+ +{% for video in videos %} + + + + + +{% endfor %} +
+ + +{{video.definition}}
+{{video.duration}} +
+{{video.title}} · {{video.description}} +
+{% endblock %} diff --git a/src/templates/yt_video.tmpl b/src/templates/yt_video.tmpl deleted file mode 100644 index 73ffbab..0000000 --- a/src/templates/yt_video.tmpl +++ /dev/null @@ -1,23 +0,0 @@ -{% extends '_base.tmpl' %} - - -{% block body %} -{{ macros.nav_head(page_names) }} - - - - - - - - - - - - -
title:{{video_data.title}}
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 %}
linked queries: - -
-{% endblock %} diff --git a/src/ytplom/misc.py b/src/ytplom/misc.py index f654657..cd86352 100644 --- a/src/ytplom/misc.py +++ b/src/ytplom/misc.py @@ -65,11 +65,11 @@ PATH_CONFFILE = PathStr(path_join(PATH_HOME, '.config/ytplom/config.json')) # template paths PATH_TEMPLATES = PathStr(path_join(PATH_APP_DATA, 'templates')) -NAME_TEMPLATE_QUERIES = PathStr('queries.tmpl') -NAME_TEMPLATE_RESULTS = PathStr('results.tmpl') -NAME_TEMPLATE_VIDEOS = PathStr('videos.tmpl') -NAME_TEMPLATE_VIDEO = PathStr('video.tmpl') -NAME_TEMPLATE_YT_VIDEO = PathStr('yt_video.tmpl') +NAME_TEMPLATE_QUERIES = PathStr('yt_queries.tmpl') +NAME_TEMPLATE_RESULTS = PathStr('yt_results.tmpl') +NAME_TEMPLATE_FILES = PathStr('files.tmpl') +NAME_TEMPLATE_FILE_DATA = PathStr('file_data.tmpl') +NAME_TEMPLATE_YT_VIDEO = PathStr('yt_result.tmpl') NAME_TEMPLATE_PLAYLIST = PathStr('playlist.tmpl') # page names @@ -81,7 +81,7 @@ PAGE_NAMES: PageNames = { 'missing': PathStr('missing.json'), 'playlist': PathStr('playlist'), 'thumbnails': PathStr('thumbnails'), - 'yt_result': PathStr('yt_video'), + 'yt_result': PathStr('yt_result'), 'yt_query': PathStr('yt_query'), 'yt_queries': PathStr('yt_queries') } @@ -112,7 +112,7 @@ ENVIRON_PREFIX = 'YTPLOM_' TIMESTAMP_FMT = '%Y-%m-%d %H:%M:%S.%f' LEGAL_EXTENSIONS = {'webm', 'mp4', 'mkv'} NAME_INSTALLER = PathStr('install.sh') -VIDEO_FLAGS: dict[FlagName, FlagsInt] = { +FILE_FLAGS: dict[FlagName, FlagsInt] = { FlagName('delete'): FlagsInt(1 << 62) } @@ -165,7 +165,7 @@ class Config: if k.isupper() and k.startswith(ENVIRON_PREFIX)}) -class DatabaseConnection: +class DbConnection: """Wrapped sqlite3.Connection.""" def __init__(self, path: PathStr = PATH_DB) -> None: @@ -221,7 +221,7 @@ class DbData: return cls(**kwargs) @classmethod - def get_one(cls, conn: DatabaseConnection, id_: str) -> Self: + def get_one(cls, conn: DbConnection, id_: str) -> Self: """Return single entry of id_ from DB.""" sql = SqlText(f'SELECT * FROM {cls._table_name} WHERE id = ?') row = conn.exec(sql, (id_,)).fetchone() @@ -231,13 +231,13 @@ class DbData: return cls._from_table_row(row) @classmethod - def get_all(cls, conn: DatabaseConnection) -> list[Self]: + def get_all(cls, conn: DbConnection) -> list[Self]: """Return all entries from DB.""" sql = SqlText(f'SELECT * FROM {cls._table_name}') rows = conn.exec(sql).fetchall() return [cls._from_table_row(row) for row in rows] - def save(self, conn: DatabaseConnection) -> Cursor: + def save(self, conn: DbConnection) -> Cursor: """Save entry to DB.""" vals = [getattr(self, col_name) for col_name in self._cols] q_marks = '(' + ','.join(['?'] * len(vals)) + ')' @@ -261,7 +261,7 @@ class YoutubeQuery(DbData): @classmethod def get_all_for_video(cls, - conn: DatabaseConnection, + conn: DbConnection, video_id: YoutubeId ) -> list[Self]: """Return YoutubeQueries containing YoutubeVideo's ID in results.""" @@ -318,7 +318,7 @@ class YoutubeVideo(DbData): @classmethod def get_all_for_query(cls, - conn: DatabaseConnection, + conn: DbConnection, query_id: QueryId ) -> list[Self]: """Return all videos for query of query_id.""" @@ -328,10 +328,7 @@ class YoutubeVideo(DbData): return [cls.get_one(conn, video_id_tup[0]) for video_id_tup in video_ids] - def save_to_query(self, - conn: DatabaseConnection, - query_id: QueryId - ) -> None: + def save_to_query(self, conn: DbConnection, query_id: QueryId) -> None: """Save inclusion of self in results to query of query_id.""" conn.exec(SqlText('REPLACE INTO yt_query_results VALUES (?, ?)'), (query_id, self.id_)) @@ -361,7 +358,7 @@ class VideoFile(DbData): self.last_update = DatetimeStr(datetime.now().strftime(TIMESTAMP_FMT)) @classmethod - def get_by_yt_id(cls, conn: DatabaseConnection, yt_id: YoutubeId) -> Self: + def get_by_yt_id(cls, conn: DbConnection, yt_id: YoutubeId) -> Self: """Return VideoFile of .yt_id.""" sql = SqlText(f'SELECT * FROM {cls._table_name} WHERE yt_id = ?') row = conn.exec(sql, (yt_id,)).fetchone() @@ -396,7 +393,7 @@ class VideoFile(DbData): def is_flag_set(self, flag_name: FlagName) -> bool: """Return if flag of flag_name is set in flags field.""" - return bool(self._flags & VIDEO_FLAGS[flag_name]) + return bool(self._flags & FILE_FLAGS[flag_name]) def ensure_absence_if_deleted(self) -> None: """If 'delete' flag set, ensure no actual file in filesystem.""" @@ -422,7 +419,7 @@ class QuotaLog(DbData): self.cost = cost @classmethod - def update(cls, conn: DatabaseConnection, cost: QuotaCost) -> None: + def update(cls, conn: DbConnection, cost: QuotaCost) -> None: """Adds cost mapped to current datetime.""" cls._remove_old(conn) new = cls(None, @@ -431,14 +428,14 @@ class QuotaLog(DbData): new.save(conn) @classmethod - def current(cls, conn: DatabaseConnection) -> QuotaCost: + def current(cls, conn: DbConnection) -> QuotaCost: """Returns quota cost total for last 24 hours, purges old data.""" cls._remove_old(conn) quota_costs = cls.get_all(conn) return QuotaCost(sum(c.cost for c in quota_costs)) @classmethod - def _remove_old(cls, conn: DatabaseConnection) -> None: + def _remove_old(cls, conn: DbConnection) -> None: cutoff = datetime.now() - timedelta(days=1) sql = SqlText(f'DELETE FROM {cls._table_name} WHERE timestamp < ?') conn.exec(SqlText(sql), (cutoff.strftime(TIMESTAMP_FMT),)) @@ -582,7 +579,7 @@ class DownloadsManager: self._sync_db() def _sync_db(self): - conn = DatabaseConnection() + conn = DbConnection() files_via_db = VideoFile.get_all(conn) old_cwd = getcwd() chdir(PATH_DOWNLOADS) @@ -696,14 +693,14 @@ class TaskHandler(BaseHTTPRequestHandler): body_length = int(self.headers['content-length']) postvars = parse_qs(self.rfile.read(body_length).decode()) if PAGE_NAMES['playlist'] == page_name: - self._post_player_command(list(postvars.keys())[0]) + self._receive_player_command(list(postvars.keys())[0]) elif PAGE_NAMES['file'] == page_name: - self._post_video_flag(YoutubeId(toks_url[2]), - [FlagName(k) for k in postvars]) + self._receive_video_flag(YoutubeId(toks_url[2]), + [FlagName(k) for k in postvars]) elif PAGE_NAMES['yt_queries'] == page_name: - self._post_query(QueryText(postvars['query'][0])) + self._receive_yt_query(QueryText(postvars['query'][0])) - def _post_player_command(self, command: str) -> None: + def _receive_player_command(self, command: str) -> None: if 'pause' == command: self.server.player.toggle_pause() elif 'prev' == command: @@ -717,15 +714,15 @@ class TaskHandler(BaseHTTPRequestHandler): sleep(0.5) # avoid redir happening before current_file update self._send_http(headers=[('Location', '/')], code=302) - def _post_video_flag(self, - yt_id: YoutubeId, - flag_names: list[FlagName] - ) -> None: - conn = DatabaseConnection() + def _receive_video_flag(self, + yt_id: YoutubeId, + flag_names: list[FlagName] + ) -> None: + conn = DbConnection() file = VideoFile.get_by_yt_id(conn, yt_id) flags = FlagsInt(0) for flag_name in flag_names: - flags = FlagsInt(file.flags | VIDEO_FLAGS[flag_name]) + flags = FlagsInt(file.flags | FILE_FLAGS[flag_name]) file.flags = flags file.save(conn) conn.commit_close() @@ -734,8 +731,8 @@ class TaskHandler(BaseHTTPRequestHandler): f'/{PAGE_NAMES["file"]}/{yt_id}')], code=302) - def _post_query(self, query_txt: QueryText) -> None: - conn = DatabaseConnection() + def _receive_yt_query(self, query_txt: QueryText) -> None: + conn = DbConnection() def collect_results(query_txt: QueryText) -> list[YoutubeVideo]: _ensure_expected_dirs([PATH_THUMBNAILS]) @@ -800,17 +797,17 @@ class TaskHandler(BaseHTTPRequestHandler): elif PAGE_NAMES['download'] == page_name: self._send_or_download_video(YoutubeId(toks_url[2])) elif PAGE_NAMES['files'] == page_name: - self._send_videos_index() + self._send_files_index() elif PAGE_NAMES['file'] == page_name: - self._send_video_data(YoutubeId(toks_url[2])) + self._send_file_data(YoutubeId(toks_url[2])) elif PAGE_NAMES['yt_result'] == page_name: - self._send_yt_video_data(YoutubeId(toks_url[2])) + self._send_yt_result(YoutubeId(toks_url[2])) elif PAGE_NAMES['missing'] == page_name: self._send_missing_json() elif PAGE_NAMES['yt_query'] == page_name: - self._send_query_page(QueryId(toks_url[2])) + self._send_yt_query_page(QueryId(toks_url[2])) elif PAGE_NAMES['yt_queries'] == page_name: - self._send_queries_index_and_search() + self._send_yt_queries_index_and_search() elif PAGE_NAMES['last_update'] == page_name: self._send_last_playlist_update() else: # e.g. for / @@ -855,8 +852,8 @@ class TaskHandler(BaseHTTPRequestHandler): f'/{PAGE_NAMES["yt_result"]}/{video_id}')], code=302) - def _send_query_page(self, query_id: QueryId) -> None: - conn = DatabaseConnection() + def _send_yt_query_page(self, query_id: QueryId) -> None: + conn = DbConnection() query = YoutubeQuery.get_one(conn, str(query_id)) results = YoutubeVideo.get_all_for_query(conn, query_id) conn.commit_close() @@ -864,8 +861,8 @@ class TaskHandler(BaseHTTPRequestHandler): NAME_TEMPLATE_RESULTS, {'query': query.text, 'videos': results}) - def _send_queries_index_and_search(self) -> None: - conn = DatabaseConnection() + def _send_yt_queries_index_and_search(self) -> None: + conn = DbConnection() quota_count = QuotaLog.current(conn) queries_data = YoutubeQuery.get_all(conn) conn.commit_close() @@ -874,8 +871,8 @@ class TaskHandler(BaseHTTPRequestHandler): NAME_TEMPLATE_QUERIES, {'queries': queries_data, 'quota_count': quota_count}) - def _send_yt_video_data(self, video_id: YoutubeId) -> None: - conn = DatabaseConnection() + def _send_yt_result(self, video_id: YoutubeId) -> None: + conn = DbConnection() linked_queries = YoutubeQuery.get_all_for_video(conn, video_id) try: video_data = YoutubeVideo.get_one(conn, video_id) @@ -891,19 +888,19 @@ class TaskHandler(BaseHTTPRequestHandler): 'youtube_prefix': YOUTUBE_URL_PREFIX, 'queries': linked_queries}) - def _send_video_data(self, yt_id: YoutubeId) -> None: - conn = DatabaseConnection() + def _send_file_data(self, yt_id: YoutubeId) -> None: + conn = DbConnection() file = VideoFile.get_by_yt_id(conn, yt_id) conn.commit_close() self._send_rendered_template( - NAME_TEMPLATE_VIDEO, - {'file': file, 'flag_names': list(VIDEO_FLAGS)}) + NAME_TEMPLATE_FILE_DATA, + {'file': file, 'flag_names': list(FILE_FLAGS)}) - def _send_videos_index(self) -> None: + def _send_files_index(self) -> None: videos = [(id_, PathStr(basename(path))) for id_, path in self.server.downloads.ids_to_paths.items()] videos.sort(key=lambda t: t[1]) - self._send_rendered_template(NAME_TEMPLATE_VIDEOS, {'videos': videos}) + self._send_rendered_template(NAME_TEMPLATE_FILES, {'videos': videos}) def _send_missing_json(self) -> None: self._send_http(