Skip to content

Commit

Permalink
refactor: convert contents util to function
Browse files Browse the repository at this point in the history
  • Loading branch information
stae1102 committed Jan 31, 2024
1 parent abb246a commit eecc484
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 106 deletions.
11 changes: 1 addition & 10 deletions src/contents/contents.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ContentsController } from './contents.controller';
import { ContentsService } from './contents.service';
import { Category } from '../categories/category.entity';
import { Content } from './entities/content.entity';
import { CategoryUtil } from './util/category.util';
import { ContentRepository } from './repository/content.repository';
import { CategoryRepository } from '../categories/category.repository';
import { UsersModule } from '../users/users.module';
import { ContentUtil } from './util/content.util';
import { OpenaiModule } from '../openai/openai.module';

@Module({
imports: [TypeOrmModule.forFeature([Content]), UsersModule, OpenaiModule],
controllers: [ContentsController],
providers: [
ContentsService,
ContentRepository,
ContentUtil,
CategoryRepository,
CategoryUtil,
],
providers: [ContentsService, ContentRepository, CategoryRepository],
exports: [ContentsService],
})
export class ContentsModule {}
21 changes: 10 additions & 11 deletions src/contents/contents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import { Content } from './entities/content.entity';
import { LoadReminderCountOutput } from './dtos/load-personal-remider-count.dto';
import { UserRepository } from '../users/repository/user.repository';
import { ContentRepository } from './repository/content.repository';
import { CategoryUtil } from './util/category.util';
import { CategoryRepository } from '../categories/category.repository';
import { ContentUtil } from './util/content.util';
import { getLinkInfo } from './util/content.util';
import { GetLinkInfoResponseDto } from './dtos/get-link.response.dto';
import { checkContentDuplicateAndAddCategorySaveLog } from './util/category.util';

@Injectable()
export class ContentsService {
Expand All @@ -39,8 +39,6 @@ export class ContentsService {
private readonly contentRepository: ContentRepository,
private readonly summaryService: SummaryService,
private readonly categoryRepository: CategoryRepository,
private readonly categoryUtil: CategoryUtil,
private readonly contentUtil: ContentUtil,
private readonly dataSource: DataSource,
) {}

Expand Down Expand Up @@ -73,7 +71,7 @@ export class ContentsService {
siteName,
description,
coverImg,
} = await this.contentUtil.getLinkInfo(link);
} = await getLinkInfo(link);
title = title ? title : linkTitle;

let category: Category | null = null;
Expand All @@ -85,7 +83,7 @@ export class ContentsService {
queryRunner.manager,
);

