Files: ba8080319c06284e2f8fe71cddad735d3897657a / run.js
2149 bytesRaw
1 | var fs = require('fs') |
2 | var path = require('path') |
3 | var pull = require('pull-stream') |
4 | var File = require('pull-file') |
5 | var Split = require('pull-split') |
6 | var PNG = require('pngjs').PNG |
7 | var common = require('./common') |
8 | |
9 | var start = common.startTime |
10 | var startDay = Math.floor(start / 86400000) |
11 | var startWeek = Math.floor(start / 86400000 / 7) |
12 | var startMonth = Math.floor(start / 86400000 / 30.43666666666666666666) |
13 | var startT |
14 | |
15 | var now = Date.now() |
16 | var nowDay = Math.floor(now / 86400000) |
17 | var nowWeek = Math.floor(now / 86400000 / 7) |
18 | var nowMonth = Math.floor(now / 86400000 / 30.43666666666666666666) |
19 | |
20 | var series = process.argv[2] |
21 | if (!series) throw new TypeError('Missing series argument') |
22 | |
23 | if (series === 'day') { |
24 | width = nowDay - startDay |
25 | startT = startDay |
26 | } else if (series === 'week') { |
27 | width = nowWeek - startWeek |
28 | startT = startWeek |
29 | } else if (series === 'month') { |
30 | width = nowMonth - startMonth |
31 | startT = startMonth |
32 | } else { |
33 | throw new TypeError('Invalid series') |
34 | } |
35 | |
36 | var info = JSON.parse(fs.readFileSync(path.join(__dirname, series + '.json'))) |
37 | var ids = JSON.parse(fs.readFileSync(path.join(__dirname, 'ids.json'))) |
38 | |
39 | var width |
40 | var height = info.max |
41 | |
42 | var png = new PNG({ |
43 | width: width, |
44 | height: height, |
45 | colorType: 6 |
46 | }) |
47 | |
48 | var userColors = common.computeIdColors(ids) |
49 | var userNumbers = {} |
50 | for (var i = 0; i < ids.length; i++) { |
51 | var id = ids[i] |
52 | userNumbers[id] = i |
53 | } |
54 | |
55 | function setPixel(data, x, y, color) { |
56 | var i = (y * width + x) << 2 |
57 | data[i] = color[0] |
58 | data[i+1] = color[1] |
59 | data[i+2] = color[2] |
60 | data[i+3] = color[3] |
61 | } |
62 | |
63 | function compareIds(a, b) { |
64 | return userNumbers[a] - userNumbers[b] |
65 | } |
66 | |
67 | pull( |
68 | File(path.join(__dirname, series + '.jsonp')), |
69 | Split(), |
70 | pull.filter(Boolean), |
71 | pull.map(JSON.parse), |
72 | pull.drain(function (interval) { |
73 | var x = interval.time - startT |
74 | var ids = interval.ids.sort(compareIds) |
75 | for (var i = 0; i < ids.length; i++) { |
76 | var id = ids[i] |
77 | var y = height - i |
78 | var color = userColors[id] || [0, 0, 0, 255] |
79 | setPixel(png.data, x, y, color) |
80 | } |
81 | }, function (err) { |
82 | if (err) throw err |
83 | png.pack().pipe(fs.createWriteStream(series + '.png')) |
84 | }) |
85 | ) |
86 |
Built with git-ssb-web