-q actually does something now, moved some stuff that should be in functions into functions

This commit is contained in:
Lynne Megido 2020-10-16 18:25:02 +10:00
parent 8cc483bfa8
commit 09d1e58c78
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90

62
bcao.py
View file

@ -7,6 +7,14 @@
import subprocess, argparse, sys, os, re, base64 import subprocess, argparse, sys, os, re, base64
def log(message: str, importance: int = 0):
if not args.quiet or importance > 0:
print(message)
def die(message: str, code: int = 1):
log(message)
exit(code)
try: try:
from mutagen.oggvorbis import OggVorbis from mutagen.oggvorbis import OggVorbis
from mutagen.mp3 import MP3 from mutagen.mp3 import MP3
@ -14,36 +22,31 @@ try:
from mutagen.flac import Picture from mutagen.flac import Picture
from mutagen.aac import AAC from mutagen.aac import AAC
except ImportError: except ImportError:
print("Please install python3-mutagen (pip install mutagen)") die("Please install python3-mutagen (pip install mutagen)")
sys.exit(1)
try: try:
subprocess.check_output(["unzip", "--help"]) subprocess.check_output(["unzip", "--help"])
except: except:
print("Please install unzip (apt install unzip)") die("Please install unzip (apt install unzip)")
sys.exit(1)
try: try:
subprocess.check_output(["which", "convert"]) subprocess.check_output(["which", "convert"])
subprocess.check_output(["which", "identify"]) subprocess.check_output(["which", "identify"])
except: except:
print("Please install imagemagick, and ensure convert and identify are in your $PATH (apt install imagemagick") die("Please install imagemagick, and ensure convert and identify are in your $PATH (apt install imagemagick")
sys.exit(1)
parser = argparse.ArgumentParser(description = "BandCamp Automatic Organiser. Extracts the given zip file downloaded from Bandcamp and organises it.") parser = argparse.ArgumentParser(description="Extracts the given zip file downloaded from Bandcamp and organises it.")
parser.add_argument('zip', help='The zip file to use') parser.add_argument('zip', help='The zip file to use')
# KEEP THESE IN ALPHABETICAL ORDER!
parser.add_argument('-d', '--destination', dest='destination', default='/home/lynne/Music/Music/', help="The directory to organise the music into. Default: /home/lynne/Music/Music/") parser.add_argument('-d', '--destination', dest='destination', default='/home/lynne/Music/Music/', help="The directory to organise the music into. Default: /home/lynne/Music/Music/")
parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', help='Disable non-error output') parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', help='Disable non-error output and assume default artist name.')
parser.add_argument('-t', '--threshold', dest='threshold', nargs=1, default=300, help="Maximum acceptable cover art file size in kilobytes. Default: 300") parser.add_argument('-t', '--threshold', dest='threshold', nargs=1, default=300, help="Maximum acceptable cover art file size in kilobytes. Default: 300")
args = parser.parse_args() args = parser.parse_args()
if not os.path.exists(args.zip): if not os.path.exists(args.zip):
print("heh.... nice try kid... {0} aint a real file... i've been aroud the block a few times, ya know... you'll have to do a lot better than that to trick me.... kid...".format(args.zip)) die(f"Couldn't find {args.zip}.", 2)
sys.exit(2)
print("Extracting...") log("Extracting...")
zipname = os.path.splitext(os.path.basename(args.zip))[0] zipname = os.path.splitext(os.path.basename(args.zip))[0]
tmp = "/tmp/bcao/{0}".format(zipname) tmp = "/tmp/bcao/{0}".format(zipname)
subprocess.check_output(["rm", "-rf", tmp]) subprocess.check_output(["rm", "-rf", tmp])
@ -59,13 +62,13 @@ for root, dirs, filez in os.walk(tmp):
cover = "" cover = ""
artists = [] artists = []
album = "" album = ""
fileExtensionRegex = re.compile(r"[^\.]+$") fileExtensionRegex = re.compile(r"[^.]+$")
probablyASongRegex = re.compile(r"^.+ - .+ - \d{2,} .+\.[^\.]+$") #matches "artist - album - 01 track.xyz" but not "some weird bonus thing.mp3" probablyASongRegex = re.compile(r"^.+ - .+ - \d{2,} .+\.[^.]+$") # matches "artist - album - 01 track.xyz" but not "some weird bonus thing.mp3"
musicExts = ["ogg", "flac", "alac", "aiff", "wav", "mp3", "opus", "m4a", "aac", "oga"] musicExts = ["ogg", "flac", "alac", "aiff", "wav", "mp3", "opus", "m4a", "aac", "oga"]
bannedCharacters = ["?", "\\", "/", ":", "|", "*", "\"", "<", ">"] #characters that kill wangblows. all of these are fine on lincucks/crapOS except "/" bannedCharacters = ["?", "\\", "/", ":", "|", "*", "\"", "<", ">"] # characters disallowed in NTFS filenames
trackCount = 0 trackCount = 0
print("Processing... please wait.") log("Processing... please wait.")
# use "01 Song.ogg" instead of "1 Song.ogg". Also works for albums with more than 99 tracks if that ever happens # use "01 Song.ogg" instead of "1 Song.ogg". Also works for albums with more than 99 tracks if that ever happens
trackNumberLength = len(str(len(files))) trackNumberLength = len(str(len(files)))
if trackNumberLength < 2: if trackNumberLength < 2:
@ -94,14 +97,13 @@ for file in files:
subprocess.check_output(["mv", file, os.path.dirname(file) + os.sep + name]) subprocess.check_output(["mv", file, os.path.dirname(file) + os.sep + name])
else: else:
print("UNSUPPORTED FORMAT BECAUSE LYNNE IS A LAZY MORON") log("UNSUPPORTED FORMAT BECAUSE LYNNE IS A LAZY MORON")
if len(artists) > 1: if len(artists) > 1:
artists.append("Various Artists") artists.append("Various Artists")
if cover == "": if cover == "":
# TODO: HANDLE THIS PROPERLY # TODO: HANDLE THIS PROPERLY
print("couldn't find the cover art :)))))") die("couldn't find the cover art :)))))")
sys.exit(1)
while os.path.getsize(cover) / 1024 > args.threshold: while os.path.getsize(cover) / 1024 > args.threshold:
if os.path.basename(cover) != "cover-lq.jpg": if os.path.basename(cover) != "cover-lq.jpg":
@ -136,33 +138,33 @@ for song in songs:
artist = artists[0] artist = artists[0]
artists.append("Custom...") artists.append("Custom...")
# print("Please choose the artist name to use when creating the folder.") # log("Please choose the artist name to use when creating the folder.")
choice = 0 choice = 0
while True: while True:
print("Artist directory:") log("Artist directory:")
for i in range(len(artists)): for i in range(len(artists)):
print("{0}) {1}".format(i + 1, artists[i])) log("{0}) {1}".format(i + 1, artists[i]))
choice = input("> ") choice = 1 if args.quiet else input("> ")
try: try:
choice = artists[int(choice) - 1] choice = artists[int(choice) - 1]
except: except KeyError:
print() log(f"Please choose an option from 1 to {len(artists)}.")
continue continue
break break
if choice == "Custom...": if choice == "Custom...":
print("Enter the name to use.") log("Enter the name to use.")
choice = input("> ") choice = input("> ")
# print("Setting artist to {0}".format(choice)) # log("Setting artist to {0}".format(choice))
artist = choice artist = choice
mPath = "{}/{}/{}".format(args.destination, artist, album) mPath = os.path.join(args.destination, artist, album)
subprocess.check_output(["mkdir", "-p", mPath]) subprocess.check_output(["mkdir", "-p", mPath])
for root, dirs, filez in os.walk(tmp): for root, dirs, filez in os.walk(tmp):
for file in filez: # for every file for file in filez: # for every file
subprocess.check_output(["mv", root + os.sep + file, mPath + os.sep + file]) subprocess.check_output(["mv", root + os.sep + file, mPath + os.sep + file])
print("Deleting {}...".format(tmp)) log("Deleting {}...".format(tmp))
subprocess.check_output(["rm", "-rf", tmp]) subprocess.check_output(["rm", "-rf", tmp])
print("Done!") log("Done!")