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