File size: 7,965 Bytes
5a30a94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""Spotify Web API client with token caching and retry handling."""

import base64
import os
import time
from typing import Any, Dict, List, Optional

import requests
from dotenv import load_dotenv

load_dotenv(os.path.join(os.path.dirname(__file__), "..", ".env"))


class SpotifyClient:
    """Minimal Spotify Web API client (Client Credentials flow)."""

    AUTH_URL = "https://accounts.spotify.com/api/token"
    API_BASE = "https://api.spotify.com/v1"

    def __init__(
        self,
        client_id: Optional[str] = None,
        client_secret: Optional[str] = None,
        timeout: int = 20,
    ):
        self.client_id = client_id or os.getenv("SPOTIFY_CLIENT_ID")
        self.client_secret = client_secret or os.getenv("SPOTIFY_CLIENT_SECRET")
        self.timeout = timeout
        self._access_token: Optional[str] = None
        self._token_expires_at = 0.0

        if (
            not self.client_id
            or not self.client_secret
            or "your_spotify_client_id_here" in self.client_id
            or "your_spotify_client_secret_here" in self.client_secret
        ):
            raise RuntimeError(
                "Spotify credentials are missing. Set SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET in .env."
            )

    def _refresh_access_token(self) -> str:
        encoded = base64.b64encode(
            f"{self.client_id}:{self.client_secret}".encode("utf-8")
        ).decode("utf-8")
        headers = {
            "Authorization": f"Basic {encoded}",
            "Content-Type": "application/x-www-form-urlencoded",
        }

        response = requests.post(
            self.AUTH_URL,
            headers=headers,
            data={"grant_type": "client_credentials"},
            timeout=self.timeout,
        )
        if response.status_code != 200:
            raise RuntimeError(
                f"Spotify auth failed ({response.status_code}): {response.text}"
            )

        payload = response.json()
        token = payload.get("access_token")
        expires_in = int(payload.get("expires_in", 3600))
        if not token:
            raise RuntimeError("Spotify auth failed: missing access token")

        self._access_token = token
        self._token_expires_at = time.time() + max(expires_in - 30, 30)
        return token

    def _get_access_token(self) -> str:
        if self._access_token and time.time() < self._token_expires_at:
            return self._access_token
        return self._refresh_access_token()

    def _request(
        self,
        method: str,
        path: str,
        params: Optional[Dict[str, Any]] = None,
        retries: int = 5,
    ) -> Dict[str, Any]:
        url = path if path.startswith("http") else f"{self.API_BASE}{path}"
        backoff = 0.5

        for attempt in range(retries):
            token = self._get_access_token()
            headers = {"Authorization": f"Bearer {token}"}
            response = requests.request(
                method,
                url,
                params=params,
                headers=headers,
                timeout=self.timeout,
            )

            if response.status_code == 401 and attempt < retries - 1:
                self._access_token = None
                time.sleep(backoff)
                backoff = min(backoff * 2, 8)
                continue

            if response.status_code == 429 and attempt < retries - 1:
                retry_after = response.headers.get("Retry-After")
                sleep_for = float(retry_after) if retry_after else backoff
                sleep_for = min(sleep_for, 10)  # Cap at 10s to avoid long hangs
                time.sleep(max(sleep_for, 0.5))
                backoff = min(backoff * 2, 8)
                continue

            if response.status_code >= 500 and attempt < retries - 1:
                time.sleep(backoff)
                backoff = min(backoff * 2, 8)
                continue

            if response.status_code >= 400:
                raise RuntimeError(
                    f"Spotify API error ({response.status_code}): {response.text}"
                )

            if not response.text:
                return {}
            return response.json()

        raise RuntimeError("Spotify API request failed after retries")

    @staticmethod
    def _dedupe_by_id(items: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
        seen = set()
        deduped = []
        for item in items:
            item_id = item.get("id")
            if not item_id or item_id in seen:
                continue
            seen.add(item_id)
            deduped.append(item)
        return deduped

    def search_artist(self, name: str, limit: int = 5) -> List[Dict[str, Any]]:
        payload = self._request(
            "GET",
            "/search",
            params={
                "q": name,
                "type": "artist",
                "limit": max(1, min(limit, 50)),
            },
        )
        return payload.get("artists", {}).get("items", [])

    def get_artist(self, artist_id: str) -> Dict[str, Any]:
        return self._request("GET", f"/artists/{artist_id}")

    def get_artist_top_tracks(self, artist_id: str, market: str = "IN") -> List[Dict[str, Any]]:
        payload = self._request(
            "GET",
            f"/artists/{artist_id}/top-tracks",
            params={"market": market},
        )
        return payload.get("tracks", [])

    def get_artist_albums(
        self,
        artist_id: str,
        include_groups: str = "album,single",
        limit: int = 20,
        market: Optional[str] = None,
    ) -> List[Dict[str, Any]]:
        results: List[Dict[str, Any]] = []
        offset = 0
        # Some Spotify apps currently enforce a lower cap for this endpoint.
        limit = max(1, min(limit, 100))

        while len(results) < limit:
            page_limit = min(50, limit - len(results))
            params: Dict[str, Any] = {
                "include_groups": include_groups,
                "limit": page_limit,
                "offset": offset,
            }
            if market:
                params["market"] = market

            payload = self._request("GET", f"/artists/{artist_id}/albums", params=params)
            items = payload.get("items", [])
            if not items:
                break

            results.extend(items)
            if not payload.get("next"):
                break
            offset += len(items)

        return self._dedupe_by_id(results)

    def get_album_tracks(self, album_id: str, limit: int = 50) -> List[Dict[str, Any]]:
        results: List[Dict[str, Any]] = []
        offset = 0
        limit = max(1, min(limit, 200))

        while len(results) < limit:
            page_limit = min(50, limit - len(results))
            payload = self._request(
                "GET",
                f"/albums/{album_id}/tracks",
                params={"limit": page_limit, "offset": offset},
            )
            items = payload.get("items", [])
            if not items:
                break

            results.extend(items)
            if not payload.get("next"):
                break
            offset += len(items)

        return self._dedupe_by_id(results)

    def get_tracks(self, track_ids: List[str]) -> List[Dict[str, Any]]:
        if not track_ids:
            return []

        unique_ids = []
        seen = set()
        for track_id in track_ids:
            if track_id and track_id not in seen:
                seen.add(track_id)
                unique_ids.append(track_id)

        results: List[Dict[str, Any]] = []
        for i in range(0, len(unique_ids), 50):
            batch = unique_ids[i : i + 50]
            payload = self._request("GET", "/tracks", params={"ids": ",".join(batch)})
            tracks = payload.get("tracks", [])
            for track in tracks:
                if track:
                    results.append(track)
        return self._dedupe_by_id(results)