Skip to content

Commit 28a3a66

Browse files
committed
playlist apis now require a token too
- adds a 9 minutes expiry on the cache file
1 parent 55938ca commit 28a3a66

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

downloader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def remove_empty_files(outpath):
167167
def download_playlist_tracks(playlist_link, outpath, create_folder, trackname_convention, token, max_attempts=3, mode='playlist'):
168168
# print(f"\n{mode[0].upper()}{mode[1:]} link identified")
169169
print(f"\n{mode.capitalize()} link identified")
170-
song_list_dict, playlist_name_old = get_playlist_info(playlist_link, trackname_convention, mode)
170+
song_list_dict, playlist_name_old = get_playlist_info(playlist_link, trackname_convention, mode, token)
171171
playlist_name = re.sub(NAME_SANITIZE_REGEX, "_", playlist_name_old)
172172
if (playlist_name != playlist_name_old):
173173
print(f'\n"{playlist_name_old}" is not a valid folder name. Using "{playlist_name}" instead.')

spotify_api.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ def get_track_info(link, token):
77
response = requests.get(f"https://api.spotidownloader.com/download/{track_id}?token={token}", headers=CUSTOM_HEADER)
88
return response.json()
99

10-
def get_playlist_info(link, trackname_convention, mode):
10+
def get_playlist_info(link, trackname_convention, mode, token):
1111
playlist_id = link.split("/")[-1].split("?")[0]
12-
response = requests.get(f"https://api.spotidownloader.com/metadata/{mode}/{playlist_id}", headers=CUSTOM_HEADER)
12+
response = requests.get(f"https://api.spotidownloader.com/metadata/{mode}/{playlist_id}?token={token}", headers=CUSTOM_HEADER)
1313
metadata = response.json()
1414
playlist_name = metadata['title']
1515
if metadata['success']:
@@ -18,12 +18,12 @@ def get_playlist_info(link, trackname_convention, mode):
1818

1919
print(f"Getting songs from {mode} (this might take a while ...)")
2020
track_list = []
21-
response = requests.get(f"https://api.spotidownloader.com/tracks/{mode}/{playlist_id}", headers=CUSTOM_HEADER)
21+
response = requests.get(f"https://api.spotidownloader.com/tracks/{mode}/{playlist_id}?token={token}", headers=CUSTOM_HEADER)
2222
tracks_data = response.json()
2323
track_list.extend(tracks_data['trackList'])
2424
next_offset = tracks_data['nextOffset']
2525
while next_offset:
26-
response = requests.get(f"https://api.spotidownloader.com/tracks/{mode}/{playlist_id}?offset={next_offset}", headers=CUSTOM_HEADER)
26+
response = requests.get(f"https://api.spotidownloader.com/tracks/{mode}/{playlist_id}?offset={next_offset}&token={token}", headers=CUSTOM_HEADER)
2727
tracks_data = response.json()
2828
track_list.extend(tracks_data['trackList'])
2929
next_offset = tracks_data['nextOffset']

utils.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import json
4+
import time
45
from config import NAME_SANITIZE_REGEX
56
from models import Song
67

@@ -70,16 +71,19 @@ def trackname_convention():
7071
return "Title - Artist", 1
7172

7273
def get_token(reset=False):
73-
if os.path.exists("./.cache") and not reset:
74-
with open(".cache") as f:
75-
token = json.load(f).get("token")
74+
cache_file = "./.cache"
75+
if os.path.exists(cache_file) and not reset:
7676

77-
else:
77+
file_age = time.time() - os.path.getmtime(cache_file)
78+
79+
if file_age > 540:
80+
reset = True
81+
82+
if reset or not os.path.exists(cache_file):
7883
token = input("Enter Token: ").strip()
79-
with open(".cache", "w") as f:
80-
json.dump(
81-
{
82-
"token": token,
83-
}, f
84-
)
84+
with open(cache_file, "w") as f:
85+
json.dump({"token": token}, f)
86+
else:
87+
with open(cache_file) as f:
88+
token = json.load(f).get("token")
8589
return token

0 commit comments

Comments
 (0)