diff options
| author | Yuval Adam <_@yuv.al> | 2017-10-21 16:52:41 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2017-10-21 16:52:41 +0300 |
| commit | 2bd9dea6e0dbf22af1893f8963a6e8d872daa1d4 (patch) | |
| tree | b365b2e6822692f247da80b1359b8fa3c55a7867 /server.js | |
| parent | 1a0e8e97f2000186045cb45fee2b69ce9cffdf5f (diff) | |
Implement fetchLatestImages()
Diffstat (limited to 'server.js')
| -rw-r--r-- | server.js | 60 |
1 files changed, 60 insertions, 0 deletions
@@ -1,14 +1,74 @@ const express = require('express') const app = express() +const redis = require('redis') +const client = redis.createClient(process.env.REDIS_URL) + +const request = require('request') +const aws = require('aws-sdk') +const s3 = new aws.S3() + +function fetchImages() { + request('http://map.govmap.gov.il/rainradar/radar.json', (error, response, body) => { + 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') { + client.set(key, 'fetching') + request({ url: url, encoding: null }, (error, response, body) => { + if (err) { + console.log(err) + client.del(key) + } + else { + const d = ts.slice(0, 3).join('') + const t = ts.slice(3, 5).join('') + console.log(`Uploading ${url} to bucket`) + s3.putObject({ + Bucket: 'imgs.geshem.space', + Key: `${d}_${t}.png`, + ContentType: response.headers['content-type'], + ContentLength: response.headers['content-length'], + Body: body + }, (err, data) => { + if (err) { + console.log(err) + client.del(key) + } + else { + client.set(key, data.ETag.slice(1, -1)) + } + }) + } + }) + } + }) + }) + } + }) +} + app.set('view engine', 'pug') app.use('/static', express.static('static')) +app.use('/', function(req, res, next){ + res.on('finish', function(){ + console.log('hello world!') + }) + next() +}); + app.get('/', function (req, res) { res.render('index', { prod: process.env.NODE_ENV === 'production' }) }) app.listen(3000, function () { console.log('Example app listening on port 3000!') + fetchImages() }) |
