Rename database column download to state.

This commit is contained in:
voussoir 2020-09-04 15:55:48 -07:00
parent 0ae041f001
commit e5910c55f2
7 changed files with 46 additions and 32 deletions

View file

@ -17,10 +17,10 @@ def get_channels():
@site.route('/videos') @site.route('/videos')
@site.route('/watch') @site.route('/watch')
@site.route('/videos/<download_filter>') @site.route('/videos/<state>')
@site.route('/channel/<channel_id>') @site.route('/channel/<channel_id>')
@site.route('/channel/<channel_id>/<download_filter>') @site.route('/channel/<channel_id>/<state>')
def get_channel(channel_id=None, download_filter=None): def get_channel(channel_id=None, state=None):
if channel_id is not None: if channel_id is not None:
try: try:
common.ycdldb.add_channel(channel_id) common.ycdldb.add_channel(channel_id)
@ -42,7 +42,7 @@ def get_channel(channel_id=None, download_filter=None):
else: else:
videos = common.ycdldb.get_videos( videos = common.ycdldb.get_videos(
channel_id=channel_id, channel_id=channel_id,
download_filter=download_filter, state=state,
orderby=orderby, orderby=orderby,
) )
@ -72,7 +72,7 @@ def get_channel(channel_id=None, download_filter=None):
'channel.html', 'channel.html',
all_states=all_states, all_states=all_states,
channel=channel, channel=channel,
download_filter=download_filter, state=state,
query_string='?' + request.query_string.decode('utf-8'), query_string='?' + request.query_string.decode('utf-8'),
videos=videos, videos=videos,
) )

View file

