-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch-data.js
85 lines (71 loc) · 2.89 KB
/
fetch-data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import fs from 'node:fs/promises';
import yaml from 'js-yaml';
import * as core from '@actions/core';
import { PNG } from 'pngjs';
import { Buffer } from 'node:buffer';
const PAGES_DIRECTORY_PATH = './_site/';
const data = [];
for (const url of yaml.load(await fs.readFile(new URL('satellites.yaml', import.meta.url), { encoding: 'utf-8' }))) {
const responseText = await (await fetch(url)).text();
if (!responseText.includes('\n')) {
// 改行が含まれていなければ、404とみなす
core.error(`${url} から「${responseText}」が返りました。`);
continue;
}
data.push(await (await fetch(url)).text());
}
const pagesDirectory = new URL(PAGES_DIRECTORY_PATH, import.meta.url);
await fs.mkdir(pagesDirectory, { recursive: true });
await fs.writeFile(new URL('satellites.txt', pagesDirectory), data.join(''));
// generate png
const blockSize = 4;
const targetSize = 128;
const json = (await fetch('https://celestrak.org/NORAD/elements/gp.php?GROUP=active&FORMAT=json')).text();
const obj = JSON.parse(await json);
const buff = Buffer.alloc(targetSize * blockSize * targetSize * blockSize * 4);
const currentTime = Date.now();
const currentTimeIncsThick = BigInt(Date.now()) * 10000n + 621355968000000000n;
const buff2 = Buffer.alloc(8);
buff2.writeBigInt64BE(currentTimeIncsThick);
for (let i = 0; i < 2; i++) {
for (let j = 0; j < 4; j++) {
buff[(i * blockSize + 3 + (targetSize - 1) * targetSize * blockSize * blockSize) * 4 + j] = buff2[i * 4 + j];
}
}
let idx = 0;
const writeFloat = (buf, val, idx, x, y) => {
const ix = idx % targetSize;
const iy = targetSize - Math.floor(idx / targetSize) - 1;
const pos = ix * blockSize * 4 + iy * targetSize * blockSize * blockSize * 4;
const offset = x * 4 + (blockSize - y - 1) * targetSize * blockSize * 4;
buf.writeFloatBE(val, pos + offset);
};
const writebyte = (buf, val, idx, x, y) => {
const ix = idx % targetSize;
const iy = targetSize - Math.floor(idx / targetSize) - 1;
const pos = ix * blockSize * 4 + iy * targetSize * blockSize * blockSize * 4;
const offset = x * 4 + (blockSize - y - 1) * targetSize * blockSize * 4;
buf[pos + offset] = val;
};
for (const sat of obj) {
const ep = (currentTime - Date.parse(sat.EPOCH + 'Z')) / 1000;
const isStarlink = (sat.OBJECT_NAME.indexOf('STARLINK') !== -1);
writeFloat(buff, ep, idx, 0, 0);
writeFloat(buff, sat.INCLINATION, idx, 0, 1);
writeFloat(buff, sat.RA_OF_ASC_NODE, idx, 0, 2);
writeFloat(buff, sat.ECCENTRICITY, idx, 0, 3);
writeFloat(buff, sat.ARG_OF_PERICENTER, idx, 1, 0);
writeFloat(buff, sat.MEAN_ANOMALY, idx, 1, 1);
writeFloat(buff, sat.MEAN_MOTION, idx, 1, 2);
writeFloat(buff, sat.BSTAR, idx, 1, 3);
writebyte(buff, isStarlink ? 2 : 1, idx, 2, 0);
idx++;
}
const png = new PNG({
colorType: 6,
bitDepth: 8,
width: targetSize * blockSize,
height: targetSize * blockSize,
});
png.data = buff;
await fs.writeFile(new URL('satellites.png', pagesDirectory), PNG.sync.write(png));