Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,17 @@ https://hltv-api.vercel.app/api/players.json
#### **Player Stats**

```js
app.get('/players/:playerId', async (req, res) => {
const player = await HLTV.getPlayerById(req.params.playerId)
app.get('/players/:playerId/:matchType/:dateFilterStart/:dateFilterEnd', async (req, res) => {
const player = await HLTV.getPlayerById(req.params.playerId, req.params.matchType, req.params.dateFilterStart, req.params.dateFilterEnd)
res.json(player)
})
```

- request

```
http://localhost:3000/players/11893
http://localhost:3000/players/11893/All
http://localhost:3000/players/11893/Majors
```

- response
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const CONFIG = {
RSS: 'rss',
RESULTS: 'results',
MATCHES: 'matches',
MATCHTYPE: '?matchType=',
PLAYERS: 'stats/players',
TEAMS: 'ranking/teams',
TEAM: 'team',
Expand Down
95 changes: 57 additions & 38 deletions src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@ import fetch from 'node-fetch'
import { CONFIG, USER_AGENT } from './config'

interface IPlayer {
id: number
team: {
id: number
name: string
}
image: string
nickname: string
name: string
age: number | null
rating: number
impact: number | null
dpr: number | null
adr: number | null
kast: number | null
kpr: number
headshots: number
mapsPlayed: number | null
id?: number
teamLogo?: string
image?: string
nickname?: string
name?: string
age?: number | null
rating?: number
impact?: number | null
dpr?: number | null
adr?: number | null
kast?: number | null
kpr?: number
headshots?: number
mapsPlayed?: number | null
kills?: number
deaths?: number
kdr?: number
roundsPlayed?: number

}

export async function getPlayerById(id: number): Promise<IPlayer> {
const url = `${CONFIG.BASE}/${CONFIG.PLAYERS}/${id}/_`
export async function getPlayerById(id: number, matchType: string, dateFilterStart: string, dateFilterEnd: string): Promise<IPlayer[]> {
const url = `${CONFIG.BASE}/${CONFIG.PLAYERS}/${id}/_?matchType=${matchType}&startDate=${dateFilterStart}&endDate=${dateFilterEnd}`

try {
const body = await (
Expand Down Expand Up @@ -60,6 +62,7 @@ export async function getPlayerById(id: number): Promise<IPlayer> {
.attr('href')
?.split('/')[3]
)
const teamLogo = imageBlock.children('img').eq(0).attr('src');
const age = parseInt(mainTableContent.find('.summaryPlayerAge').text(), 10)

const statRow1 = mainTableContent
Expand Down Expand Up @@ -91,25 +94,41 @@ export async function getPlayerById(id: number): Promise<IPlayer> {
10
)

return {
id: Number(id),
team: {
id: teamId,
name: teamName,
},
image,
nickname,
name,
age: age || null,
rating,
impact: impact || null,
dpr: dpr || null,
adr: adr || null,
kast: kast || null,
kpr,
headshots,
mapsPlayed: maps || null,
}
const kills = parseInt(
additionalStats.eq(0).children('.stats-row').eq(0).children('span').eq(1).text(),
10
)

const deaths = parseInt(
additionalStats.eq(0).children('.stats-row').eq(2).children('span').eq(1).text(),
10
)

const rounds = parseInt(
additionalStats.eq(1).children('.stats-row').eq(0).children('span').eq(1).text(),
10
)

const kdr = parseFloat(
additionalStats.eq(0).children('.stats-row').eq(3).children('span').eq(1).text()
)

return [
{mapsPlayed: maps,
roundsPlayed: rounds,
kills,
deaths,
rating,
impact,
kast,
adr,
kpr,
dpr,
kdr},
{image,
teamLogo,
nickname}
];
} catch (error) {
throw new Error(error as any)
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"preserveConstEnums": true,
"sourceMap": true
},
"files": ["./src/index.ts"],
"include": ["./src/*"],
"exclude": ["node_modules", "dist"]
}
Loading