restructured into package

This commit is contained in:
Lynne Megido 2021-01-18 19:58:44 +10:00
parent ebf7651501
commit 55034017f7
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
8 changed files with 263 additions and 291 deletions

6
.gitignore vendored
View file

@ -1,8 +1,10 @@
!bcao/
*
!*/
__pycache__/**
!bcao/*.py
!requirements.txt
!.gitignore
!mypy.ini
!.run/
!.idea/
!README.md
/*

View file

@ -20,14 +20,28 @@
</component>
<component name="ChangeListManager">
<list default="true" id="f581197a-f26b-4fde-b746-e72c0ed1bb2a" name="Default Changelist" comment="my py dot ini">
<change afterPath="$PROJECT_DIR$/bcao/__init__.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/bcao/__main__.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/bcao/song_info.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/bcao.py" beforeDir="false" afterPath="$PROJECT_DIR$/bcao.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.run/bcao (ceres).run.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.run/bcao (ceres).run.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.run/bcao.run.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.run/bcao.run.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.run/mypy.run.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.run/mypy.run.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/bcao.py" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Python Script" />
</list>
</option>
</component>
<component name="FlaskConsoleOptions" custom-start-script="import sys&#10;sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])&#10;from flask.cli import ScriptInfo&#10;locals().update(ScriptInfo(create_app=None).load_app().make_shell_context())&#10;print(&quot;Python %s on %s\nApp: %s [%s]\nInstance: %s&quot; % (sys.version, sys.platform, app.import_name, app.env, app.instance_path))">
<envs>
<env key="FLASK_APP" value="app" />
@ -43,6 +57,14 @@
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
<option name="RESET_MODE" value="HARD" />
</component>
<component name="GitSEFilterConfiguration">
<file-type-list>
<filtered-out-file-type name="LOCAL_BRANCH" />
<filtered-out-file-type name="REMOTE_BRANCH" />
<filtered-out-file-type name="TAG" />
<filtered-out-file-type name="COMMIT_BY_MESSAGE" />
</file-type-list>
</component>
<component name="JupyterTrust" id="c9c24a84-69f9-4c1e-bffb-78383de38689" />
<component name="ProjectId" id="1iwv1rbtMpCLK7D695td98N37pr" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
@ -51,19 +73,33 @@
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="ASKED_MARK_IGNORED_FILES_AS_EXCLUDED" value="true" />
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="com.intellij.ide.scratch.LRUPopupBuilder$1/New Scratch File" value="TEXT" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="last_opened_file_path" value="$USER_HOME$/.local/bin/mypy" />
<property name="node.js.detected.package.eslint" value="true" />
<property name="node.js.detected.package.tslint" value="true" />
<property name="node.js.path.for.package.eslint" value="project" />
<property name="node.js.path.for.package.tslint" value="project" />
<property name="node.js.selected.package.eslint" value="(autodetect)" />
<property name="node.js.selected.package.tslint" value="(autodetect)" />
<property name="settings.editor.selected.configurable" value="preferences.pluginManager" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component>
<component name="RunManager" selected="Python.bcao (ceres)">
<component name="PyConsoleOptionsProvider">
<option name="myPythonConsoleState">
<console-settings module-name="bcao" is-module-sdk="true">
<option name="myUseModuleSdk" value="true" />
<option name="myModuleName" value="bcao" />
</console-settings>
</option>
</component>
<component name="RecentsManager">
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/bcao" />
</key>
</component>
<component name="RunManager" selected="Python.mypy">
<configuration default="true" type="PythonConfigurationType" factoryName="Python">
<module name="bcao" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -87,8 +123,8 @@
<method v="2" />
</configuration>
<list>
<item itemvalue="Python.bcao (io)" />
<item itemvalue="Python.bcao (ceres)" />
<item itemvalue="Python.bcao (io)" />
<item itemvalue="Python.mypy" />
</list>
</component>
@ -104,7 +140,9 @@
<workItem from="1602850978698" duration="7902000" />
<workItem from="1602908398925" duration="34104000" />
<workItem from="1603714609431" duration="5637000" />
<workItem from="1603720261881" duration="6236000" />
<workItem from="1603720261881" duration="8249000" />
<workItem from="1605688147310" duration="310000" />
<workItem from="1610959328356" duration="4539000" />
</task>
<task id="LOCAL-00001" summary="mp3 support! more helpful interface! better code! yahoo!!">
<created>1602927759343</created>
@ -183,7 +221,14 @@
<option name="project" value="LOCAL" />
<updated>1603814270092</updated>
</task>
<option name="localTasksCounter" value="12" />
<task id="LOCAL-00012" summary="check against tag format type instead of file extension&#10;&#10;sorta like, &quot;if tag_format == 'id3'&quot; rather than &quot;if song_format == ['mp3', 'wav', 'aiff']&quot;">
<created>1603888340561</created>
<option name="number" value="00012" />
<option name="presentableId" value="LOCAL-00012" />
<option name="project" value="LOCAL" />
<updated>1603888340561</updated>
</task>
<option name="localTasksCounter" value="13" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -213,116 +258,12 @@
<MESSAGE value="added project files, aiff support" />
<MESSAGE value="turns out i didn't need to do anything to add alac support - they work the same as aac m4a files do. although i did find and fix a bug in the m4a handling so that's good at least 0uo" />
<MESSAGE value="remove unneeded file extension" />
<option name="LAST_COMMIT_MESSAGE" value="remove unneeded file extension" />
</component>
<component name="WindowStateProjectService">
<state x="555" y="188" width="800" height="672" key="#Deployment" timestamp="1602927147820">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="555" y="188" width="800" height="672" key="#Deployment/0.0.1920.1055@0.0.1920.1055" timestamp="1602927147820" />
<state x="811" y="199" width="732" height="632" key="#Inspections" timestamp="1602832260834">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="811" y="199" width="732" height="632" key="#Inspections/0.0.1920.1055@0.0.1920.1055" timestamp="1602832260834" />
<state x="697" y="368" width="516" height="313" key="#Notifications" timestamp="1602831592098">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="697" y="368" width="516" height="313" key="#Notifications/0.0.1920.1055@0.0.1920.1055" timestamp="1602831592098" />
<state x="555" y="170" width="800" height="706" key="#Plugins" timestamp="1603714662919">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state x="555" y="170" width="800" height="706" key="#Plugins/0.0.1920.1054@0.0.1920.1054" timestamp="1603714662919" />
<state x="719" y="227" key="#Python" timestamp="1603720399983">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state x="719" y="227" key="#Python/0.0.1920.1054@0.0.1920.1054" timestamp="1603720399983" />
<state x="418" y="185" width="1084" height="709" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1603723412351">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state x="418" y="185" width="1084" height="709" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1920.1054@0.0.1920.1054" timestamp="1603723412351" />
<state x="418" y="185" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1920.1055@0.0.1920.1055" timestamp="1602931259631" />
<state x="888" y="193" width="424" height="721" key="#com.intellij.ide.macro.MacrosDialog" timestamp="1602931237546">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="888" y="193" width="424" height="721" key="#com.intellij.ide.macro.MacrosDialog/0.0.1920.1055@0.0.1920.1055" timestamp="1602931237546" />
<state x="707" y="363" width="797" height="527" key="#com.intellij.tools.ToolEditorDialog" timestamp="1602908296483">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="707" y="363" width="797" height="527" key="#com.intellij.tools.ToolEditorDialog/0.0.1920.1055@0.0.1920.1055" timestamp="1602908296483" />
<state x="549" y="98" width="1059" height="853" key="CommitChangelistDialog2" timestamp="1602927110754">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="549" y="98" width="1059" height="853" key="CommitChangelistDialog2/0.0.1920.1055@0.0.1920.1055" timestamp="1602927110754" />
<state x="100" y="99" width="1720" height="856" key="DiffContextDialog" timestamp="1603814233028">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state x="100" y="99" width="1720" height="856" key="DiffContextDialog/0.0.1920.1054@0.0.1920.1054" timestamp="1603814233028" />
<state x="100" y="99" width="1720" height="856" key="DiffContextDialog/0.0.1920.1055@0.0.1920.1055" timestamp="1602915909590" />
<state x="743" y="285" width="424" height="479" key="FileChooserDialogImpl" timestamp="1602850965686">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="743" y="285" width="424" height="479" key="FileChooserDialogImpl/0.0.1920.1055@0.0.1920.1055" timestamp="1602850965686" />
<state width="1878" height="281" key="GridCell.Tab.0.bottom" timestamp="1603723362538">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="281" key="GridCell.Tab.0.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1603723362538" />
<state width="1878" height="282" key="GridCell.Tab.0.bottom/0.0.1920.1055@0.0.1920.1055" timestamp="1602942953878" />
<state width="1878" height="281" key="GridCell.Tab.0.center" timestamp="1603723362538">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="281" key="GridCell.Tab.0.center/0.0.1920.1054@0.0.1920.1054" timestamp="1603723362538" />
<state width="1878" height="282" key="GridCell.Tab.0.center/0.0.1920.1055@0.0.1920.1055" timestamp="1602942953877" />
<state width="1878" height="281" key="GridCell.Tab.0.left" timestamp="1603723362537">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="281" key="GridCell.Tab.0.left/0.0.1920.1054@0.0.1920.1054" timestamp="1603723362537" />
<state width="1878" height="282" key="GridCell.Tab.0.left/0.0.1920.1055@0.0.1920.1055" timestamp="1602942953877" />
<state width="1878" height="281" key="GridCell.Tab.0.right" timestamp="1603723362538">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="281" key="GridCell.Tab.0.right/0.0.1920.1054@0.0.1920.1054" timestamp="1603723362538" />
<state width="1878" height="282" key="GridCell.Tab.0.right/0.0.1920.1055@0.0.1920.1055" timestamp="1602942953877" />
<state width="1878" height="347" key="GridCell.Tab.1.bottom" timestamp="1603723347647">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="347" key="GridCell.Tab.1.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1603723347647" />
<state width="1878" height="347" key="GridCell.Tab.1.center" timestamp="1603723347647">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="347" key="GridCell.Tab.1.center/0.0.1920.1054@0.0.1920.1054" timestamp="1603723347647" />
<state width="1878" height="347" key="GridCell.Tab.1.left" timestamp="1603723347647">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="347" key="GridCell.Tab.1.left/0.0.1920.1054@0.0.1920.1054" timestamp="1603723347647" />
<state width="1878" height="347" key="GridCell.Tab.1.right" timestamp="1603723347647">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="347" key="GridCell.Tab.1.right/0.0.1920.1054@0.0.1920.1054" timestamp="1603723347647" />
<state x="182" y="88" width="1536" height="869" key="MergeDialog" timestamp="1602851077617">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="182" y="88" width="1536" height="869" key="MergeDialog/0.0.1920.1055@0.0.1920.1055" timestamp="1602851077617" />
<state x="596" y="306" width="718" height="437" key="MultipleFileMergeDialog" timestamp="1602851077619">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="596" y="306" width="718" height="437" key="MultipleFileMergeDialog/0.0.1920.1055@0.0.1920.1055" timestamp="1602851077619" />
<state x="334" y="44" width="1315" height="941" key="SettingsEditor" timestamp="1603776383135">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state x="334" y="44" key="SettingsEditor/0.0.1920.1054@0.0.1920.1054" timestamp="1603776383135" />
<state x="334" y="44" width="1315" height="941" key="SettingsEditor/0.0.1920.1055@0.0.1920.1055" timestamp="1602927410438" />
<state x="100" y="99" width="1720" height="856" key="com.intellij.history.integration.ui.views.FileHistoryDialog" timestamp="1602937117208">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="100" y="99" width="1720" height="856" key="com.intellij.history.integration.ui.views.FileHistoryDialog/0.0.1920.1055@0.0.1920.1055" timestamp="1602937117208" />
<state x="623" y="232" width="672" height="678" key="search.everywhere.popup" timestamp="1602936893566">
<screen x="0" y="0" width="1920" height="1055" />
</state>
<state x="623" y="232" width="672" height="678" key="search.everywhere.popup/0.0.1920.1055@0.0.1920.1055" timestamp="1602936893566" />
<MESSAGE value="check against tag format type instead of file extension&#10;&#10;sorta like, &quot;if tag_format == 'id3'&quot; rather than &quot;if song_format == ['mp3', 'wav', 'aiff']&quot;" />
<option name="LAST_COMMIT_MESSAGE" value="check against tag format type instead of file extension&#10;&#10;sorta like, &quot;if tag_format == 'id3'&quot; rather than &quot;if song_format == ['mp3', 'wav', 'aiff']&quot;" />
</component>
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/bcao$mypy.coverage" NAME="mypy Coverage Results" MODIFIED="1603717428705" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/bcao$bcao.coverage" NAME="bcao Coverage Results" MODIFIED="1603719196915" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/bcao$bcao__ceres_.coverage" NAME="bcao (ceres) Coverage Results" MODIFIED="1603723362525" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/bcao$mypy.coverage" NAME="mypy Coverage Results" MODIFIED="1610962969226" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/bcao$bcao__ceres_.coverage" NAME="bcao (ceres) Coverage Results" MODIFIED="1610962396086" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
</component>
</project>

View file

@ -12,11 +12,11 @@
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/bcao.py" />
<option name="SCRIPT_NAME" value="bcao" />
<option name="PARAMETERS" value="&quot;A Cerulean State - As if I remembered something.zip&quot; -d &quot;/tmp/out/&quot;" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="MODULE_MODE" value="true" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />

View file

@ -12,7 +12,7 @@
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/bcao.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/bcao/__main__.py" />
<option name="PARAMETERS" value="&quot;Braxton Burks - Time &amp; Space.zip&quot; -d &quot;$USER_HOME$/Documents&quot;" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />

View file

@ -6,14 +6,14 @@
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="SDK_HOME" value="/usr/bin/python3.9" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/venv/bin/mypy" />
<option name="PARAMETERS" value="bcao.py" />
<option name="SCRIPT_NAME" value="$USER_HOME$/.local/bin/mypy" />
<option name="PARAMETERS" value="bcao" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />

26
bcao/__init__.py Normal file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env python3
import re
import mutagen
from mutagen.flac import FLAC
from mutagen.mp3 import MP3
# noinspection PyProtectedMember
from mutagen.id3 import ID3Tags
# noinspection PyProtectedMember
from mutagen.mp4 import Tags
from mutagen.oggvorbis import OggVorbis
from typing import Dict, List, Union
format_lookup: Dict[str, str] = {
"mp3": "id3",
"m4a": "m4a",
"ogg": "vorbis",
"flac": "vorbis",
"wav": "id3",
"aiff": "id3"
}
fully_supported: List[str] = ["ogg", "flac", "mp3", "m4a", "wav", "aiff"]
MutagenFile = Union[MP3, FLAC, OggVorbis, mutagen.FileType]
MutagenTags = Union[ID3Tags, Tags, mutagen.oggvorbis.OggVCommentDict]
sanitisation_regex = re.compile(r"[?\\/:|*\"<>]")

View file

@ -5,6 +5,14 @@
# input: a .zip from bandcamp
# output: it organises it, adds cover art, puts it in the right place...
# pycharm tells me some of these classes shouldn't be imported because they're not declared in __all__.
# however, the mutagen docs show example code where someone creates a mutagen.flac.Picture by referring to it as
# 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
from . import *
from bcao.song_info import SongInfo
import argparse
import io
import os
@ -13,182 +21,21 @@ import sys
import tempfile
import shutil
from os import path
from base64 import b64encode
from zipfile import ZipFile
from typing import Optional, List, Dict
from pathlib import Path
from typing import Optional, Union, List, Dict
# pycharm tells me some of these classes shouldn't be imported because they're not declared in __all__.
# however, the mutagen docs show example code where someone creates a mutagen.flac.Picture by referring to it as
# 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
import mutagen
# noinspection PyProtectedMember
from mutagen.flac import Picture, FLAC
from mutagen.oggvorbis import OggVorbis
from mutagen.mp3 import MP3
from mutagen.mp4 import MP4, MP4Cover
from mutagen.flac import Picture
from mutagen.mp4 import MP4Cover
# noinspection PyProtectedMember
from mutagen.id3 import APIC, PictureType, Frame, TRCK, TPE1, TIT2, TALB, TPE2
from mutagen.id3 import APIC, PictureType
from PIL import Image
fully_supported: List[str] = ["ogg", "flac", "mp3", "m4a", "wav", "aiff"]
MutagenFile = Union[MP3, FLAC, OggVorbis, mutagen.FileType]
MutagenTags = Union[mutagen.id3.ID3Tags, mutagen.mp4.Tags, mutagen.oggvorbis.OggVCommentDict]
args: argparse.Namespace
tmp_dir: tempfile.TemporaryDirectory # type: ignore
format_lookup: Dict[str, str] = {
"mp3": "id3",
"m4a": "m4a",
"ogg": "vorbis",
"flac": "vorbis",
"wav": "id3",
"aiff": "id3"
}
class SongInfo:
tag_lookup: Dict[str, Dict[str, str]] = {
"track": {"id3": "TRCK", "m4a": "trkn", "vorbis": "tracknumber"},
"artist": {"id3": "TPE1", "m4a": "©ART", "vorbis": "artist"},
"title": {"id3": "TIT2", "m4a": "©nam", "vorbis": "title"},
"album": {"id3": "TALB", "m4a": "©alb", "vorbis": "album"},
"album_artist": {"id3": "TPE2", "m4a": "aART", "vorbis": "albumartist"}
}
def __init__(self, file_name: Path):
self.m_file: MutagenFile = mutagen.File(file_name)
self.m_tags: MutagenTags = self.m_file.tags
self.file_name = str(file_name.name)
self.format = path.splitext(file_name)[1][1:]
self.fallback = False
if self.format not in format_lookup:
raise ValueError(f"Unsupported file type: {self.format}")
fallbacks = re.match(
r"^(?P<artist>.+) - (?P<album>.+) - (?P<track>\d{2,}) (?P<title>.+)\.(?:ogg|flac|aiff|wav|mp3|m4a)$",
self.file_name
)
if fallbacks is None:
die("Couldn't determine fallback tags!")
return # needed for mypy
# set default values for the tags, in case the file is missing any (or all!) of them
self.tags: Dict[str, str] = {
"track": str(int(fallbacks.group("track"))), # convert to int and str again to turn e.g. "01" into "1"
"artist": fallbacks.group("artist"),
"title": fallbacks.group("title"),
"album": fallbacks.group("album"),
"album_artist": fallbacks.group("artist")
}
# set list_tags to the default tags in list form
# i.e. for every tag, set list_tags[x] = [tags[x]]
self.list_tags: Dict[str, List[str]] = dict((x[0], [x[1]]) for x in self.tags.items())
if self.m_tags is None:
# file has no tags
# generate empty tags
self.m_file.add_tags()
self.m_tags = self.m_file.tags
self.fallback = True
# write fallback tags to file
for standard_name, tag_set in self.tag_lookup.items():
tag = tag_set[format_lookup[self.format]]
self.m_tags[tag] = self.new_id3_tag(standard_name, self.tags[standard_name])
self.m_file.save()
else:
for standard_name, tag_set in self.tag_lookup.items():
tag = tag_set[format_lookup[self.format]]
if tag not in self.m_tags:
print(f"{tag} not in self.m_tags")
self.fallback = True
continue
value_list = self.m_tags[tag]
if self.format == "m4a" and standard_name == "track":
# every tag in the MP4 file (from what i can tell) is a list
# this includes the track number tag, which is a tuple of ints in a list.
# because every other format is either a non-list, or a list of non-lists, we need to account for this case
# (a list of lists of non-lists) specially, by turning it into a list of non-lists.
value_list = value_list[0]
if not isinstance(value_list, (list, tuple)):
value_list = [value_list]
# convert the list of strings/ID3 frames/ints/whatevers to sanitised strings
value_list = [sanitise(str(val)) for val in value_list]
self.tags[standard_name] = value_list[0]
self.list_tags[standard_name] = value_list
@staticmethod
def new_id3_tag(tag: str, value: str) -> Frame:
if tag == "track":
return TRCK(encoding=3, text=value)
elif tag == "artist":
return TPE1(encoding=3, text=value)
elif tag == "title":
return TIT2(encoding=3, text=value)
elif tag == "album":
return TALB(encoding=3, text=value)
elif tag == "album_artist":
return TPE2(encoding=3, text=value)
else:
raise ValueError(f"Unknown tag type {tag}!")
def get_target_name(self, zeroes: int) -> str:
return f"{self.tags['track'].zfill(zeroes)} {self.tags['title']}.{self.format}"
def has_cover(self) -> bool:
if self.format == "flac":
# needs to be handled separately from ogg, as it doesn't use the vorbis tags for cover art for whatever reason
return len(self.m_file.pictures) != 0
if format_lookup[self.format] == "vorbis":
return "metadata_block_picture" in self.m_tags and len(self.m_tags["metadata_block_picture"]) != 0
if format_lookup[self.format] == "id3":
apics: List[APIC] = self.m_tags.getall("APIC")
for apic in apics:
if apic.type == PictureType.COVER_FRONT:
return True
return False
if format_lookup[self.format] == "m4a":
return 'covr' in self.m_tags and len(self.m_tags['covr']) != 0
raise NotImplementedError("Song format not yet implemented.")
def set_cover(self, to_embed: Union[Picture, APIC, MP4Cover]) -> None:
# embed cover art
if self.format == "flac":
self.m_file.clear_pictures()
self.m_file.add_picture(to_embed)
elif format_lookup[self.format] == "vorbis":
self.m_tags["metadata_block_picture"] = [b64encode(to_embed.write()).decode("ascii")]
elif format_lookup[self.format] == "id3":
self.m_tags.add(to_embed)
elif format_lookup[self.format] == "m4a":
self.m_tags['covr'] = [to_embed]
self.m_file.save()
def __getitem__(self, item: str) -> str:
return self.tags[item]
def log(message: str, importance: int = 0) -> None:
if not args.quiet or importance > 0:
print(message)
@ -202,10 +49,9 @@ def die(message: str, code: int = 1) -> None:
def sanitise(in_str: str) -> str:
if args.sanitise:
return re.sub(r"[?\\/:|*\"<>]", "_", in_str)
return re.sub(sanitisation_regex, "_", in_str)
return in_str
def main() -> None:
global args, tmp_dir

157
bcao/song_info.py Normal file
View file

@ -0,0 +1,157 @@
from . import *
import re
from os import path
from typing import Union, List, Dict
from pathlib import Path
from base64 import b64encode
import mutagen
# noinspection PyProtectedMember
from mutagen.flac import Picture
from mutagen.mp4 import MP4Cover
# noinspection PyProtectedMember
from mutagen.id3 import APIC, PictureType, Frame, TRCK, TPE1, TIT2, TALB, TPE2
class FallbackError(Exception):
pass
class SongInfo:
tag_lookup: Dict[str, Dict[str, str]] = {
"track": {"id3": "TRCK", "m4a": "trkn", "vorbis": "tracknumber"},
"artist": {"id3": "TPE1", "m4a": "©ART", "vorbis": "artist"},
"title": {"id3": "TIT2", "m4a": "©nam", "vorbis": "title"},
"album": {"id3": "TALB", "m4a": "©alb", "vorbis": "album"},
"album_artist": {"id3": "TPE2", "m4a": "aART", "vorbis": "albumartist"}
}
def __init__(self, file_name: Path):
self.m_file: MutagenFile = mutagen.File(file_name)
self.m_tags: MutagenTags = self.m_file.tags
self.file_name = str(file_name.name)
self.format = path.splitext(file_name)[1][1:]
self.fallback = False
if self.format not in format_lookup:
raise ValueError(f"Unsupported file type: {self.format}")
fallbacks = re.match(
r"^(?P<artist>.+) - (?P<album>.+) - (?P<track>\d{2,}) (?P<title>.+)\.(?:ogg|flac|aiff|wav|mp3|m4a)$",
self.file_name
)
if fallbacks is None:
raise FallbackError("Couldn't determine fallback tags!")
# set default values for the tags, in case the file is missing any (or all!) of them
self.tags: Dict[str, str] = {
"track": str(int(fallbacks.group("track"))), # convert to int and str again to turn e.g. "01" into "1"
"artist": fallbacks.group("artist"),
"title": fallbacks.group("title"),
"album": fallbacks.group("album"),
"album_artist": fallbacks.group("artist")
}
# set list_tags to the default tags in list form
# i.e. for every tag, set list_tags[x] = [tags[x]]
self.list_tags: Dict[str, List[str]] = dict((x[0], [x[1]]) for x in self.tags.items())
if self.m_tags is None:
# file has no tags
# generate empty tags
self.m_file.add_tags()
self.m_tags = self.m_file.tags
self.fallback = True
# write fallback tags to file
for standard_name, tag_set in self.tag_lookup.items():
tag = tag_set[format_lookup[self.format]]
self.m_tags[tag] = self.new_id3_tag(standard_name, self.tags[standard_name])
self.m_file.save()
else:
for standard_name, tag_set in self.tag_lookup.items():
tag = tag_set[format_lookup[self.format]]
if tag not in self.m_tags:
print(f"{tag} not in self.m_tags")
self.fallback = True
continue
value_list = self.m_tags[tag]
if self.format == "m4a" and standard_name == "track":
# every tag in the MP4 file (from what i can tell) is a list
# this includes the track number tag, which is a tuple of ints in a list.
# because every other format is either a non-list, or a list of non-lists, we need to account for this case
# (a list of lists of non-lists) specially, by turning it into a list of non-lists.
value_list = value_list[0]
if not isinstance(value_list, (list, tuple)):
value_list = [value_list]
# convert the list of strings/ID3 frames/ints/whatevers to sanitised strings
value_list = [re.sub(sanitisation_regex, "_", str(val)) for val in value_list]
self.tags[standard_name] = value_list[0]
self.list_tags[standard_name] = value_list
@staticmethod
def new_id3_tag(tag: str, value: str) -> Frame:
if tag == "track":
return TRCK(encoding=3, text=value)
elif tag == "artist":
return TPE1(encoding=3, text=value)
elif tag == "title":
return TIT2(encoding=3, text=value)
elif tag == "album":
return TALB(encoding=3, text=value)
elif tag == "album_artist":
return TPE2(encoding=3, text=value)
else:
raise ValueError(f"Unknown tag type {tag}!")
def get_target_name(self, zeroes: int) -> str:
return f"{self.tags['track'].zfill(zeroes)} {self.tags['title']}.{self.format}"
def has_cover(self) -> bool:
if self.format == "flac":
# needs to be handled separately from ogg, as it doesn't use the vorbis tags for cover art for whatever reason
return len(self.m_file.pictures) != 0
if format_lookup[self.format] == "vorbis":
return "metadata_block_picture" in self.m_tags and len(self.m_tags["metadata_block_picture"]) != 0
if format_lookup[self.format] == "id3":
apics: List[APIC] = self.m_tags.getall("APIC")
for apic in apics:
if apic.type == PictureType.COVER_FRONT:
return True
return False
if format_lookup[self.format] == "m4a":
return 'covr' in self.m_tags and len(self.m_tags['covr']) != 0
raise NotImplementedError("Song format not yet implemented.")
def set_cover(self, to_embed: Union[Picture, APIC, MP4Cover]) -> None:
# embed cover art
if self.format == "flac":
self.m_file.clear_pictures()
self.m_file.add_picture(to_embed)
elif format_lookup[self.format] == "vorbis":
self.m_tags["metadata_block_picture"] = [b64encode(to_embed.write()).decode("ascii")]
elif format_lookup[self.format] == "id3":
self.m_tags.add(to_embed)
elif format_lookup[self.format] == "m4a":
self.m_tags['covr'] = [to_embed]
self.m_file.save()
def __getitem__(self, item: str) -> str:
return self.tags[item]