Skip to content

Commit

Permalink
Merge pull request #179 from deldesir/deldesir-patch-40
Browse files Browse the repository at this point in the history
Handle restricted/unavailable videos
  • Loading branch information
holta authored Jun 11, 2024
2 parents 48deb54 + a5b6f90 commit cf0303a
Showing 1 changed file with 16 additions and 6 deletions.
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
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

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

0 comments on commit cf0303a

Please sign in to comment.