-
Notifications
You must be signed in to change notification settings - Fork 0
/
scorecard.js
125 lines (120 loc) · 4.23 KB
/
scorecard.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// const url = "https://www.espncricinfo.com/series/ipl-2020-21-1210595/mumbai-indians-vs-chennai-super-kings-1st-match-1216492/full-scorecard";
// Venue date opponent result runs balls fours sixes sr
const request = require("request");
const cheerio = require("cheerio");
const path = require("path");
const fs = require("fs");
const xlsx=require("xlsx");
// home page
function processScorecard(url) {
request(url, cb);
}
function cb(err, response, html) {
if (err) {
console.log(err);
} else {
// console.log(html);
extractMatchDetails(html);
}
}
function extractMatchDetails(html) {
// Venue date opponent result runs balls fours sixes sr
// ipl
// team
// player
// runs balls fours sixes sr opponent venue date result
// venue date
// .event .description
// result -> .event .status-text
let $ = cheerio.load(html);
let descElem = $(".event .description");
let result = $(".event .status-text");
let stringArr = descElem.text().split(",");
let venue = stringArr[1].trim();
let date = stringArr[2].trim();
result = result.text();
// segregating innings
let innings = $(".card.content-block.match-scorecard-table>.Collapsible");
// let htmlString = "";
for (let i = 0; i < innings.length; i++) {
// htmlString = $(innings[i]).html();
// team opponent
let teamName = $(innings[i]).find("h5").text();
teamName = teamName.split("INNINGS")[0].trim();
let opponentIndex = i == 0 ? 1 : 0;
let opponentName = $(innings[opponentIndex]).find("h5").text();
opponentName = opponentName.split("INNINGS")[0].trim();
let cInning = $(innings[i]);
console.log(`${venue}| ${date} |${teamName}| ${opponentName} |${result}`);
let allRows = cInning.find(".table.batsman tbody tr");
for (let j = 0; j < allRows.length; j++) {
let allCols = $(allRows[j]).find("td");
// checking valid row
let isWorthy = $(allCols[0]).hasClass("batsman-cell");
if (isWorthy == true) {
// console.log(allCols.text());
// Player runs balls fours sixes sr
let playerName = $(allCols[0]).text().trim();
let runs = $(allCols[2]).text().trim();
let balls = $(allCols[3]).text().trim();
let fours = $(allCols[5]).text().trim();
let sixes = $(allCols[6]).text().trim();
let sr = $(allCols[7]).text().trim();
console.log(`${playerName} ${runs} ${balls} ${fours} ${sixes} ${sr}`);
processPlayer(teamName, playerName, runs, balls, fours, sixes, sr, opponentName, venue, date, result);
}
}
}
console.log("`````````````````````````````````````````````````");
// console.log(htmlString);
}
function processPlayer(teamName, playerName, runs, balls, fours, sixes, sr, opponentName, venue, date, result) {
let teamPath = path.join(__dirname, "ipl", teamName);
// if directory does not exist then make it
dirCreater(teamPath);
let filePath = path.join(teamPath, playerName + ".xlsx");
let content = excelReader(filePath, playerName);
let playerObj = {
teamName,
playerName,
runs,
balls,
fours,
sixes,
sr,
opponentName,
venue,
date,
result
}
content.push(playerObj);
excelWriter(filePath, content, playerName);
}
function dirCreater(filePath) {
if (fs.existsSync(filePath) == false) {
fs.mkdirSync(filePath);
}
}
function excelWriter(filePath, json, sheetName) {
let newWB = xlsx.utils.book_new();
let newWS = xlsx.utils.json_to_sheet(json);
xlsx.utils.book_append_sheet(newWB, newWS, sheetName);
xlsx.writeFile(newWB, filePath);
}
// // json data -> excel format convert
// // -> newwb , ws , sheet name
// // filePath
// read
// workbook get
function excelReader(filePath, sheetName) {
if (fs.existsSync(filePath) == false) {
return [];
}
let wb = xlsx.readFile(filePath);
let excelData = wb.Sheets[sheetName];
let ans = xlsx.utils.sheet_to_json(excelData);
return ans;
}
module.exports = {
ps: processScorecard
}