-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle restricted/unavailable videos [and list them in "Tasks" view, when downloading (stale!) playlists] #179
Changes from all commits
c6f02ca
9fac313
111a36f
a6be96b
723d250
a5b6f90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ def __init__(self, task_message, media_url, original_url, current_user_name): | |
self.columns = None | ||
self.shelf_title = None | ||
self.shelf_id = None | ||
self.unavailable = [] | ||
|
||
def _format_media_url(self, media_url): | ||
return media_url.split("&")[0] if "&" in media_url else media_url | ||
|
@@ -66,7 +67,10 @@ def _fetch_requested_urls(self, conn): | |
if "error" in self.columns | ||
else "SELECT path, duration FROM media WHERE path LIKE 'http%'") | ||
rows = conn.execute(query).fetchall() | ||
return {row[0]: {"duration": row[1]} for row in rows} | ||
requested_urls = {row[0]: {"duration": row[1]} if row[1] is not None and row[1] > 0 else self.unavailable.append(row[0]) for row in rows} | ||
for url in self.unavailable: | ||
requested_urls.pop(url) | ||
return requested_urls | ||
except sqlite3.Error as db_error: | ||
log.error("An error occurred while trying to connect to the database: %s", db_error) | ||
self.message = f"{self.media_url_link} failed: An error occurred ({db_error}) while trying to connect to the database." | ||
|
@@ -85,15 +89,15 @@ def _send_shelf_title(self): | |
def _update_metadata(self, requested_urls): | ||
failed_urls = [] | ||
subprocess_args_list = [[os.getenv("LB_WRAPPER", "lb-wrapper"), "tubeadd", requested_url] for requested_url in requested_urls.keys()] | ||
|
||
for index, subprocess_args in enumerate(subprocess_args_list): | ||
try: | ||
p = self._execute_subprocess(subprocess_args) | ||
if p is not None: | ||
self.progress = (index + 1) / len(subprocess_args_list) | ||
else: | ||
failed_urls.append(subprocess_args[2]) | ||
p.wait() | ||
p.wait() | ||
except Exception as e: | ||
log.error("An error occurred during updating the metadata of %s: %s", subprocess_args[2], e) | ||
self.message = f"{subprocess_args[2]} failed: {e}" | ||
|
@@ -124,7 +128,11 @@ def _add_download_tasks_to_worker(self, requested_urls): | |
WorkerThread.add(self.current_user_name, task_download) | ||
num_requested_urls = len(requested_urls) | ||
total_duration = sum(url_data["duration"] for url_data in requested_urls.values()) | ||
self.message = self.media_url_link + f"<br><br>Number of Videos: {index + 1}/{num_requested_urls}<br>Total Duration: {datetime.utcfromtimestamp(total_duration).strftime('%H:%M:%S')}" | ||
self.message = self.media_url_link + f"<br><br>" \ | ||
f"Number of Videos: {index + 1}/{num_requested_urls}<br>" \ | ||
f"Total Duration: {datetime.utcfromtimestamp(total_duration).strftime('%H:%M:%S')}" | ||
if self.unavailable: | ||
self.message += f"<br><br>Unavailable Video(s):<br>{'<br>'.join(f'<a href="{url}" target="_blank">{url}</a>' for url in self.unavailable)}" | ||
|
||
def run(self, worker_thread): | ||
self.worker_thread = worker_thread | ||
|
@@ -145,9 +153,11 @@ def run(self, worker_thread): | |
self._remove_shorts_from_db(conn) | ||
requested_urls = self._fetch_requested_urls(conn) | ||
if not requested_urls: | ||
return | ||
self.message = f"{self.media_url_link} failed: Video not available." | ||
conn.execute("DELETE FROM media WHERE path = ?", (self.media_url,)) | ||
self.stat = STAT_FAIL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This gets overridden with STAT_FINISH_SUCCESS at line 178. Is it ok? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, previously this line was skipped when requested_urls is empty, now it's executed every time: self._add_download_tasks_to_worker(requested_urls) Are there any unwanted side-effects? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. STAT_FINISH_SUCCESS and STAT_FAIL both end the task. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This one send every requested_url to the download task. It could be done in a single task, but was discarded in favor of individual task for every download. See #99 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The list of requested URLs is retrieved from xklb-metadata.db. It's basically a search for every row which have no text in its error column and path is a URL. This list can be empty if these conditions are not met. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
My original question was: is it ok to call _add_download_tasks_to_worker() even when requested_urls is an empty list? I looked at the body of _add_download_tasks_to_worker(), and it does nothing when called with an empty list. So... OK. |
||
|
||
if self.is_playlist: | ||
elif self.is_playlist: | ||
self._send_shelf_title() | ||
self._update_metadata(requested_urls) | ||
self._calculate_views_per_day(requested_urls, conn) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally untested code that seems simpler and more readable than the list comprehension with conditionals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@deldesir I'm in favor of simple + more readable if you can consider?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! In 2 steps...