Skip to content

Instantly share code, notes, and snippets.

@internaut
Created April 28, 2022 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save internaut/3a3b3a2dc05eac572b16f5f96372a1e0 to your computer and use it in GitHub Desktop.
Save internaut/3a3b3a2dc05eac572b16f5f96372a1e0 to your computer and use it in GitHub Desktop.
Copy contents of a XSFP music playlist to a target folder
#!/bin/python3
# Copy contents of a XSFP music playlist to a target folder
#
# required two arguments: path to xspf file, target path
# requires Python >= 3.8
#
# author: Markus Konrad <post@mkonrad.net>
import os.path
import sys
import shutil
from math import log10, ceil
import xml.etree.ElementTree as ET
if len(sys.argv) < 3:
print('required arguments: path to xspf file, target path')
exit(1)
xspffile, targetpath = sys.argv[1:3]
print(f'parsing {xspffile} and will copy to {targetpath}')
root = ET.parse(xspffile)
src_locations = root.findall('.//{*}track/{*}location')
n_files = len(src_locations)
n_zfill = ceil(log10(n_files+1))
for i, elem in enumerate(src_locations):
src = elem.text
src_base = os.path.basename(elem.text)
trg_file = str(i+1).zfill(n_zfill) + '-' + src_base
trg = os.path.join(targetpath, trg_file)
print(f'[{i+1}/{n_files}] {src} -> {trg}')
shutil.copy(src, trg)
print('done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment