libpytunes

repository·master·Indexed 42 days ago

https://github.com/liamks/libpytunes

A Python library for parsing and interacting with iTunes Library XML files. It provides the Library class to programmatically access song metadata, ratings, and playlist structures, with support for using Python's pickle module to accelerate access for large libraries.

Tokens
1.6K
Snippets
5
Records
5
Agent score
3%

What's inside libpytunes

  1. Use the Library class to parse iTunes XML

    master

    The Library class is the primary entry point for interacting with your iTunes data. You must provide the path to your iTunes Library.xml file.

    Note: If you do not see an .xml file in ~/Music/iTunes, you must enable sharing in iTunes Preferences | Advanced > "Share iTunes Library XML with other applications."

    from libpytunes import Library
    
    # Initialize the library with the path to your XML file
    l = Library("/path/to/iTunes Library.xml")
    
    # Access songs via the .songs dictionary (keyed by TrackID)
    for id, song in l.songs.items():
        if song and song.rating:
            if song.rating > 80:
                print(song.name, song.rating)
    
    # Access playlists
    playlists = l.getPlaylistNames()
    for song in l.getPlaylist(playlists[0]).tracks:
        print("[{t}] {a} - {n}".format(t=song.track_number, a=song.artist, n=song.name))
  2. Speed up library access using Pickle

    master

    For very large libraries, parsing the XML into memory can be slow. You can significantly increase access speed (up to 10x) by saving a binary representation of the Library object using Python's pickle module.

    It is recommended to implement a check that regenerates the pickle file if it is missing or older than a specific expiry threshold.

    import os.path
    import pickle
    import time
    from libpytunes import Library
    
    lib_path = "/Users/[username]/Music/iTunes/iTunes Library.xml"
    pickle_file = "itl.p"
    expiry = 60 * 60  # Refresh pickled file if older than 1 hour
    epoch_time = int(time.time())
    
    # Generate pickled version of database if stale or doesn't exist
    if not os.path.isfile(pickle_file) or os.path.getmtime(pickle_file) + expiry < epoch_time:
        itl_source = Library(lib_path)
        pickle.dump(itl_source, open(pickle_file, "wb"))
    
    # Load the fast binary version
    itl = pickle.load(open(pickle_file, "rb"))
    
    for id, song in itl.songs.items():
        if song and song.rating:
            print("{n}, {r}".format(n=song.name, r=song.rating))
  3. Install libpytunes

    master

    To install the libpytunes library from the source repository, clone the repository, install the required dependencies via pip, and then run the setup script.

    # 1. Clone the repository
    git clone https://github.com/liamks/libpytunes.git
    
    # 2. Navigate to the project directory
    cd libpytunes
    
    # 3. Install the required dependencies
    pip install -r requirements.txt
    
    # 4. Install the library
    python setup.py install
  4. Playlist class attributes

    master

    The Playlist class represents a collection of songs. Playlists are lists of Song objects, and their order is preserved via the playlist_order attribute on the songs within them.

    # Key attributes available on a Playlist object:
    is_folder: bool
    persistent_id: int
    parent_persistent_id: int
    distinguished_kind: str
    playlist_id: int
    name: str
    is_genius_playlist: bool
    is_smart_playlist: bool
    tracks: list[Song]
  5. Song class attributes

    master

    The Song object represents an individual track. The songs dictionary in the Library object is keyed by the track_id.

    Note: Songs retrieved as part of a playlist include an additional attribute: playlist_order (Integer).

    You can iterate through a Song object's attributes like a dictionary:

    for key, value in song_item:
        # interact with specific key, value pair
    # Key attributes available on a Song object:
    name: str
    track_id: int
    artist: str
    album_artist: str
    composer: str
    album: str
    genre: str
    kind: str
    size: int
    total_time: int
    start_time: int
    stop_time: int
    track_number: int
    track_count: int
    disc_number: int
    disc_count: int
    year: int
    date_modified: str
    date_added: str
    bit_rate: int
    sample_rate: int
    comments: str
    rating: int
    rating_computed: bool
    album_rating: int
    play_count: int
    skip_count: int
    skip_date: str
    location: str
    location_escaped: str
    compilation: bool
    grouping: str
    lastplayed: str
    length: int
    track_type: str
    podcast: bool
    movie: bool
    has_video: bool
    loved: bool
    album_loved: bool
    persistent_id: str
    apple_music: bool
    protected: bool
    disabled: bool
    release_date: str
    
    # Additional attribute for songs in playlists:
    playlist_order: int