-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdailyapi.js
58 lines (52 loc) · 1.45 KB
/
dailyapi.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
#!/usr/bin/env node
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
httpRequest = require('request'),
Baby = require('babyparse'),
mongoose = require('mongoose');
mongoose.Promise = global.Promise,
mongoose.createConnection(process.env.MONGODB_URI ||process.env.MONGOLAB_URI||
process.env.MONGOHQ_URL);
var db = require('./models');
console.log('working...')
var date = new Date(),
dd = date.getDate()-1, //yesterdays info
mm = date.getMonth()+1,
yyyy = date.getFullYear(),
url = 'http://hedonometer.org/data/shifts/world/',
shift = '-shift.csv',
metashift = '-metashift.csv';
var getShift = url+date+shift,
getMetashift = url+date+metashift,
storeObj={};
function getCsv(url,callback){
httpRequest.get(url, function (err, res, body) {
if (!err && res.statusCode == 200) {
callback(body);
}
})
}
function babyParseFunc (url,which) {
getCsv(url,function(res){
var parsed = Baby.parse(res).data;
if(which=='shift'){
storeObj.shift=parsed;
console.log(typeof storeObj.shift)
} else if(which=='meta'){
storeObj.meta=parsed;
console.log(typeof storeObj.meta)
}
if(storeObj.shift!== undefined &&storeObj.meta!== undefined){
storeObj.date = date;
console.log(storeObj)
db.DailyInfo.create(storeObj)
process.exit();
}
});
}
function getHedoData(){
babyParseFunc(getShift,'shift') //babyfunc -> getCsv = returned
babyParseFunc(getMetashift,'meta')
}
getHedoData();