Skip to content
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

Merged
merged 6 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions cps/tasks/metadata_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Comment on lines +70 to +73
Copy link

@codewiz codewiz Jun 11, 2024

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.

requested_urls = {}
for path, duration in rows:
    if duration is not None and duration > 0:
      requested_urls[path] = {"duration": duration};
   else:
      self.unavailable.append(path)
   return requested_urls

Copy link
Member

@holta holta Jun 11, 2024

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?!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."
Expand All @@ -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}"
Expand Down Expand Up @@ -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
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The 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?

Copy link

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. STAT_FINISH_SUCCESS and STAT_FAIL both end the task.

Copy link
Collaborator Author

@deldesir deldesir Jun 11, 2024

Choose a reason for hiding this comment

The 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?

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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.

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)
Expand Down