GithubHelp home page GithubHelp logo

hmartel / libpytunes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from liamks/libpytunes

0.0 1.0 0.0 112 KB

Python Itunes Library parser

Home Page: https://github.com/liamks/pyitunes

License: MIT License

Python 100.00%

libpytunes's Introduction

Travis CI Master branch

libpytunes

Created by Liam Kaufman (liamkaufman.com)

Contributions by Liam Kaufman (liamkaufman.com), Steven Miller (copart), dpchu, selftext, z4r, pschorf, Mathew Bramson (mbramson), Roger Filmyer (rfilmyer), cktse, Scot Hacker (shacker)

Before using libpytunes it is recommended that you backup your Itunes Library XML file. Use libpytunes at your own risk - there is no guarantee that it works or will not blow-up your computer!

If you don't see an .xml library file in ~/Music/iTunes, you probably started using iTunes after version 12.2, and have never enabled sharing between iTunes and other apps. To generate one, go to iTunes Preferences | Advanced and select "Share iTunes Library XML with other applications." (Apple docs)

Usage:

from libpytunes import Library

l = Library("/path/to/iTunes Library.xml")

for id, song in l.songs.items():
    if song and song.rating:
        if song.rating > 80:
            print(song.name, song.rating)

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))

See below for available song attributes.

If your library is very large, reading the XML into memory could be quite slow. If you need to access the library repeatedly, Python's "pickle" can save a binary representation of the XML object to disk for much faster access (up to 10x faster). To use a pickled version, do something like this:

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
epoch_time = int(time.time())  # Now

# 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"))

itl = pickle.load(open(pickle_file, "rb"))

for id, song in itl.songs.items():
    if song and song.rating:
        if song.rating > 80:
            print("{n}, {r}".format(n=song.name, r=song.rating))

Notes

Track counts may not match those shown in iTunes. e.g. This may report a higher number than the song count shown in iTunes itself. :

l = Library("iTunes Library.xml")
len(l.songs)

This is because iTunes does not count things like Podcasts and Voice Memos as "Music," whereas libpytunes counts all tracks.

The songs dictionary is keyed on TrackID (as coded in iTunes xml). Playlists are lists of Song objects, with their order noted as a playlist_order attribute.

Attributes of the Song class:

persistent_id (String)
name (String)
artist (String)
album_artist (String)
composer = None (String)
album = None (String)
genre = None (String)
kind = None (String)
size = None (Integer)
total_time = None (Integer)
track_number = None (Integer)
track_count = None (Integer)
disc_number = None (Integer)
disc_count = None (Integer)
year = None (Integer)
date_modified = None (Time)
date_added = None (Time)
bit_rate = None (Integer)
sample_rate = None (Integer)
comments = None (String)
rating = None (Integer)
album_rating = None (Integer)
play_count = None (Integer)
location = None (String)
location_escaped = None (String)
compilation = False (Boolean)
grouping = None (String)
lastplayed = None (Time)
skip_count = None (Integer)
skip_date = None(Time)
length = None (Integer)
work = None (String)
movement_name = None (String)
movement_number = None (Integer)
movement_count = None (Integer)
loved = False (Boolean)
album_loved = False (Boolean)

Songs retrieved as part of a playlist have an additional attribute:

playlist_order = None (Integer)

Song object attributes can be iterated through like this:

for key, value in SongItem:
	<interact with specific key,value pair>.

You can also convert songs directly to Dictionaries with the ToDict() Method.

SongDictionary = SongItem.ToDict()

Attributes of the Playlist class:

name (String)
tracks (List[Song])
is_folder = False (Boolean)
playlist_persistent_id = None (String)
parent_persistent_id = None (String)

Legacy Mode

Support for legacymode has been removed with version 1.5

libpytunes's People

Contributors

liamks avatar shacker avatar rfilmyer avatar phauer avatar nathandotto avatar allanmarcus avatar z4r avatar askedrelic avatar copart avatar cktse avatar dgbc avatar jemaga avatar jilljenn avatar mbramson avatar gerpsh avatar andrenam avatar flolom avatar kerin avatar pschorf avatar santazhang avatar codyzu avatar dpchu avatar selftext avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.