-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15c9325
commit 3261755
Showing
8 changed files
with
104 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from typing import Any, Dict, Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Document(BaseModel): | ||
title: Optional[str] = None | ||
metadata: Optional[Dict[str, Any]] = None | ||
content: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import xml.etree.ElementTree as ET | ||
from urllib.parse import unquote_plus | ||
|
||
import requests | ||
import yt_dlp | ||
|
||
from .types import Document | ||
|
||
|
||
def fetch_youtube(url: str) -> Document: | ||
""" | ||
Fetch a YouTube video transcript | ||
:param url: | ||
:return: | ||
""" | ||
ydl_opts = { | ||
"writesubtitles": True, | ||
"writeautomaticsub": True, | ||
"subtitleslangs": ["en"], | ||
"subtitlesformat": "srv1", | ||
} | ||
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | ||
info = ydl.extract_info(url, download=False) | ||
if not info: | ||
raise ValueError("Could not extract video information") | ||
|
||
r = requests.get(info["requested_subtitles"]["en"]["url"]) | ||
|
||
# Parse XML | ||
root = ET.fromstring(r.text) | ||
# Extract and decode text from each <text> element | ||
texts = [unquote_plus(text.text or "") for text in root.findall("text")] | ||
|
||
# Join all texts with space | ||
return Document( | ||
title=info.get("title", ""), | ||
content=" ".join(texts), | ||
metadata={ | ||
"url": url, | ||
"description": info.get("description", ""), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters