From 4d7b396e0cc0d8264dea21f20779523fa371794f Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 11 Nov 2022 15:24:47 -0800 Subject: [PATCH] Add column last_refresh to channels. --- utilities/database_upgrader.py | 33 +++++++++++++++++++++++++++++++++ ycdl/constants.py | 5 +++-- ycdl/objects.py | 7 +++++++ ycdl/ycdldb.py | 6 ++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/utilities/database_upgrader.py b/utilities/database_upgrader.py index 0534d0c..342293d 100644 --- a/utilities/database_upgrader.py +++ b/utilities/database_upgrader.py @@ -314,6 +314,39 @@ def upgrade_9_to_10(ycdldb): m.go() +def upgrade_10_to_11(ycdldb): + ''' + In this version, the `last_refresh` column was added to the channels table. + ''' + m = Migrator(ycdldb) + + m.tables['channels']['create'] = ''' + CREATE TABLE IF NOT EXISTS channels( + id TEXT, + name TEXT, + uploads_playlist TEXT, + download_directory TEXT COLLATE NOCASE, + queuefile_extension TEXT COLLATE NOCASE, + automark TEXT, + autorefresh INT, + last_refresh INT + ); + ''' + m.tables['channels']['transfer'] = ''' + INSERT INTO channels SELECT + id, + name, + uploads_playlist, + download_directory, + queuefile_extension, + automark, + autorefresh, + NULL + FROM channels_old; + ''' + + m.go() + def upgrade_all(data_directory): ''' Given the directory containing a ycdl database, apply all of the diff --git a/ycdl/constants.py b/ycdl/constants.py index 986ecbc..518b57f 100644 --- a/ycdl/constants.py +++ b/ycdl/constants.py @@ -1,6 +1,6 @@ from voussoirkit import sqlhelpers -DATABASE_VERSION = 10 +DATABASE_VERSION = 11 DB_INIT = f''' CREATE TABLE IF NOT EXISTS channels( @@ -10,7 +10,8 @@ CREATE TABLE IF NOT EXISTS channels( download_directory TEXT COLLATE NOCASE, queuefile_extension TEXT COLLATE NOCASE, automark TEXT, - autorefresh INT + autorefresh INT, + last_refresh INT ); CREATE INDEX IF NOT EXISTS index_channel_id on channels(id); ---------------------------------------------------------------------------------------------------- diff --git a/ycdl/objects.py b/ycdl/objects.py index efccfd7..b439f43 100644 --- a/ycdl/objects.py +++ b/ycdl/objects.py @@ -4,6 +4,7 @@ import typing from voussoirkit import pathclass from voussoirkit import stringtools +from voussoirkit import timetools from voussoirkit import vlogging from voussoirkit import worms @@ -244,6 +245,12 @@ class Channel(ObjectBase): for video in self.ycdldb.youtube.get_videos(refresh_ids): self.ycdldb.ingest_video(video) + pairs = { + 'id': self.id, + 'last_refresh': timetools.now().timestamp(), + } + self.ycdldb.update(table='channels', pairs=pairs, where_key='id') + def reset_uploads_playlist_id(self): ''' Reset the stored uploads_playlist id with current data from the API. diff --git a/ycdl/ycdldb.py b/ycdl/ycdldb.py index bba33b7..a33a361 100644 --- a/ycdl/ycdldb.py +++ b/ycdl/ycdldb.py @@ -5,6 +5,7 @@ from voussoirkit import cacheclass from voussoirkit import configlayers from voussoirkit import lazychain from voussoirkit import pathclass +from voussoirkit import timetools from voussoirkit import vlogging from voussoirkit import worms @@ -130,6 +131,11 @@ class YCDLDBChannelMixin: try: most_recent_video = channel.get_most_recent_video_id() new_ids = ytrss.get_user_videos_since(channel.id, most_recent_video) + pairs = { + 'id': channel.id, + 'last_refresh': timetools.now().timestamp(), + } + self.update(table='channels', pairs=pairs, where_key='id') yield from new_ids except (exceptions.NoVideos, exceptions.RSSAssistFailed) as exc: log.debug(