Add get_things_by_sql.

This commit is contained in:
voussoir 2021-03-31 14:39:10 -07:00
parent 9f5726e5d3
commit 1e59d7be06
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -93,6 +93,15 @@ class YCDLDBCacheManagerMixin:
thing_cache[thing_id] = thing thing_cache[thing_id] = thing
return thing return thing
def get_things_by_sql(self, thing_type, query, bindings=None):
'''
Use an arbitrary SQL query to select things from the database.
Your query select *, all the columns of the thing's table.
'''
thing_rows = self.sql_select(query, bindings)
for thing_row in thing_rows:
yield self.get_cached_instance(thing_type, thing_row)
class YCDLDBChannelMixin: class YCDLDBChannelMixin:
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -150,6 +159,9 @@ class YCDLDBChannelMixin:
channels.sort(key=lambda c: c.name.lower()) channels.sort(key=lambda c: c.name.lower())
return channels return channels
def get_channels_by_sql(self, query, bindings=None):
return self.get_things_by_sql('channel', query, bindings)
def _rss_assisted_refresh(self, skip_failures=False, commit=True): def _rss_assisted_refresh(self, skip_failures=False, commit=True):
''' '''
Youtube provides RSS feeds for every channel. These feeds do not Youtube provides RSS feeds for every channel. These feeds do not
@ -389,6 +401,9 @@ class YCDLDBVideoMixin:
for row in rows: for row in rows:
yield self.get_cached_instance('video', row) yield self.get_cached_instance('video', row)
def get_videos_by_sql(self, query, bindings=None):
return self.get_things_by_sql('video', query, bindings)
def insert_playlist(self, playlist_id, commit=True): def insert_playlist(self, playlist_id, commit=True):
video_generator = self.youtube.get_playlist_videos(playlist_id) video_generator = self.youtube.get_playlist_videos(playlist_id)
results = [self.insert_video(video, commit=False) for video in video_generator] results = [self.insert_video(video, commit=False) for video in video_generator]