Files: cec705118d65476d8ba5b903fb836cae42f2f1dd / run.js
2163 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 info1 = JSON.parse(fs.readFileSync(path.join(__dirname, 'ids-colors.json'))) |
38 | var ids = info1.ids |
39 | var userColors = info1.colors |
40 | |
41 | var width |
42 | var height = info.max |
43 | |
44 | var png = new PNG({ |
45 | width: width, |
46 | height: height, |
47 | colorType: 6 |
48 | }) |
49 | |
50 | var userNumbers = {} |
51 | for (var i = 0; i < ids.length; i++) { |
52 | var id = ids[i] |
53 | userNumbers[id] = i |
54 | } |
55 | |
56 | function setPixel(data, x, y, color) { |
57 | var i = (y * width + x) << 2 |
58 | data[i] = color[0] |
59 | data[i+1] = color[1] |
60 | data[i+2] = color[2] |
61 | data[i+3] = color[3] |
62 | } |
63 | |
64 | function compareIds(a, b) { |
65 | return userNumbers[a] - userNumbers[b] |
66 | } |
67 | |
68 | pull( |
69 | File(path.join(__dirname, series + '.jsonp')), |
70 | Split(), |
71 | pull.filter(Boolean), |
72 | pull.map(JSON.parse), |
73 | pull.drain(function (interval) { |
74 | var x = interval.time - startT |
75 | var ids = interval.ids.sort(compareIds) |
76 | for (var i = 0; i < ids.length; i++) { |
77 | var id = ids[i] |
78 | var y = height - i |
79 | var color = userColors[id] || [0, 0, 0, 255] |
80 | setPixel(png.data, x, y, color) |
81 | } |
82 | }, function (err) { |
83 | if (err) throw err |
84 | png.pack().pipe(fs.createWriteStream(series + '.png')) |
85 | }) |
86 | ) |
87 |
Built with git-ssb-web