Last active
August 2, 2024 21:34
-
-
Save BenMcLean/19ab5c7b42d9dae3f7cd657ebbf95e2d to your computer and use it in GitHub Desktop.
Gets just the track titles from MusicBrainz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ECHO OFF | |
cd %~dp0 | |
python.exe %~dpn0.py %1 %2 %3 %4 %5 %6 %7 %8 | |
@PAUSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python -m pip install pyperclip requests | |
import argparse | |
import os | |
import pyperclip | |
import requests | |
import sys | |
from uuid import UUID | |
import xml.etree.ElementTree | |
parser = argparse.ArgumentParser(description='Gets just the track titles from MusicBrainz') | |
parser.add_argument('-c', '--clipboard', action='store_true', help='Copies output to the clipboard') | |
parser.add_argument('-d', '--disc', metavar='disc', type=int, nargs=1, required=False, default=[1], help='Disc number') | |
parser.add_argument('-m', '--mbid', metavar='mbid', type=str, nargs=1, required=False, help='MBID (MusicBrainz ID) of a release') | |
def error(msg: str, exitcode=3): | |
print(msg, file=sys.stderr) | |
sys.exit(exitcode) | |
args = parser.parse_args() | |
disc = int(args.disc[0]) | |
mbid = args.mbid | |
if not mbid: | |
mbid = pyperclip.paste() | |
mbid = mbid[mbid.rfind('/')+1:].split('?')[0] | |
def is_valid_uuid(uuid_to_test, version=4): | |
try: | |
uuid_obj = UUID(uuid_to_test, version=version) | |
except ValueError: | |
return False | |
return str(uuid_obj) == uuid_to_test | |
if not is_valid_uuid(mbid): | |
error('Invalid MBID!', 3) | |
content = requests.get('http://musicbrainz.org/ws/2/release/' + mbid + '?inc=recordings').content | |
if not content: | |
error('Couldn\'t get content!', 4) | |
metadata = xml.etree.ElementTree.fromstring(content) | |
if not metadata: | |
error('Couldn\'t find metadata!', 5) | |
ns = metadata.tag.split('}')[0] + '}' | |
release = metadata.find(ns + 'release') | |
if not release: | |
error('Couldn\'t find release!', 6) | |
mediumList = release.find(ns + 'medium-list') | |
if not mediumList: | |
error('Couldn\'t find medium-list!', 7) | |
medium = None | |
for i in mediumList.iter(ns + 'medium'): | |
position = i.find(ns + 'position') | |
if position is not None and int(position.text) == disc: | |
medium = i | |
break | |
if not medium: | |
error('Couldn\'t find medium!', 8) | |
trackList = medium.find(ns + 'track-list') | |
if not trackList: | |
error('Couldn\'t find track-list!', 9) | |
output = '' | |
for track in trackList.iter(ns + 'track'): | |
title = track.find(ns + 'title') | |
if title is not None: | |
output += title.text + os.linesep | |
else: | |
output += track.find(ns + 'recording').find(ns + 'title').text + os.linesep | |
if args.clipboard: | |
pyperclip.copy(output) | |
print(output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ECHO OFF | |
cd %~dp0 | |
SET scriptname=%~n0 | |
python.exe JustTheTrackTitlesPlease.py -c -d %scriptname:~-2% %1 %2 %3 %4 %5 %6 %7 %8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Parse cuesheets in Python: https://github.com/artur-shaik/CueParser/blob/master/cueparser.py