Skip to content

Instantly share code, notes, and snippets.

@jasonbot
Created July 26, 2023 16:11
Show Gist options
  • Save jasonbot/15a2c2caf35d51fba8560b03b30cbca1 to your computer and use it in GitHub Desktop.
Save jasonbot/15a2c2caf35d51fba8560b03b30cbca1 to your computer and use it in GitHub Desktop.
Rename files in Rhythmbox lib that can't be on NTFS
import os
import pathlib
from urllib.parse import urlparse, urlunparse, unquote, quote
import xml.dom.minidom
REPLACE_CHARS = '<>:'
path = pathlib.Path("~/.local/share/rhythmbox/rhythmdb.xml").expanduser()
out_path = path.parent / "new_rhythmdb.xml"
print("Updating unNTFSable names in", path, "and outputting to", out_path)
d = xml.dom.minidom.parse(str(path))
for e in d.getElementsByTagName("entry"):
if e.getAttribute("type") == "song":
locationNode = e.getElementsByTagName("location")[0].firstChild
location = locationNode.nodeValue
u = urlparse(location)
if u.scheme == "file" and any(c in unquote(u.path) for c in REPLACE_CHARS):
oldp = unquote(u.path)
newp = oldp
for c in REPLACE_CHARS:
newp = newp.replace(c, "_")
print("GOTTA FIX", newp)
new_out_file = pathlib.Path(newp)
print(" Making", new_out_file.parent)
try:
os.makedirs(new_out_file.parent)
except FileExistsError:
print(" Already there:", new_out_file.parent)
os.rename(oldp, newp)
print("MV", oldp, "->", newp)
u = u._replace(path=quote(newp))
print(" JJJJ", urlunparse(u))
e.getElementsByTagName("location")[0].removeChild(locationNode)
newPathNode = d.createTextNode(urlunparse(u))
e.getElementsByTagName("location")[0].appendChild(newPathNode)
with open(out_path, "w") as out_handle:
d.writexml(out_handle)
print("Copy", out_path, "to", path, "if you're satified with the results")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment