-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathawards.js
executable file
·71 lines (68 loc) · 1.89 KB
/
awards.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* getAwards - get award of each type
*
* @param {Integer} n position of award function
* @param {Integer} f postion of award type
* @param {Integer} $ loaded cheerio html string
*
* @returns {Object} the key are category,wonBy
*/
function getAwards(n, f, $) {
const k = n === 1 ? 2 : 1;
const position = $(
`table.awards:nth-child(${f}) > tbody:nth-child(1) > tr:nth-child(${n}) > td:nth-child(${k})`
).text();
var refactor = position
.split("\n")
.join("")
.split(" ");
const category = refactor[6].trim();
const wonBy = refactor[10].trim();
return { category, wonBy };
}
/**
* getAwardCategory - get category for each award
*
* @param {NUMBER} n position of the award category
* @param {Object} $ cheerio loaded object from html string
*
* @returns {Object} contains category name
*/
function getAwardCategory(n, $) {
// table.awards:nth-child(n) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > span:nth-child(3)
const position = $(
`table.awards:nth-child(${n}) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > span:nth-child(3)`
)
.text()
.trim();
return position;
}
/**
* getWinner - get the name of the winner person
*
* @param {Number} k postion of the winner
* @param {Object} $ cheerio loaded html string
*
* @returns {Object} uses all the above function and get award winner
*/
function getWinner(k, $) {
var type = getAwardCategory(k, $).trim();
let award = { name: type, winner: [] };
try {
var loop = $(
`table.awards:nth-child(${k}) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1)`
)[0].attribs.rowspan;
} catch (e) {}
// table.awards:nth-child(7) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1)
var i = 1;
while (loop--) {
try {
award.winner.push(getAwards(i, k, $));
i++;
} catch (e) {
i++;
}
}
return award;
}
module.exports = { getWinner };