Align the fetch-lyrics summary with fetch-artists via shared utils

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-04 15:34:26 +08:00
co-authored by Claude Fable 5
parent 81975b72e4
commit 0d3cd69a0f
8 changed files with 68 additions and 36 deletions
+1 -19
View File
@@ -37,6 +37,7 @@ from sqlalchemy.orm import Session
from . import VERSION
from .database import ds
from .models import Artist, Song, SongArtist
from .utils import format_duration
API_URL: str = "https://www.wikidata.org/w/api.php"
"""The URL of the Wikidata API endpoint."""
@@ -779,25 +780,6 @@ def write_snapshot(file: TextIO) -> None:
writer.writerows(ordered)
def format_duration(seconds: float) -> str:
"""Format an elapsed duration for the closing summary line.
:param seconds: The elapsed duration, in seconds.
:return: The duration formatted ``mm:ss``, or ``h:mm:ss``
once it reaches one hour.
"""
total: int = round(seconds)
hours: int
remainder: int
hours, remainder = divmod(total, 3600)
minutes: int
secs: int
minutes, secs = divmod(remainder, 60)
if hours > 0:
return f"{hours}:{minutes:02d}:{secs:02d}"
return f"{minutes:02d}:{secs:02d}"
def main(argv: list[str] | None = None) -> int:
"""Fetch the artist metadata from Wikidata.
@@ -43,6 +43,7 @@ from .models import (
Song,
SongArtist,
)
from .utils import format_duration
PROVENANCE_FIELDS: Sequence[str] = (
"song_id", "source", "method", "acquired_at", "note")
@@ -228,6 +229,7 @@ def main(argv: list[str] | None = None) -> int:
:return: The exit status: 0 on success, misses included,
non-zero on a setup error.
"""
started: float = time.monotonic()
args: argparse.Namespace = parse_args(argv)
fetcher: LyricsFetcher = LyricsFetcher()
fetched: int = 0
@@ -264,6 +266,9 @@ def main(argv: list[str] | None = None) -> int:
return 1
finally:
session.close()
print(f"done: {fetched} fetched, {missed} missed",
attempted: int = fetched + missed
elapsed: str = format_duration(time.monotonic() - started)
print(f"Done. Fetched lyrics for {fetched}/{attempted}"
f" songs. {elapsed} elapsed.",
file=sys.stderr)
return 0
+24
View File
@@ -0,0 +1,24 @@
# Tools for A Feminist Audit of Pop Music.
# Copyright 2026 imacat. All rights reserved.
# Authors:
# imacat@mail.imacat.idv.tw (imacat), 2026/8/4
"""The shared utilities of the package."""
def format_duration(seconds: float) -> str:
"""Format an elapsed duration for the closing summary line.
:param seconds: The elapsed duration, in seconds.
:return: The duration formatted ``mm:ss``, or ``h:mm:ss``
once it reaches one hour.
"""
total: int = round(seconds)
hours: int
remainder: int
hours, remainder = divmod(total, 3600)
minutes: int
secs: int
minutes, secs = divmod(remainder, 60)
if hours > 0:
return f"{hours}:{minutes:02d}:{secs:02d}"
return f"{minutes:02d}:{secs:02d}"