explorer-data/utils/w3pdata.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-10-07 20:48:35 +02:00
import yaml from "npm:js-yaml";
2023-10-07 20:01:09 +02:00
2023-12-20 12:02:27 +01:00
const DATA_URL = 'https://explorer-data.web3privacy.info'
2023-10-08 04:04:22 +02:00
2023-10-07 20:01:09 +02:00
export class W3PData {
2023-10-07 20:48:35 +02:00
constructor() {
}
2023-10-07 20:01:09 +02:00
2023-10-07 20:48:35 +02:00
async init() {
this.data = await this.load("./src");
}
2023-10-07 20:01:09 +02:00
2023-10-07 20:48:35 +02:00
async load(dataDir) {
const out = {};
for await (const f of Deno.readDir(dataDir)) {
if (f.isFile && f.name.match(/\.yaml$/)) {
const name = f.name.split(".")[0];
out[name] = await this.loadYaml(`${dataDir}/${f.name}`);
}
if (f.isDirectory && f.name === "projects") {
out.projects = [];
const projectsDir = `${dataDir}/${f.name}`;
2023-10-20 05:02:35 +02:00
for await (const pd of Deno.readDir(projectsDir)) {
if (!pd.isDirectory) {
continue;
}
const pDir = `${dataDir}/${f.name}/${pd.name}`;
const indexFn = `${pDir}/index.yaml`;
const index = Object.assign({ id: pd.name }, yaml.load(
await Deno.readTextFile(indexFn),
))
2023-10-08 04:04:22 +02:00
2023-10-20 05:02:35 +02:00
index._path = pDir
// read attachments
const logos = []
for await (const pa of Deno.readDir(pDir)) {
const pam = pa.name.match(/^(logo)\.(.+)$/)
if (pam && pam[1] === 'logo') {
logos.push({ file: pam[0], ext: pam[2], url: `${DATA_URL}/assets/projects/${index.id}/${pam[0]}` })
2023-10-08 04:04:22 +02:00
}
2023-10-07 20:48:35 +02:00
}
2023-10-20 05:02:35 +02:00
if (logos.length > 0) {
index.logos = logos
}
out.projects.push(index);
2023-10-07 20:01:09 +02:00
}
2023-10-07 20:48:35 +02:00
}
2023-10-07 20:01:09 +02:00
}
2023-10-07 20:48:35 +02:00
return out;
}
async loadYaml(f) {
return yaml.load(await Deno.readTextFile(f));
}
}