Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JUNNETWORKS/8fce036e1f1799c399dc466c92ccbba4 to your computer and use it in GitHub Desktop.
Save JUNNETWORKS/8fce036e1f1799c399dc466c92ccbba4 to your computer and use it in GitHub Desktop.
copy music from a Rhythmbox playlist to a directory.
"""
copy music from a Rhythmbox playlist to a directory
"""
import os
import argparse
import re
import shutil
from xml.etree import ElementTree as ET
import urllib.parse
from tqdm import tqdm
# rhythmbox の名前空間
ns = {"box": "http://xspf.org/ns/0/"}
# トラック番号をファイル名から削除するための正規表現
repatter = re.compile(r"^\d+ ")
def main(playlist_path: str, destination_dir: str):
root = ET.parse(playlist_path).getroot()
for track in tqdm(root.findall("box:trackList/box:track", ns)):
# title = track.find("box:title", ns).text
location = urllib.parse.unquote(track.find("box:location", ns).text)
location = location.replace("file://", "") # 先頭の不要な文字を削除
file_name = os.path.basename(location)
file_name = repatter.sub("", file_name) # トラック番号を削除
if not os.path.exists(location):
print("{}はありません.".format(location))
continue
shutil.copyfile(location, os.path.join(destination_dir, file_name))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="copy music in playlist to directory")
parser.add_argument("--playlist_path", "-i", type=str, help="Path of playlist.xspf", required=True)
parser.add_argument("--destination_dir", "-o", type=str, help="Destination directory", required=True)
args = parser.parse_args()
main(args.playlist_path, args.destination_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment