-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMusicAPIFunctions.py
73 lines (70 loc) · 2.45 KB
/
MusicAPIFunctions.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
def readJsonUrl(url):
"""This reads the contents of a URL. Works for json data and Python 3"""
#####################################################################
# In this block of code:
# - Python opens the url
# - reads the data
# - stores it as a string
# - closes the url
import urllib.request
try:
page = urllib.request.urlopen(url)
except urllib.error.URLError as e:
print("There was an error opening the URL (description below).")
print(e)
print("Ask for help?")
return(None)
data_bytes = page.read()
data_str = data_bytes.decode('utf-8')
page.close()
#####################################################################
# In this block of code
# - The json string is converted to a Python dictionary.
# - This is returned
import json
try:
output = json.loads(data_str)
except:
print("Error")
return(None)
return(output)
#####################################################################
def checkSpotifyPopularity(artist):
""" Function to check Spotify popularity rating of artist."""
# The code:
# - Replaces spaces in artist by %20 (which means space in urls)
# - Defines URL which makes request for rating
# - Uses function readJsonUrl to read url
# - If lyrics found returns them (as a string)
A = artist.replace(" ", "%20")
url = "https://api.spotify.com/v1/search?q=%s&type=artist&limit=1"%(A)
output = readJsonUrl(url)
return( output["artists"]["items"][0]["popularity"] )
def printRadioOnePlaylist(Letter):
""" Function to fetch this weeks Radio 1 playlists."""
# The code:
# - Defines URL where playlist is stored
# - Uses function readJsonUrl to read url
# - Extracts the playlist requested by user
# - prints it
myURL = "http://www.bbc.co.uk/radio1/playlist.json"
output = readJsonUrl(myURL)
playlist = output["playlist"]
plB = playlist["b"]
plC = playlist["c"]
plI = playlist["introducing"]
if Letter=="A":
pl = playlist["a"]
elif Letter=="B":
pl = playlist["b"]
elif Letter=="C":
pl = playlist["c"]
elif Letter=="I":
pl = playlist["introducing"]
else:
error("The input to fetchRadioOnePlaylist should be either 'A', 'B', 'C' or 'I'")
print("-"*50)
print("Radio 1 Playlist " + Letter)
print("-"*50)
for s in pl:
print( s["artist"] + ": " + s["title"] )