Update youtube.py

This commit is contained in:
Classic298 authored and GitHub committed 2025-05-05 19:57:06 +02:00
1 parent db0a0b395f
commit 7680ac2517
1 file changed
+25 -6
@@ -101,17 +101,36 @@ class YoutubeLoader:
log.exception("Loading YouTube transcript failed") log.exception("Loading YouTube transcript failed")
return [] return []
# Try each language in order of priority
last_exception = None
for lang in self.language:
try: try:
transcript = transcript_list.find_transcript(self.language) log.debug(f"Attempting to find transcript for language '{lang}'")
except NoTranscriptFound: transcript = transcript_list.find_transcript([lang])
transcript = transcript_list.find_transcript(["en"]) log.info(f"Found transcript for language '{lang}'")
transcript_pieces: List[Dict[str, Any]] = transcript.fetch() transcript_pieces: List[Dict[str, Any]] = transcript.fetch()
transcript_text = " ".join(
transcript = " ".join(
map( map(
lambda transcript_piece: transcript_piece.text.strip(" "), lambda transcript_piece: transcript_piece.text.strip(" "),
transcript_pieces, transcript_pieces,
) )
) )
return [Document(page_content=transcript, metadata=self._metadata)] return [Document(page_content=transcript_text, metadata=self._metadata)]
except NoTranscriptFound as e:
log.debug(f"No transcript found for language '{lang}'")
last_exception = e
continue
except Exception as e:
# If we hit any other type of exception, log it and re-raise
log.exception(f"Error finding transcript for language '{lang}'")
raise e
# If all specified languages fail, raise the last exception
# This maintains compatibility with the error handling in the rest of the application
if last_exception:
log.warning(f"No transcript found for any of the specified languages: {', '.join(self.language)}")
raise last_exception
# This should never happen (we'd have raised an exception above)
return []