Skip to content

Commit

Permalink
Typecheck with mypy, lint and format with ruff (#499)
Browse files Browse the repository at this point in the history
* Lint and format with ruff, check types with mypy

* Fix mypy checks

* Update CI

* Remove shebangs

* Ignore EXE rules

* Fix EOL

* Fix mypy for 3.7

* Fix CI

* Fix CI

* Ensure filelock path exists

* ci: various ci fixes (#500)

---------

Co-authored-by: Arsenii es3n1n <[email protected]>
  • Loading branch information
7x11x13 and es3n1n authored Jul 9, 2024
1 parent 399ea88 commit 9fd4814
Show file tree
Hide file tree
Showing 15 changed files with 801 additions and 549 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
name: pypi-publish
on:
push:
branches: [ master ]
tags:
- 'v*'
pull_request:
branches: [ master ]
jobs:
test:
strategy:
matrix:
version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install ffmpeg
run: |
sudo apt update
sudo apt install -yq --no-install-recommends ffmpeg
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.version }}
- name: Install dependencies
run: |
pip install -e .[dev]
- name: Lint
run: ruff check
- name: Format check
run: ruff format --check
- name: Type check
run: mypy
- name: Test
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
AUTH_TOKEN: ${{ secrets.AUTH_TOKEN }}
run: |
pytest --exitfirst
publish:
needs: test
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ __pycache__/
.venv
.env
.coverage*
.idea
.idea
.python-version
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[mypy]
packages = scdl, tests
check_untyped_defs = true
6 changes: 5 additions & 1 deletion requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
pytest
music-tag
music-tag
ruff
mypy
types-requests
types-tqdm
13 changes: 13 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target-version = "py37"
line-length = 100

[lint]
select = ["ALL"]
ignore = [
"C90", "D",
"S", "BLE", "FBT", "A", "EM", "FA", "G", "SLF", "PTH",
"PLR", "TRY",
"PLW2901", "ANN204",
"COM812", "ISC001",
"EXE"
]
2 changes: 1 addition & 1 deletion scdl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- encoding: utf-8 -*-
"""Python Soundcloud Music Downloader."""

__version__ = "v2.10.0"
102 changes: 50 additions & 52 deletions scdl/metadata_assembler.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from base64 import b64encode
from dataclasses import dataclass
from typing import Optional, Type, TypeVar, Union, Callable
from types import MappingProxyType
from functools import singledispatch
from typing import Optional, Union

from mutagen import FileType, flac, oggopus, id3, wave, mp3, mp4
from mutagen import FileType, flac, id3, mp3, mp4, oggopus, wave


JPEG_MIME_TYPE: str = 'image/jpeg'
JPEG_MIME_TYPE: str = "image/jpeg"


@dataclass(frozen=True)
Expand All @@ -26,6 +25,11 @@ class MetadataInfo:
album_track_num: Optional[int]


@singledispatch
def assemble_metadata(file: FileType, meta: MetadataInfo) -> None: # noqa: ARG001
raise NotImplementedError


def _get_flac_pic(jpeg_data: bytes) -> flac.Picture:
pic = flac.Picture()
pic.data = jpeg_data
Expand All @@ -39,119 +43,113 @@ def _get_apic(jpeg_data: bytes) -> id3.APIC:
encoding=3,
mime=JPEG_MIME_TYPE,
type=3,
desc='Cover',
desc="Cover",
data=jpeg_data,
)


def _assemble_common(file: FileType, meta: MetadataInfo) -> None:
file['artist'] = meta.artist
file['title'] = meta.title
file["artist"] = meta.artist
file["title"] = meta.title

if meta.genre:
file['genre'] = meta.genre
file["genre"] = meta.genre

if meta.link:
file['website'] = meta.link
file["website"] = meta.link

if meta.date:
file['date'] = meta.date
file["date"] = meta.date

if meta.album_title:
file['album'] = meta.album_title
file["album"] = meta.album_title

if meta.album_author:
file['albumartist'] = meta.album_author
file["albumartist"] = meta.album_author

if meta.album_track_num is not None:
file['tracknumber'] = str(meta.album_track_num)
file["tracknumber"] = str(meta.album_track_num)


def _assemble_flac(file: flac.FLAC, meta: MetadataInfo) -> None:
@assemble_metadata.register(flac.FLAC)
def _(file: flac.FLAC, meta: MetadataInfo) -> None:
_assemble_common(file, meta)

if meta.description:
file['description'] = meta.description
file["description"] = meta.description

