fix 유튜브 채널 수집 실패 버그
This commit is contained in:
1 parent
18d01357c0
commit
1f45b3e53d
4 files changed
+40
-124
No files matched your search
+12
-12
@@ -38,8 +38,8 @@ class ApifyClient:
|
||||
items = await self._run_actor("apify~instagram-profile-scraper", {"usernames": [username], "resultsLimit": 12})
|
||||
return items[0] if items else None
|
||||
|
||||
async def get_instagram_profile(self, handle: str) -> dict | None:
|
||||
profile = await self.fetch_instagram_profile(handle)
|
||||
async def get_instagram_profile(self, url: str) -> dict | None:
|
||||
profile = await self.fetch_instagram_profile(url)
|
||||
if not profile or profile.get("error"):
|
||||
return None
|
||||
return {
|
||||
@@ -62,15 +62,15 @@ class ApifyClient:
|
||||
],
|
||||
}
|
||||
|
||||
async def fetch_instagram_posts(self, handle: str, limit: int = 20) -> list[dict]:
|
||||
clean = handle.lstrip("@")
|
||||
async def fetch_instagram_posts(self, url: str, limit: int = 20) -> list[dict]:
|
||||
username = urlparse(url).path.strip("/").split("/")[0] if "://" in url else url.lstrip("@")
|
||||
return await self._run_actor("apify~instagram-post-scraper", {
|
||||
"directUrls": [f"https://www.instagram.com/{clean}/"],
|
||||
"directUrls": [f"https://www.instagram.com/{username}/"],
|
||||
"resultsLimit": limit,
|
||||
})
|
||||
|
||||
async def get_instagram_posts(self, handle: str, limit: int = 20) -> dict:
|
||||
items = await self.fetch_instagram_posts(handle, limit)
|
||||
async def get_instagram_posts(self, url: str, limit: int = 20) -> dict:
|
||||
items = await self.fetch_instagram_posts(url, limit)
|
||||
posts = [
|
||||
{
|
||||
"id": p["id"],
|
||||
@@ -92,15 +92,15 @@ class ApifyClient:
|
||||
"avgComments": round(sum(p["commentsCount"] for p in posts) / n),
|
||||
}
|
||||
|
||||
async def fetch_instagram_reels(self, handle: str, limit: int = 15) -> list[dict]:
|
||||
clean = handle.lstrip("@")
|
||||
async def fetch_instagram_reels(self, url: str, limit: int = 15) -> list[dict]:
|
||||
username = urlparse(url).path.strip("/").split("/")[0] if "://" in url else url.lstrip("@")
|
||||
return await self._run_actor("apify~instagram-reel-scraper", {
|
||||
"directUrls": [f"https://www.instagram.com/{clean}/reels/"],
|
||||
"directUrls": [f"https://www.instagram.com/{username}/reels/"],
|
||||
"resultsLimit": limit,
|
||||
})
|
||||
|
||||
async def get_instagram_reels(self, handle: str, limit: int = 15) -> dict:
|
||||
items = await self.fetch_instagram_reels(handle, limit)
|
||||
async def get_instagram_reels(self, url: str, limit: int = 15) -> dict:
|
||||
items = await self.fetch_instagram_reels(url, limit)
|
||||
reels = [
|
||||
{
|
||||
"id": r["id"],
|
||||
|
||||
+28
-14
@@ -1,5 +1,4 @@
|
||||
from http import HTTPMethod
|
||||
from urllib.parse import urlparse
|
||||
from common.utils import http_request
|
||||
|
||||
YT = "https://www.googleapis.com/youtube/v3"
|
||||
@@ -9,25 +8,40 @@ class YouTubeClient:
|
||||
def __init__(self, api_key: str):
|
||||
self.api_key = api_key
|
||||
|
||||
async def _resolve_channel_id(self, handle: str) -> str:
|
||||
h = urlparse(handle).path.strip("/").lstrip("@") if "://" in handle else handle.lstrip("@")
|
||||
if h.startswith("UC") and len(h) == 24:
|
||||
return h
|
||||
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: h, "key": self.api_key},
|
||||
params={"part": "id", param: val, "key": self.api_key},
|
||||
label="yt-resolve",
|
||||
)
|
||||
if resp and resp.is_success:
|
||||
items = resp.json().get("items", [])
|
||||
if items:
|
||||
return items[0]["id"]
|
||||
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, handle_or_id: str) -> dict | None:
|
||||
channel_id = await self._resolve_channel_id(handle_or_id)
|
||||
async def fetch_channel(self, url: str) -> dict | None:
|
||||
channel_id = await self._resolve_channel_id(url)
|
||||
if not channel_id:
|
||||
return None
|
||||
|
||||
@@ -67,8 +81,8 @@ class YouTubeClient:
|
||||
|
||||
return {"channelId": channel_id, "channel": channel, "videos": videos}
|
||||
|
||||
async def get_channel(self, handle_or_id: str) -> dict | None:
|
||||
raw = await self.fetch_channel(handle_or_id)
|
||||
async def get_channel(self, url: str) -> dict | None:
|
||||
raw = await self.fetch_channel(url)
|
||||
if not raw:
|
||||
return None
|
||||
ch = raw["channel"]
|
||||
|
||||
Reference in new issue
Block a user