@ -208,7 +208,7 @@ https://stackoverflow.com/a/35153397
<div id="video_card_{{video.id}}" <div id="video_card_{{video.id}}"
data-ytid="{{video.id}}" data-ytid="{{video.id}}"
onclick="return onclick_select(event);" onclick="return onclick_select(event);"
class="video_card video_card_{{video.download}}" class="video_card video_card_{{video.state}}"
> >
<img class="video_thumbnail" loading="lazy" src="http://i3.ytimg.com/vi/{{video.id}}/default.jpg" height="100px"> <img class="video_thumbnail" loading="lazy" src="http://i3.ytimg.com/vi/{{video.id}}/default.jpg" height="100px">
<div class="video_details"> <div class="video_details">
@ -222,7 +222,7 @@ https://stackoverflow.com/a/35153397
<div class="action_toolbox"> <div class="action_toolbox">
<button <button
{% if video.download == "pending" %} {% if video.state == "pending" %}
class="video_action_pending hidden" class="video_action_pending hidden"
{% else %} {% else %}
class="video_action_pending" class="video_action_pending"
@ -231,7 +231,7 @@ https://stackoverflow.com/a/35153397
>Revert to Pending</button> >Revert to Pending</button>
<button <button
{% if video.download == "pending" %} {% if video.state == "pending" %}
class="video_action_download" class="video_action_download"
{% else %} {% else %}
class="video_action_download hidden" class="video_action_download hidden"
@ -240,7 +240,7 @@ https://stackoverflow.com/a/35153397
>Download</button> >Download</button>
<button <button
{% if video.download == "pending" %} {% if video.state == "pending" %}
class="video_action_ignore" class="video_action_ignore"
{% else %} {% else %}
class="video_action_ignore hidden" class="video_action_ignore hidden"
@ -288,7 +288,7 @@ https://stackoverflow.com/a/35153397
var CHANNEL_ID = "{{channel.id}}"; var CHANNEL_ID = "{{channel.id}}";
{% endif %} {% endif %}
var DOWNLOAD_FILTER = "{{download_filter if download_filter else ""}}"; var STATE = "{{state if state else ""}}";
var video_card_first_selected = null; var video_card_first_selected = null;
var video_card_selections = []; var video_card_selections = [];
@ -337,11 +337,11 @@ function filter_video_cards(search_term)
video_cards = document.getElementById("video_cards"); video_cards = document.getElementById("video_cards");
video_cards.classList.add("hidden"); video_cards.classList.add("hidden");
search_term = search_term.toLocaleLowerCase(); search_term = search_term.toLocaleLowerCase();
let download_filter_class = "video_card_" + DOWNLOAD_FILTER; let state_class = "video_card_" + STATE;
Array.from(video_cards.getElementsByClassName("video_card")).forEach(function(video_card) Array.from(video_cards.getElementsByClassName("video_card")).forEach(function(video_card)
{ {
let title = video_card.getElementsByClassName("video_title")[0].innerText.toLocaleLowerCase(); let title = video_card.getElementsByClassName("video_title")[0].innerText.toLocaleLowerCase();
if (DOWNLOAD_FILTER && !video_card.classList.contains(download_filter_class)) if (STATE && !video_card.classList.contains(state_class))
{ {
video_cards.removeChild(video_card); video_cards.removeChild(video_card);
} }

View file

@ -144,6 +144,20 @@ def upgrade_5_to_6(ycdldb):
''') ''')
ycdldb.sql.execute('DROP TABLE channels_old') ycdldb.sql.execute('DROP TABLE channels_old')
def upgrade_6_to_7(ycdldb):
'''
In this version, the `download` column of the videos table was renamed to
`state`. The vocabulary throughout the rest of the program had already
evolved and the database column was behind the times.
'''
ycdldb.sql.execute('ALTER TABLE videos RENAME COLUMN download TO state')
ycdldb.sql.execute('DROP INDEX IF EXISTS index_video_author_download')
ycdldb.sql.execute('DROP INDEX IF EXISTS index_video_download')
ycdldb.sql.execute('DROP INDEX IF EXISTS index_video_download_published')
ycdldb.sql.execute('CREATE INDEX index_video_author_state on videos(author_id, state)')
ycdldb.sql.execute('CREATE INDEX index_video_state on videos(state)')
ycdldb.sql.execute('CREATE INDEX index_video_state_published on videos(state, published)')
def upgrade_all(data_directory): def upgrade_all(data_directory):
''' '''
Given the directory containing a ycdl database, apply all of the Given the directory containing a ycdl database, apply all of the

View file

@ -1,6 +1,6 @@
from voussoirkit import sqlhelpers from voussoirkit import sqlhelpers
DATABASE_VERSION = 6 DATABASE_VERSION = 7
DB_VERSION_PRAGMA = f''' DB_VERSION_PRAGMA = f'''
PRAGMA user_version = {DATABASE_VERSION}; PRAGMA user_version = {DATABASE_VERSION};
''' '''
@ -32,16 +32,16 @@ CREATE TABLE IF NOT EXISTS videos(
duration INT, duration INT,
views INT, views INT,
thumbnail TEXT, thumbnail TEXT,
download TEXT state TEXT
); );
CREATE INDEX IF NOT EXISTS index_channel_id on channels(id); CREATE INDEX IF NOT EXISTS index_channel_id on channels(id);
CREATE INDEX IF NOT EXISTS index_video_author on videos(author_id); CREATE INDEX IF NOT EXISTS index_video_author on videos(author_id);
CREATE INDEX IF NOT EXISTS index_video_author_download on videos(author_id, download); CREATE INDEX IF NOT EXISTS index_video_author_state on videos(author_id, state);
CREATE INDEX IF NOT EXISTS index_video_id on videos(id); CREATE INDEX IF NOT EXISTS index_video_id on videos(id);
CREATE INDEX IF NOT EXISTS index_video_published on videos(published); CREATE INDEX IF NOT EXISTS index_video_published on videos(published);
CREATE INDEX IF NOT EXISTS index_video_download on videos(download); CREATE INDEX IF NOT EXISTS index_video_state on videos(state);
CREATE INDEX IF NOT EXISTS index_video_download_published on videos(download, published); CREATE INDEX IF NOT EXISTS index_video_state_published on videos(state, published);
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
COMMIT; COMMIT;
''' '''

View file

@ -24,6 +24,6 @@ def video(v):
'duration': v.duration, 'duration': v.duration,
'views': v.views, 'views': v.views,
'thumbnail': v.thumbnail, 'thumbnail': v.thumbnail,
'download': v.download, 'state': v.state,
} }
return j return j

