Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions votify/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,49 @@ def update_playlist_file(
playlist_file_lines[playlist_track - 1] = final_path_relative.as_posix() + "\n"
with playlist_file_path.open("w", encoding="utf8") as playlist_file:
playlist_file.writelines(playlist_file_lines)

def get_gid_metadata(
self,
media_id: str,
media_type: str,
) -> dict:
gid = self.spotify_api.media_id_to_gid(media_id)
return self.spotify_api.get_gid_metadata(gid, media_type)
metadata = self.spotify_api.get_gid_metadata(gid, media_type)

target_key = "file" if media_type == "track" else "audio"

existing_entries = metadata.get(target_key, [])
if any(e.get("format") in ("MP4_256", "MP4_128") for e in existing_entries):
return metadata

playback_info = self.spotify_api.get_track_playback_info(media_id, media_type)
if not playback_info:
return metadata

media_key = f"spotify:{media_type}:{media_id}"
file_ids_mp4 = (
playback_info.get("media", {})
.get(media_key, {})
.get("item", {})
.get("manifest", {})
.get("file_ids_mp4", [])
)
if not file_ids_mp4:
return metadata

metadata.setdefault(target_key, [])

for item in file_ids_mp4:
file_id = item.get("file_id")
bitrate = item.get("bitrate")

fmt = {256000: "MP4_256", 128000: "MP4_128"}.get(bitrate)
if file_id and fmt:
entry = {"file_id": file_id, "format": fmt}
if entry not in metadata[target_key]:
metadata[target_key].append(entry)

return metadata

def get_playplay_decryption_key(self, file_id: str) -> bytes:
raise NotImplementedError()
Expand Down
19 changes: 19 additions & 0 deletions votify/spotify_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SpotifyApi:
GID_METADATA_API_URL = "https://spclient.wg.spotify.com/metadata/4/{media_type}/{gid}?market=from_token"
PATHFINDER_API_URL = "https://api-partner.spotify.com/pathfinder/v1/query"
VIDEO_MANIFEST_API_URL = "https://gue1-spclient.spotify.com/manifests/v7/json/sources/{gid}/options/supports_drm"
TRACK_PLAYBACK_API_URL = "https://gue1-spclient.spotify.com/track-playback/v1/media/spotify:{type}:{id}"
PLAYPLAY_LICENSE_API_URL = (
"https://gew4-spclient.spotify.com/playplay/v1/key/{file_id}"
)
Expand All @@ -43,6 +44,7 @@ class SpotifyApi:
EXTEND_TRACK_COLLECTION_WAIT_TIME = 0.5
SERVER_TIME_URL = "https://open.spotify.com/api/server-time"
SESSION_TOKEN_URL = "https://open.spotify.com/api/token"
CLIENT_TOKEN_URL = "https://clienttoken.spotify.com/v1/clienttoken"

def __init__(
self,
Expand Down Expand Up @@ -183,6 +185,23 @@ def get_gid_metadata(
check_response(response)
return response.json()

def get_track_playback_info(
self,
media_id: str,
media_type: str
) -> dict | None:
self._refresh_session_auth()

params = {"manifestFileFormat": ["file_ids_mp4"]}

response = self.session.get(
self.TRACK_PLAYBACK_API_URL.format(type=media_type, id=media_id),
params=params
)

check_response(response)
return response.json()

def get_lyrics(self, track_id: str) -> dict | None:
self._refresh_session_auth()
response = self.session.get(self.LYRICS_API_URL.format(track_id=track_id))
Expand Down
2 changes: 1 addition & 1 deletion votify/totp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# thanks to https://github.com/glomatico/votify/pull/42#issuecomment-2727036757
class TOTP:
SPOTIFY_SECRETS_JSON = "https://raw.githubusercontent.com/Thereallo1026/spotify-secrets/refs/heads/main/secrets/secretDict.json"
SPOTIFY_SECRETS_JSON = "https://code.thetadev.de/ThetaDev/spotify-secrets/raw/branch/main/secrets/secretDict.json"
PERIOD = 30
DIGITS = 6

Expand Down