Skip to content

Commit

Permalink
Add test and note for handling track without ID3 header
Browse files Browse the repository at this point in the history
  • Loading branch information
chazeon committed May 29, 2024
1 parent 5218fb5 commit 0016ff2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Binary file added tests/2/data/id3_removed.mp3
Binary file not shown.
48 changes: 48 additions & 0 deletions tests/2/test_id3_tagging_with_missing_id3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sys
from pathlib import Path
import shutil
import mutagen

TESTS_DIR = Path(__file__).parent.parent
sys.path.insert(0, str(TESTS_DIR.parent))
sys.path.insert(0, str(TESTS_DIR.parent / "vistopia"))
from vistopia.visitor import Visitor

CURR_TEST_DIR = Path(__file__).parent


# The missing_id3 file is created using the following command:
# ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t 0.01 -q:a 9 -acodec libmp3lame regular.mp3
# ffmpeg -i regular.mp3 -map 0:a -codec:a copy -write_id3v1 0 -id3v2_version 0 id3_removed.mp3


def test_id3_tagging_with_missing_id3(tmpdir):

test_mp3 = tmpdir / "id3_removed.mp3"

shutil.copyfile(
CURR_TEST_DIR / "data" / "id3_removed.mp3",
test_mp3
)

Visitor.retag(
test_mp3,
article_info={
"title": "测试标题",
"sort_number": "7",
"content_url": "http://example.com",
},
series_info={
"title": "测试系列",
"author": "测试作者",
},
catalog_info={}
)

from mutagen.easyid3 import EasyID3
tag = EasyID3(test_mp3)

assert tag["title"] == ["测试标题"]
assert tag["album"] == ["测试系列"]
assert tag["artist"] == ["测试作者"]

3 changes: 2 additions & 1 deletion vistopia/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def save_transcript_with_single_file(self, id: int,
print(f"Failed to fetch page using single-file: {e}")

@staticmethod
def retag(fname, article_info, catalog_info, series_info):
def retag(fname, article_info: dict, catalog_info: dict, series_info: dict):

from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3NoHeaderError
Expand All @@ -158,6 +158,7 @@ def retag(fname, article_info, catalog_info, series_info):
track = EasyID3(fname)
except ID3NoHeaderError:
# No ID3 tag found, creating a new ID3 tag
# See: https://github.com/quodlibet/mutagen/issues/327
track = EasyID3()

track['title'] = article_info['title']
Expand Down

0 comments on commit 0016ff2

Please sign in to comment.