lyricsgenius

repository·master·Indexed 22 days ago

https://github.com/johnwmillr/lyricsgenius

A Python client for the Genius.com API (version 3.12.2) used to download lyrics and metadata. It provides a high-level Genius class to search for artists and songs, retrieve lyrics via web-scraping, and save data to JSON files. The library supports OAuth2 authentication, a command-line interface (CLI), and provides access to both the authenticated developer API and the unauthenticated public API.

Tokens
11K
Snippets
40
Records
54
Agent score
76%

What's inside lyricsgenius

  1. Explore the LyricsGenius API Reference

    master

    The LyricsGenius documentation is organized into several key modules for interacting with the Genius.com API:

    • API and PublicAPI classes: Core interfaces for interacting with the service.
    • OAuth2 class: Handles authentication via OAuth2.
    • Genius class: The primary client class used to interact with the Genius API.
    • Request sender: The underlying mechanism for dispatching requests.
    • Types: Definitions of the data structures returned by the API.
    • Utility functions: Helper functions for common tasks.
  2. Accessing non-standard API response values

    master
    The package-defined types (Album, Artist, and Song) only expose a subset of the data returned by the Genius API. All attributes listed in these type definitions are guaranteed to be present. To access any other values present in the original API response body that are not explicitly defined as attributes on these objects, use the to_dict() method.
  3. Understand the LyricsGenius API architecture

    master

    LyricsGenius provides access to Genius.com data through three primary layers:

    1. API class: Interfaces with the authenticated developer API (api.genius.com). This requires a free access token and provides access to song, artist, and annotation search.
    2. PublicAPI class: Interfaces with the unauthenticated public API (genius.com/api), which mimics the service end-users access via a web browser.
    3. Genius class: A high-level interface that inherits from both API and PublicAPI. This is the primary class users should interact with, as it combines authenticated and unauthenticated methods with high-level utilities like search_song to retrieve lyrics.

    Note on Lyrics Retrieval: The official Genius API does not provide lyrics due to legal restrictions. LyricsGenius retrieves lyrics by web-scraping song pages using Beautiful Soup. Be aware that scraping lyrics may violate Genius' terms of service.

  4. Use the Genius class for high-level API access

    master
    The Genius class is the primary entry point for the lyricsgenius library. It provides a high-level interface that wraps the standard Genius API and the Public API, while also adding specialized features like downloading lyrics. It serves as the central hub for accessing methods related to accounts, albums, artists, songs, and more.
  5. Use the Genius class instead of API or PublicAPI directly

    master
    The Genius class inherits from both API and PublicAPI. It is recommended to call all methods through a Genius instance rather than accessing the API or PublicAPI classes directly. The Genius class acts as the primary entry point for all functionality provided by the library.
  6. Obtain a Genius API access token

    master

    To use lyricsgenius, you must first obtain an access token from Genius.com.

    1. Sign up for a free account at Genius.
    2. Navigate to the API Clients page.
    3. Create a new API client.
    4. Generate an access token.

    Genius provides two types of tokens:

    • client access token: Recommended for most use cases. Use this to retrieve song lyrics and song information. It does not require user authentication via OAuth2. You can generate this by clicking Generate Access Token on the API Clients page.
    • user token: Used for advanced features like managing annotations or accessing specific user account information. If you are building a website that uses the Genius Web Annotator, you will need this. lyricsgenius provides an auth class to assist with user authentication.
  7. Authenticate another user via OAuth2

    master

    To authenticate a third-party user, use the OAuth2 class to generate a redirect URL. After the user completes the flow at the redirect URI, capture the code and state from the request parameters and pass them to auth.get_user_token(code, state) to obtain the token.

    from lyricsgenius import OAuth2, Genius
    
    # 1. Initialize auth (Full code exchange example)
    auth = OAuth2.full_code_exchange(
        'my_client_id',
        'my_redirect_uri',
        'my_client_secret',
        scope='all',
        state='some_unique_value'
    )
    
    # 2. Get the URL to redirect the user to
    url_for_user = auth.url
    print('Redirecting you to ' + url_for_user)
    
    # 3. After user redirects back, get token from request params
    # (Example using Flask-like logic)
    code = request.args.get('code')
    state = request.args.get('state')
    token = auth.get_user_token(code, state)
    
    genius = Genius(token)
  8. Authenticate using OAuth2

    master

    LyricsGenius supports two OAuth2 flows via the OAuth2 class:

    1. Client-only app (OAuth2.client_only_app): Requires client_id, redirect_uri, and optionally scope.
    2. Full code exchange (OAuth2.full_code_exchange): Requires client_id, redirect_uri, client_secret, scope, and state.

    Environment Variables

    You can use lyricsgenius.auth_from_environment() to load credentials from these environment variables:

    • GENIUS_CLIENT_ID
    • GENIUS_REDIRECT_URI
    • GENIUS_CLIENT_SECRET
    import lyricsgenius as lg
    
    # Load from environment
    client_id, redirect_uri, client_secret = lg.auth_from_environment() 
    
    # Example: Client-only app flow for self-authentication
    from lyricsgenius import OAuth2, Genius
    
    auth = OAuth2.client_only_app(
        'my_client_id',
        'my_redirect_uri',
        scope='all'
    )
    
    token = auth.prompt_user()
    genius = Genius(token)
  9. Initialize the Genius client

    master

    To use the library, import lyricsgenius and instantiate the Genius class. You must provide an access_token obtained from the Genius API.

    If you do not pass a token explicitly, the library will automatically look for an environment variable named GENIUS_ACCESS_TOKEN.

    import lyricsgenius
    
    # Using an explicit token
    genius = lyricsgenius.Genius(token)
    
    # Using the GENIUS_ACCESS_TOKEN environment variable
    genius = lyricsgenius.Genius()