Update youtube.py
This commit is contained in:
1 parent
db0a0b395f
commit
7680ac2517
1 file changed
+25
-6
@@ -101,17 +101,36 @@ class YoutubeLoader:
|
||||
log.exception("Loading YouTube transcript failed")
|
||||
return []
|
||||
|
||||
# Try each language in order of priority
|
||||
last_exception = None
|
||||
for lang in self.language:
|
||||
try:
|
||||
transcript = transcript_list.find_transcript(self.language)
|
||||
except NoTranscriptFound:
|
||||
transcript = transcript_list.find_transcript(["en"])
|
||||
log.debug(f"Attempting to find transcript for language '{lang}'")
|
||||
transcript = transcript_list.find_transcript([lang])
|
||||
log.info(f"Found transcript for language '{lang}'")
|
||||
|
||||
transcript_pieces: List[Dict[str, Any]] = transcript.fetch()
|
||||
|
||||
transcript = " ".join(
|
||||
transcript_text = " ".join(
|
||||
map(
|
||||
lambda transcript_piece: transcript_piece.text.strip(" "),
|
||||
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 []
|
||||
Reference in new issue
Block a user