Skip to content

Instantly share code, notes, and snippets.

@astoeckel
Created September 10, 2017 19:11
Show Gist options
  • Save astoeckel/ced6c28192792792d88c929897cb3a9b to your computer and use it in GitHub Desktop.
Save astoeckel/ced6c28192792792d88c929897cb3a9b to your computer and use it in GitHub Desktop.
Script for converting a Rhythmbox database to a XSPF playlist
#!/usr/bin/env python3
import sys
import urllib.parse
import urllib.request
import xml.etree.ElementTree as ET
BASE_PATH = "file:///home/andreas/.audio_rhythmbox/"
if len(sys.argv) != 3:
print(
"Usage: ./rhythmdb_playlists <RHYTHMDB FILE> <MIN RATING>",
file=sys.stderr)
sys.exit(1)
xout_xspf = ET.Element("playlist",
{"version": "1",
"xmlns": "http://xspf.org/ns/0/"})
xout_trackList = ET.SubElement(xout_xspf, "trackList")
# Read the XML file
xin_tree = ET.parse(sys.argv[1])
xin_root = xin_tree.getroot()
for xin_child in xin_root:
if xin_child.tag == 'entry':
def text_or_default(key, default):
elem = xin_child.find(key)
if elem is None:
return default
return elem.text
# Filter by rating
rating = text_or_default("rating", 0)
if int(rating) < int(sys.argv[2]):
continue
# Read some metadata
duration = text_or_default("duration", -1)
artist = text_or_default("artist", "")
title = text_or_default("title", "")
album = text_or_default("album", "")
track_number = text_or_default("track-number", "")
# Extract the file path
location = text_or_default("location", "")
location = urllib.parse.unquote(location)
if not location.startswith(BASE_PATH):
continue
location = location[len(BASE_PATH):]
xout_track = ET.SubElement(xout_trackList, "track")
xout_title = ET.SubElement(xout_track, "title")
xout_title.text = str(title)
xout_creator = ET.SubElement(xout_track, "creator")
xout_creator.text = str(artist)
xout_album = ET.SubElement(xout_track, "album")
xout_album.text = str(album)
xout_trackNum = ET.SubElement(xout_track, "trackNum")
xout_trackNum.text = str(track_number)
xout_duration = ET.SubElement(xout_track, "duration")
xout_duration.text = str(int(float(duration) * 1000))
xout_location = ET.SubElement(xout_track, "location")
xout_location.text = urllib.request.pathname2url(str(location))
import os
os.write(sys.stdout.fileno(),
ET.tostring(xout_xspf, encoding="utf8", method="xml"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment