This commit is contained in:
1 parent
cf7c96eaff
commit
051452d3b1
21 files changed
+865
-311
No files matched your search
Vendored
+19
@@ -1,4 +1,6 @@
|
||||
import asyncio
|
||||
import re
|
||||
import xml.etree.ElementTree as ET
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
@@ -7,6 +9,9 @@ import httpx
|
||||
API_URL = "https://www.googleapis.com/youtube/v3"
|
||||
# 키 없이 영상 제목만 받을 때
|
||||
OEMBED_URL = "https://www.youtube.com/oembed"
|
||||
# 키 없이 채널 쇼츠 목록을 받을 때: 채널 ID의 UC 를 UUSH 로 바꾼 재생목록이 그 채널의 쇼츠만 모은 목록이다
|
||||
FEED_URL = "https://www.youtube.com/feeds/videos.xml"
|
||||
_NS = {"a": "http://www.w3.org/2005/Atom", "yt": "http://www.youtube.com/xml/schemas/2015"}
|
||||
SHORT_MAX_SEC = 180 # 쇼츠는 3분 이하
|
||||
|
||||
_DURATION = re.compile(r"^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$")
|
||||
@@ -53,3 +58,17 @@ async def video_title(client: httpx.AsyncClient, video_id: str) -> Optional[str]
|
||||
return None
|
||||
res.raise_for_status()
|
||||
return res.json().get("title")
|
||||
|
||||
|
||||
async def feed_shorts(client: httpx.AsyncClient, channel_id: str, limit: int) -> list[dict]:
|
||||
"""키 없이 채널의 공개 쇼츠 [{id, title, published}], 최신순. 퍼가기를 막은 영상은 뺀다."""
|
||||
res = await client.get(FEED_URL, params={"playlist_id": "UUSH" + channel_id.removeprefix("UC")})
|
||||
res.raise_for_status()
|
||||
root = ET.fromstring(res.text)
|
||||
entries = [
|
||||
{"id": e.findtext("yt:videoId", namespaces=_NS), "title": e.findtext("a:title", namespaces=_NS), "published": e.findtext("a:published", namespaces=_NS)}
|
||||
for e in root.findall("a:entry", _NS)
|
||||
]
|
||||
entries = sorted((e for e in entries if e["id"] and e["title"]), key=lambda e: e["published"] or "", reverse=True)[:limit]
|
||||
embeddable = await asyncio.gather(*(video_title(client, e["id"]) for e in entries))
|
||||
return [e for e, ok in zip(entries, embeddable) if ok]
|
||||
Reference in new issue
Block a user