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

Adding getHotMatches Function #29

Closed
wants to merge 6 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
.DS_Store
coverage/
dist/
src/playground.ts
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ app.get('/all-matches', function(req, res) {
})
})

app.get('/hot-matches', function(req, res) {
HLTV.getHotMatches(function(stats) {
return res.json(stats);
});
});

app.get('/:matchId(*)', function(req, res) {
HLTV.getMatches(req.params.matchId, function(stats) {
return res.json(stats)
Expand All @@ -63,7 +69,13 @@ app.listen(3000, function() {
- Using babel and necessary plugins ([demo app](/demo-app/index.js))

```js
import { getNews, getResults, getMatches } from 'hltv-api'
import {
getNews,
getResults,
getMatches,
getAllMatches,
getHotMatches,
} from 'hltv-api';
```

##### News
Expand Down Expand Up @@ -173,6 +185,36 @@ http://localhost:3000/all-matches
]
```

###### Hot Matches
```js
app.get('/hot-matches', (req, res) => {
getHotMatches((stats) => res.json(stats));
});
```

- request
```
http://localhost:3000/hot-matches
```

- response
```json
[{
"star":"1",
"matchId":"2336982",
"lan":true,
"live":true,
"teams": [
{
"id":"8297",
"name":"FURIA",
"lang":"Brazil",
"crest":"https://static.hltv.org/images/bigflags/30x20/BR.gif"
},
]
}]
```

###### Single Match

```js
Expand Down
6 changes: 6 additions & 0 deletions examples/with-babel/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line import/no-unresolved
import express from 'express'
import * as hltv from '../../dist'

Expand All @@ -15,6 +16,10 @@ app.get('/all-matches', (req, res) => {
hltv.getAllMatches(stats => res.json(stats))
})

app.get('/hot-matches', (req, res) => {
hltv.getHotMatches(stats => res.json(stats))
})

app.get('/:matchId(*)', (req, res) => {
const { matchId } = req.params
hltv.getMatches(matchId, stats => res.json(stats))
Expand All @@ -23,5 +28,6 @@ app.get('/:matchId(*)', (req, res) => {
const PORT = 3000

app.listen(PORT, () => {
// eslint-disable-next-line no-console
console.log(`Listening on port http://localhost:${PORT} ...`)
})
14 changes: 13 additions & 1 deletion examples/with-typescript/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// eslint-disable-next-line import/no-unresolved
import express from 'express'
import { getNews, getResults, getMatches, getAllMatches } from '../../dist'
import {
getNews,
getResults,
getMatches,
getHotMatches,
getAllMatches,
} from '../../dist'

const app = express()

Expand All @@ -15,6 +22,10 @@ app.get('/all-matches', (req, res) => {
getAllMatches((stats: any) => res.json(stats))
})

app.get('/hot-matches', (req, res) => {
getHotMatches((stats: any) => res.json(stats))
})

app.get('/:matchId(*)', (req, res) => {
const { matchId } = req.params
getMatches(matchId, (stats: any) => res.json(stats))
Expand All @@ -23,5 +34,6 @@ app.get('/:matchId(*)', (req, res) => {
const PORT: number = 3000

app.listen(PORT, () => {
// eslint-disable-next-line no-console
console.log(`Listening on port http://localhost:${PORT} ...`)
})
2 changes: 1 addition & 1 deletion examples/with-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
"ts-node": "^8.1.0",
"typescript": "^3.4.5"
}
}
}
6 changes: 4 additions & 2 deletions examples/with-typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"strict": true,
"esModuleInterop": true,
"allowJs": true,
"typeRoots": ["./node_modules/@types"]
"typeRoots": [
"./node_modules/@types"
]
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@
"git add"
]
}
}
}
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Matches from './matches'
export const getNews = (cb: (response: any) => void) => new RSS('news', cb)
export const getResults = (cb: (response: any) => void) => new Results(cb)
export const getMatches = (id: string, cb: (response: any) => void) =>
new Matches(id, cb).getSingleMatch()
new Matches(cb).getSingleMatch(id)
export const getAllMatches = (cb: (response: any) => void) =>
new Matches('false', cb).getAllMatches()
new Matches(cb).getAllMatches()
export const getHotMatches = (cb: (response: any) => void) =>
new Matches(cb).getHotMatches()
88 changes: 78 additions & 10 deletions src/matches.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import request from 'request'
import cheerio from 'cheerio'
import { CONFIG, MAPS } from './config'
import { toArray } from './utility'

/**
* Scraping matches
Expand All @@ -11,8 +12,6 @@ import { CONFIG, MAPS } from './config'
export default class Matches {
private callback: Function

private matchId: string

/**
* Creates an instance of Matches.
*
Expand All @@ -21,20 +20,21 @@ export default class Matches {
*
* @memberOf Matches
*/
constructor(matchId: string, callback: any) {
constructor(callback: any) {
this.callback = callback
}

getSingleMatch(matchId: string) {
let singleMatchId
if (matchId.split('/').length >= 3) {
this.matchId =
singleMatchId =
parseInt(matchId.split('/')[1], 10) > 100
? matchId.split('/')[1]
: matchId.split('/')[2]
} else {
this.matchId = matchId
singleMatchId = matchId
}
this.callback = callback
}

getSingleMatch() {
const uri = `${CONFIG.BASE}${CONFIG.MATCHES}/${this.matchId}/-`
const uri = `${CONFIG.BASE}${CONFIG.MATCHES}/${singleMatchId}/-`

request({ uri }, (error, response, body) => {
const $ = cheerio.load(body, {
Expand Down Expand Up @@ -228,4 +228,72 @@ export default class Matches {
this.callback(returnArr, error)
})
}

getHotMatches() {
const uri = `${CONFIG.BASE}`

request({ uri }, (error, response, body) => {
const $ = cheerio.load(body, {
normalizeWhitespace: true,
})

const returnArr: any[] = []

const hotMatchesContent = $('.rightCol > aside > div.top-border-hide > a')

toArray(hotMatchesContent).map((value: Cheerio) => {
const el = $(value)

const star = el.children('div.teambox').attr('stars')
const lan = Boolean(el.children('div.teambox').attr('lan'))
const live = Boolean(el.children('div.teambox').attr('filteraslive'))
const matchId = el.attr('href').split('/')[2]
const team1Id = el.children('div.teambox').attr('team1')
const team1Name = el
.find('div.teambox > div > div:first-of-type span')
.text()
const team1Nation = el
.find('div.teambox > div > div:first-of-type img')
.attr('alt')
const team1Crest = el
.find('div.teambox > div > div:first-of-type img')
.attr('src')

const team2Id = el.children('div.teambox').attr('team2')
const team2Name = el
.find('div.teambox > div > div:last-of-type span')
.text()
const team2Nation = el
.find('div.teambox > div > div:last-of-type img')
.attr('alt')
const team2Crest = el
.find('div.teambox > div > div:first-of-type img')
.attr('src')

returnArr[returnArr.length] = {
star,
matchId,
lan,
live,
teams: [
{
id: team1Id,
name: team1Name,
lang: team1Nation,
crest: team1Crest,
},
{
id: team2Id,
name: team2Name,
lang: team2Nation,
crest: team2Crest,
},
],
}
return true
})

this.callback(returnArr, error)
})
}
}
12 changes: 12 additions & 0 deletions src/utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import cheerio from 'cheerio'

/**
* Converting the selected elemnt in an Cherrio Array
*
* @export
* @param {Cheerio} elements
* @returns {Cheerio[]}
*/
export function toArray(elements: Cheerio): Cheerio[] {
return elements.toArray().map(cheerio)
}
36 changes: 35 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getNews, getResults, getMatches, getAllMatches } from '../src'
import {
getNews,
getResults,
getMatches,
getHotMatches,
getAllMatches,
} from '../src'
import { CONFIG } from '../src/config'

describe('hltv-api', () => {
Expand All @@ -21,7 +27,9 @@ describe('hltv-api', () => {
done()
})
})
})

describe('get appropriate responses `getResults`', () => {
it('should have all details when we call `getResults`', done => {
getResults(response => {
expect(response.length).toBeDefined()
Expand All @@ -34,7 +42,9 @@ describe('hltv-api', () => {
done()
})
})
})

describe('get appropriate responses `getMatches`', () => {
it('should have match stats when we call `getMatches` with long Id', done => {
getMatches(
'matches/2332210/liquid-vs-faze-blast-pro-series-miami-2019',
Expand Down Expand Up @@ -373,7 +383,9 @@ describe('hltv-api', () => {
done()
})
})
})

describe('get appropriate responses `getAllMatches`', () => {
it('should have stats off all matches when we call `getAllMatches`', done => {
getAllMatches(response => {
expect(response.length).toBeGreaterThanOrEqual(50)
Expand All @@ -391,4 +403,26 @@ describe('hltv-api', () => {
})
})
})

describe('get appropriate responses `getHotMatches`', () => {
it('should have stats off all hot matches when we call `getHotMatches`', done => {
getHotMatches(response => {
expect(response.length).toBeGreaterThanOrEqual(5)
const result = response[0]
expect(result.star).toBeDefined()
expect(result.matchId).toBeDefined()
expect(result.lan).toBeDefined()
expect(result.live).toBeDefined()
expect(result.teams[0].id).toBeDefined()
expect(result.teams[0].name).toBeDefined()
expect(result.teams[0].lang).toBeDefined()
expect(result.teams[0].crest).toBeDefined()
expect(result.teams[1].id).toBeDefined()
expect(result.teams[1].name).toBeDefined()
expect(result.teams[1].lang).toBeDefined()
expect(result.teams[1].crest).toBeDefined()
done()
})
})
})
})
17 changes: 13 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["es2017", "es7", "es6"],
"lib": [
"es2017",
"es7",
"es6"
],
"declaration": true,
"outDir": "dist",
"strict": true,
Expand All @@ -11,6 +15,11 @@
"preserveConstEnums": true,
"sourceMap": true
},
"files": ["./src/index.ts"],
"exclude": ["node_modules", "dist"]
}
"files": [
"./src/index.ts"
],
"exclude": [
"node_modules",
"dist"
]
}