o2o-infinith-backend/app/integrations/youtube.py

174 lines
7.2 KiB
Python

from http import HTTPMethod
from common.utils import http_request
YT = "https://www.googleapis.com/youtube/v3"
class YouTubeClient:
def __init__(self, api_key: str):
self.api_key = api_key
async def _resolve_channel_id(self, url: str) -> str:
print("input yt url : ", url)
# /channel/UCxxxxx → 채널 ID 직접 반환
if "/channel/" in url:
return url.split("/channel/")[1].split("/")[0]
if "/@" in url:
val = "@" + url.split("/@")[1].split("/")[0]
elif "/c/" in url:
val = url.split("/c/")[1].split("/")[0]
elif "/user/" in url:
val = url.split("/user/")[1].split("/")[0]
elif url.startswith("UC") and len(url) == 24:
return url
else:
val = url
print("val : ", val)
for param in ("forHandle", "forUsername"):
resp = await http_request(
HTTPMethod.GET,
url=f"{YT}/channels",
params={"part": "id", param: val, "key": self.api_key},
label="yt-resolve",
)
if resp and resp.is_success and (items := resp.json().get("items", [])):
print("items : ", items)
return items[0]["id"]
print("YT NOT FOUND")
return ""
async def fetch_channel(self, url: str) -> dict | None:
channel_id = await self._resolve_channel_id(url)
if not channel_id:
return None
resp = await http_request(
HTTPMethod.GET,
url=f"{YT}/channels",
params={"part": "snippet,statistics,contentDetails", "id": channel_id, "key": self.api_key},
label="yt-channel",
)
if not resp or not resp.is_success:
return None
items = resp.json().get("items", [])
if not items:
return None
channel = items[0]
async def _video_details(video_ids: list[str]) -> list[dict]:
"""video_ids 순서를 보존한 채 snippet/statistics/contentDetails 채워서 반환."""
if not video_ids:
return []
resp = await http_request(
HTTPMethod.GET,
url=f"{YT}/videos",
params={"part": "snippet,statistics,contentDetails", "id": ",".join(video_ids), "key": self.api_key},
label="yt-videos",
)
if not resp or not resp.is_success:
return []
video_map = {v["id"]: v for v in resp.json().get("items", [])}
return [video_map[vid] for vid in video_ids if vid in video_map]
# 인기 영상 top 10 (조회수순) — search index 기반, 정확한 정렬 보장.
resp = await http_request(
HTTPMethod.GET,
url=f"{YT}/search",
params={"part": "snippet", "channelId": channel_id, "order": "viewCount", "type": "video", "maxResults": 10, "key": self.api_key},
label="yt-search-top",
)
top_ids: list[str] = []
if resp and resp.is_success:
top_ids = [i["id"]["videoId"] for i in resp.json().get("items", []) if i.get("id", {}).get("videoId")]
videos = await _video_details(top_ids)
# 최근 영상 10개 — search index는 누락이 흔해 채널의 실제 uploads 재생목록에서 직접 읽고
# publishedAt 기준으로 코드에서 직접 정렬 (재생목록 순서 자체는 보장되지 않으므로).
recents: list[dict] = []
uploads_id = (channel.get("contentDetails") or {}).get("relatedPlaylists", {}).get("uploads")
if uploads_id:
resp = await http_request(
HTTPMethod.GET,
url=f"{YT}/playlistItems",
params={"part": "snippet", "playlistId": uploads_id, "maxResults": 10, "key": self.api_key},
label="yt-uploads",
)
if resp and resp.is_success:
entries = resp.json().get("items", [])
entries.sort(key=lambda i: i.get("snippet", {}).get("publishedAt") or "", reverse=True)
recent_ids = [
vid for e in entries
if (vid := e.get("snippet", {}).get("resourceId", {}).get("videoId"))
]
recents = await _video_details(recent_ids)
playlists: list[dict] = []
resp = await http_request(
HTTPMethod.GET,
url=f"{YT}/playlists",
params={"part": "snippet", "channelId": channel_id, "maxResults": 50, "key": self.api_key},
label="yt-playlists",
)
if resp and resp.is_success:
playlists = resp.json().get("items", [])
return {"channelId": channel_id, "channel": channel, "videos": videos, "recents": recents, "playlists": playlists}
@staticmethod
def _video_item(v: dict) -> dict:
return {
"title": v.get("snippet", {}).get("title"),
"views": int(v.get("statistics", {}).get("viewCount", 0)),
"likes": int(v.get("statistics", {}).get("likeCount", 0)),
"comments": int(v.get("statistics", {}).get("commentCount", 0)),
"date": v.get("snippet", {}).get("publishedAt"),
"duration": v.get("contentDetails", {}).get("duration"),
"url": f"https://www.youtube.com/watch?v={v['id']}",
}
async def get_channel(self, url: str) -> dict | None:
raw = await self.fetch_channel(url)
if not raw:
return None
ch = raw["channel"]
stats = ch.get("statistics", {})
snippet = ch.get("snippet", {})
thumbs = snippet.get("thumbnails", {})
print(snippet)
return {
"channelId": raw["channelId"],
"channelName": snippet.get("title"),
"profileImage": (thumbs.get("high") or thumbs.get("medium") or thumbs.get("default") or {}).get("url"),
"handle": snippet.get("customUrl"),
"description": snippet.get("description", ""),
"publishedAt": snippet.get("publishedAt"),
"subscribers": int(stats.get("subscriberCount", 0)),
"totalViews": int(stats.get("viewCount", 0)),
"totalVideos": int(stats.get("videoCount", 0)),
"videos": [self._video_item(v) for v in raw["videos"]],
"recents": [self._video_item(v) for v in raw["recents"]],
"playlists": [
p.get("snippet", {}).get("title")
for p in raw["playlists"]
if p.get("snippet", {}).get("title")
],
}
async def search_channels(self, query: str, max_results: int = 3) -> list[str]:
resp = await http_request(
HTTPMethod.GET,
url=f"{YT}/search",
params={"part": "snippet", "type": "channel", "q": query, "maxResults": max_results, "key": self.api_key},
label="yt-search-channels",
)
if not resp or not resp.is_success:
return []
return [
i.get("snippet", {}).get("channelId") or i.get("id", {}).get("channelId")
for i in resp.json().get("items", [])
if i.get("snippet", {}).get("channelId") or i.get("id", {}).get("channelId")
]