-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(the-camp): trainUnitEduSeq, traineeMgrSeq 뽑을 수 있도록 기능 추가
- Loading branch information
Showing
25 changed files
with
4,953 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# .env | ||
.env | ||
|
||
# compiled output | ||
/lib | ||
/node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export function extractInnerText( | ||
target: string, | ||
lefts: string[], | ||
right: string, | ||
): string { | ||
let beforeStartIdx = -1; | ||
let startIdx = -1; | ||
for (const left of lefts) { | ||
startIdx = target.indexOf(left, beforeStartIdx); | ||
|
||
if (startIdx < 0) { | ||
break; | ||
} | ||
|
||
startIdx += left.length; | ||
beforeStartIdx = startIdx; | ||
} | ||
|
||
const endIdx = target.indexOf(right, startIdx); | ||
if (startIdx < 0 || endIdx < 0) { | ||
return ''; | ||
} | ||
|
||
return target.substring(startIdx, endIdx); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export function extractInnerTexts( | ||
target: string, | ||
left: string, | ||
right: string, | ||
): string[] { | ||
const extractedTexts = []; | ||
let offset = 0; | ||
|
||
while (target.includes(left) && target.includes(right)) { | ||
const startIdx = target.indexOf(left, offset); | ||
const endIdx = target.indexOf(right, startIdx); | ||
|
||
if (startIdx === -1 || endIdx === -1) { | ||
break; | ||
} | ||
|
||
const extractedText = target.substring(startIdx + left.length, endIdx); | ||
|
||
extractedTexts.push(extractedText); | ||
offset = endIdx + right.length; | ||
} | ||
|
||
return extractedTexts; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './extractInnerText'; | ||
export * from './extractInnerTexts'; |
12 changes: 12 additions & 0 deletions
12
src/services/the-camp/requesters/fetch-soldiers/fetch-soldiers.requester.e2e.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { loginRequester } from '../login'; | ||
import { fetchSoldiersRequester } from './fetch-soldiers.requester'; | ||
|
||
describe.skip('FetchSoldiersRequester e2e', () => { | ||
it('성공', async () => { | ||
const session = await loginRequester.request({ | ||
id: '', | ||
password: '', | ||
}); | ||
await fetchSoldiersRequester.request(session); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
src/services/the-camp/requesters/fetch-soldiers/fetch-soldiers.requester.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { TheCampSession } from '@common/types'; | ||
import axios, { AxiosRequestConfig } from 'axios'; | ||
|
||
import { FetchSoldierRawInfo, parseSoldiers } from './parse-soldiers'; | ||
|
||
export class FetchSoldiersRequester { | ||
constructor(private readonly parse = parseSoldiers) {} | ||
|
||
async request(session: TheCampSession): Promise<FetchSoldierRawInfo[]> { | ||
const response = await axios.get( | ||
'https://www.thecamp.or.kr/eduUnitCafe/viewEduUnitCafeMain.do', | ||
this.createOptions(session), | ||
); | ||
|
||
return this.parse(response); | ||
} | ||
|
||
private createOptions(session: TheCampSession): AxiosRequestConfig { | ||
return { | ||
headers: { | ||
Accept: | ||
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', | ||
'Accept-Encoding': 'gzip, deflate, br', | ||
'Accept-Language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7', | ||
'User-Agent': | ||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.70 Whale/3.13.131.27 Safari/537.36', | ||
Host: 'www.thecamp.or.kr', | ||
Origin: 'https://www.thecamp.or.kr', | ||
Referer: 'https://www.thecamp.or.kr/eduUnitCafe/viewEduUnitCafeMain.do', | ||
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Whale";v="3"', | ||
'sec-ch-ua-mobile': '?0', | ||
'sec-ch-ua-platform': '"macOS"', | ||
'Sec-Fetch-Site': 'same-origin', | ||
'Sec-Fetch-Mode': 'cors', | ||
'Sec-Fetch-Dest': 'empty', | ||
Cookie: session.cookies | ||
.map(({ key, value }) => `${key}=${value}`) | ||
.join('; '), | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
export const fetchSoldiersRequester = new FetchSoldiersRequester(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './fetch-soldiers.requester'; | ||
export { FetchSoldierRawInfo as FetchSoldierRawInfo } from './parse-soldiers'; |
61 changes: 61 additions & 0 deletions
61
src/services/the-camp/requesters/fetch-soldiers/parse-soldiers.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
import { parseSoldiers } from './parse-soldiers'; | ||
|
||
describe('parseSoldiers', () => { | ||
it('성공', () => { | ||
const data = readFileSync(join(__dirname, 'test/성공.txt'), 'utf-8'); | ||
|
||
expect(parseSoldiers({ data } as any)).toEqual([ | ||
{ | ||
입영부대Code: '20020191700', | ||
입영부대EduId: '6506', | ||
군인Code: '6142000', | ||
입영일: '20210302', | ||
생년월일: '20010928', | ||
이름: '김민석', | ||
}, | ||
{ | ||
입영부대Code: '20121190200', | ||
입영부대EduId: '6611', | ||
군인Code: '3051000', | ||
입영일: '20210329', | ||
생년월일: '20010822', | ||
이름: '이예건', | ||
}, | ||
{ | ||
입영부대Code: '20220280600', | ||
입영부대EduId: '6629', | ||
군인Code: '2050000', | ||
입영일: '20210406', | ||
생년월일: '20010814', | ||
이름: '이지원', | ||
}, | ||
{ | ||
입영부대Code: '20020191700', | ||
입영부대EduId: '6698', | ||
군인Code: '6142000', | ||
입영일: '20210426', | ||
생년월일: '20010405', | ||
이름: '김민수', | ||
}, | ||
{ | ||
입영부대Code: '20220280100', | ||
입영부대EduId: '12117', | ||
군인Code: '2031000', | ||
입영일: '20220125', | ||
생년월일: '20011020', | ||
이름: '김명훈', | ||
}, | ||
{ | ||
입영부대Code: '20020191700', | ||
입영부대EduId: '14030', | ||
군인Code: '6142000', | ||
입영일: '20220214', | ||
생년월일: '20011126', | ||
이름: '이상택', | ||
}, | ||
]); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
src/services/the-camp/requesters/fetch-soldiers/parse-soldiers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { extractInnerTexts } from '@core/string'; | ||
import { AxiosResponse } from 'axios'; | ||
|
||
export interface FetchSoldierRawInfo { | ||
입영부대Code: string; // trainUnitCd | ||
입영부대EduId: string; // trainUnitEduSeq | ||
군인Code: string; // unitCd | ||
입영일: string; // enterDate | ||
생년월일: string; // birthDay | ||
이름: string; // name | ||
} | ||
|
||
export function parseSoldiers({ | ||
data, | ||
}: AxiosResponse<string>): FetchSoldierRawInfo[] { | ||
const fnCafeCreateCheckLines = extractInnerTexts( | ||
data, | ||
'javascript:fn_cafeMainLink2(', | ||
'보고싶은군인', | ||
); | ||
return fnCafeCreateCheckLines.map(parseSoldier); | ||
} | ||
|
||
// fnLine: fn_cafeCreateCheck(...) | ||
function parseSoldier(fnLine: string): FetchSoldierRawInfo { | ||
const [fn_cafeMainLink2, fn_findArmyArrngmtResult] = fnLine.split( | ||
'javascript:fn_findArmyArrngmtResult(', | ||
); | ||
const [입영부대Code, 입영부대EduId] = fn_cafeMainLink2 | ||
.split(');')[0] | ||
.replaceAll("'", '') | ||
.split(','); | ||
|
||
const [군인Code, 입영일, 생년월일, 이름] = fn_findArmyArrngmtResult | ||
.split(');')[0] | ||
.replaceAll("'", '') | ||
.split(','); | ||
|
||
return { | ||
입영부대Code, | ||
입영부대EduId, | ||
군인Code, | ||
입영일: 입영일.replaceAll('.', ''), | ||
생년월일, | ||
이름, | ||
}; | ||
} |
Oops, something went wrong.