Skip to content

Commit

Permalink
Added getTeamByName & getEventByName (#401)
Browse files Browse the repository at this point in the history
* added getTeamByName

* Delete package-lock.json

* Update package.json

* Update README.md

added getTeamByName function

* added getEventByName

* Update README.md

* Update README.md

* Update README.md

* Update index.ts

fixed typo
  • Loading branch information
MarcoPstr authored Feb 25, 2021
1 parent 9871e7f commit bc64d74
Show file tree
Hide file tree
Showing 5 changed files with 4,027 additions and 3,734 deletions.
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Table of contents
- [getRecentThreads](#getrecentthreads)
- [getTeamRanking](#getteamranking)
- [getTeam](#getteam)
- [getTeamByName](#getteambyname)
- [getTeamStats](#getteamstats)
- [getPlayer](#getplayer)
- [getPlayerByName](#getplayerbyname)
Expand All @@ -31,6 +32,7 @@ Table of contents
- [getOngoingEvents](#getOngoingEvents)
- [getEvents](#getevents)
- [getEvent](#getevent)
- [getEventByName](#geteventbyname)
- [connectToScorebot](#connecttoscorebot)

## Installation
Expand Down Expand Up @@ -266,6 +268,24 @@ HLTV.getTeam({id: 6137}).then(res => {

---

#### getTeamByName

Same as getTeam but accepts a team name instead of ID. (2 requests)

| Option | Type | Default value | Description |
| :----: | :----: | :-----------: | :-------------: |
| name | string | - | The team name |

```javascript
HLTV.getTeamByName({name: "BIG"}).then(res => {
...
})
```

**[See getTeam schema](https://github.com/gigobyte/HLTV/blob/master/src/models/FullTeam.ts)**

---

#### getTeamStats

Parses the info from the `hltv.org/stats/teams/*` page (4 requests + 1 more if `currentRosterOnly` is true)
Expand Down Expand Up @@ -369,7 +389,7 @@ HLTV.getPlayerRanking({startDate: '2018-07-01', endDate: '2018-10-01'}).then(res

---

### getEvents
#### getEvents

Parses the info from the `hltv.org/events` page (1 request)

Expand All @@ -387,7 +407,7 @@ HLTV.getEvents().then(res => {

---

### getOngoingEvents
#### getOngoingEvents

Parses the info from the `hltv.org/events` page (1 request)

Expand All @@ -405,7 +425,7 @@ HLTV.getOngoingEvents().then(res => {

---

### getEvent
#### getEvent

Parses the info from the `hltv.org/event/` page (1 request)

Expand All @@ -423,6 +443,24 @@ HLTV.getEvent({id: 3389}).then(res => {

---

#### getEventByName

Same as getEvent but accepts a event name instead of ID. (2 requests)

| Option | Type | Default value | Description |
| :----: | :----: | :-----------: | :-------------: |
| name | string | - | The event name |

```javascript
HLTV.getEventByName({name: "IEM Katowice 2019"}).then(res => {
...
})
```

**[See getEvent schema](https://github.com/gigobyte/HLTV/blob/master/src/models/FullEvent.ts)**

---

#### connectToScorebot

Presents an interface to receive data when the HLTV scorebot updates
Expand Down
20 changes: 20 additions & 0 deletions src/endpoints/getEventByName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { HLTVConfig } from '../config'
import { FullEvent } from '../models/FullEvent'
import HLTV from '../index'

export const getEventByName = (config: HLTVConfig) => async ({
name
}: {
name: string
}): Promise<FullEvent> => {
const pageContent = JSON.parse(
await config.loadPage!(`${config.hltvUrl}/search?term=${name}`)
)
const firstResult = pageContent[0].events[0]

if (!firstResult) {
throw new Error(`Event ${name} not found`)
}

return HLTV.getEvent({ id: firstResult.id })
}
20 changes: 20 additions & 0 deletions src/endpoints/getTeamByName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { HLTVConfig } from '../config'
import { FullTeam } from '../models/FullTeam'
import HLTV from '../index'

export const getTeamByName = (config: HLTVConfig) => async ({
name
}: {
name: string
}): Promise<FullTeam> => {
const pageContent = JSON.parse(
await config.loadPage!(`${config.hltvUrl}/search?term=${name}`)
)
const firstResult = pageContent[0].teams[0]

if (!firstResult) {
throw new Error(`Team ${name} not found`)
}

return HLTV.getTeam({ id: firstResult.id })
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { getResults } from './endpoints/getResults'
import { getStreams } from './endpoints/getStreams'
import { getTeamRanking } from './endpoints/getTeamRanking'
import { getTeam } from './endpoints/getTeam'
import { getTeamByName } from './endpoints/getTeamByName'
import { getTeamStats } from './endpoints/getTeamStats'
import { getPlayer } from './endpoints/getPlayer'
import { getPlayerByName } from './endpoints/getPlayerByName'
import { getEvent } from './endpoints/getEvent'
import { getEventByName } from './endpoints/getEventByName'
import { getPlayerStats } from './endpoints/getPlayerStats'
import { getPlayerRanking } from './endpoints/getPlayerRanking'
import { MapSlug } from './enums/MapSlug'
Expand Down Expand Up @@ -60,10 +62,12 @@ export class HLTVFactory {
getStreams = getStreams(this.config)
getTeamRanking = getTeamRanking(this.config)
getTeam = getTeam(this.config)
getTeamByName = getTeamByName(this.config)
getTeamStats = getTeamStats(this.config)
getPlayer = getPlayer(this.config)
getPlayerByName = getPlayerByName(this.config)
getEvent = getEvent(this.config)
getEventByName = getEventByName(this.config)
getEvents = getEvents(this.config)
getOngoingEvents = getOngoingEvents(this.config)
getPlayerStats = getPlayerStats(this.config)
Expand Down
Loading

0 comments on commit bc64d74

Please sign in to comment.