Skip to content

Commit

Permalink
Merge pull request #10 from MattBlackOnly/testing
Browse files Browse the repository at this point in the history
Add an option to trim metadata length via environmental variable
  • Loading branch information
MattBlackOnly authored Oct 23, 2024
2 parents b7fa1c0 + 354afea commit d89986d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
17 changes: 17 additions & 0 deletions tubetube/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import yt_dlp


def parse_video_id(url):
Expand All @@ -13,3 +14,19 @@ def parse_video_id(url):
if match:
return match.group(1)
return None


class TrimDescriptionPP(yt_dlp.postprocessor.PostProcessor):
def run(self, info):
description = info.get("description", "")

if description:
trimmed_description = description[:250]
self.to_screen(f"Original description length: {len(description)}")
self.to_screen(f"Trimmed description to 250 chars: {trimmed_description}")

info["description"] = trimmed_description
else:
self.to_screen("No description found to trim.")

return [], info
9 changes: 7 additions & 2 deletions tubetube/yt_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import random
import yt_dlp
from settings import DownloadCancelledException
from helpers import parse_video_id
import helpers


class DownloadManager:
Expand All @@ -26,6 +26,9 @@ def __init__(self):
self.verbose_ytdlp = bool(os.getenv("VERBOSE_LOGS", "False").lower() == "true")
logging.info(f"Verbose logging for yt-dlp set to: {self.verbose_ytdlp}")

self.trim_metadata = bool(os.getenv("TRIM_METADATA", "False").lower() == "true")
logging.info(f"Trim Metadata set to: {self.trim_metadata}")

self.thread_count = int(os.getenv("THREAD_COUNT", "4"))
logging.info(f"Thread Count: {self.thread_count}")

Expand Down Expand Up @@ -69,7 +72,7 @@ def add_to_queue(self, item_info):
url = re.sub(r"&list=.*", "", url)

with self.lock:
parsed_identifier = parse_video_id(url)
parsed_identifier = helpers.parse_video_id(url)
if any(item["url"] == url or item["video_identifier"] == parsed_identifier for item in self.all_items.values()):
logging.info(f"URL {url} is already in the queue or being downloaded.")
self.socketio.emit("toast", {"title": "Duplicate URL", "body": f"The video '{url}' is already in the queue or being processed."})
Expand Down Expand Up @@ -208,6 +211,8 @@ def _download_item(self, download_id):
try:
logging.info(f'Starting {threading.current_thread().name} Download: {item.get("title")}')
ydl = yt_dlp.YoutubeDL(ydl_opts)
if self.trim_metadata:
ydl.add_post_processor(helpers.TrimDescriptionPP(), when="before_dl")
result = ydl.download([item["url"]])
item["progress"] = "Done" if result == 0 else "Incomplete"
item["status"] = "Complete"
Expand Down

0 comments on commit d89986d

Please sign in to comment.