anime-database-sync/index.js

110 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-05-23 22:46:06 +02:00
import fetch from 'node-fetch';
import fs from 'fs-extra';
import path from 'path';
import {markdownTable} from 'markdown-table';
2021-06-15 09:52:18 +02:00
async function start() {
fs.removeSync('data');
2022-05-23 22:46:06 +02:00
const stats = await backup('/page/export', 'pages', 'anime', 1, {}, {});
2021-06-15 09:52:18 +02:00
await backup('/mal/export', 'myanimelist');
2022-05-23 22:46:06 +02:00
await backup('/anilist/export', 'anilist');
await updateReadme(stats);
2021-06-15 09:52:18 +02:00
}
2022-05-23 22:46:06 +02:00
async function backup(url, name, type = 'anime', page = 1, ids = {}, stats = null) {
return await getData(url + '/' + type + '/' + page).then(async (json) => {
2021-06-15 09:52:18 +02:00
const promises = json.data.map(data => {
if(!ids[data.page ?? type]) {
ids[data.page ?? type] = [];
}
ids[data.page ?? type].push(data.identifier ?? data.id);
2022-05-23 22:46:06 +02:00
if(name === 'pages') {
if(!stats[data.page]) {
stats[data.page] = {
total: 0,
mal: 0,
noMal: 0,
ani: 0,
noAni: 0
}
}
stats[data.page].total += 1;
if(data.malId) {
stats[data.page].mal += 1;
} else {
stats[data.page].noMal += 1;
}
if(data.aniId) {
stats[data.page].ani += 1;
} else {
stats[data.page].noAni += 1;
}
}
2021-06-15 09:52:18 +02:00
return fs.outputFile(`data/${name}/${data.page ?? type}/${data.identifier ?? data.id}.json`, JSON.stringify(data, null, 2))
})
await Promise.all(promises);
if(json.next) {
2022-05-23 22:46:06 +02:00
return await backup(url, name, type, page + 1, ids, stats);
2021-06-15 09:52:18 +02:00
} else {
2021-06-15 17:29:58 +02:00
if(type === 'anime') {
2022-05-23 22:46:06 +02:00
return await backup(url, name, 'manga', 1, ids, stats);
2021-06-15 17:29:58 +02:00
}
2022-05-23 22:46:06 +02:00
for(const item in ids) {
await fs.outputFile(`data/${name}/${item}/_index.json`, JSON.stringify(ids[item].sort(), null, 2));
2021-06-15 09:52:18 +02:00
}
2021-06-15 17:29:58 +02:00
if(name === 'pages') {
2022-05-23 22:46:06 +02:00
await fs.outputFile(`data/${name}/_index.json`, JSON.stringify(Object.keys(ids).sort(), null, 2));
return stats;
2021-06-15 09:52:18 +02:00
}
return true;
}
2022-05-23 22:46:06 +02:00
}).catch((e) => {
throw 'backup error'
});
2021-06-15 09:52:18 +02:00
}
async function getData(url) {
return fetch(process.env.DOMAIN + url, {
headers: {
2022-05-23 22:46:06 +02:00
'Authorization': "Permanent " + process.env.ACCESS_TOKEN
2021-06-15 09:52:18 +02:00
}
}).then(res => {
if(res.status !== 200) {
throw "response status not 200";
}
return res.json()
})
}
2022-05-23 22:46:06 +02:00
async function updateReadme(statsData) {
let statsArray = [['Page', 'Total', 'MalID', 'noMalID', 'AniID', 'noAniID']];
2021-06-15 09:52:18 +02:00
2022-05-23 22:46:06 +02:00
for (const pageName in statsData) {
const stats = statsData[pageName];
statsArray.push([pageName, stats.total, stats.mal, stats.noMal, stats.ani, stats.noAni])
}
2021-06-15 09:52:18 +02:00
2022-05-23 22:46:06 +02:00
const statstable = markdownTable(statsArray);
const descFile = path.resolve('./README.md');
2021-06-15 09:52:18 +02:00
2022-05-23 22:46:06 +02:00
fs.readFile(descFile, 'utf8', function(err, data) {
if (err) {
throw err;
}
const result = data.replace(/<!--statstable-->((.|\n|\r)*)<!--\/statstable-->/g, `<!--statstable-->\n${statstable}\n<!--/statstable-->`);
2021-06-15 09:52:18 +02:00
2022-05-23 22:46:06 +02:00
fs.writeFile(descFile, result, 'utf8', function(err) {
if (err) throw err;
2021-06-15 09:52:18 +02:00
});
});
}
process.on('unhandledRejection', err => {
console.error(err);
process.exit(1);
});
2021-06-15 17:29:58 +02:00
start();