-
Notifications
You must be signed in to change notification settings - Fork 1
/
scoreCard.js
171 lines (141 loc) · 4.46 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// temp url for testing :
// const url =
// "https://www.espncricinfo.com/series/ipl-2020-21-1210595/mumbai-indians-vs-chennai-super-kings-1st-match-1216492/full-scorecard";
const request = require("request");
const cheerio = require("cheerio");
const path = require("path");
const fs = require("fs");
const xlsx = require("xlsx");
const { log } = require("console");
// this function called from getAllMatch module :
function processScoreCard(url) {
request(url, cb);
}
function cb(error, response, html) {
if (error) {
console.log(error);
} else {
getScore(html);
}
}
function getScore(html) {
let $ = cheerio.load(html);
// get venue and date of match :
let data = $(".ds-text-tight-m.ds-font-regular.ds-text-ui-typo-mid");
let getData = $(data).text();
// convert str to array geting venue and date sapreatly
getData = getData.split(",");
let venue = getData[1];
let date = getData[2].trim();
// now get result of the match : =>
let result = $(
".ds-text-tight-m.ds-font-regular.ds-truncate.ds-text-typo-title"
).text();
// console.log(venue);
// console.log(date);
// console.log(result);
let innings = $(".ds-rounded-lg.ds-mt-2");
let htmlStr = "";
for (let i = 0; i < innings.length; i++) {
htmlStr += $(innings[i]).html();
let teamName = $(innings[i])
.find(".ds-text-title-xs.ds-font-bold.ds-capitalize")
.text();
let oppenentName = i == 0 ? 1 : 0;
oppenentName = $(innings[oppenentName])
.find(".ds-text-title-xs.ds-font-bold.ds-capitalize")
.text();
let cInings = $(innings[i]);
let rowData = cInings.find(".ds-w-full.ds-table.ds-table-md.ds-table-auto.ci-scorecard-table>tbody tr");
// console.log( `--------------------------------- ${teamName} -------------------------------------------`);
for (let j = 0; j < rowData.length; j++) {
let tableCol = $(rowData[j]).find("td");
let isWorthy = $(tableCol[0]).hasClass("ds-w-0");
if (isWorthy == true) {
let playerName = $(tableCol[0]).text().trim();
let runs = $(tableCol[2]).text().trim();
let balls = $(tableCol[3]).text().trim();
let four = $(tableCol[5]).text().trim();
let six = $(tableCol[6]).text().trim();
let str = $(tableCol[7]).text().trim();
// now make dir inside of ipl dir for each team :
storeData(
teamName,
oppenentName,
playerName,
runs,
balls,
four,
six,
str,
date
);
// console.log(
// `PlayerName :${playerName} || Runs : ${runs} || Balls ${balls} || Fours : ${four} || Sixes : ${six} || Strike-Rate : ${str}`
// );
}
}
}
}
function storeData(
teamName,
oppenentName,
playerName,
runs,
balls,
four,
six,
str,
date
) {
let teamPath = path.join(__dirname, "IPL", teamName);
iplDir(teamPath);
let filePath = path.join(teamPath, playerName + ".xlsx");
// console.log(filePath);
let readContent = excelReader(filePath, playerName);
let objData = {
teamName,
oppenentName,
playerName,
runs,
balls,
four,
six,
str,
date,
};
readContent.push(objData);
excelWriter(filePath,playerName,readContent);
}
function iplDir(teamPath) {
if (fs.existsSync(teamPath) == false) {
fs.mkdirSync(teamPath);
}}
function excelWriter(filePath, sheetName, jsonData) {
let newWB = xlsx.utils.book_new();
// add new WorkBook (new sheet added)
let newWS = xlsx.utils.json_to_sheet(jsonData);
// this will take JSON data and convert intp Excel formate
xlsx.utils.book_append_sheet(newWB, newWS, sheetName);
// this will add new sheet and and make it also named added that perticulaer sheet name
xlsx.writeFile(newWB, filePath);
// this will write workBook and provide the file name
}
// now its time read data using xlsx module =>
function excelReader(filePath, sheetName) {
if (fs.existsSync(filePath) == false) {
console.log("heelo");
return [];
}
console.log("heelo");
let wb = xlsx.readFile(filePath);
// which excel book to read
let excelData = wb.Sheets[sheetName];
// which sheet to read from excel workbook
let ans = xlsx.utils.sheet_to_json(excelData);
// converting excel data to json formate
return ans;
}
module.exports ={
processScoreCard
}