await this.categoryUtil.checkContentDuplicateAndAddCategorySaveLog(
await checkContentDuplicateAndAddCategorySaveLog(
link,
category,
userInDb,
Expand Down Expand Up @@ -141,11 +139,12 @@ export class ContentsService {
);
}
for (const link of contentLinks) {
const { title, description, coverImg, siteName } =
await this.contentUtil.getLinkInfo(link);
const { title, description, coverImg, siteName } = await getLinkInfo(
link,
);

if (category) {
await this.categoryUtil.checkContentDuplicateAndAddCategorySaveLog(
await checkContentDuplicateAndAddCategorySaveLog(
link,
category,
userInDb,
Expand Down Expand Up @@ -220,7 +219,7 @@ export class ContentsService {
queryRunnerManager,
);

await this.categoryUtil.checkContentDuplicateAndAddCategorySaveLog(
await checkContentDuplicateAndAddCategorySaveLog(
link,
category,
userInDb,
Expand Down Expand Up @@ -399,7 +398,7 @@ export class ContentsService {
}

async getLinkInfo(link: string) {
const data = await this.contentUtil.getLinkInfo(link);
const data = await getLinkInfo(link);

return new GetLinkInfoResponseDto(data);
}
Expand Down
167 changes: 82 additions & 85 deletions src/contents/util/content.util.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,95 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { BadRequestException } from '@nestjs/common';
import * as cheerio from 'cheerio';
import axios from 'axios';

@Injectable()
export class ContentUtil {
async getLinkInfo(link: string) {
let title: string | undefined = '';
let coverImg: string | undefined = '';
let description: string | undefined = '';
let siteName: string | undefined;
export const getLinkInfo = async (link: string) => {
let title: string | undefined = '';
let coverImg: string | undefined = '';
let description: string | undefined = '';
let siteName: string | undefined;

if (!link.match(/^(http|https):\/\//)) {
link = `http://${link}`;
}
if (!link.match(/^(http|https):\/\//)) {
link = `http://${link}`;
}

await axios
.get(link)
.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';
$('meta').each((i, el) => {
const meta = $(el);
if (meta.attr('property') === 'og:image') {
coverImg = meta.attr('content');
}
if (meta.attr('property') === 'og:description') {
description = meta.attr('content');
}
if (meta.attr('property') === 'og:site_name') {
siteName = meta.attr('content');
}
});
}
await axios
.get(link)
.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';
$('meta').each((i, el) => {
const meta = $(el);
if (meta.attr('property') === 'og:image') {
coverImg = meta.attr('content');
}
if (meta.attr('property') === 'og:description') {
description = meta.attr('content');
}
if (meta.attr('property') === 'og:site_name') {
siteName = meta.attr('content');
}
});
}
})
.catch((e) => {
// Control unreachable link
// if(e.message === 'Request failed with status code 403') {
// 403 에러가 발생하는 링크는 크롤링이 불가능한 링크이다.
// }
for (let idx = 1; idx < 3; idx++) {
if (link.split('/').at(-idx) !== '') {
title = link.split('/').at(-idx);
break;
}
}
})
.catch((e) => {
// Control unreachable link
// if(e.message === 'Request failed with status code 403') {
// 403 에러가 발생하는 링크는 크롤링이 불가능한 링크이다.
// }
for (let idx = 1; idx < 3; idx++) {
if (link.split('/').at(-idx) !== '') {
title = link.split('/').at(-idx);
break;
}
title = title ? title : 'Untitled';
});
}
title = title ? title : 'Untitled';
});

return {
title,
description,
coverImg,
siteName,
};
}
return {
title,
description,
coverImg,
siteName,
};
};

async getLinkContent(link: string) {
let content: string | undefined = '';
export const getLinkContent = async (link: string) => {
let content: string | undefined = '';

if (!link.match(/^(http|https):\/\//)) {
link = `http://${link}`;
}
if (!link.match(/^(http|https):\/\//)) {
link = `http://${link}`;
}

await axios
.get(link)
.then((res) => {
if (res.status !== 200) {
throw new BadRequestException('잘못된 링크입니다.');
} else {
const data = res.data;
if (typeof data === 'string') {
const $ = cheerio.load(data);
content = '';
$('p').each((i, elem) => {
// 모든 p 태그에 대해 작업을 수행합니다.
content += $(elem).text() + '\n'; // 각 p 태그의 텍스트를 가져와서 누적합니다.
});
}
await axios
.get(link)
.then((res) => {
if (res.status !== 200) {
throw new BadRequestException('잘못된 링크입니다.');
} else {
const data = res.data;
if (typeof data === 'string') {
const $ = cheerio.load(data);
content = '';
$('p').each((i, elem) => {
// 모든 p 태그에 대해 작업을 수행합니다.
content += $(elem).text() + '\n'; // 각 p 태그의 텍스트를 가져와서 누적합니다.
});
}
})
.catch((e) => {
// Control unreachable link
// if(e.message === 'Request failed with status code 403') {
// 403 에러가 발생하는 링크는 크롤링이 불가능한 링크이다.
// }
content = '';
});
}
})
.catch((e) => {
// Control unreachable link
// if(e.message === 'Request failed with status code 403') {
// 403 에러가 발생하는 링크는 크롤링이 불가능한 링크이다.
// }
content = '';
});

return content;
}
}
return content;
};

0 comments on commit eecc484

Please sign in to comment.