Use generators instead of lists for loading / searching videos.

Previously, creating the list meant that all database rows would be
loaded and objected even if you had a limit in place on your page
query, which was a huge waste.
master
voussoir 2020-07-01 17:53:11 -07:00
parent de5992c906
commit 4911cee910
2 changed files with 11 additions and 3 deletions

View File

@ -1,5 +1,6 @@
import datetime import datetime
import flask; from flask import request import flask; from flask import request
import itertools
import traceback import traceback
import ycdl import ycdl
@ -50,10 +51,12 @@ def get_channel(channel_id=None, download_filter=None):
if limit is not None: if limit is not None:
try: try:
limit = int(limit) limit = int(limit)
videos = videos[:limit] videos = itertools.islice(videos, limit)
except ValueError: except ValueError:
pass pass
videos = list(videos)
for video in videos: for video in videos:
published = video.published published = video.published
published = datetime.datetime.utcfromtimestamp(published) published = datetime.datetime.utcfromtimestamp(published)

View File

@ -307,9 +307,14 @@ class YCDLDBVideoMixin:
orderbys = ' ORDER BY ' + orderbys orderbys = ' ORDER BY ' + orderbys
query = 'SELECT * FROM videos' + wheres + orderbys query = 'SELECT * FROM videos' + wheres + orderbys
print(query, bindings)
explain = self.sql_execute('EXPLAIN QUERY PLAN ' + query, bindings)
print('\n'.join(str(x) for x in explain.fetchall()))
rows = self.sql_select(query, bindings) rows = self.sql_select(query, bindings)
videos = [self.get_cached_instance('video', row) for row in rows] for row in rows:
return videos yield self.get_cached_instance('video', row)
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)