diff --git a/utilities/database_upgrader.py b/utilities/database_upgrader.py index ff7c047..e6ee384 100644 --- a/utilities/database_upgrader.py +++ b/utilities/database_upgrader.py @@ -73,6 +73,38 @@ def upgrade_3_to_4(ycdldb): DROP TABLE videos_old; ''') +def upgrade_4_to_5(ycdldb): + ''' + In this version, the `uploads_playlist` column was added to the channels table. + ''' + ycdldb.sql.executescript(''' + ALTER TABLE channels RENAME TO channels_old; + CREATE TABLE channels( + id TEXT, + name TEXT, + uploads_playlist TEXT, + directory TEXT COLLATE NOCASE, + automark TEXT + ); + INSERT INTO channels SELECT + id, + name, + NULL, + directory, + automark + FROM channels_old; + DROP TABLE channels_old; + ''') + rows = ycdldb.sql.execute('SELECT id FROM channels').fetchall() + channels = [row[0] for row in rows] + for channel in channels: + uploads_playlist = ycdldb.youtube.get_user_uploads_playlist_id(channel) + print(f'{channel} has playlist {uploads_playlist}.') + ycdldb.sql.execute( + 'UPDATE channels SET uploads_playlist = ? WHERE id = ?', + [uploads_playlist, channel] + ) + def upgrade_all(data_directory): ''' diff --git a/ycdl/constants.py b/ycdl/constants.py index 1ddf936..6f566db 100644 --- a/ycdl/constants.py +++ b/ycdl/constants.py @@ -1,6 +1,6 @@ from voussoirkit import sqlhelpers -DATABASE_VERSION = 4 +DATABASE_VERSION = 5 DB_VERSION_PRAGMA = f''' PRAGMA user_version = {DATABASE_VERSION}; ''' @@ -18,6 +18,7 @@ BEGIN; CREATE TABLE IF NOT EXISTS channels( id TEXT, name TEXT, + uploads_playlist TEXT, directory TEXT COLLATE NOCASE, automark TEXT ); diff --git a/ycdl/objects.py b/ycdl/objects.py index dbeef25..aa9c90d 100644 --- a/ycdl/objects.py +++ b/ycdl/objects.py @@ -20,6 +20,7 @@ class Channel(Base): self.id = db_row['id'] self.name = db_row['name'] + self.uploads_playlist = db_row['uploads_playlist'] self.directory = db_row['directory'] self.automark = db_row['automark'] or "pending" @@ -37,7 +38,7 @@ class Channel(Base): def refresh(self, force=False, commit=True): seen_ids = set() - video_generator = self.ycdldb.youtube.get_user_videos(uid=self.id) + video_generator = self.ycdldb.youtube.get_playlist_videos(self.uploads_playlist) self.ycdldb.log.debug('Refreshing channel: %s', self.id) for video in video_generator: seen_ids.add(video.id) diff --git a/ycdl/ycdldb.py b/ycdl/ycdldb.py index 428389b..abf5737 100644 --- a/ycdl/ycdldb.py +++ b/ycdl/ycdldb.py @@ -126,6 +126,7 @@ class YCDLDBChannelMixin: data = { 'id': channel_id, 'name': name, + 'uploads_playlist': self.youtube.get_user_uploads_playlist_id(channel_id), 'directory': download_directory, 'automark': "pending", }