Skip to content

Commit

Permalink
Merge pull request #357 from Quickchive/chore/QA-356/improve-og-crawling
Browse files Browse the repository at this point in the history
chore: add user agent header to axios
  • Loading branch information
stae1102 authored Nov 2, 2024
2 parents dc6d5f4 + 0fd5602 commit 18b0e0a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/contents/util/content.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@ export const getLinkInfo = async (link: string) => {

// ! TODO 크롤링에서 대략 초 단위 시간 소요 -> 개선 필요
await axios
.get(link)
.get(link, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; OGCrawler/1.0)',
},
})
.then((res) => {
if (res.status !== 200) {
throw new BadRequestException('잘못된 링크입니다.');
} else {
const data = res.data;
if (typeof data === 'string') {
const $ = cheerio.load(data);
title = $('title').text() !== '' ? $('title').text() : 'Untitled';
title =
$('title').text() !== ''
? $('title').text() || $('meta[name="title"]').attr('content')
: 'Untitled';
$('meta').each((i, el) => {
const meta = $(el);
if (meta.attr('property') === 'og:image') {
Expand Down

0 comments on commit 18b0e0a

Please sign in to comment.