Compare commits

...

17 Commits

Author SHA1 Message Date
Rafael Moraes a989d9fefa Include index and total for music-video media fetch 2026-04-24 12:17:19 -03:00
Rafael Moraes fd3b6216c9 Use error() for URL parse errors 2026-04-24 12:08:24 -03:00
Rafael Moraes 84c21c0013 Pass total=1 when fetching single Apple Music song 2026-04-24 12:06:49 -03:00
Rafael Moraes aca3339b16 Remove string fallback for media_index 2026-04-24 12:04:52 -03:00
Rafael Moraes 6d6f9f4441 Provide index=0 to _get_song_media call 2026-04-24 12:01:51 -03:00
Rafael Moraes fe98bdb42c Process download items inline, remove queue 2026-04-24 11:55:35 -03:00
Rafael Moraes 7c8b20d8f3 Include track index/total in media objects 2026-04-24 11:55:11 -03:00
Rafael Moraes 6232493eed Add index and total fields to AppleMusicMedia 2026-04-24 11:54:57 -03:00
Rafael Moraes 09997bd6a1 Document --wrapper-m3u8-ip CLI option 2026-04-24 11:36:32 -03:00
Rafael Moraes 54c318908c Bump version to 3.1 2026-04-24 11:33:59 -03:00
Rafael Moraes dc6f2e8506 Use ExceptionPrettyPrinter and .exception logging 2026-04-24 11:26:21 -03:00
Rafael Moraes eff41a40f5 Await get_wrapper_m3u8 call 2026-04-24 11:22:33 -03:00
Rafael Moraes b00163a71c Add optional m3u8 wrapper support 2026-04-24 11:18:01 -03:00
Rafael Moraes 9f60043375 Add wrapper m3u8 IP and consolidate use_wrapper 2026-04-24 11:17:34 -03:00
Rafael Moraes 004ecd7c64 Guard against missing response on HTTP errors 2026-04-24 11:17:04 -03:00
Rafael Moraes 581bb7e094 Make GamdlApiResponseError.content optional 2026-04-24 11:15:57 -03:00
Rafael Moraes 5fd10d897e Extract cover URL formatting to helper 2026-04-23 11:45:57 -03:00
14 changed files with 186 additions and 100 deletions
+1
View File
@@ -142,6 +142,7 @@ The file is created automatically on first run. Command-line arguments override
| `--cover-format` | Cover format | `jpg` |
| `--cover-size` | Cover size in pixels | `1200` |
| `--wvd-path` | .wvd file path | - |
| `--wrapper-m3u8-ip` | Wrapper m3u8 IP address and port | - |
| **Song Options** | | |
| `--synced-lyrics-format` | Synced lyrics format | `lrc` |
| `--song-codec-priority` | Comma-separated codec priority | `aac-legacy` |
+1 -1
View File
@@ -1 +1 @@
__version__ = "3.0"
__version__ = "3.1"
+17 -10
View File
@@ -70,6 +70,7 @@ class AppleMusicApi:
async def get_token() -> str:
log = logger.bind(action="get_token")
response = None
async with httpx.AsyncClient() as client:
try:
response = await client.get(
@@ -81,7 +82,7 @@ class AppleMusicApi:
except httpx.HTTPError:
raise GamdlApiResponseError(
"Error fetching Apple Music homepage",
status_code=response.status_code,
status_code=response.status_code if response is not None else None,
)
index_js_uri_match = re.search(
@@ -94,6 +95,7 @@ class AppleMusicApi:
)
index_js_uri = index_js_uri_match.group(1)
response = None
async with httpx.AsyncClient(follow_redirects=True) as client:
try:
response = await client.get(
@@ -104,7 +106,7 @@ class AppleMusicApi:
except httpx.HTTPError:
raise GamdlApiResponseError(
"Error fetching index.js page",
status_code=response.status_code,
status_code=response.status_code if response is not None else None,
)
token_match = re.search('(?=eyJh)(.*?)(?=")', index_js_page)
@@ -124,6 +126,7 @@ class AppleMusicApi:
) -> dict:
log = logger.bind(action="get_account_info", meta=meta)
response = None
async with httpx.AsyncClient() as client:
try:
response = await client.get(
@@ -142,7 +145,7 @@ class AppleMusicApi:
except httpx.HTTPError:
raise GamdlApiResponseError(
"Error fetching account info",
status_code=response.status_code,
status_code=response.status_code if response is not None else None,
)
log.debug("success", account_info=account_info)
@@ -243,6 +246,7 @@ class AppleMusicApi:
*args,
**kwargs,
) -> "AppleMusicApi":
response = None
async with httpx.AsyncClient() as client:
try:
response = await client.get(wrapper_account_url)
@@ -251,7 +255,7 @@ class AppleMusicApi:
except httpx.HTTPError:
raise GamdlApiResponseError(
"Error fetching wrapper account info",
status_code=response.status_code,
status_code=response.status_code if response is not None else None,
)
return await cls.create(
@@ -266,6 +270,7 @@ class AppleMusicApi:
uri: str,
params: dict | None = None,
) -> dict:
response = None
try:
response = await self.client.get(
APPLE_MUSIC_AMP_API_URL + uri,
@@ -276,8 +281,8 @@ class AppleMusicApi:
except httpx.HTTPError:
raise GamdlApiResponseError(
"Error fetching from AMP API",
content=response.text,
status_code=response.status_code,
content=response.text if response is not None else None,
status_code=response.status_code if response is not None else None,
)
if "errors" in response_json:
@@ -533,6 +538,7 @@ class AppleMusicApi:
) -> dict:
log = logger.bind(action="get_webplayback", track_id=track_id)
response = None
try:
response = await self.client.post(
APPLE_MUSIC_WEBPLAYBACK_API_URL,
@@ -546,8 +552,8 @@ class AppleMusicApi:
except httpx.HTTPError:
raise GamdlApiResponseError(
"Error fetching webplayback data",
content=response.text,
status_code=response.status_code,
content=response.text if response is not None else None,
status_code=response.status_code if response is not None else None,
)
if "dialog" in webplayback:
@@ -570,6 +576,7 @@ class AppleMusicApi:
) -> dict:
log = logger.bind(action="get_license_exchange", track_id=track_id)
response = None
try:
response = await self.client.post(
APPLE_MUSIC_LICENSE_API_URL,
@@ -587,8 +594,8 @@ class AppleMusicApi:
except httpx.HTTPError:
raise GamdlApiResponseError(
"Error fetching license exchange data",
content=response.text,
status_code=response.status_code,
content=response.text if response is not None else None,
status_code=response.status_code if response is not None else None,
)
if license_exchange.get("status") != 0:
+1 -1
View File
@@ -9,7 +9,7 @@ class GamdlApiResponseError(GamdlApiError):
def __init__(
self,
message: str,
content: str,
content: str | None = None,
status_code: int | None = None,
):
self.message = message
+8 -5
View File
@@ -30,6 +30,7 @@ class ItunesApi:
async def get_storefront_id(storefront: str) -> int:
log = logger.bind(action="get_storefront_id", storefront=storefront)
response = None
async with httpx.AsyncClient() as client:
try:
response = await client.get(APPLE_MUSIC_MUSIC_KIT_URL)
@@ -38,7 +39,7 @@ class ItunesApi:
except httpx.HTTPError:
raise GamdlApiResponseError(
"Error fetching MusicKit content",
status_code=response.status_code,
status_code=response.status_code if response is not None else None,
)
normalized_storefront = storefront.upper()
@@ -92,6 +93,7 @@ class ItunesApi:
) -> dict:
log = logger.bind(action="get_lookup_result", media_id=media_id, entity=entity)
response = None
try:
response = await self.client.get(
ITUNES_LOOKUP_API_URL,
@@ -107,8 +109,8 @@ class ItunesApi:
except httpx.HTTPError:
raise GamdlApiResponseError(
"Error fetching iTunes lookup result",
content=response.text,
status_code=response.status_code,
content=response.text if response is not None else None,
status_code=response.status_code if response is not None else None,
)
log.debug("success", lookup_result=lookup_result)
@@ -126,6 +128,7 @@ class ItunesApi:
media_id=media_id,
)
response = None
try:
response = await self.client.get(
ITUNES_PAGE_API_URL.format(media_type=media_type, media_id=media_id),
@@ -138,8 +141,8 @@ class ItunesApi:
except httpx.HTTPError:
raise GamdlApiResponseError(
"Error fetching iTunes page",
content=response.text,
status_code=response.status_code,
content=response.text if response is not None else None,
status_code=response.status_code if response is not None else None,
)
log.debug("success", itunes_page=itunes_page)
+54 -59
View File
@@ -1,6 +1,5 @@
import asyncio
import logging
import traceback
from functools import wraps
from pathlib import Path
@@ -78,6 +77,7 @@ async def main(config: CliConfig):
structlog.configure(
processors=[
structlog.processors.add_log_level,
structlog.processors.ExceptionPrettyPrinter(),
custom_structlog_formatter,
],
logger_factory=structlog.stdlib.LoggerFactory(),
@@ -142,6 +142,8 @@ async def main(config: CliConfig):
apple_music_api=apple_music_api,
cover_format=config.cover_format,
cover_size=config.cover_size,
use_wrapper=config.use_wrapper,
wrapper_m3u8_ip=config.wrapper_m3u8_ip,
wvd_path=config.wvd_path,
)
@@ -184,7 +186,6 @@ async def main(config: CliConfig):
mp4decrypt_path=config.mp4decrypt_path,
ffmpeg_path=config.ffmpeg_path,
mp4box_path=config.mp4box_path,
use_wrapper=config.use_wrapper,
wrapper_decrypt_ip=config.wrapper_decrypt_ip,
download_mode=config.download_mode,
album_folder_template=config.album_folder_template,
@@ -245,64 +246,58 @@ async def main(config: CliConfig):
url_log.info(f'Processing "{url}"')
try:
download_queue: list[DownloadItem] = []
async for media in downloader.get_download_item_from_url(url):
download_queue.append(media)
except GamdlInterfaceUrlParseError as e:
url_log.warning(f"{e}")
continue
except Exception as e:
url_log.error(f'Error processing "{url}": {e}')
error_count += 1
if not config.no_exceptions:
traceback.print_exc()
continue
async for download_item in downloader.get_download_item_from_url(url):
media_index = download_item.media.index + 1
media_total = download_item.media.total or "-"
for download_index, download_item in enumerate(
download_queue,
1,
):
track_log = logger.bind(
action=f"Track {download_index:>3}/{len(download_queue):<3}"
)
media_title = (
download_item.media.media_metadata["attributes"]["name"]
if download_item.media.media_metadata
and download_item.media.media_metadata.get("attributes", {}).get("name")
else "Unknown Title"
)
track_log.info(f'Downloading "{media_title}"')
try:
await downloader.download(download_item)
except (
GamdlInterfaceMediaNotStreamableError,
GamdlInterfaceFormatNotAvailableError,
GamdlInterfaceDecryptionNotAvailableError,
GamdlInterfaceArtistMediaTypeError,
GamdlDownloaderSyncedLyricsOnlyError,
GamdlDownloaderMediaFileExistsError,
GamdlDownloaderDependencyNotFoundError,
GamdlDownloaderFlatFilterExcludedError,
) as e:
track_log.warning(f'Skipping "{media_title}": {e}')
continue
except Exception as e:
error_count += 1
track_log.error(f'Error downloading "{media_title}"')
if not config.no_exceptions:
traceback.print_exc()
if (
database
and download_item.media.media_metadata
and download_item.final_path
):
database.add(
download_item.media.media_metadata["id"],
download_item.final_path,
track_log = logger.bind(
action=f"Track {media_index:>3}/{media_total:<3}"
)
media_title = (
download_item.media.media_metadata["attributes"]["name"]
if download_item.media.media_metadata
and download_item.media.media_metadata.get("attributes", {}).get(
"name"
)
else "Unknown Title"
)
track_log.info(f'Downloading "{media_title}"')
try:
await downloader.download(download_item)
except (
GamdlInterfaceMediaNotStreamableError,
GamdlInterfaceFormatNotAvailableError,
GamdlInterfaceDecryptionNotAvailableError,
GamdlInterfaceArtistMediaTypeError,
GamdlDownloaderSyncedLyricsOnlyError,
GamdlDownloaderMediaFileExistsError,
GamdlDownloaderDependencyNotFoundError,
GamdlDownloaderFlatFilterExcludedError,
) as e:
track_log.warning(f'Skipping "{media_title}": {e}')
continue
except Exception as e:
error_count += 1
track_log.exception(f'Error downloading "{media_title}"')
if (
database
and download_item.media.media_metadata
and download_item.final_path
):
database.add(
download_item.media.media_metadata["id"],
download_item.final_path,
)
except GamdlInterfaceUrlParseError as e:
url_log.error(f"{e}")
continue
except Exception as e:
url_log.exception(f'Error processing "{url}": {e}')
error_count += 1
continue
logger.info(f"Finished with {error_count} error(s)")
+16 -8
View File
@@ -210,6 +210,22 @@ class CliConfig:
),
),
]
use_wrapper: Annotated[
bool,
option(
"--use-wrapper",
help="Use wrapper for decrypting songs",
is_flag=True,
),
]
wrapper_m3u8_ip: Annotated[
str,
option(
"--wrapper-m3u8-ip",
help="Wrapper m3u8 IP address and port",
default=base_interface_create_sig.parameters["wrapper_m3u8_ip"].default,
),
]
# Song Interface Options
synced_lyrics_format: Annotated[
SyncedLyricsFormat,
@@ -328,14 +344,6 @@ class CliConfig:
default=base_downloader_sig.parameters["mp4box_path"].default,
),
]
use_wrapper: Annotated[
bool,
option(
"--use-wrapper",
help="Use wrapper for decrypting songs",
is_flag=True,
),
]
wrapper_decrypt_ip: Annotated[
str,
option(
+1 -1
View File
@@ -96,7 +96,7 @@ class AppleMusicSongDownloader:
staged_path=staged_path,
)
if self.base.use_wrapper and not legacy:
if self.base.interface.base.use_wrapper and not legacy:
await self._decrypt_amdecrypt(
encrypted_path,
staged_path,
+24 -4
View File
@@ -29,12 +29,16 @@ class AppleMusicBaseInterface:
itunes_api: ItunesApi,
cover_format: CoverFormat,
cover_size: int,
use_wrapper: bool,
wrapper_m3u8_ip: str,
cdm: Cdm,
) -> None:
self.apple_music_api = apple_music_api
self.itunes_api = itunes_api
self.cover_format = cover_format
self.cover_size = cover_size
self.use_wrapper = use_wrapper
self.wrapper_m3u8_ip = wrapper_m3u8_ip
self.cdm = cdm
@staticmethod
@@ -103,14 +107,28 @@ class AppleMusicBaseInterface:
return response
@staticmethod
def format_cover(
template_cover_url: str,
cover_size: int,
cover_format: CoverFormat,
) -> str:
return re.sub(
r"/\{w\}x\{h\}([a-z]{2})\.jpg",
f"/{cover_size}x{cover_size}bb.{cover_format.value}",
template_cover_url,
)
@classmethod
async def create(
cls,
apple_music_api: AppleMusicApi,
cover_format: CoverFormat = CoverFormat.JPG,
cover_size: int = 1200,
itunes_api: ItunesApi | None = None,
use_wrapper: bool = False,
wrapper_m3u8_ip: str = "127.0.0.1:20020",
wvd_path: str | None = None,
itunes_api: ItunesApi | None = None,
):
itunes_api = itunes_api or await ItunesApi.create(
storefront=apple_music_api.storefront,
@@ -123,6 +141,8 @@ class AppleMusicBaseInterface:
itunes_api=itunes_api,
cover_format=cover_format,
cover_size=cover_size,
use_wrapper=use_wrapper,
wrapper_m3u8_ip=wrapper_m3u8_ip,
cdm=cdm,
)
return base
@@ -246,10 +266,10 @@ class AppleMusicBaseInterface:
if self.cover_format == CoverFormat.RAW:
cover_url = template_url
else:
cover_url = re.sub(
r"/\{w\}x\{h\}([a-z]{2})\.jpg",
f"/{self.cover_size}x{self.cover_size}bb.{self.cover_format.value}",
cover_url = self.format_cover(
template_url,
self.cover_size,
self.cover_format,
)
cover_file_extension = await self._get_cover_file_extension(cover_url)
+35 -7
View File
@@ -66,6 +66,8 @@ class AppleMusicInterface:
async def _get_song_media(
self,
index: int,
total: int = 0,
media_id: str | None = None,
media_metadata: dict | None = None,
playlist_metadata: dict | None = None,
@@ -84,13 +86,15 @@ class AppleMusicInterface:
return AppleMusicMedia(
media_id=media_id,
media_metadata=None,
index=index,
total=total,
error=e,
)
if not media_id:
media_id = self.base.parse_catalog_media_id(media_metadata)
base_media = AppleMusicMedia(media_id, media_metadata)
base_media = AppleMusicMedia(media_id, media_metadata, index, total)
if self.flat_filter_function:
flat_filter_result = self.flat_filter_function(media_metadata)
@@ -113,17 +117,22 @@ class AppleMusicInterface:
return base_media
try:
return await self.song.get_media(
media = await self.song.get_media(
media_metadata,
playlist_metadata,
playlist_track,
)
media.index = index
media.total = total
return media
except Exception as e:
base_media.error = e
return base_media
async def _get_music_video_media(
self,
index: int,
total: int = 0,
media_id: str | None = None,
media_metadata: dict | None = None,
playlist_metadata: dict | None = None,
@@ -140,13 +149,15 @@ class AppleMusicInterface:
return AppleMusicMedia(
media_id=media_id,
media_metadata=None,
index=index,
total=total,
error=e,
)
if not media_id:
media_id = self.music_video.parse_catalog_media_id(media_metadata)
media_id = self.base.parse_catalog_media_id(media_metadata)
base_media = AppleMusicMedia(media_id, media_metadata)
base_media = AppleMusicMedia(media_id, media_metadata, index, total)
if self.flat_filter_function:
flat_filter_result = self.flat_filter_function(media_metadata)
@@ -169,11 +180,14 @@ class AppleMusicInterface:
return base_media
try:
return await self.music_video.get_media(
media = await self.music_video.get_media(
media_metadata,
playlist_metadata,
playlist_track,
)
media.index = index
media.total = total
return media
except Exception as e:
base_media.error = e
return base_media
@@ -278,18 +292,22 @@ class AppleMusicInterface:
tasks = [
(
self._get_song_media(
index=index,
total=media_metadata["attributes"]["trackCount"],
media_id=track["id"],
media_metadata=track,
playlist_metadata=media_metadata,
)
if track["type"] in {"songs", "library-songs"}
else self._get_music_video_media(
index=index,
total=media_metadata["attributes"]["trackCount"],
media_id=track["id"],
media_metadata=track,
playlist_metadata=media_metadata,
)
)
for track in tracks
for index, track in enumerate(tracks)
]
if self.concurrency == 1:
@@ -374,6 +392,7 @@ class AppleMusicInterface:
tasks = [
(
self._get_song_media(
index=index,
media_id=track["id"],
media_metadata=track,
playlist_metadata=media_metadata,
@@ -381,6 +400,7 @@ class AppleMusicInterface:
)
if track["type"] in {"songs", "library-songs"}
else self._get_music_video_media(
index=index,
media_id=track["id"],
media_metadata=track,
playlist_metadata=media_metadata,
@@ -498,7 +518,7 @@ class AppleMusicInterface:
selected_items = items[:1]
tasks = []
for item in selected_items:
for index, item in enumerate(selected_items):
if item["type"] in {"songs", "library-songs"}:
tasks.append(
(
@@ -506,6 +526,8 @@ class AppleMusicInterface:
self._get_song_media(
media_id=item["id"],
media_metadata=item,
index=index,
total=len(selected_items),
),
)
)
@@ -525,6 +547,8 @@ class AppleMusicInterface:
self._get_music_video_media(
media_id=item["id"],
media_metadata=item,
index=index,
total=len(selected_items),
),
)
)
@@ -571,12 +595,16 @@ class AppleMusicInterface:
if url_info.type == "song" or url_info.sub_id:
media = await self._get_song_media(
index=0,
total=1,
media_id=url_info.sub_id or url_info.id,
)
yield media
elif url_info.type == "music-video":
media = await self._get_music_video_media(
index=0,
total=1,
media_id=url_info.id,
)
yield media
+24 -2
View File
@@ -5,6 +5,7 @@ import json
import re
from typing import Callable
from xml.dom import minidom
import struct
from xml.etree import ElementTree
import m3u8
@@ -258,6 +259,25 @@ class AppleMusicSongInterface:
else:
return await self._get_stream_info(song_metadata, codec)
async def get_wrapper_m3u8(self, adam_id: str) -> str | None:
host, port = self.base.wrapper_m3u8_ip.split(":")
reader, writer = await asyncio.open_connection(host, port)
data = struct.pack("B", len(adam_id)) + adam_id.encode()
writer.write(data)
await writer.drain()
response = await reader.readuntil(b"\n")
m3u8_url = response.decode().strip()
writer.close()
await writer.wait_closed()
if m3u8_url:
return m3u8_url
return None
async def _get_stream_info(
self,
song_metadata: dict,
@@ -272,8 +292,10 @@ class AppleMusicSongInterface:
)
)["data"][0]
m3u8_master_url = song_metadata["attributes"]["extendedAssetUrls"].get(
"enhancedHls"
m3u8_master_url = (
await self.get_wrapper_m3u8(self.base.parse_catalog_media_id(song_metadata))
if self.base.use_wrapper
else song_metadata["attributes"]["extendedAssetUrls"].get("enhancedHls")
)
if not m3u8_master_url:
return None
+2
View File
@@ -156,6 +156,8 @@ class Cover:
class AppleMusicMedia:
media_id: str
media_metadata: dict
index: int = 0
total: int = 0
error: BaseException | None = None
playlist_metadata: dict | None = None
playlist_tags: PlaylistTags | None = None
+1 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "gamdl"
version = "3.0"
version = "3.1"
description = "A command-line app for downloading Apple Music songs, music videos and post videos."
readme = "README.md"
license = "MIT"
Generated
+1 -1
View File
@@ -223,7 +223,7 @@ wheels = [
[[package]]
name = "gamdl"
version = "3.0"
version = "3.1"
source = { virtual = "." }
dependencies = [
{ name = "async-lru" },