forked from shatfield4/SongSwipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserfunctions.py
More file actions
154 lines (112 loc) · 5.05 KB
/
userfunctions.py
File metadata and controls
154 lines (112 loc) · 5.05 KB
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
from flask import redirect
import spotipy
import pprint
from spotipy.oauth2 import SpotifyClientCredentials
from dotenv import load_dotenv, find_dotenv
import os
from spotipy.oauth2 import SpotifyOAuth
from sqlite_test import Sqlite_test
import json
import random
# Load env variables
load_dotenv(find_dotenv())
# Env Variables
CLIENT_ID = os.environ.get("CLIENT_ID")
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
REDIRECT_URI = os.environ.get("REDIRECT_URI")
# scope = 'user-read-playback-state'
scope = 'user-follow-read'
# Intialize spotipy instance
# client_credentials_manager = SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
oAuth = auth_manager=SpotifyOAuth(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, scope=scope, redirect_uri=REDIRECT_URI)
sp = spotipy.Spotify(auth_manager=oAuth)
pp = pprint.PrettyPrinter(indent=2)
sqlite = Sqlite_test(r'database.db')
def getArtist(name):
results = sp.search(q='artist:' + name, type='artist')
items = results['artists']['items']
if len(items) > 0:
return items[0]
else:
return None
def getArtistGenres(name):
results = sp.search(q='artist:' + name, type='artist')
items = results['artists']['items']
if len(items) > 0:
return items[0]['genres']
else:
return None
def getReccomendationFromArtist(artist):
artistGenreInfo = getArtistGenres(artist)
allReccomendationGenres = sp.recommendation_genre_seeds()
validGenres = []
if artistGenreInfo != None and len(artistGenreInfo) > 0:
# Loop through genres grabbed from calling getArtistGenres
for n in range(0, len(artistGenreInfo)):
currentGrabbedGenre = artistGenreInfo[n]
# Loop through all genres from spotify and add them to the validGenres dictionary if there is a match
for x in range(0, len(allReccomendationGenres['genres'])):
if currentGrabbedGenre == allReccomendationGenres['genres'][x]:
validGenres.append(currentGrabbedGenre)
else:
print('No results found...')
print('Valid Genres: ' + str(validGenres))
if len(validGenres) > 0:
print('\n\n\nReccomendations:')
for i in range(len(sp.recommendations(seed_genres=validGenres)['tracks'])):
pp.pprint(sp.recommendations(seed_genres=validGenres)['tracks'][i]['artists'][0]['name'])
pp.pprint(sp.recommendations(seed_genres=validGenres)['tracks'][i]['artists'][0]['id'])
# pp.pprint(sp.recommendations(seed_genres=validGenres))
print('\n\n\n')
def getUsername():
return sp.current_user()['display_name']
# def getFollowedArtists():
# followedArtists = len(sp.current_user_followed_artists()['artists']['items'])
# for i in range(followedArtists):
# print(i+1, ". ", sp.current_user_followed_artists()['artists']['items'][i]['name'])
def getFollowedArtists():
response = {}
followResults = sp.current_user_followed_artists()
followedArtists = len(followResults['artists']['items'])
i = random.randint(0,followedArtists-1)
response['name'] = followResults['artists']['items'][i]['name']
response['artistID'] = followResults['artists']['items'][i]['id']
if len(followResults['artists']['items'][i]['genres']) == 0 :
response['genre'] = ''
else:
response['genre'] = followResults['artists']['items'][i]['genres'][0]
return(response)
def getRelatedArtists(name):
response = {}
results = sp.search(q='artist:' + name, type='artist')
artist = results['artists']['items'][0]['id']
relatedResults = sp.artist_related_artists(artist)
relatedArtistsLength = len(relatedResults['artists'])
i = random.randint(0,relatedArtistsLength-1)
response['name'] = relatedResults['artists'][i]['name']
response['artistID'] = relatedResults['artists'][i]['id']
response['song_url'] = relatedResults['artists'][i]['uri']
if len(relatedResults['artists'][i]['genres']) == 0 :
response['genre'] = ''
else:
response['genre'] = relatedResults['artists'][i]['genres'][0]
if len(relatedResults['artists'][i]['images']) == 0:
response['img_url'] = 'https://i.scdn.co/image/ab6761610000e5ebf4593f7b778219838d858c34'
else:
response['img_url'] = relatedResults['artists'][i]['images'][0]['url']
return(response)
if __name__ == '__main__':
Current_user = getUsername()
# sqlite.addToSavedArtists("SZA", "RB")
# sqlite.removeArtist("SZA")
# sqlite.cursor.execute("DROP TABLE Song")
# pp.pprint(sp.current_user()['display_name'])
artist_name = input("Input artist name: ")
# pp.pprint(getArtist(artist_name))
print("\n\n\n\n")
# getReccomendationFromArtist(artist_name)
# getRelatedArtists('0BMfVLB7t0VCzNBZZKBy6A')
# pp.pprint(sp.artist_related_artists('1AhjOkOLkbHUfcHDSErXQs')['artists'])
# print(Current_user, "'s following artists:\n")
print(getRelatedArtists(artist_name))
# print(getFollowedArtists())