code cleanup

This commit is contained in:
Lynne Megido 2020-10-26 22:44:33 +10:00
parent 7334f67e45
commit 1804ddf457
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90

45
bcao.py
View file

@ -23,10 +23,12 @@ from typing import Optional, Union, List, Dict
# Picture(), implying that they had imported mutagen.flac.Picture, and therefore i'm right and the computer is WRONG # Picture(), implying that they had imported mutagen.flac.Picture, and therefore i'm right and the computer is WRONG
# https://mutagen.readthedocs.io/en/latest/api/flac.html#mutagen.Picture.data # https://mutagen.readthedocs.io/en/latest/api/flac.html#mutagen.Picture.data
import mutagen import mutagen
# noinspection PyProtectedMember
from mutagen.flac import Picture, FLAC from mutagen.flac import Picture, FLAC
from mutagen.oggvorbis import OggVorbis from mutagen.oggvorbis import OggVorbis
from mutagen.mp3 import MP3 from mutagen.mp3 import MP3
from mutagen.mp4 import MP4, MP4Cover from mutagen.mp4 import MP4, MP4Cover
# noinspection PyProtectedMember
from mutagen.id3 import APIC, PictureType from mutagen.id3 import APIC, PictureType
from PIL import Image from PIL import Image
@ -58,7 +60,11 @@ class SongInfo:
self.format = path.splitext(file_name)[1][1:] self.format = path.splitext(file_name)[1][1:]
self.fallback = False self.fallback = False
fallbacks = re.match(r"^(?P<artist>.+) - (?P<album>.+) - (?P<track>\d{2,}) (?P<title>.+)\.(?:ogg|flac|alac|aiff|wav|mp3|m4a)$", self.file_name) fallbacks = re.match(
r"^(?P<artist>.+) - (?P<album>.+) - (?P<track>\d{2,}) (?P<title>.+)\.(?:ogg|flac|alac|aiff|wav|mp3|m4a)$",
self.file_name
)
if fallbacks is None: if fallbacks is None:
die("Couldn't determine fallback tags!") die("Couldn't determine fallback tags!")
return # needed for mypy return # needed for mypy
@ -151,43 +157,6 @@ def die(message: str, code: int = 1):
print(message) print(message)
sys.exit(code) sys.exit(code)
# def get_tag(mut_song: MutagenFile, tag: str, allow_list: bool = False, allow_sanitising: bool = True)\
# -> Union[str, List[str]]:
# if isinstance(mut_song, MP3):
# tag = vorbis_to_id3[tag]
# tag_list = mut_song.tags.getall(tag)
#
# elif isinstance(mut_song, MP4):
# # every tag in the MP4 file (from what i can tell) is a list
# # this includes the track number tag, which is a list, containing a single tuple, containing two ints (track, total)
# # unless we account for this, tag_list will be set to [(1, 5)], and then converted to a string, resulting in
# # ['(1, 5)'], which (if not allow_list) will be returned as '(1, 5)', which is not exactly helpful.
# tag = vorbis_to_itunes[tag]
# if tag == 'trkn':
# # mut_song[tag] == [(1, 5)]
# # mut_song[tag][0] == (1, 5)
# tag_list = mut_song[tag][0]
# else:
# tag_list = mut_song[tag]
#
# else:
# if tag == "track":
# tag = "tracknumber"
# tag = tag.replace("_", "")
# tag_list = mut_song[tag] if isinstance(mut_song[tag], list) else [mut_song[tag]]
#
# # convert the list of strings/ID3 frames/ints/whatevers to strings
# tag_list = list(map(str, tag_list))
#
# # sanitise everything
# if allow_sanitising:
# tag_list = [sanitise(tag) for tag in tag_list]
#
# if allow_list:
# return tag_list
#
# return tag_list[0]
def has_cover(mut_song: MutagenFile): def has_cover(mut_song: MutagenFile):
if isinstance(mut_song, OggVorbis): if isinstance(mut_song, OggVorbis):
return "metadata_block_picture" in mut_song and len(mut_song["metadata_block_picture"]) != 0 return "metadata_block_picture" in mut_song and len(mut_song["metadata_block_picture"]) != 0