Skip to content

Commit

Permalink
fix(results): Fix result scraper
Browse files Browse the repository at this point in the history
- Fix scraper to adapt to new hltv markup
- Add tests
- Update readme
  • Loading branch information
dajk committed Nov 2, 2017
1 parent 390f60a commit 3a5bf1f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 42 deletions.
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,18 @@ getResults(results => console.log(results));
- response
```json
[{
"event": "Farmskins Championship #1",
"maps": { "0": "Inferno", "1": "Train" },
"event": "ECS Season 4 Europe",
"maps": "trn",
"team1": {
"name": "LDLC",
"crest": "https://static.hltv.org/images/team/logo/4674",
"result": { "0": 8, "1": 10 },
"total": 0
"name": "fnatic",
"crest": "https://static.hltv.org/images/team/logo/4991",
"result": 13
},
"team2": {
"name": "BIG",
"crest": "https://static.hltv.org/images/team/logo/7532",
"result": { "0": 16, "1": 16 },
"total": 2
"name": "FaZe",
"crest": "https://static.hltv.org/images/team/logo/6667",
"result": 16
},
"matchId": "/matches/2311152/ldlc-vs-big-farmskins-championship-1"
"matchId": "/matches/2316387/fnatic-vs-faze-ecs-season-4-europe"
}, ]
```
3 changes: 1 addition & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ describe('hltv-api', () => {

expect(result.team1.name).to.have.length.above(0);
expect(result.team1.crest).to.contain(CONFIG.STATIC);
assert.isNumber(result.team1.total);

expect(result.team2.name).to.have.length.above(0);
expect(result.team2.crest).to.contain(CONFIG.STATIC);
assert.isNumber(result.team2.total);

expect(result.matchId).to.have.length.above(10);
done();
});

});

});
Expand Down
41 changes: 12 additions & 29 deletions src/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,26 @@ export default class Results {
let results = [];

$(resultElements).each((i, element) => {
const header = $(element).find('tr.header');
const team1 = header.next();
const team2 = header.next().next();
const el = $(element).find('tr');
const team1 = el.children('.team-cell').first();
const team2 = el.children('.team-cell').last();
const matchId = $(element).children('a').attr('href');

let maps = {};
const playedMaps = header.find('.map');
playedMaps.each((i, map) => {
maps[i] = $(map).text();
});

let result1 = {};
const result1Maps = team1.find('.mapscore');
result1Maps.each((i, res) => {
result1[i] = parseInt($(res).text());
});

let result2 = {};
const result2Maps = team2.find('.mapscore');
result2Maps.each((i, res) => {
result2[i] = parseInt($(res).text());
});
const maps = el.find('.map');
const result1 = el.find('.result-score').children('span').first();
const result2 = el.find('.result-score').children('span').last();

const objData = {
event: header.children('.eventName').children().first().children().first().text().trim(),
maps,
event: el.find('.event-name').text().trim(),
maps: maps.text().trim(),
team1: {
name: team1.find('.teams span').text().trim(),
name: team1.find('.team').text().trim(),
crest: team1.find('img').attr('src'),
result: result1,
total: parseInt(team1.children().last().text())
result: parseInt(result1.text().trim())
},
team2: {
name: team2.find('.teams span').text().trim(),
name: team2.find('.team').text().trim(),
crest: team2.find('img').attr('src'),
result: result2,
total: parseInt(team2.children().last().text())
result: parseInt(result2.text().trim())
},
matchId
};
Expand Down

0 comments on commit 3a5bf1f

Please sign in to comment.