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.
This commit is contained in:
voussoir 2020-07-01 16:09:11 -07:00
parent 3c432c51f0
commit 4fe4ed9cb5
4 changed files with 37 additions and 2 deletions

View file

@ -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):
'''

View file

@ -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
);

View file

@ -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)

View file

@ -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",
}