Skip to content
Closed
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
32 changes: 30 additions & 2 deletions spotify_ripper/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@
import csv
import re

import spotipy
import spotipy.client
from spotipy.oauth2 import SpotifyClientCredentials

client_credentials_sp = None

def init_client_credentials_sp():

global client_credentials_sp
if client_credentials_sp is None:
client_credentials_manager = SpotifyClientCredentials()
client_credentials_sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
client_credentials_sp.trace = False

return client_credentials_sp

def get_album(albumURI):
sp = init_client_credentials_sp()
return sp.album(albumURI)

class WebAPI(object):

Expand All @@ -32,8 +51,17 @@ def get_cached_result(self, name, uri):
return self.cache[name].get(uri)

def request_json(self, url, msg):
res = self.request_url(url, msg)
return res.json() if res is not None else res
print(Fore.GREEN + "Attempting to retrieve " + msg +
" from Spotify's Web API" + Fore.RESET)
print(Fore.CYAN + url + Fore.RESET)
sp = init_client_credentials_sp()
try:
res = sp._get(url)
except spotify.SpotifyException as e:
print(Fore.YELLOW + "URL returned non-200 HTTP code: " +
str(e.http_status) + " message: " + e.msg + Fore.RESET)
return None
return res

def request_url(self, url, msg):
print(Fore.GREEN + "Attempting to retrieve " + msg +
Expand Down