-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
getRecentThreads.ts
43 lines (37 loc) · 1.01 KB
/
getRecentThreads.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
import { HLTVConfig } from '../config'
import { HLTVScraper } from '../scraper'
import { fetchPage, generateRandomSuffix } from '../utils'
export enum ThreadCategory {
CS = 'cs',
Match = 'match',
News = 'news'
}
export interface Thread {
title: string
link: string
replies: number
category: ThreadCategory
}
export const getRecentThreads =
(config: HLTVConfig) => async (): Promise<Thread[]> => {
const $ = HLTVScraper(
await fetchPage(
`https://www.hltv.org/${generateRandomSuffix()}`,
config.loadPage
)
)
const threads = $('.activity')
.toArray()
.map((el) => {
const title = el.find('.topic').text()
const link = el.attr('href')
const replies = el.contents().last().numFromText()!
const category = el
.attr('class')
.split(' ')
.find((c) => c.includes('Cat'))!
.replace('Cat', '') as ThreadCategory
return { title, link, replies, category }
})
return threads
}