UNPKG

@guoyunhe/rgi-libretro

Version:

CLI to export RetroGameIndex images to libretro-thumbnails

100 lines (97 loc) 3.89 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { downloadThumbnails: () => downloadThumbnails }); module.exports = __toCommonJS(src_exports); // src/downloadThumbnails.ts var import_downloader = require("@guoyunhe/downloader"); var import_axios = __toESM(require("axios")); var import_crypto = require("crypto"); var import_fs = require("fs"); var import_promises = require("fs/promises"); var import_path = require("path"); var typesDict = { boxart: "Named_Boxarts", snap: "Named_Snaps", title: "Named_Titles" }; function normalize(file) { return file.replaceAll("&", "_"); } async function downloadThumbnails(platform, { apiUrl = "https://rgi-api.guoyunhe.me/" }) { import_axios.default.defaults.baseURL = apiUrl; const { data: platforms } = await import_axios.default.get("/platforms"); const platformId = platforms.find((p) => p.code === platform).id; for (let i = 1; i < 999; i++) { const res = await import_axios.default.get("/games", { params: { page: i, perPage: 50, platform: platformId } }); const games = res.data.data; for (let j = 0; j < games.length; j++) { const game = games[j]; for (let k = 0; k < 3; k++) { const [type, folder] = Object.entries(typesDict)[k]; const image = game.images.find((img) => img.type === type); if (image) { const imagePath = (0, import_path.join)(folder, normalize(game.name) + ".png"); if ((0, import_fs.existsSync)(imagePath)) { const buffer = await (0, import_promises.readFile)(imagePath); const hash = (0, import_crypto.createHash)("md5").update(buffer).digest("hex"); if (image.path === "images/" + hash + ".png") { continue; } } console.log("download", image.url); await (0, import_downloader.download)(image.url, imagePath); for (let s = 0; s < game.subs.length; s++) { const sub = game.subs[s]; const target = "./" + normalize(game.name) + ".png"; const link = (0, import_path.join)(folder, normalize(sub.name) + ".png"); await (0, import_promises.rm)(link, { force: true }); await (0, import_promises.symlink)(target, link); } } } } if (!res.data.meta.nextPageUrl) { break; } } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { downloadThumbnails });