Skip to content

Instantly share code, notes, and snippets.

@mverleg
Last active April 23, 2017 18:16
Show Gist options
  • Save mverleg/9a425a4dfe56b9af6dd761fe8d2bc7d8 to your computer and use it in GitHub Desktop.
Save mverleg/9a425a4dfe56b9af6dd761fe8d2bc7d8 to your computer and use it in GitHub Desktop.
Copy all the track files from a Clementine playlist to a separate directory.
"""
Reads a Clementine playlist, and copies all the song files to a directory.
arg 1: path to the playlist
arg 2: optionally, the output directory
You can ignore the BeautifulSoup parser warning (or fi you insist, follow it's directions)
"""
from bs4 import BeautifulSoup, Tag # pip install beautifulsoup4
from shutil import copy2
from os import makedirs
from os.path import basename, splitext
from sys import argv
def copy_playlist_files(xspf_pth, target_pth=None):
"""
Read a Clementine playlist and copy all the track files to a specific directory.
This is useful for example when copying a playlist to your phone. (You can use Clementine Remote, but it didn't work for me).
:param xspf_pth: A path to a clementine playlist (xspf file).
:param target_pth: Optionally, the directory to copy files to. Defaults to a subdir of the current directory named after the playlist file.
"""
if target_pth is None:
target_pth = splitext(basename(xspf_pth))[0]
try:
makedirs(target_pth)
except OSError:
pass
with open(xspf_pth, 'r') as fh:
soup = BeautifulSoup(fh.read())
tracks = tuple(soup.body.playlist.tracklist.children)
for track in tracks:
if not isinstance(track, Tag):
continue
loc = track.location.text.replace('file://', '')
print(loc)
copy2(loc, target_pth)
print('done!')
if __name__ == '__main__':
print(argv)
assert len(argv) >= 2, 'provide a path to a playlist file, and optionally a target dir'
xspf_pth = argv[1]
target_pth = None
if len(argv) >= 3:
target_pth = argv[2]
copy_playlist_files(xspf_pth, target_pth)
@mverleg
Copy link
Author

mverleg commented Apr 23, 2017

Fixed a few bugs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment