2018-12-18 03:17:53 +00:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sqlite3
|
|
|
|
import sys
|
|
|
|
|
2020-07-01 22:47:15 +00:00
|
|
|
import bot
|
2018-12-18 03:17:53 +00:00
|
|
|
import ycdl
|
|
|
|
|
2020-07-01 22:47:15 +00:00
|
|
|
def upgrade_1_to_2(ycdldb):
|
2020-01-07 06:07:25 +00:00
|
|
|
'''
|
2020-07-01 22:47:27 +00:00
|
|
|
In this version, the `duration` column was added to the videos table.
|
2020-01-07 06:07:25 +00:00
|
|
|
'''
|
2020-08-09 01:56:11 +00:00
|
|
|
ycdldb.sql.execute('ALTER TABLE videos RENAME TO videos_old')
|
|
|
|
ycdldb.sql.execute('''
|
2020-01-07 06:07:25 +00:00
|
|
|
CREATE TABLE videos(
|
|
|
|
id TEXT,
|
|
|
|
published INT,
|
|
|
|
author_id TEXT,
|
|
|
|
title TEXT,
|
|
|
|
description TEXT,
|
|
|
|
duration INT,
|
|
|
|
thumbnail TEXT,
|
|
|
|
download TEXT
|
2020-08-09 01:56:11 +00:00
|
|
|
)
|
|
|
|
''')
|
|
|
|
ycdldb.sql.execute('''
|
2020-01-07 06:07:25 +00:00
|
|
|
INSERT INTO videos SELECT
|
|
|
|
id,
|
|
|
|
published,
|
|
|
|
author_id,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
NULL,
|
|
|
|
thumbnail,
|
|
|
|
download
|
2020-08-09 01:56:11 +00:00
|
|
|
FROM videos_old
|
2020-01-07 06:07:25 +00:00
|
|
|
''')
|
2020-08-09 01:56:11 +00:00
|
|
|
ycdldb.sql.execute('DROP TABLE videos_old')
|
2020-01-07 06:07:25 +00:00
|
|
|
|
2020-07-01 22:47:15 +00:00
|
|
|
def upgrade_2_to_3(ycdldb):
|
2020-01-07 06:07:25 +00:00
|
|
|
'''
|
2020-07-01 22:47:27 +00:00
|
|
|
In this version, the `automark` column was added to the channels table, where
|
2020-01-07 06:07:25 +00:00
|
|
|
you can set channels to automatically mark videos as ignored or downloaded.
|
|
|
|
'''
|
2020-07-01 22:47:15 +00:00
|
|
|
ycdldb.sql.execute('ALTER TABLE channels ADD COLUMN automark TEXT')
|
2020-01-07 06:07:25 +00:00
|
|
|
|
2020-07-01 22:47:15 +00:00
|
|
|
def upgrade_3_to_4(ycdldb):
|
2018-12-18 03:17:53 +00:00
|
|
|
'''
|
2020-07-01 22:47:27 +00:00
|
|
|
In this version, the `views` column was added to the videos table.
|
2018-12-18 03:17:53 +00:00
|
|
|
'''
|
2020-08-09 01:56:11 +00:00
|
|
|
ycdldb.sql.execute('ALTER TABLE videos RENAME TO videos_old')
|
|
|
|
ycdldb.sql.execute('''
|
2018-12-18 03:17:53 +00:00
|
|
|
CREATE TABLE videos(
|
|
|
|
id TEXT,
|
|
|
|
published INT,
|
|
|
|
author_id TEXT,
|
|
|
|
title TEXT,
|
|
|
|
description TEXT,
|
|
|
|
duration INT,
|
2020-07-01 22:25:29 +00:00
|
|
|
views INT,
|
2018-12-18 03:17:53 +00:00
|
|
|
thumbnail TEXT,
|
|
|
|
download TEXT
|
2020-08-09 01:56:11 +00:00
|
|
|
)
|
|
|
|
''')
|
|
|
|
ycdldb.sql.execute('''
|
2018-12-18 03:17:53 +00:00
|
|
|
INSERT INTO videos SELECT
|
|
|
|
id,
|
|
|
|
published,
|
|
|
|
author_id,
|
|
|
|
title,
|
|
|
|
description,
|
2020-07-01 22:25:29 +00:00
|
|
|
duration,
|
2018-12-18 03:17:53 +00:00
|
|
|
NULL,
|
|
|
|
thumbnail,
|
|
|
|
download
|
2020-08-09 01:56:11 +00:00
|
|
|
FROM videos_old
|
2018-12-18 03:17:53 +00:00
|
|
|
''')
|
2020-08-09 01:56:11 +00:00
|
|
|
ycdldb.sql.execute('DROP TABLE videos_old')
|
2018-12-18 03:17:53 +00:00
|
|
|
|
2020-07-01 23:09:11 +00:00
|
|
|
def upgrade_4_to_5(ycdldb):
|
|
|
|
'''
|
|
|
|
In this version, the `uploads_playlist` column was added to the channels table.
|
|
|
|
'''
|
2020-08-09 01:56:11 +00:00
|
|
|
ycdldb.sql.execute('ALTER TABLE channels RENAME TO channels_old')
|
|
|
|
ycdldb.sql.execute('''
|
2020-07-01 23:09:11 +00:00
|
|
|
CREATE TABLE channels(
|
|
|
|
id TEXT,
|
|
|
|
name TEXT,
|
|
|
|
uploads_playlist TEXT,
|
|
|
|
directory TEXT COLLATE NOCASE,
|
|
|
|
automark TEXT
|
2020-08-09 01:56:11 +00:00
|
|
|
)
|
|
|
|
''')
|
|
|
|
ycdldb.sql.execute('''
|
2020-07-01 23:09:11 +00:00
|
|
|
INSERT INTO channels SELECT
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
NULL,
|
|
|
|
directory,
|
|
|
|
automark
|
2020-08-09 01:56:11 +00:00
|
|
|
FROM channels_old
|
2020-07-01 23:09:11 +00:00
|
|
|
''')
|
2020-08-09 01:56:11 +00:00
|
|
|
ycdldb.sql.execute('DROP TABLE channels_old')
|
|
|
|
|
2020-07-01 23:09:11 +00:00
|
|
|
rows = ycdldb.sql.execute('SELECT id FROM channels').fetchall()
|
|
|
|
channels = [row[0] for row in rows]
|
|
|
|
for channel in channels:
|
2020-07-21 02:04:27 +00:00
|
|
|
try:
|
|
|
|
uploads_playlist = ycdldb.youtube.get_user_uploads_playlist_id(channel)
|
|
|
|
except ycdl.ytapi.ChannelNotFound:
|
|
|
|
continue
|
2020-07-01 23:09:11 +00:00
|
|
|
print(f'{channel} has playlist {uploads_playlist}.')
|
|
|
|
ycdldb.sql.execute(
|
|
|
|
'UPDATE channels SET uploads_playlist = ? WHERE id = ?',
|
|
|
|
[uploads_playlist, channel]
|
|
|
|
)
|
|
|
|
|
2020-07-01 22:47:15 +00:00
|
|
|
|
|
|
|
def upgrade_all(data_directory):
|
2018-12-18 03:17:53 +00:00
|
|
|
'''
|
2020-07-01 22:47:15 +00:00
|
|
|
Given the directory containing a ycdl database, apply all of the
|
2018-12-18 03:17:53 +00:00
|
|
|
needed upgrade_x_to_y functions in order.
|
|
|
|
'''
|
2020-07-01 22:47:15 +00:00
|
|
|
youtube = ycdl.ytapi.Youtube(bot.get_youtube_key())
|
|
|
|
ycdldb = ycdl.ycdldb.YCDLDB(youtube, data_directory, skip_version_check=True)
|
2018-12-18 03:17:53 +00:00
|
|
|
|
2020-07-01 22:47:15 +00:00
|
|
|
cur = ycdldb.sql.cursor()
|
2018-12-18 03:17:53 +00:00
|
|
|
|
|
|
|
cur.execute('PRAGMA user_version')
|
|
|
|
current_version = cur.fetchone()[0]
|
2020-07-01 22:47:15 +00:00
|
|
|
needed_version = ycdl.constants.DATABASE_VERSION
|
2018-12-18 03:17:53 +00:00
|
|
|
|
|
|
|
if current_version == needed_version:
|
|
|
|
print('Already up to date with version %d.' % needed_version)
|
|
|
|
return
|
|
|
|
|
|
|
|
for version_number in range(current_version + 1, needed_version + 1):
|
|
|
|
print('Upgrading from %d to %d.' % (current_version, version_number))
|
|
|
|
upgrade_function = 'upgrade_%d_to_%d' % (current_version, version_number)
|
|
|
|
upgrade_function = eval(upgrade_function)
|
2020-08-09 01:56:11 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
ycdldb.sql.execute('BEGIN')
|
|
|
|
upgrade_function(ycdldb)
|
|
|
|
except Exception as exc:
|
|
|
|
ycdldb.rollback()
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
ycdldb.sql.cursor().execute('PRAGMA user_version = %d' % version_number)
|
|
|
|
ycdldb.commit()
|
|
|
|
|
2018-12-18 03:17:53 +00:00
|
|
|
current_version = version_number
|
|
|
|
print('Upgrades finished.')
|
|
|
|
|
|
|
|
|
|
|
|
def upgrade_all_argparse(args):
|
2020-07-01 22:47:15 +00:00
|
|
|
return upgrade_all(data_directory=args.data_directory)
|
2018-12-18 03:17:53 +00:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
2020-07-01 22:47:15 +00:00
|
|
|
parser.add_argument('data_directory')
|
2018-12-18 03:17:53 +00:00
|
|
|
parser.set_defaults(func=upgrade_all_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
2020-02-09 01:20:27 +00:00
|
|
|
return args.func(args)
|
2018-12-18 03:17:53 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|