Ejemplo n.º 1
0
 def getUserInformation(self, parse=True):
     error = refreshAndRetry(self, lambda: self.getUserInformation())
     res = safeGet(URL + "me", bearer=self.accessToken, error=error)
     if not res: return None
     if parse:
         return json.load(res)
     else:
         return res.read()
Ejemplo n.º 2
0
 def getAudioFeatures(self, trackIDs, parse=True):
     error = refreshAndRetry(self, lambda: self.getAudioFeatures())
     res = safeGet(URL + "audio-features/?ids=" + ",".join(trackIDs),
                   bearer=self.accessToken,
                   error=error)
     if not res: return None
     if parse:
         return json.load(res)
     else:
         return res.read()
Ejemplo n.º 3
0
 def getPlaylists(self, parse=True):
     error = refreshAndRetry(self, lambda: self.getPlaylists())
     res = safeGet(URL + "me/playlists",
                   bearer=self.accessToken,
                   error=error)
     if not res: return None
     if parse:
         return json.load(res)
     else:
         return res.read()
Ejemplo n.º 4
0
 def getGenres(self, parse=True):
     error = refreshAndRetry(self, lambda: self.getGenres())
     res = safeGet(URL + "recommendations/available-genre-seeds",
                   bearer=self.accessToken,
                   error=error)
     if not res: return None
     if parse:
         return json.load(res)
     else:
         return res.read()
Ejemplo n.º 5
0
 def getAudioAnalysis(self, trackID, parse=True):
     error = refreshAndRetry(self, lambda: self.getAudioAnalysis())
     res = safeGet(URL + "audio-analysis/" + trackID,
                   bearer=self.accessToken,
                   error=error)
     if not res: return None
     if parse:
         return json.load(res)
     else:
         return res.read()
Ejemplo n.º 6
0
 def setPlaylistName(self, playlistID, name):
     error = refreshAndRetry(
         self, lambda: self.setPlaylistName(playlistID, name, parse=parse))
     res = safeGet(URL + "playlists/" + playlistID,
                   method="PUT",
                   bearer=self.accessToken,
                   error=error,
                   data={"name": name},
                   json=True)
     if not res: return None
     return True
Ejemplo n.º 7
0
 def setTracks(self, uris, playlistID, parse=True):
     error = refreshAndRetry(self, lambda: self.addTracks())
     res = safeGet(URL + "playlists/" + playlistID + "/tracks",
                   data={"uris": uris},
                   json=True,
                   method="PUT",
                   bearer=self.accessToken,
                   error=error)
     if not res: return None
     if parse:
         return json.load(res)
     else:
         return res.read()
Ejemplo n.º 8
0
 def getFromCode(cls, code, callback, error=None):
     b64string = base64.b64encode(
         "%s:%s" % (getenv('SPOTIFY_ID'), getenv('SPOTIFY_SECRET')))
     auth = "Basic " + b64string
     res = safeGet("https://accounts.spotify.com/api/token", {
         "grant_type": "authorization_code",
         "code": code,
         "redirect_uri": callback,
         "client_id": getenv("SPOTIFY_ID"),
         "client_secret": getenv("SPOTIFY_SECRET")
     },
                   auth=auth,
                   error=error)
     if not res: return None
     data = json.load(res)
     return SpotifyAPI(accessToken=data["access_token"],
                       refreshToken=data["refresh_token"])
Ejemplo n.º 9
0
 def search(self, query, limit=20, offset=0, parse=True):
     searchType = "artist,track"
     params = {
         "q": query,
         "type": searchType,
         "limit": limit,
         "offset": offset
     }
     error = refreshAndRetry(self, lambda: self.search())
     res = safeGet(URL + "search?" + urllib.urlencode(params),
                   bearer=self.accessToken,
                   error=error)
     if not res: return None
     if parse:
         return json.load(res)
     else:
         return res.read()
Ejemplo n.º 10
0
 def createPlaylist(self, username, playlistName, parse=True):
     error = refreshAndRetry(self, lambda: self.createPlaylist())
     playlistData = {
         "name": playlistName,
         "public": False,
         "description": "A running playlist made just for you by ZoomyTunez"
     }
     res = safeGet(URL + "users/" + username + "/playlists",
                   bearer=self.accessToken,
                   data=playlistData,
                   error=error,
                   json=True)
     if not res: return None
     if parse:
         return json.load(res)
     else:
         return res.read()
Ejemplo n.º 11
0
    def get(self):
        if 'error' in self.request.GET:
            self.response.status = 401
            self.response.write("<h1>Authorization unsuccessful</h1>")
            self.response.write("<p>An error occured: <code>")
            self.response.write(
                cgi.escape(self.request.GET['error']) + "</code>.</p>")
            return
        code = self.request.GET['code']
        scope = self.request.GET['scope']

        if scope != STRAVA_SCOPE:
            self.response.status = 401
            self.response.write("<h1>Authorization not granted</h1>")
            self.response.write("<p>Cannot read private activities. ")
            self.response.write(
                '<a href="/auth/login/strava">(try again)</a></p>')
            self.response.write(
                '<script>history.replaceState({}, "", location.pathname)</script>'
            )
            return

        def errorHandler(e):
            self.response.status = 500
            self.response.write(
                "An error occurred when getting the authorization token.")

        resp = safeGet(STRAVA_TOKEN_URL, {
            "grant_type": "authorization_code",
            "code": code,
            "client_id": getenv("STRAVA_ID"),
            "client_secret": getenv("STRAVA_SECRET")
        },
                       error=errorHandler)
        if not resp: return

        data = json.load(resp)

        # self.response.headers.add("Content-Type", "application/json")
        self.response.write("<h1>Authorization successful</h1>")
        self.response.write(
            "<p>The following information was received:</p><pre>")
        self.response.write(cgi.escape(pretty(data)) + "</pre>")
        self.response.write(
            '<script>history.replaceState({}, "", location.pathname)</script>')
Ejemplo n.º 12
0
 def refreshAccessToken(self, error=None):
     """Gets a new refresh token for this user; returns true if successful"""
     b64string = base64.b64encode(
         "%s:%s" % (getenv('SPOTIFY_ID'), getenv('SPOTIFY_SECRET')))
     auth = "Basic " + b64string
     res = safeGet("https://accounts.spotify.com/api/token",
                   data={
                       "grant_type": "refresh_token",
                       "refresh_token": self.refreshToken
                   },
                   auth=auth,
                   error=error)
     if not res: return None
     logging.warn(res)
     data = json.load(res)
     self.accessToken = data["access_token"]
     if self.dbUser:
         self.dbUser.spotifyAccessToken = self.accessToken
         self.dbUser.put()
     return True
Ejemplo n.º 13
0
 def getRecommendations(self,
                        artists,
                        tracks,
                        genres,
                        bpmrange=None,
                        parse=True):
     params = {
         "seed_artists": artists,
         "seed_tracks": tracks,
         "seed_genres": genres,
         "limit": 100
     }
     if bpmrange:
         params["min_tempo"] = bpmrange[0]
         params["max_tempo"] = bpmrange[1]
     error = refreshAndRetry(self, lambda: self.getRecommendations())
     res = safeGet(URL + "recommendations?" + urllib.urlencode(params),
                   bearer=self.accessToken,
                   error=error)
     if not res: return None
     if parse:
         return json.load(res)
     else:
         return res.read()