From 1e59d7be067fd105edd689da17733f36e7067232 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 31 Mar 2021 14:39:10 -0700 Subject: [PATCH] Add get_things_by_sql. --- ycdl/ycdldb.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ycdl/ycdldb.py b/ycdl/ycdldb.py index 6e285f8..1aa4a96 100644 --- a/ycdl/ycdldb.py +++ b/ycdl/ycdldb.py @@ -93,6 +93,15 @@ class YCDLDBCacheManagerMixin: thing_cache[thing_id] = 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: def __init__(self): super().__init__() @@ -150,6 +159,9 @@ class YCDLDBChannelMixin: channels.sort(key=lambda c: c.name.lower()) 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): ''' Youtube provides RSS feeds for every channel. These feeds do not @@ -389,6 +401,9 @@ class YCDLDBVideoMixin: for row in rows: 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): video_generator = self.youtube.get_playlist_videos(playlist_id) results = [self.insert_video(video, commit=False) for video in video_generator]