-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (50 loc) · 1.48 KB
/
server.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
var http = require('http')
var dispatcher = require('httpdispatcher')
var request = require('request')
var movesApi = require('moves-api').MovesApi
var moves = new movesApi()
var fs = require('fs')
var dbApi = require('./dbApi')
var server = http.createServer(handleRequest)
var port = 1243
var config = require('./config')
moves.options.accessToken = config.accessToken
crawlUserdata()
server.listen(port, function(){
console.log("Server listening on: http://localhost:%s", port)
})
function handleRequest(request, response){
try {
console.log(request.url)
dispatcher.dispatch(request, response)
} catch(err) {
console.log(err)
}
}
dispatcher.onPost('/api/rest', function(req,res){
res.writeHead(200, {'Content-Type': 'application/json'})
res.write(JSON.stringify(req.params))
res.end()
})
function updateUserData(){
crawlUserdata()
setTimeout(function(){
crawlUserdata()
},86400000)
}
function crawlUserdata(){
var dateObj = new Date()
var month = (dateObj.getUTCMonth() + 1).toString() //months from 1-12
month=("0"+month).slice(-2)
var day = (dateObj.getUTCDate()-4).toString()
day = ("0"+day).slice(-2)
var year = (dateObj.getUTCFullYear()).toString()
var time = ''
console.log(month,day,year)
time = year+month+day
moves.getStoryline(time, function(err,data){
// console.log(JSON.stringify(data))
fs.writeFileSync('./test.txt', JSON.stringify(data,null,2))
dbApi.savePlaces(data[0].segments)
})
}