-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
97 lines (88 loc) · 2.62 KB
/
main.js
File metadata and controls
97 lines (88 loc) · 2.62 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
'use strict'
//for server
const http = require('http');
const port = process.env.PORT || 3000;
//for scraping job
const fs = require('fs');
const {PythonShell} = require('python-shell');
const scriptPath = "./scraper/webScraper.py";
//for formating
const formatter = require('./format.js');
const csvFilePath = './stats/players.csv';
const advCsvFilePath = './stats/Advplayers.csv';
const playerURLs = './stats/URLplayers.csv';
const csv = require('csvtojson');
//s3 storage
const aws = require('aws-sdk');
const S3_BUCKET = process.env.S3_BUCKET;
aws.config.region = 'us-east-1';
aws.config.update({ accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY });
dailyScrape();
//create server
http.createServer(function(req, res) { //make this a callable function to run after dailyscrape to make sure updates are had.
const s3 = new aws.S3()
s3.getObject({
Bucket: "quickstatsbacknbadatabucket",
Key: "nbaCurrentPlayerData"
}, function (err, data) {
if(err){
console.log(err)
}
else {
let s3Stats = JSON.parse(data.Body.toString());
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.write(JSON.stringify(s3Stats));
res.end()
}
})
}).listen(port, function() {
console.log("server started");
});
function dailyScrape() {
const pyshell = new PythonShell(scriptPath);
const scrapeDate = new Date();
pyshell.on('message', function(message) {
console.log(message);
});
pyshell.end(function (err) {
if(err){
throw err;
}
//get csvs from scrape and format to be sent out
csv()
.fromFile(csvFilePath)
.then((jsonObj) => {
csv()
.fromFile(advCsvFilePath)
.then((advJsonObj) => {
csv()
.fromFile(playerURLs)
.then((playerURLsObj) => {
const stats = formatter.format(jsonObj, advJsonObj, playerURLsObj); //reassign stats to newly scraped and formatted stats
const s3 = new aws.S3();
s3.putObject({
Bucket: "quickstatsbacknbadatabucket",
Key: 'nbaCurrentPlayerData',
Body: JSON.stringify(stats),
ContentType: "application/json"
},
function (err, data) {
if(err){
console.log(err)
}
else {
console.log(data)
}
}
)
});
});
});
console.log("scraped and uploaded on/at " + scrapeDate);
})
}
let CronJob = require('cron').CronJob;
new CronJob('00 04 * * *', function() {
dailyScrape();
}, null, true, 'America/Denver');