Meting Music API Framework

repository·master·Indexed 24 days ago

https://github.com/metowolf/meting

A Node.js music API framework providing a unified, standardized interface for accessing data from multiple music platforms, including NetEase, Tencent, Kugou, Baidu, and Kuwo. It features a Provider-based architecture to isolate platform-specific logic, allowing developers to search for songs, albums, and artists, and retrieve media resources like streaming URLs, lyrics, and artwork through a consistent API.

Tokens
3K
Snippets
8
Records
22
Agent score
79%

What's inside @meting/core

  1. How Meting's Provider architecture works

    master

    Meting uses a Provider pattern to isolate the logic for different music platforms. Instead of a single monolithic file, each platform (e.g., NetEase, Tencent, Baidu) has its own dedicated Provider class that inherits from a common base.

    • The Meting class acts as a coordinator (orchestrator). It provides a unified API to the user and delegates all platform-specific execution logic to the active Provider.
    • The ProviderFactory manages the creation and lifecycle of these providers.
    • The BaseProvider defines the standard interface that all platform providers must implement to ensure compatibility with the main Meting class.
    import Meting from './src/meting.js';
    
    // The Meting instance delegates calls to the underlying provider
    const meting = new Meting('netease');
    const result = await meting.search('稻香');
  2. Standardized Data Format with format(true)

    master

    When meting.format(true) is enabled, all platforms return a standardized JSON object structure. This allows you to write platform-agnostic code.

    Example JSON structure:

    {
      "id": "35847388",
      "name": "Hello",
      "artist": ["Adele"],
      "album": "Hello",
      "pic_id": "1407374890649284",
      "url_id": "35847388", 
      "lyric_id": "35847388",
      "source": "netease"
    }
  3. Build Meting for ESM and CJS

    master
    Use the provided build script to generate the distribution files. The build process uses Rollup and injects the version number from package.json into the source code using the __VERSION__ placeholder to avoid runtime file system reads.
  4. Use the Meting API

    master
    The Meting class provides a unified interface for interacting with various music platforms. You can initialize a specific platform or switch platforms dynamically using the .site() method. The API is designed to be backward compatible with previous versions.
  5. Initialize Meting and search for songs

    master

    To use Meting, import the core module and instantiate it with a platform identifier. Use .format(true) to ensure the returned JSON strings are standardized across different platforms. Note that when format(true) is used, you must JSON.parse() the results.

    import Meting from '@meting/core';
    
    // Initialize with a music platform
    const meting = new Meting('netease'); // 'netease', 'tencent', 'kugou', 'baidu', 'kuwo'
    
    // Enable data formatting for consistent output
    meting.format(true);
    
    // Search for songs
    try {
      const searchResult = await meting.search('Hello Adele', { page: 1, limit: 10 });
      const songs = JSON.parse(searchResult);
      console.log(songs);
    } catch (error) {
      console.error('Search failed:', error);
    }
  6. Implement a custom music platform Provider

    master

    To extend Meting with a new music platform, follow these three steps:

    1. Create a new Provider file in src/providers/.
    2. Extend BaseProvider and implement the required methods (e.g., getHeaders, search, song, etc.).
    3. Register the new Provider in src/providers/index.js so the ProviderFactory can recognize it.

    Example implementation:

    import BaseProvider from './base.js';
    
    export default class NewPlatformProvider extends BaseProvider {
      constructor(meting) {
        super(meting);
        this.name = 'newplatform';
      }
    
      getHeaders() {
        // Implement platform-specific request headers
      }
    
      search(keyword, option = {}) {
        // Implement search logic
      }
    
      // ... implement other required methods
    }
    import BaseProvider from './base.js';
    
    export default class NewPlatformProvider extends BaseProvider {
      constructor(meting) {
        super(meting);
        this.name = 'newplatform';
      }
    
      getHeaders() {
        // Implement platform-specific request headers
      }
    
      search(keyword, option = {}) {
        // Implement search logic
      }
    
      // ... implement other required methods
    }
  7. Initialize and configure the Meting client

    master

    To use Meting, instantiate the Meting class. You can specify the music platform (server) during initialization. The client supports a chainable configuration pattern for setting cookies and data formatting.

    Initialization Options:

    • server: The music platform to use (e.g., 'netease'). If an unsupported platform is provided, it defaults to 'netease'. Use Meting.getSupportedPlatforms() to see available options.

    Configuration Methods:

    • .site(server): Switches the music platform. Returns the Meting instance.
    • .cookie(cookie): Sets the Cookie header for requests. Returns the Meting instance.
    • .format(format): Enables or disables data formatting. Defaults to true. Returns the Meting instance.
  8. Handle API errors and rate limiting

    master

    Meting uses Promise-based error handling. Always wrap calls in try-catch blocks to handle network issues or platform changes.

    To avoid being rate-limited by music platforms, implement delays between consecutive requests.

    Error Handling Example:

    try {
      const result = await meting.search('keyword');
    } catch (error) {
      console.error('API Error:', error);
      // Fallback logic
      meting.site('tencent');
      const fallback = await meting.search('keyword');
    }

    Rate Limiting Example:

    // Add a 2-second delay between requests
    await new Promise(resolve => setTimeout(resolve, 2000));
  9. Search for music content

    master

    Search for songs, albums, or artists using a keyword.

    await meting.search(keyword, options)

    Options:

    • type (number, optional): Search category. For NetEase: 1 for songs (default), 10 for albums, 100 for artists.
    • page (number, optional): Page number starting from 1. Defaults to 1.
    • limit (number, optional): Number of results per page. Defaults to 30.
    await meting.search('keyword', {
      type: 1,
      page: 1,
      limit: 30,
    });
  10. Use the Meting Constructor

    master

    Create a new instance of Meting by specifying the target music platform.

    new Meting(server)

    • server (string): The music platform to use. Supported values: 'netease', 'tencent', 'kugou', 'baidu', 'kuwo'.
    const meting = new Meting('netease');
  11. Manage Meting platform settings

    master

    Use these methods to configure the instance behavior and authentication.

    • meting.site(server): Switch the active music platform.
    • meting.cookie(cookie): Set platform-specific cookies for authentication or session management.
    • meting.format(enable): Enable or disable standardized data formatting. When enabled, results are returned as standardized JSON strings.