-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
142 lines (118 loc) · 4.82 KB
/
app.js
File metadata and controls
142 lines (118 loc) · 4.82 KB
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
'use strict';
var express = require("express");
var fs = require("fs");
var request = require("request");
var cheerio = require("cheerio");
var app = express();
var cors = require("cors");
var Fifa = require("./lib/Fifa");
var Observer = require("./lib/Observer");
var url = "http://www.fifa.com/worldcup/matches/index.html";
var IPs = new Observer.IPCollection();
// Allows for cross origin requests
app.use(cors());
// Getting matchup data everyday leading up to today
app.get("/api/today", function(req, res) {
console.log("GET Request from %s", req.ip);
var user = new Observer.User(req.ip);
IPs.push(user);
// Checking for local data
fs.exists("./public/assets/data/fifa-matchup-data.json", function(exists) {
if (!exists) {
console.log("No data found. Scraping for latest Fifa matchup data.");
request(url, function(err, response, html) {
if (!err && response.statusCode === 200) {
var matchData = Fifa.today(html);
var date = Fifa.date();
Fifa.setTime(date);
Fifa.saveToday(matchData);
Fifa.exporter(Fifa.getTodayData());
res.send(matchData);
} else {
throw err;
}
});
} else {
var curr = Fifa.date();
// Checking if local data is up-to-date
if (curr > Fifa.last()) {
console.log('Data is outdated. Updating to latest FIFA match data.');
request(url, function(err, response, html) {
if (!err && response.statusCode === 200) {
var matchData = Fifa.today(html);
var date = Fifa.date();
Fifa.setTime(date);
Fifa.saveToday(matchData);
Fifa.exporter(Fifa.getTodayData());
res.send(matchData);
} else {
throw err;
}
});
return;
} else {
fs.readFile("./public/assets/data/fifa-matchup-data.json", {
encoding: "utf8"
}, function(err, data) {
// File should exist since it just checked
// Another layer of error checking just in case
if (err) {
console.log('Local match data copy not found unexpectedly...');
var matchData = Fifa.today(html);
var date = Fifa.date();
Fifa.setTime(date);
Fifa.saveToday(matchData);
Fifa.exporter(Fifa.geTodayData());
res.send(Fifa.getData());
} else {
console.log("Sending local data...");
res.send(data);
console.log("Local data successfully sent.");
}
});
}
}
});
});
// Gets all matchup data > today
app.get('/api/tomorrow', function(req, res) {
console.log('GET request from %s', req.ip);
var user = new Observer.User(req.ip);
IPs.push(user);
fs.exists("./public/assets/data/fifa-future-matchup-data.json", function(exists) {
if (!exists) {
console.log('No local data found. Searching for future match data.');
request(url, function(err, response, html) {
if (!err && response.statusCode === 200) {
var matchData = Fifa.tomorrow(html);
var date = Fifa.date();
Fifa.saveTomorrow(matchData);
Fifa.exportTomorrow(Fifa.getTomorrowData());
res.send(Fifa.getTomorrowData());
}
});
} else {
fs.readFile("./public/assets/data/fifa-future-matchup-data.json", {
encoding: "utf8"
}, function(err, data) {
if (err) {
console.log('Local future match data copy not found unexpectedly...');
var matchData = Fifa.today(html);
var date = Fifa.date();
Fifa.setTime(date);
Fifa.saveToday(matchData);
Fifa.exporter(Fifa.geTodayData());
res.send(Fifa.getData());
} else {
console.log("Sending local future data...");
res.send(data);
console.log("Local future data successfully sent.");
}
});
}
});
});
var server = app.listen(8081, function() {
console.log('Awaiting connections on port %d', server.address().port);
});
exports = module.exporters = app;