-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge.py
More file actions
90 lines (76 loc) · 2.78 KB
/
challenge.py
File metadata and controls
90 lines (76 loc) · 2.78 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
import requests
import base64
TMBD_KEY = ''
SPOTIFY_CLIENT_ID = ''
SPOTIFY_CLIENT_SECRET = ''
def get_trending_movies(type='day', lang='en-US'):
url = f"https://api.themoviedb.org/3/trending/movie/{type}?language={lang}"
headers = {
"accept": "application/json",
"Authorization": "Bearer "
}
response = requests.get(url, headers=headers).json()
#for debugging purposes
print("get_tranding_movies")
return [x['id'] for x in response['results']]
def get_movie_credits(movie_id):
url = f"https://api.themoviedb.org/3/movie/{movie_id}/credits?language=en-US"
headers = {
"accept": "application/json",
"Authorization": "Bearer "
}
response = requests.get(url, headers=headers).json()
#print(response)
a = [x['name'] for x in response['crew'] if x["known_for_department"] == 'Sound']
#for debugging purposes
print("get_movie_credits")
return a
def get_spotify_req_token():
url = 'https://accounts.spotify.com/api/token'
auth = SPOTIFY_CLIENT_ID + ":" + SPOTIFY_CLIENT_SECRET
encoded_auth = auth.encode("utf-8")
auth_base64 = str(base64.b64encode(encoded_auth), "utf-8")
payload = {
"grant_type" : "client_credentials"
}
headers = {
"Authorization": "Basic " + auth_base64,
"Content-Type": "application/x-www-form-urlencoded"
}
response = requests.post(url, data=payload, headers=headers).json()
#for debugging purposes
print("get_spotify_req_token")
return response['access_token']
def search_spotify(bearer, content):
url = f'https://api.spotify.com/v1/search?q={content}&type=artist&limit=1'
headers = {
"Authorization" : "Bearer " + bearer,
"Content-Type" : "application/json"
}
response = requests.get(url, headers=headers).json()
#for debugging purposes
print("search_spotify")
return response['artists']['items'][0]['id']
def get_artist_tracks(bearer,artist_id):
url = f'https://api.spotify.com/v1/artists/{artist_id}/top-tracks'
headers = {
"Authorization" : "Bearer " + bearer,
"Content-Type" : "application/json"
}
response = requests.get(url, headers=headers).json()
#for debugging purposes
print("get_artist_tracks")
return [x['name'] for x in response['tracks'][:1]]
#Part1 populating artists
artists = []
movies = get_trending_movies()
for movie in movies:
artists.extend(get_movie_credits(movie))
#Part2 finding and populating songs
top_tracks = []
spotify_token = get_spotify_req_token()
#there are too many artists so to save time and space only doing first 5 same can be done for rest of them.
for artist in artists[:5]:
artist_id = search_spotify(spotify_token, artist)
top_tracks.extend(get_artist_tracks(spotify_token, artist_id))
print(top_tracks)