Skip to content

Commit

Permalink
Merge pull request #360 from Quickchive/chore/QA-356/improve-og-crawling
Browse files Browse the repository at this point in the history
[QA-356] youtube data api를 통해 유튜브 영상을 가져오도록 개선
  • Loading branch information
stae1102 authored Nov 16, 2024
2 parents 5609133 + 445d915 commit cc608e9
Showing 1 changed file with 27 additions and 39 deletions.
66 changes: 27 additions & 39 deletions src/contents/util/content.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,50 +82,38 @@ class OGCrawler {

private async fetchYouTubeData(videoId: string): Promise<any> {
try {
// YouTube Data API v3를 사용하는 것이 더 안정적
const response = await axios({
method: 'get',
url: `https://www.youtube.com/watch?v=${videoId}`,
headers: {
'User-Agent': this.userAgent,
Accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
Cookie: this.cookies,
const { data } = await axios.get(
'https://www.googleapis.com/youtube/v3/videos',
{
params: {
part: 'snippet',
id: videoId,
key: process.env.YOUTUBE_DATA_API_KEY!,
},
},
});
);

const item = data?.items[0];

const $ = cheerio.load(response.data);

// YouTube 특정 메타데이터 추출
const title =
$('meta[property="og:title"]').attr('content') ||
$('title').text().replace('- YouTube', '').trim();
const description =
$('meta[property="og:description"]').attr('content') ||
$('meta[name="description"]').attr('content');
const coverImg =
$('meta[property="og:image"]').attr('content') ||
`https://i.ytimg.com/vi/${videoId}/maxresdefault.jpg`;
const siteName =
$('meta[property="og:site_name"]').attr('content') || 'YouTube';

return {
title,
description,
coverImg,
siteName,
};
if (item && item.snippet) {
return {
title: item.snippet.title,
description: item.snippet.description,
coverImg: item.snippet.thumbnails?.default?.url,
siteName: 'YouTube',
};
}
} catch (error) {
console.error('Failed to fetch YouTube data:', error);
// 폴백: 기본 썸네일 URL 사용
return {
title: '',
description: '',
coverImg: `https://i.ytimg.com/vi/${videoId}/maxresdefault.jpg`,
siteName: 'YouTube',
};
}

// 폴백: 기본 썸네일 URL 사용
return {
title: '',
description: '',
coverImg: `https://i.ytimg.com/vi/${videoId}/maxresdefault.jpg`,
siteName: 'YouTube',
};
}

private extractVideoId(url: string): string | null {
Expand Down

0 comments on commit cc608e9

Please sign in to comment.