UNPKG

osu-api-extended

Version:

Advanced osu! api wrapper cover all V2 and V1 endpoints, and provide useful tools

207 lines (206 loc) 8.34 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.beatmaps_download = void 0; const request_1 = require("../../utility/request"); const auth_1 = require("../../utility/auth"); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const handleErrors_1 = require("../../utility/handleErrors"); /** * `async` Downloads a beatmap or beatmap set by given ID. (Supports different hosts) * *   * * ### Available hosts * - For `type:'difficulty'`: osu, osu_direct_mirror, catboy * - For `type:'set'`: osu, beatconnect, nerinyan, osu_direct_mirror, sayobot, gatari, ripple, catboy * *   * * ### Global Parameters * - `params.type` - Type of the beatmap. * - `params.id` - ID of the beatmap or beatmap set. * - `params.host` - Host of the download source. * - `params.file_path` - Path to the save location. * - `params.overwrite` - Whether to overwrite the file if it already exists. * *   * * ### Parameters for `params.type:'set'` * - `params.no_video?` - Whether to include video in the download. * - `params.progress_log_fn?` - Callback function to send progress. * *   * * ### Usage Example * ```js * const { auth, v2, tools } = require('osu-api-extended'); * * async function main() { * try { * // only for downloading sets from osu host * await auth.login({ * type: 'lazer', * login: login, * password: password, * cachedTokenPath: './test.json' // path to the file your auth token will be saved (to prevent osu!api spam) * }); * * * const progress_update = (...args) => { * console.log(args); * }; * const set_id = 320118; * * * const result = await v2.beatmaps.download({ * type: 'set', * host: 'gatari', * id: set_id, * file_path: `./cache/${set_id}.osz`, * progress_log_fn: progress_update * }); * // or * const result = await tools.download_beatmaps({ * type: 'set', * host: 'gatari', * id: set_id, * file_path: `./cache/${set_id}.osz`, * progress_log_fn: progress_update * }); * * console.log(result); * } catch (error) { * console.log(error); * }; * }; * * main(); * ``` * *   * * [See documentation](https://github.com/cyperdark/osu-api-extended/wiki/v2.beatmaps.download_v3) */ const beatmaps_download = (params, addons) => __awaiter(void 0, void 0, void 0, function* () { const { dir } = path_1.default.parse(params.file_path); if (!fs_1.default.existsSync(dir)) fs_1.default.mkdirSync(dir, { recursive: true }); if (fs_1.default.existsSync(params.file_path) && params.overwrite != true) { return { status: 'File already exists' }; } ; const progress_track = (progress) => { params.progress_log_fn(params.host, progress); }; let url = ''; let headers = { accept: 'application/octet-stream', }; if (params.type == 'set') { switch (params.host) { // case 'chimu': // url = `https://api.chimu.moe/v1/download/${params.id}`; // break; case 'beatconnect': url = `https://beatconnect.io/b/${params.id}/`; break; case 'sayobot': url = `https://dl.sayobot.cn/beatmaps/download/${params.no_video ? 'novideo' : 'full'}/${params.id}`; break; // DOESNT WORK FOR SOME REASON case 'nerinyan': url = `https://api.nerinyan.moe/d/${params.id}${params.no_video ? '?nv=1' : ''}`; headers['Accept'] = 'application/x-osu-beatmap-archive'; headers['Referer'] = 'https://nerinyan.moe/'; headers['User-Agent'] = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36`; break; case 'osu_direct_mirror': url = `https://osu.direct/api/d/${params.id}`; // headers['User-Agent'] = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36`; break; case 'gatari': url = `https://osu.gatari.pw/d/${params.id}`; headers['Referer'] = 'https://osu.gatari.pw/'; break; case 'ripple': url = `https://storage.ripple.moe/d/${params.id}`; headers['Referer'] = 'https://ripple.moe/'; break; case 'akatsuki': url = `https://akatsuki.gg/d/${params.id}`; headers['Referer'] = 'https://akatsuki.gg/'; break; case 'mino': case 'catboy': url = `https://catboy.best/d/${params.id}`; headers['Referer'] = 'https://catboy.best/'; headers['User-Agent'] = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36`; break; case 'osu': if (auth_1.credentials.type != 'lazer') { return (0, handleErrors_1.handleErrors)(new Error(`Login via lazer to use this endpoint`)); } ; if (((addons === null || addons === void 0 ? void 0 : addons.authKey) || auth_1.cache.v2) == null) { return (0, handleErrors_1.handleErrors)(new Error('osu is not authorized')); } ; url = `https://osu.ppy.sh/api/v2/beatmapsets/${params.id}/download${params.no_video ? '?noVideo=1' : ''}`; headers['Referer'] = 'https://osu.ppy.sh/'; break; default: return (0, handleErrors_1.handleErrors)(new Error(`Unsupported host: ${params.host}`)); } ; const data = yield (0, request_1.download)(url, params.file_path, { _callback: params.progress_log_fn != null, headers, addons: addons, callback: progress_track, }); if (data.error) return (0, handleErrors_1.handleErrors)(new Error(data.error)); return data; } ; if (params.type == 'difficulty') { switch (params.host) { case 'osu_direct_mirror': url = `https://osu.direct/api/osu/${params.id}/raw`; break; case 'catboy': url = `https://catboy.best/osu/${params.id}`; headers['Referer'] = 'https://catboy.best/'; break; default: url = `https://osu.ppy.sh/osu/${params.id}`; break; } ; const data = yield (0, request_1.download)(url, params.file_path, { _callback: params.progress_log_fn != null, headers, addons: addons, callback: progress_track, }); if (data.error) return (0, handleErrors_1.handleErrors)(new Error(data.error)); return data; } ; return (0, handleErrors_1.handleErrors)(new Error(`Unsupported type: ${params.type}`)); }); exports.beatmaps_download = beatmaps_download;