View file

@ -33,7 +33,7 @@ class Channel(Base):
self.ycdldb.commit() self.ycdldb.commit()
def has_pending(self): def has_pending(self):
query = 'SELECT 1 FROM videos WHERE author_id == ? AND download == "pending" LIMIT 1' query = 'SELECT 1 FROM videos WHERE author_id == ? AND state == "pending" LIMIT 1'
bindings = [self.id] bindings = [self.id]
return self.ycdldb.sql_select_one(query, bindings) is not None return self.ycdldb.sql_select_one(query, bindings) is not None
@ -109,7 +109,7 @@ class Video(Base):
self.duration = db_row['duration'] self.duration = db_row['duration']
self.views = db_row['views'] self.views = db_row['views']
self.thumbnail = db_row['thumbnail'] self.thumbnail = db_row['thumbnail']
self.download = db_row['download'] self.state = db_row['state']
@property @property
def author(self): def author(self):
@ -133,9 +133,9 @@ class Video(Base):
pairs = { pairs = {
'id': self.id, 'id': self.id,
'download': state, 'state': state,
} }
self.download = state self.state = state
self.ycdldb.sql_update(table='videos', pairs=pairs, where_key='id') self.ycdldb.sql_update(table='videos', pairs=pairs, where_key='id')
if commit: if commit:

View file

@ -313,7 +313,7 @@ class YCDLDBVideoMixin:
video_id = video video_id = video
video = self.get_video(video_id) video = self.get_video(video_id)
if video.download != 'pending' and not force: if video.state != 'pending' and not force:
print(f'{video.id} does not need to be downloaded.') print(f'{video.id} does not need to be downloaded.')
return return
@ -339,7 +339,7 @@ class YCDLDBVideoMixin:
def get_video(self, video_id): def get_video(self, video_id):
return self.get_thing_by_id('video', video_id) return self.get_thing_by_id('video', video_id)
def get_videos(self, channel_id=None, *, download_filter=None, orderby=None): def get_videos(self, channel_id=None, *, state=None, orderby=None):
wheres = [] wheres = []
orderbys = [] orderbys = []
@ -348,9 +348,9 @@ class YCDLDBVideoMixin:
wheres.append('author_id') wheres.append('author_id')
bindings.append(channel_id) bindings.append(channel_id)
if download_filter is not None: if state is not None:
wheres.append('download') wheres.append('state')
bindings.append(download_filter) bindings.append(state)
if wheres: if wheres:
wheres = [x + ' == ?' for x in wheres] wheres = [x + ' == ?' for x in wheres]
@ -428,7 +428,7 @@ class YCDLDBVideoMixin:
try: try:
existing = self.get_video(video.id) existing = self.get_video(video.id)
download_status = existing.download download_status = existing.state
except exceptions.NoSuchVideo: except exceptions.NoSuchVideo:
existing = None existing = None
download_status = 'pending' download_status = 'pending'
@ -442,7 +442,7 @@ class YCDLDBVideoMixin:
'duration': video.duration, 'duration': video.duration,
'views': video.views, 'views': video.views,
'thumbnail': video.thumbnail['url'], 'thumbnail': video.thumbnail['url'],
'download': download_status, 'state': download_status,
} }
if existing: if existing:
@ -527,13 +527,13 @@ class YCDLDB(
def get_all_states(self): def get_all_states(self):
''' '''
Get a list of all the different `download` states that are currently in Get a list of all the different states that are currently in use in
use in the database. the database.
''' '''
# Note: This function was added while I was considering the addition of # Note: This function was added while I was considering the addition of
# arbitrarily many states for user-defined purposes, but I kind of went # arbitrarily many states for user-defined purposes, but I kind of went
# back on that so I'm not sure if it will be useful. # back on that so I'm not sure if it will be useful.
query = 'SELECT DISTINCT download FROM videos' query = 'SELECT DISTINCT state FROM videos'
states = self.sql_select(query) states = self.sql_select(query)
states = [row[0] for row in states] states = [row[0] for row in states]
return sorted(states) return sorted(states)