-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest1.js
162 lines (107 loc) · 4.09 KB
/
request1.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
let request = require("request");
// npm install cheerio
let cheerio = require("cheerio");
// preinstalled
let fs = require("fs");
let path = require("path")
let xlsx= require("xlsx")
//home page
request("https://www.espncricinfo.com/series/_/id/8048/season/2020/indian-premier-league",MainMatchCb)
function MainMatchCb(err,res,html){
let sTool= cheerio.load(html);
let allmatchPageUrl= sTool("a[data-hover='View All Results']").attr("href");
let fUrl="https://www.espncricinfo.com"+allmatchPageUrl;
AllMatchPage(fUrl);
}
// all match page
function AllMatchPage(fUrl){
request (fUrl,getAMUrl);
function getAMUrl(err,resp,html){
// console.log(html);
let sTool= cheerio.load(html);
let allmatchUrlElem= sTool("a[data-hover='Scorecard']");
for(let i=0;i<allmatchUrlElem.length;i++){
let href= sTool(allmatchUrlElem[i]).attr("href");
let fUrl="https://www.espncricinfo.com"+href;
findDataofAMatch(fUrl);
}
}
}
function findDataofAMatch(url) {
request( url, whenDataArrive);
function whenDataArrive(err, resp, html) {
console.log("recieved html");
let sTool = cheerio.load(html);
let tableElem = sTool("div.card.content-block.match-scorecard-table .Collapsible");
console.log(tableElem.length);
let count = 0;
for (let i = 0; i < tableElem.length; i++) {
let teamName = sTool(tableElem[i]).find("h5.header-title.label").text();
let teamstrArr = teamName.split("Innings")
teamName = teamstrArr[0].trim()
let rowsOfATeam = sTool(tableElem[i]).find(".table.batsman").find("tbody tr");
for (let j = 0; j < rowsOfATeam.length; j++) {
let rCols = sTool(rowsOfATeam[j]).find("td");
let isBatsManRow = sTool(rCols[0]).hasClass("batsman-cell");
if (isBatsManRow == true) {
count++;
let pName = sTool(rCols[0]).text().trim()
let runs = sTool(rCols[2]).text().trim()
let balls = sTool(rCols[3]).text().trim()
let fours = sTool(rCols[5]).text().trim()
let sixes = sTool(rCols[6]).text().trim()
let sr = sTool(rCols[7]).text().trim()
processPlayer(teamName, pName, runs, balls, fours, sixes, sr)
}
}
console.log("```````````````````````````````````````````````````````````");
}
}
function processPlayer(team, name, runs, balls, fours, sixes, sr) {
// teamNAme=> does this belong to an existing team
let dirPath = team
let pMatchstats = {
Team: team,
Name: name,
Runs: runs,
Balls: balls,
fours: fours,
Sixes: sixes,
StrikeRate: sr
}
if (fs.existsSync(dirPath)) {
//file check
}
// => check if player file exist or not
else {
fs.mkdirSync(dirPath);
}
let playerfilePath = path.join(dirPath, name + ".xlsx")
let pData = []
if (fs.existsSync(playerfilePath)) {
pData = excelReader(playerfilePath,name)
pData.push(pMatchstats)
} else {
//create file
console.log("file Of Player", playerfilePath, "created")
pData = [pMatchstats]
}
excelWriter(playerfilePath,pData,name)
}
function excelReader(filePath, name) {
if (!fs.existsSync(filePath)) {
return null;
}else{
let wt = xlsx.readFile(filePath)
let excelData = wt.Sheets[name]
let ans = xlsx.utils.sheet_to_json(excelData)
return ans;
}
}
function excelWriter(filePath, json, name) {
let newWb = xlsx.utils.book_new()
let newWS = xlsx.utils.json_to_sheet(json);
xlsx.utils.book_append_sheet(newWb ,newWS, name)
xlsx.writeFile(newWb ,filePath)
}
}