-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicFlyer2.py
118 lines (92 loc) · 4.12 KB
/
musicFlyer2.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
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
import requests
import json
class search:
def __init__(self):
self.accessToken = None
self.artist = None
self.artistUrl = None
self.artistImage = None
self.artistId = None
self.songNames = {}
self.artistEmbed = None
self.relatedArtists={}
def getAccessToken(self):
url = 'https://accounts.spotify.com/api/token'
clientID = '35f0eceb93b64bd99da23d2517513a37'
clientSecret = 'ecdacecd08894c6f9f7cd07ed6e1b9c9'
grantType = 'client_credentials'
body_params = {'grant_type': grantType}
requestToken = requests.post(url, data=body_params, auth=(clientID, clientSecret))
jsonData = json.loads(requestToken.text)
accessToken = jsonData["access_token"]
self.accessToken = accessToken
def searchArtist(self, artist):
searchUrl = 'https://api.spotify.com/v1/search'
searchParams = {'q': artist, 'type': 'artist'}
headers = {"Authorization": "Bearer " + self.accessToken}
requestArtist = requests.get(searchUrl, params=searchParams, headers=headers)
jsonArtist = requestArtist.json()
self.artist = jsonArtist
self.artistUrl = jsonArtist["artists"]["items"][0]["external_urls"]["spotify"]
self.artistImage = jsonArtist["artists"]["items"][0]["images"][0]["url"]
self.artistId = jsonArtist["artists"]["items"][0]["id"]
def addEmbedtoUrl(self, url):
newUrl = url[0:25] + 'embed/' + url[25:]
return newUrl
def searchArtistEmbed(self):
searchUrl = 'https://api.spotify.com/v1/artists/' + self.artistId
headers = {"Authorization": "Bearer " + self.accessToken}
requestArtistInfo = requests.get(searchUrl, headers=headers)
jsonArtist = requestArtistInfo.json()
self.artistEmbed = self.addEmbedtoUrl(jsonArtist["external_urls"]["spotify"])
print(self.artistEmbed)
def searchArtistTopTracks(self):
tracksUrl = 'https://api.spotify.com/v1/artists/' + self.artistId + '/top-tracks'
tracksParams = {'country': 'US'}
headers = {"Authorization": "Bearer " + self.accessToken}
requestArtistTopTracks = requests.get(tracksUrl, params=tracksParams, headers=headers)
jsonArtist = requestArtistTopTracks.json()
for i in range(0, 5):
self.songNames[jsonArtist["tracks"][i]["name"]] = self.addEmbedtoUrl(
jsonArtist["tracks"][i]["external_urls"]["spotify"])
# Gets related artists
def searchRelatedArtists(self):
artistsUrl = 'https://api.spotify.com/v1/artists/' + self.artistId + '/related-artists'
artistParams = {'country': 'US'}
headers = {"Authorization": "Bearer " + self.accessToken}
requestArtistTopTracks = requests.get(artistsUrl, params=artistParams, headers=headers)
jsonArtist = requestArtistTopTracks.json()
print(jsonArtist)
# Loops through the json object for related artists to get three artists' info
for i in range(0, 3):
artistInfo = []
# Adds a link to the profile and profile image of an artist
artistInfo.append(jsonArtist["artists"][i]["external_urls"]["spotify"])
artistInfo.append(jsonArtist["artists"][i]["images"][0]["url"])
self.relatedArtists[jsonArtist["artists"][i]["name"]] = artistInfo
artistInfo = []
def getRelatedArtists(self):
return self.relatedArtists
def getArtist(self):
return self.artist
def getArtistUrl(self):
return self.artistUrl
def getArtistImage(self):
return self.artistImage
def getArtistId(self):
return self.artistId
def getSongNames(self):
return self.songNames
def getGenres(self):
self.artist
def getArtistEmbed(self):
return self.artistEmbed
if __name__ == "__main__":
searched = search()
searched.getAccessToken()
searched.searchArtist("khalid")
searched.searchArtistTopTracks()
searched.searchArtistEmbed()
# print(searched.getArtistImage())
# print(searched.getArtistId())
# print(searched.getSongNames())