-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
66 lines (47 loc) · 1.67 KB
/
functions.py
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
from flask import Flask, url_for, session
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import time
import config
CLIENT_ID = config.api_id
CLIENT_SECRET = config.api_secret
sp_scope = "playlist-modify-public, playlist-modify-private, user-library-read, user-top-read"
def create_spotify_oauth():
# Return upon receiving Spotify authorization
return SpotifyOAuth(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=url_for("authorize", _external=True),
scope=sp_scope)
def get_token():
# Get token from session
token_info = session.get("token_info", None)
if not token_info:
raise "exception"
# If token expires within next 2 minutes, refresh it
current_time = int(time.time())
expiration = token_info["expires_at"] - current_time < 120
if (expiration):
sp_oauth = create_spotify_oauth()
token_info = sp_oauth.refresh_access_token(token_info["refresh_token"])
return token_info
def format_songs(song_dict, key):
post_songs = []
for i in song_dict[key]:
artists = []
for j in i["artists"]:
artists.append(j["name"])
name = i["name"]
image = i["album"]["images"][0]["url"]
song = {"name": name, "artists": artists, "image": image}
post_songs.append(song)
return post_songs
def format_artists(artists_dict, key):
post_artists = []
for i in artists_dict[key]:
name = i["name"]
popularity = i["popularity"]
image = i["images"][0]["url"]
artist = {"name": name, "popularity": popularity, "image": image}
post_artists.append(artist)
return post_artists