-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchFukkanComments.ts
76 lines (60 loc) · 2.05 KB
/
FetchFukkanComments.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { FukkanComment } from './@types/index'
import { chromium } from 'playwright'
class FetchFukkanComments {
bookNo: number
pageNo: number
constructor(bookNo: number, pageNo: number = 1) {
this.bookNo = bookNo
this.pageNo = pageNo
}
async exec() {
const browser = await chromium.launch()
const page = await browser.newPage()
const baseUrl = 'https://www.fukkan.com/fk/VoteComment'
const params = `?no=${this.bookNo}&page=${this.pageNo}&s=date`
await page.goto(`${baseUrl}${params}`)
const comments: any = await page.locator('ul.comment_list li')
const fukkanComments = await this.setFukkanComments(comments)
await browser.close()
return fukkanComments
}
async setFukkanComments(comments: any): Promise<FukkanComment[]> {
const numberOfComments = await comments.count()
const fukkanComments: FukkanComment[] = []
for (let i = 0; i < numberOfComments; i++) {
const userIdPath: string = await comments
.nth(i)
.locator('.comment_user a')
.getAttribute('href')
const userId: string = this.convertUserIdPathToUserId(userIdPath)
const userName: string = await comments
.nth(i)
.locator('.comment_user p')
.innerText()
const commentDetail: string = await comments
.nth(i)
.locator('.comment_detail')
.innerText()
const postedOn: string = this.extractPostedOn(commentDetail)
fukkanComments.push({
userId,
userName,
commentDetail,
postedOn,
})
}
return fukkanComments
}
// userIdPath: '/fk/user/?no=f98350f6a61e8245b'
convertUserIdPathToUserId(userIdPath: string): string {
const params = Object.fromEntries(new URLSearchParams(userIdPath))
return Object.values(params)[0] || ''
}
// commentDetail: 'ああああああ (2020/05/24)'
extractPostedOn(commentDetail: string): string {
const regex = /\((\d{4}\/\d{2}\/\d{2})\)$/
const result = commentDetail.match(regex) || []
return result[1] || ''
}
}
export default FetchFukkanComments