Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get event results #165

Merged
merged 4 commits into from May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/endpoints/getResults.ts
Expand Up @@ -6,18 +6,33 @@ import { popSlashSource } from '../utils/parsing'
import { HLTVConfig } from '../config'
import { fetchPage, toArray, getMatchFormatAndMap } from '../utils/mappers'

export const getResults = (config: HLTVConfig) => async ({ pages = 1 } = {}): Promise<
MatchResult[]
> => {
export const getResults = (config: HLTVConfig) => async ({
pages = 1,
eventId
}: {
pages: number
eventId?: number
}): Promise<MatchResult[]> => {
if (pages < 1) {
console.error('getLatestResults: pages cannot be less than 1')
return []
}

// All results for events are always on one page
if (eventId) {
pages = 1
}

let matches = [] as MatchResult[]

for (let i = 0; i < pages; i++) {
const $ = await fetchPage(`${config.hltvUrl}/results?offset=${i * 100}`, config.loadPage)
let fullUrl = `${config.hltvUrl}/results?offset=${i * 100}`

if (eventId) {
fullUrl += `&event=${eventId}`
}

const $ = await fetchPage(fullUrl, config.loadPage)

matches = matches.concat(
toArray($('.results-holder > .results-all > .results-sublist .result-con .a-reset')).map(
Expand Down Expand Up @@ -48,11 +63,21 @@ export const getResults = (config: HLTVConfig) => async ({ pages = 1 } = {}): Pr
}

const event: Event = {
name: matchEl.find('.event-logo').attr('alt'),
id: Number(popSlashSource(matchEl.find('.event-logo'))!.split('.')[0])
name: eventId ? $('.eventname').text() : matchEl.find('.event-logo').attr('alt'),
id: eventId
? eventId
: Number(popSlashSource(matchEl.find('.event-logo'))!.split('.')[0])
}

const date = Number(matchEl.parent().attr('data-zonedgrouping-entry-unix'))
const date = Number(
eventId
? matchEl
.find('.date-cell')
.children()
.first()
.attr('data-unix')
: matchEl.parent().attr('data-zonedgrouping-entry-unix')
)

return { id, team1, team2, result, event, map, format, stars, date }
}
Expand Down
10 changes: 7 additions & 3 deletions src/playground.ts
@@ -1,7 +1,7 @@
import HLTV from './index'
HLTV.getMatch({ id: 2332676 })
.then(res => console.dir(res, { depth: null }))
.catch(err => console.log(err))
// HLTV.getMatch({ id: 2332676 })
// .then(res => console.dir(res, { depth: null }))
// .catch(err => console.log(err))
// HLTV.getMatches().then(res => console.log(res))
// HLTV.getResults({pages: 1}).then(res => console.log(res))
// HLTV.getStreams({ loadLinks: true }).then(res => console.log(res))
Expand All @@ -28,3 +28,7 @@ HLTV.getMatch({ id: 2332676 })
// HLTV.getTeamStats({id: 6669}).then(res => console.dir(res, { depth: null })).catch(err => console.log(err))
// HLTV.getPlayer({id: 9216}).then(res => console.dir(res, { depth: null })).catch(err => console.log(err))
// HLTV.getEvent({id: 3773}).then(res => console.dir(res, { depth: null })).catch(err => console.log(err))


HLTV.getResults({pages: 1, eventId: 3883}).then(res => console.log(res))
HLTV.getResults({pages: 1, eventId: 3047}).then(res => console.log(3047 + ':' + res.length))