Skip to content

Commit

Permalink
feat: smartstore crawling useragent 설정 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Muungi committed May 27, 2024
1 parent 4901615 commit 6eaf4ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
15 changes: 3 additions & 12 deletions backend/src/utils/product.info.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { HttpException, HttpStatus } from '@nestjs/common';
import { ProductInfoDto } from 'src/dto/product.info.dto';
import {
API_VERSION_1,
API_VERSION_NEUTRAL,
BASE_URL_11ST,
BROWSER_VERSION_20,
OPEN_API_KEY_11ST,
REGEX_SHOP,
} from 'src/constants';
import { API_VERSION_1, API_VERSION_NEUTRAL, BASE_URL_11ST, OPEN_API_KEY_11ST, REGEX_SHOP } from 'src/constants';
import { JSDOM } from 'jsdom';
import * as convert from 'xml-js';
import * as iconv from 'iconv-lite';
import axios from 'axios';
import * as randomUseragent from 'random-useragent';
import { ProductIdentifierDto } from 'src/dto/product.identifier';
import { getRandomUserAgent } from './util';

export async function getProductInfo(shop: string, productCode: string): Promise<ProductInfoDto> {
if (shop === '11번가') {
Expand Down Expand Up @@ -66,9 +59,7 @@ async function getProductInfo11st(productCode: string): Promise<ProductInfoDto>
}

async function getProductInfoByBrandSmartStore(productCode: string): Promise<ProductInfoDto> {
const userAgent = randomUseragent.getRandom(function (ua) {
return parseFloat(ua.browserVersion) >= BROWSER_VERSION_20;
});
const userAgent = getRandomUserAgent();
const instance = axios.create();
const smartstoreURL = `https://smartstore.naver.com/main/products/${productCode}`;
instance.interceptors.request.use((req: any) => {
Expand Down
23 changes: 23 additions & 0 deletions backend/src/utils/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as randomUseragent from 'random-useragent';

export function createRandomNumber(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Expand All @@ -9,3 +11,24 @@ export function getSecondsUntilMidnight() {
const secondsUntilMidnight = Math.floor((midnight.getTime() - now.getTime()) / 1000);
return secondsUntilMidnight;
}

function isVersionInRange(version: string) {
const numericVersion = parseFloat(version);
return (numericVersion >= 20 && numericVersion < 29) || (numericVersion >= 535 && numericVersion < 538);
}

function isOsNameValid(osName: string) {
return osName === 'Windows' || osName === 'Mac OS';
}

export function getRandomUserAgent() {
const userAgent = randomUseragent.getRandom(function (ua) {
return (
ua.userAgent.includes('AppleWebKit') &&
ua.browserName === 'Chrome' &&
isOsNameValid(ua.osName) &&
isVersionInRange(ua.engineVersion)
);
});
return userAgent;
}

0 comments on commit 6eaf4ab

Please sign in to comment.