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
|
const express = require('express')
const app = express()
const redis = require('redis')
const client = redis.createClient(process.env.REDIS_URL)
const moment = require('moment')
const request = require('request')
const aws = require('aws-sdk')
const s3 = new aws.S3()
function fetchImages() {
console.debug('Running fetchImages()')
request('http://map.govmap.gov.il/rainradar/radar.json', (error, response, body) => {
if (error) {
console.log(error)
}
else {
var res = JSON.parse(body)
for (let size of ['images140', 'images280']) {
var imgs = res[size]
Object.keys(imgs).forEach(function (img) {
const ts = img.split(':')
const url = 'http://' + imgs[img]
const key = `fetched:${url}`
client.get(key, (err, reply) => {
if (!reply && reply != 'fetching') {
console.debug(`Fetching ${url}`)
client.set(key, 'fetching')
request({ url: url, encoding: null }, (error, response, body) => {
if (err) {
console.log(err)
client.del(key)
}
else {
const r = size.slice(-3)
const d = ts.slice(0, 3).join('')
const t = ts.slice(3, 5).join('')
console.log(`Uploading to bucket`)
s3.putObject({
Bucket: 'imgs.geshem.space',
Key: `${d}/${t}/${r}.png`,
ContentType: response.headers['content-type'],
ContentLength: response.headers['content-length'],
Body: body,
ACL: 'public-read'
}, (err, data) => {
if (err) {
console.log(err)
client.del(key)
}
else {
client.multi([
['set', key, data.ETag.slice(1, -1)],
['del', 'images:latest']
]).exec()
}
})
}
})
}
else {
console.debug(`No need to fetch ${url}`)
}
})
})
}
}
})
}
app.set('port', (process.env.PORT || 3000));
app.set('view engine', 'pug')
app.set('views', __dirname + '/views');
app.use('/static', express.static(__dirname + 'static'))
app.use('/', function (req, res, next){
res.on('finish', function(){
client.get('fresh', (err, reply) => {
if (err) {
console.log(err)
}
else if (!reply) {
client.setex('fresh', 60, true)
fetchImages()
}
else {
console.debug('Skipping fetch, waiting 60 seconds max')
}
})
})
next()
});
app.get('/imgs', function (req, res) {
res.setHeader('Content-Type', 'application/json');
client.get('images:latest', (err, reply) => {
if (err) {
console.log(err)
}
else if (!reply) {
s3.listObjectsV2({
Bucket: 'imgs.geshem.space',
//StartAfter: moment().subtract(1, 'days').format('YYYYMMDD')
}, (err, data) => {
const imgs = data.Contents.slice(-20).map( (d) => d.Key)
const imgres = JSON.stringify({
'140': imgs.filter( (d) => d.endsWith('140.png') ),
'280': imgs.filter( (d) => d.endsWith('280.png') )
})
client.set('images:latest', imgres)
res.send(imgres)
})
}
else {
res.send(reply)
}
})
})
app.get('/', function (req, res) {
res.render('index', { prod: process.env.NODE_ENV === 'production' })
})
app.listen(app.get('port'), function () {
console.log('Starting geshem.space server on port', app.get('port'))
})
|