git ssb

0+

Dominic / weather



Tree: a6eb08d4725dc6c74cbb79d0bacb719c0fc3cf19

Files: a6eb08d4725dc6c74cbb79d0bacb719c0fc3cf19 / server.js

3936 bytesRaw
1
2var http = require('http')
3var Stack = require('stack')
4var route = require('tiny-route')
5var db = require('./db')
6var pull = require('pull-stream')
7var toPull = require('stream-to-pull-stream')
8var pl = require('pull-level')
9var stringify = require('pull-stringify')
10var urls = require('./urls')
11var marked = require('marked')
12
13function render(data, format, md) {
14 if(format === '.json')
15 return JSON.stringify(data, null, 2) + '\n\n'
16 if(format === '.md') return md(data)
17 return '<div id=content>'+marked(md(data))+'</div>'
18}
19
20function capitalize (n) {
21 return n[0].toUpperCase() + n.substring(1).toLowerCase()
22}
23function log (e) {
24 console.log(e)
25 return e
26}
27
28function sections (text) {
29 return log(text.split(/([\w,\s]+):/).map(function (e, i) {
30 if(!(i%2)) return e + '\n\n'
31 if(/\s/.test(e.trim())) return '### ' + e
32 else return '#### ' + e
33 })).join('\n')
34}
35
36function renderTides(tides) {
37 return tides.map(function (e) {
38 return '### ' + capitalize(e.location) + '\n' +
39 e.tides.map(function (tide) {
40 return '* ' + tide.height + 'm at ' + tide.time
41 }).join('\n')
42 }).join('\n\n')
43}
44
45function get(key, format, res, next) {
46 db.sublevel('raw').get(key, function (err, value) {
47 if(err) return next(err)
48
49 if(value.forecast) //render a weather forecast
50 res.end(render(value, format, function (e) {
51 return [
52 '# ' + capitalize(value.name),
53 'issued at: [' + value.issued +'](/ls/'+value.name+')',
54 '## Situation',
55 e.situation,
56 '## Forecast',
57 sections(e.forecast),
58 '## Outlook',
59 sections(e.outlook),
60 e.swell ? '## Swell\n\n' + e.swell : '',
61 e.tides ? '## Tides\n\n' + renderTides(e.tides) : '',
62 //oceanic forecasts
63 e.warnings ? '## Warnings\n\n' +
64 e.warnings.map(function (e) {
65 return '### warning: '+e.name + ' (Analysis ' + e.analysis+')\n\n' +
66 e.warning.replace(/<br\/>/g, '\n\n') + '\n\n issued at:' + e.issued
67 }).join('\n\n') : ''
68 ].join('\n')
69 }))
70 else if(value.imageData)
71 res.end(render(value, format, function (e) {
72 return e.imageData.map(function (e) {
73 return '## ' + e.description + '\n\n'
74 + '![e.description](http://metservice.com' + e.url + ')\n'
75 + '\nissued at:' + e.issuedTime + ' valid from:' + e.validFromTime
76 }).join('\n\n\n')
77 }))
78
79 })
80}
81
82http.createServer(Stack(
83 route(/^\/([0-9a-f]{64})(\.\w+)?$/, function (req, res, next) {
84 get(req.params[0], req.params[1], res, next)
85 }),
86 route(/^\/([a-z\-_]+)(\.\w+)?$/, function (req, res, next) {
87 db.get(req.params[0], function (err, value) {
88 if(err) return next(err)
89 get(value, req.params[1], res, next)
90 })
91 }),
92 route(/^\/ls\/([a-z\-_]+)(\.\w+)?$/, function (req, res, next) {
93 var place = req.params[0]
94 pull(
95 pl.read(db.sublevel('sample'), {gte: [place, null], lt: [place, undefined], reverse: true}),
96 pull.collect(function (err, data) {
97 res.end(render(data, req.params[1], function (e) {
98 return '# history for:' + capitalize(e[0].key[0]) + '\n\n' +
99 e.map(function (e) {
100 return '* [' + new Date(e.key[1]) + '](/'+e.value+')'
101 }).join('\n')
102 }))
103 })
104 )
105 }),
106 route(/^\/_admin\/update$/, function (req, res, next) {
107 db.update(function (err, data) {
108 if(err) return next(err)
109 return res.end(JSON.stringify(data, null, 2))
110 })
111 }),
112 route(/^\/(\.\w+)?$/, function (req, res) {
113 res.end(render(Object.keys(urls), req.params[0], function (data) {
114 return data.map(function (e) {
115 return '* ['+e+'](/'+e+')'
116 }).join('\n')
117 }))
118 })
119)).listen(8000)
120//
121//db.update(function (err, data) {
122// console.log(data)
123//})
124
125setInterval(function () {
126 db.update(console.log)
127}, 1000*60*60)
128
129
130
131
132
133
134
135
136
137
138
139

Built with git-ssb-web