if meta.artwork_jpeg:
file.add_picture(_get_flac_pic(meta.artwork_jpeg))


def _assemble_opus(file: oggopus.OggOpus, meta: MetadataInfo) -> None:
@assemble_metadata.register(oggopus.OggOpus)
def _(file: oggopus.OggOpus, meta: MetadataInfo) -> None:
_assemble_common(file, meta)

if meta.description:
file['comment'] = meta.description
file["comment"] = meta.description

if meta.artwork_jpeg:
pic = _get_flac_pic(meta.artwork_jpeg).write()
file['metadata_block_picture'] = b64encode(pic).decode()
file["metadata_block_picture"] = b64encode(pic).decode()


def _assemble_wav_or_mp3(file: Union[wave.WAVE, mp3.MP3], meta: MetadataInfo) -> None:
file['TIT2'] = id3.TIT2(encoding=3, text=meta.title)
file['TPE1'] = id3.TPE1(encoding=3, text=meta.artist)
@assemble_metadata.register(mp3.MP3)
@assemble_metadata.register(wave.WAVE)
def _(file: Union[wave.WAVE, mp3.MP3], meta: MetadataInfo) -> None:
file["TIT2"] = id3.TIT2(encoding=3, text=meta.title)
file["TPE1"] = id3.TPE1(encoding=3, text=meta.artist)

if meta.description:
file['COMM'] = id3.COMM(encoding=3, lang='ENG', text=meta.description)
file["COMM"] = id3.COMM(encoding=3, lang="ENG", text=meta.description)

if meta.genre:
file['TCON'] = id3.TCON(encoding=3, text=meta.genre)
file["TCON"] = id3.TCON(encoding=3, text=meta.genre)

if meta.link:
file['WOAS'] = id3.WOAS(url=meta.link)
file["WOAS"] = id3.WOAS(url=meta.link)

if meta.date:
file['TDAT'] = id3.TDAT(encoding=3, text=meta.date)
file["TDAT"] = id3.TDAT(encoding=3, text=meta.date)

if meta.album_title:
file['TALB'] = id3.TALB(encoding=3, text=meta.album_title)
file["TALB"] = id3.TALB(encoding=3, text=meta.album_title)

if meta.album_author:
file['TPE2'] = id3.TPE2(encoding=3, text=meta.album_author)
file["TPE2"] = id3.TPE2(encoding=3, text=meta.album_author)

if meta.album_track_num is not None:
file['TRCK'] = id3.TRCK(encoding=3, text=str(meta.album_track_num))
file["TRCK"] = id3.TRCK(encoding=3, text=str(meta.album_track_num))

if meta.artwork_jpeg:
file['APIC'] = _get_apic(meta.artwork_jpeg)
file["APIC"] = _get_apic(meta.artwork_jpeg)


def _assemble_mp4(file: mp4.MP4, meta: MetadataInfo) -> None:
file['\251ART'] = meta.artist
file['\251nam'] = meta.title
@assemble_metadata.register(mp4.MP4)
def _(file: mp4.MP4, meta: MetadataInfo) -> None:
file["\251ART"] = meta.artist
file["\251nam"] = meta.title

if meta.genre:
file['\251gen'] = meta.genre
file["\251gen"] = meta.genre

if meta.link:
file['\251cmt'] = meta.link
file["\251cmt"] = meta.link

if meta.date:
file['\251day'] = meta.date
file["\251day"] = meta.date

if meta.album_title:
file['\251alb'] = meta.album_title
file["\251alb"] = meta.album_title

if meta.album_author:
file['aART'] = meta.album_author
file["aART"] = meta.album_author

if meta.album_track_num is not None:
file['trkn'] = str(meta.album_track_num)
file["trkn"] = str(meta.album_track_num)

if meta.description:
file['desc'] = meta.description
file["desc"] = meta.description

if meta.artwork_jpeg:
file['covr'] = [mp4.MP4Cover(meta.artwork_jpeg)]


T = TypeVar('T')
METADATA_ASSEMBLERS: MappingProxyType[Type[T], Callable[[T, MetadataInfo], None]] = MappingProxyType({
flac.FLAC: _assemble_flac,
oggopus.OggOpus: _assemble_opus,
wave.WAVE: _assemble_wav_or_mp3,
mp3.MP3: _assemble_wav_or_mp3,
mp4.MP4: _assemble_mp4,
})

file["covr"] = [mp4.MP4Cover(meta.artwork_jpeg)]
Loading

0 comments on commit 9fd4814

Please sign in to comment.