From 4fe4ed9cb573940b31d9d0b3f0bff650d0e343ad Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 1 Jul 2020 16:09:11 -0700 Subject: [PATCH] Database version 5. Store uploads_playlist id in database. I am trying to cut down the API cost of refreshing all channels. I hoped to find and endpoint that would give me videos from multiple playlists at once, but Youtube doesn't seem to support that. Next best option is to at least store the ID of the uploads playlist, saving 2 api points each. --- utilities/database_upgrader.py | 32 ++++++++++++++++++++++++++++++++ ycdl/constants.py | 3 ++- ycdl/objects.py | 3 ++- ycdl/ycdldb.py | 1 + 4 files changed, 37 insertions(+), 2 deletions(-) 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", }