forked from samridhamla06/sports-live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
171 lines (143 loc) · 5.35 KB
/
helper.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
var request = require("request");
var Feed = require('rss-to-json');
function getResponseObject(team1, team2, score, keyMoment) {
var json = {};
json.team1 = team1;
json.team2 = team2;
json.score = score;
json.currentStatus = keyMoment;
return json;
}
function extractCricketScores(match){
var description = match.description;
description = description.replace("&",",");
var indexOfV = description.indexOf(" v ")
var team1 = description.substring(0,indexOfV);
var team2 = description.substring(indexOfV + 2);
return getResponseObject(team1.trim(),team2.trim()," "," ");
}
function extractOtherScores(sport,match){
if (sport == "soccer"){ // soccer response is different
var description = match.description;
var indexOfBracket = description.indexOf(")");
description = description.substring(indexOfBracket+1);
var indexOfVs = description.indexOf("vs");
var indexOfColon = description.indexOf(":");
var indexOfHyphen = description.lastIndexOf("-");
var l = description.length - 1;
var team1 = description.substring(0,indexOfVs-1).replace("#"," ").trim();
var team2 = description.substring(indexOfVs + 2,indexOfColon).replace("#"," ").trim();
var score = description.substring(indexOfColon + 1,indexOfHyphen-1).trim();
var keyMoment = description.substring(indexOfHyphen+1).trim();
return getResponseObject(team1,team2,score,keyMoment);
}else {
var title = match.title;
var indexOfBracket = title.indexOf(")");
title = title.substring(indexOfBracket + 1);
var indexOfVs = title.indexOf("vs");
var indexOfColon = title.indexOf(":");
var l = title.length - 1;
var team1 = title.substring(0, indexOfVs - 1).replace("#", " ").trim();
var team2 = title.substring(indexOfVs + 2, indexOfColon - 1).replace("#", " ").trim();
var score = title.substring(indexOfColon + 1).trim();
var keyMoment = match.description;
return getResponseObject(team1, team2, score, keyMoment);
}
}
function isTheMatch(match,sport,pattern1,pattern2){
var description;
if (sport == "soccer" || sport == "cricket"){
description = match.description;
}else{
description = match.title;
}
return description.search(pattern1)!=-1 && description.search(pattern2)!=-1;
}
exports.getCricketScores = function(team1,team2,callback) {
var pattern1 = new RegExp(team1,"i");
var pattern2 = new RegExp(team2,"i");
var sport = "cricket";
var match;
request.post({
url: "http://cricapi.com/api/cricket/",
form: { "apikey": null }
}, function(err, resp, data) {
if (err){
callback(err);
}
if(resp.statusCode == 200){
var matches = JSON.parse(data).data;
for (var i=0; i<matches.length;i++ ){
match = matches[i];
if (isTheMatch(match,sport,pattern1,pattern2)){
var response = extractCricketScores(match);
callback(err,response);
break;
}
}
if(i==matches.length){
callback(new Error("No match available for mentioned sport or Team"));
}
}else{
callback(new Error("CricApi Server responded BAD",resp.statusCode));
}});
};
exports.getOthersScores = function (sport,team1, team2,callback) {
var pattern1 = new RegExp(team1,"i");
var pattern2 = new RegExp(team2,"i");//case insensitive
var url = 'http://www.scorespro.com/rss2/live-' + sport +'.xml';
var match;
Feed.load(url, function(err, result){
if (err) {
callback(err);
}
var matches = result.items;
for (var i =0; i <matches.length ; i++){
match = matches[i];
if(isTheMatch(match,sport,pattern1,pattern2)){
var response = extractOtherScores(sport,matches[i]);
callback(err,response);
break;
}
}
if(i==matches.length) {
callback(new Error("No match available for mentioned sport or Team"));
}
});
};
exports.getAllCricketMatches = function(callback) {
var responseArray = [];
request.post({
url: "http://cricapi.com/api/cricket/",
form: { "apikey": null }
}, function(err, resp, data) {
if (err){
callback(err);
}
if(resp.statusCode == 200){
var matches = JSON.parse(data).data;
for (var i=0; i<matches.length;i++ ){
var matchInfo = extractCricketScores(matches[i]);
responseArray.push(matchInfo);
}
callback(err,responseArray);
}else{
callback(new Error("CricApi Server responded BAD",resp.statusCode));
}
});
}
exports.getAllOtherMatches = function(sport,callback) {
var url = 'http://www.scorespro.com/rss2/live-' + sport +'.xml';
var responseObject = [];
Feed.load(url, function(err, result){
if (err) {
callback(err);
}
var matches = result.items;
for (var i =0; i <matches.length ; i++){
var matchInfo = extractOtherScores(sport,matches[i]);
responseObject.push(matchInfo);
}
callback(err,responseObject);
});
}