Exemple #1
0
def playlist_list(playlist_name, itunes_xml):
    lib = Library(itunes_xml)
    playlist = lib.getPlaylist(playlist_name)
    track_locations = []
    for song in playlist.tracks:
        # print("{a} - {n} - {p}".format(a=song.artist, n=song.name, p=song.location))
        track_locations.append(song.location)
    return track_locations
Exemple #2
0
class TestLibrary(unittest.TestCase):

    def setUp(self):
        self.it_library = Library(os.path.join(os.path.dirname(__file__), "Test Library.xml"))

    def test_songs(self):

        for id, song in self.it_library.songs.items():
            assert(hasattr(song, 'name') == True)

    def test_playlists(self):

        playlists = self.it_library.getPlaylistNames()

        for song in self.it_library.getPlaylist(playlists[0]).tracks:
            assert(hasattr(song, 'track_number'))
            assert(hasattr(song, 'artist'))
            assert(hasattr(song, 'name'))
def parse_xml(settings):
    """Parse the XML file for the wanted playlists and write the data to a file."""
    data = Library(settings['xmlFile'])
    playlists = data.getPlaylistNames()
    for name in playlists:
        if name in settings['whiteList']:
            playlist = data.getPlaylist(name)
            handle = open(settings['outputDir'] +
                          '/' + name, 'w')
            handle.write('[')
            first = True
            for song in playlist.tracks:
                if first:
                    first = False
                else:
                    handle.write(',')
                path = converttobytestr(song.location).replace(
                    settings['pattern'], settings['replacement'])
                handle.write(
                    '\n  {\n    "service": "mpd",\n    "type": "song",\n    "title" : "')
                if song.name:
                    handle.write(converttobytestr(song.name))
                handle.write('",\n    "artist": "')
                if song.artist:
                    handle.write(converttobytestr(song.artist))
                handle.write('",\n    "album": "')
                if song.album:
                    handle.write(converttobytestr(song.album))
                handle.write('",\n    "albumart": "')
                album_path = os.path.dirname(path)
                short_path = album_path.replace(settings['replacement'], '')
                # handle.write('/albumart?web=none')
                handle.write('/albumart?web=' + urllib.quote(short_path) +
                             '/large&path=' + urllib.quote(album_path) + '&icon=fa-dot-circle-o')
                handle.write('",\n    "uri": "')
                handle.write(path + '"\n  }')
                if path == song.location:
                    print 'Did not match: ' + song.location
            handle.write('\n]\n')
            handle.close()
def main():
    """Main script"""
    num_cores = multiprocessing.cpu_count()

    l = Library(FILEPATH)
    playlists = l.getPlaylistNames()

    PLEX = PlexServer(PLEX_URL, PLEX_TOKEN)
    PLEX_USERS = get_user_tokens(PLEX.machineIdentifier)
    PLEX_MUSIC = PLEX.library.section('Music')
    PLEX_TRACK_LIST = PLEX_MUSIC.searchTracks()
    PLEX_ARTIST_LIST = PLEX_MUSIC.searchArtists()

    for playlist in playlists:
        playlist_items = []
        DATA_FILE = playlist + '.pickle'

        if playlist not in PLAYLISTS:
            continue

        # Check if .pickle exists
        try:
            print("Loading the '{title}' playlist from disk...".format(title=playlist))

            with open(DATA_FILE, 'rb') as fp:
                playlist_items = pickle.load(fp)
                fp.close()

            # HACK
            playlist_items = [playlist_item for playlist_item in playlist_items if playlist_item]

        except FileNotFoundError:
            print("Building the '{title}' playlist...".format(title=playlist))

            PLAYLIST_TRACKS = l.getPlaylist(playlist).tracks

            # Multiprocessing implementation
            # playlist_items = Parallel(n_jobs=num_cores, prefer='processes')(
            #     delayed(match_track)(PLAYLIST_TRACK, PLEX_MUSIC, PLEX_ARTIST_LIST, PLEX_TRACK_LIST) for PLAYLIST_TRACK in PLAYLIST_TRACKS)

            # Standard implementation
            for PLAYLIST_TRACK in PLAYLIST_TRACKS:
                track_match = match_track(PLAYLIST_TRACK, PLEX_MUSIC, PLEX_ARTIST_LIST, PLEX_TRACK_LIST)
                if track_match:
                    playlist_items.append(track_match)

            # Save data (just in case)
            with open(DATA_FILE, 'wb') as fp:
                pickle.dump(playlist_items, fp)
                fp.close()

        # Create playlist (per user)
        for user in USERS:
            user_token = PLEX_USERS.get(user)
            if not user_token:
                print("...User '{user}' not found in shared users. Skipping.".format(user=user))
                continue

            user_plex = PlexServer(PLEX_URL, user_token)

            # Delete the old playlist
            try:
                user_playlist = user_plex.playlist(playlist)
                user_playlist.delete()
            except:
                pass

            # Create a new playlist
            user_plex.createPlaylist(playlist, playlist_items)
            print("...Created playlist for '{user}'.".format(user=user))
    return
Exemple #5
0
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))
Exemple #6
0
                    playlistContent += os.path.relpath(track.location,
                                                       start=parentPath) + "\n"
                except ValueError:
                    print("Warning: Could not add the track \"" +
                          track.location +
                          "\" as relative path to the playlist \"" +
                          playlistName +
                          "\"; added the track as absolute path instead.")
                    playlistContent += track.location + "\n"

        playlistPath = parentPath.joinpath(
            cleanupPlaylistName(playlist.name) + ".m3u")
        playlistPath.write_text(playlistContent, encoding="utf8")


playlists = {}

library = Library(libraryPath)
for playlistName in library.getPlaylistNames(ignoreList=[
        "Library", "Music", "Movies", "TV Shows", "Purchased", "iTunes DJ",
        "Podcasts", "Audiobooks", "Downloaded", "Bibliotheek", "Muziek",
        "Films", "TV-programma's", "Aangekocht", "iTunes DJ", "Podcasts",
        "Audioboeken", "Gedownload"
] + ignoreList):
    playlist = library.getPlaylist(playlistName)
    playlists[playlist.playlist_persistent_id] = playlist

for playlist in playlists.values():
    if (playlist.parent_persistent_id == None):
        exportPlaylist(playlist, playlistRootPath)
Exemple #7
0
                    action='store_true')
parser.add_argument('--spotify',
                    help='transfer playlists to a spotify account')
args = parser.parse_args()

l = Library(args.file)
playlists = l.getPlaylistNames()

# remove first element; by default it's a list of all songs
del playlists[0]

if args.playlist == True:
    for listname in playlists:
        print(listname)
else:
    # if specific playlists desired, prepare array
    lists = []
    if args.lists:
        # lists = open(args.lists).readlines()
        listfile = open(args.lists)
        for line in listfile:
            line = line.rstrip()
            lists.append(line)

    # determine if playlist flag set, if so limit scope to specified lists
    for listname in playlists:
        if listname in lists or not args.lists:
            print('Playlist: ', listname)
            for song in l.getPlaylist(listname).tracks:
                print('  ', song.name, '-', song.artist, '-', song.album)