-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.coffee
71 lines (56 loc) · 1.59 KB
/
main.coffee
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
EventEmitter = require('events').EventEmitter
http = require 'http'
express = require 'express'
request = require 'superagent'
_ = require 'lodash'
port = 8080
langs = ['en','de','it']
caches = {}
refresh_caches_period = 1*60*1000
app = express()
app.use express.compress()
app.use (req,res,next)->
if req.acceptedLanguages.length == 0
req.cache = caches[langs[0]]
else
for al in req.acceptedLanguages
if al in langs
req.cache = caches[al]
break
if !req.cache?
res.status(404).end()
else
next()
app.get '/sports', (req,res)->
ordered_sports = _.sortBy req.cache.sports, 'pos'
result = _.map ordered_sports, (s)->
id: s.id
title: s.title
res.json result
app.get '/sports/:id', (req,res)->
sport = _.find(req.cache.sports, id:+req.param('id'))
ordered_events = _.sortBy sport?.events, 'pos'
result = _.map ordered_events, (e)->
id: e.id
title: e.title
res.json result
app.get '/sports/:sport_id/events/:event_id', (req,res)->
sport = _.find(req.cache.sports, id:+req.param('sport_id'))
event = _.find(sport?.events, id:+req.param('event_id'))
result = event?.outcomes
res.json result
module.exports = new EventEmitter()
ready = _.after langs.length, _.once ->
http.createServer(app).listen(port)
console.log "port #{ port } is up."
module.exports.emit('ready')
refresh_caches = ->
for lang in langs
((lang)->
request.get("http://www.betvictor.com/live/#{ lang }/live/list.json").end (res)->
# console.log "#{lang} : #{res.body}"
caches[lang] = res.body if res.ok
ready()
)(lang)
refresh_caches()
setInterval refresh_caches, refresh_caches_period