UNPKG

musicfree-api

Version:

Multi-platform music API library for downloading and searching across various music services

218 lines (190 loc) 6.84 kB
/** * MusicFree API Library * Multi-platform music API for searching and downloading */ "use strict"; // Import all platform plugins const wy = require('./downloaded_plugins/wy'); const qq = require('./downloaded_plugins/qq'); const kg = require('./downloaded_plugins/kg'); const kw = require('./downloaded_plugins/kw'); const xiaomi = require('./downloaded_plugins/xiaomi'); // Create a map of platforms for easy reference const platforms = { 'wy': { name: '网易云音乐', module: wy }, 'qq': { name: 'QQ音乐', module: qq }, 'kg': { name: '酷狗音乐', module: kg }, 'kw': { name: '酷我音乐', module: kw }, 'xiaomi': { name: '小米音乐', module: xiaomi } }; /** * Get available platforms * @returns {Object} Dictionary of available platforms with their names */ function getAvailablePlatforms() { const result = {}; Object.entries(platforms).forEach(([key, value]) => { result[key] = value.name; }); return result; } /** * Search for music across platforms * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @param {string} keyword - Search keyword * @param {number} page - Page number (defaults to 1) * @param {string} type - Search type ('music', 'album', 'artist', 'sheet', 'lyric') * @returns {Promise<Object>} Search results */ async function search(platform, keyword, page = 1, type = 'music') { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.search(keyword, page, type); } /** * Get media source URL for a song * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @param {Object} song - Song object returned from search * @param {string} quality - Quality level ('low', 'standard', 'high', 'super') * @returns {Promise<Object>} Media source information */ async function getMediaSource(platform, song, quality = 'high') { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.getMediaSource(song, quality); } /** * Get lyrics for a song * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @param {Object} song - Song object returned from search * @returns {Promise<Object>} Lyrics information */ async function getLyric(platform, song) { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.getLyric(song); } /** * Get details for an album * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @param {Object} album - Album object returned from search * @returns {Promise<Object>} Album details */ async function getAlbumInfo(platform, album) { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.getAlbumInfo(album); } /** * Get artist works (songs or albums) * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @param {Object} artist - Artist object returned from search * @param {number} page - Page number (defaults to 1) * @param {string} type - Type of works ('music', 'album') * @returns {Promise<Object>} Artist works */ async function getArtistWorks(platform, artist, page = 1, type = 'music') { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.getArtistWorks(artist, page, type); } /** * Import a music sheet from URL * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @param {string} url - Music sheet URL * @returns {Promise<Array>} List of songs in the music sheet */ async function importMusicSheet(platform, url) { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.importMusicSheet(url); } /** * Get top lists available on the platform * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @returns {Promise<Array|Object>} Top lists information */ async function getTopLists(platform) { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.getTopLists(); } /** * Get detailed information for a top list * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @param {Object} topList - Top list object returned from getTopLists * @returns {Promise<Object>} Top list details */ async function getTopListDetail(platform, topList) { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.getTopListDetail(topList); } /** * Get recommended sheet tags * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @returns {Promise<Object>} Recommended sheet tags */ async function getRecommendSheetTags(platform) { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.getRecommendSheetTags(); } /** * Get recommendations by tag * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @param {Object} tag - Tag object from getRecommendSheetTags * @param {number} page - Page number (defaults to 1) * @returns {Promise<Object>} Recommended sheets by tag */ async function getRecommendSheetsByTag(platform, tag, page = 1) { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.getRecommendSheetsByTag(tag, page); } /** * Get music sheet information and tracks * @param {string} platform - Platform code ('wy', 'qq', 'kg', 'kw', 'xiaomi') * @param {Object} sheet - Sheet object from search * @param {number} page - Page number (defaults to 1) * @returns {Promise<Object>} Music sheet details and tracks */ async function getMusicSheetInfo(platform, sheet, page = 1) { if (!platforms[platform]) { throw new Error(`Invalid platform: ${platform}`); } return await platforms[platform].module.getMusicSheetInfo(sheet, page); } // Export the API module.exports = { getAvailablePlatforms, search, getMediaSource, getLyric, getAlbumInfo, getArtistWorks, importMusicSheet, getTopLists, getTopListDetail, getRecommendSheetTags, getRecommendSheetsByTag, getMusicSheetInfo, // Export individual platforms for direct access platforms: { wy, qq, kg, kw, xiaomi } };