Skip to content

Apple Music search fails: cookies_path fallback error and token not found in index.js #112

Description

@elszrt

Problem

Apple Music search fails in OrpheusDL-GUI v2.0.3 with the Apple Music module.

I encountered two related errors.

First error:

AppleMusicApi.create() got an unexpected keyword argument 'cookies_path'

After patching that locally, Apple Music search then fails with:

Search failed: Token not found in index.js page

Environment

  • OS: Windows
  • OrpheusDL-GUI version: v2.0.3
  • Platform: Apple Music
  • Module path:
    • modules/applemusic/interface.py
    • modules/applemusic/gamdl/gamdl/api/apple_music_api.py

Cause 1: cookies_path is passed to AppleMusicApi.create()

In modules/applemusic/interface.py, the code builds kwargs with:

kwargs['cookies_path'] = str(cookies_path)

Then it tries:

self.apple_music_api = await AppleMusicApi.create_from_netscape_cookies(**kwargs)

If that fails, it falls back to:

self.apple_music_api = await AppleMusicApi.create(**kwargs)

But kwargs still contains cookies_path, and AppleMusicApi.create() does not accept that argument.

Suggested fix 1

Before calling AppleMusicApi.create(**kwargs), remove cookies_path:

kwargs.pop('cookies_path', None)
self.apple_music_api = await AppleMusicApi.create(**kwargs)

Example:

if cookies_path and cookies_path.exists():
    kwargs['cookies_path'] = str(cookies_path)
    try:
        self.apple_music_api = await AppleMusicApi.create_from_netscape_cookies(**kwargs)
    except Exception as ce:
        if self._debug:
            print(f"[Apple Music Debug] Cookie initialization failed: {ce}. Falling back to guest.")
        kwargs.pop('cookies_path', None)
        self.apple_music_api = await AppleMusicApi.create(**kwargs)
else:
    kwargs.pop('cookies_path', None)
    self.apple_music_api = await AppleMusicApi.create(**kwargs)

Cause 2: Apple Music token regex is too strict

After fixing the first issue, search fails with:

Token not found in index.js page

In modules/applemusic/gamdl/gamdl/api/apple_music_api.py, the token regex appears to expect a token starting with eyJh:

token_match = re.search('(?=eyJh)(.*?)(?=")', index_js_page)

Current Apple Music web bundles can contain valid JWT tokens starting with other prefixes, for example eyJ0....

Also, the current index file regex may only target index-legacy, while Apple Music can serve either index~...js or index-legacy~...js.

Suggested fix 2

Support both modern and legacy index bundles:

index_js_uri_match = re.search(
    r"/(assets/index(?:-legacy)?[~-][^/\"]+\.js)",
    home_page,
)

Use a more general JWT regex:

token_match = re.search(
    r'"?(eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+)"?',
    index_js_page,
)

Expected behavior

Apple Music search should return results when valid Apple Music cookies are configured.

Actual behavior

Search fails during API initialization/token extraction.

Notes

No cookies, tokens, account credentials, or protected files are included in this report.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions