Skip to content

Commit

Permalink
[QA-348] 크롤링 함수 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
stae1102 committed Sep 15, 2024
1 parent 8d190ec commit 547bd53
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/contents/contents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { LoadReminderCountOutput } from './dtos/load-personal-remider-count.dto'
import { UserRepository } from '../users/repository/user.repository';
import { ContentRepository } from './repository/content.repository';
import { CategoryRepository } from '../categories/category.repository';
import { getLinkInfo } from './util/content.util';
import { getOgData } from './util/content.util';
import { GetLinkInfoResponseDto } from './dtos/get-link.response.dto';
import { checkContentDuplicateAndAddCategorySaveLog } from '../categories/utils/category.util';
import { Transactional } from '../common/aop/transactional';
Expand Down Expand Up @@ -69,7 +69,7 @@ export class ContentsService {
siteName,
description,
coverImg,
} = await getLinkInfo(link);
} = await getOgData(link);
title = title ? title : linkTitle;

let category: Category | undefined = undefined;
Expand Down Expand Up @@ -132,7 +132,7 @@ export class ContentsService {

await Promise.all(
contentLinks.map(async (link) => {
const { title, description, coverImg, siteName } = await getLinkInfo(
const { title, description, coverImg, siteName } = await getOgData(
link,
);

Expand Down Expand Up @@ -391,7 +391,7 @@ export class ContentsService {
}

async getLinkInfo(link: string) {
const data = await getLinkInfo(link);
const data = await getOgData(link);

return new GetLinkInfoResponseDto(data);
}
Expand Down
14 changes: 7 additions & 7 deletions src/contents/dtos/get-link.response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { LinkInfo } from '../types/link-info.interface';

export class GetLinkInfoResponseDto {
@ApiProperty({
@ApiPropertyOptional({
description: '아티클 제목',
example: '[Terraform] 테라폼 훑어보기 — 턴태의 밑바닥부터 시작하는 de-vlog',
})
private readonly title: string;
private readonly title?: string;

@ApiProperty({
@ApiPropertyOptional({
description: '아티클 본문 일부',
example:
'인프라 구조마저 코드로 조작하고 있는 현재, 가장 많이 쓰이는 도구는 테라폼과 앤서블이 있습니다.',
})
private readonly description: string;
private readonly description?: string;

@ApiProperty({
@ApiPropertyOptional({
description: '썸네일/커버 이미지',
example:
'https://img1.daumcdn.net/thumb/R800x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fz52vz%2FbtsCAOBzgTR%2FhFgGDKkr6iKWfU6eKeKUVk%2Fimg.png',
})
private readonly coverImg: string;
private readonly coverImg?: string;

@ApiPropertyOptional({
description: '아티클 사이트 주소',
Expand Down
6 changes: 3 additions & 3 deletions src/contents/types/link-info.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface LinkInfo {
title: string;
description: string;
coverImg: string;
title?: string;
description?: string;
coverImg?: string;
siteName?: string;
}
24 changes: 24 additions & 0 deletions src/contents/util/content.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ import { BadRequestException } from '@nestjs/common';
import * as cheerio from 'cheerio';
import axios from 'axios';

import ogs from 'open-graph-scraper';

export async function getOgData(link: string) {
try {
const { result } = await ogs({
url: link,
});

return {
title: result.ogTitle ?? '',
description: result.ogDescription ?? '',
coverImg: result.ogImage ? result.ogImage[0].url : '',
siteName: result.ogSiteName ?? '',
};
} catch {
return {
title: '',
description: '',
coverImg: '',
siteName: '',
};
}
}

export const getLinkInfo = async (link: string) => {
let title: string | undefined = '';
let coverImg: string | undefined = '';
Expand Down

0 comments on commit 547bd53

Please sign in to comment.