-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoutcast_wrapper.py
51 lines (39 loc) · 1.1 KB
/
shoutcast_wrapper.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
"""
wrapper for shoutcast api
"""
import urllib2
import json
import requests
class ShoutcastWrapper:
"""
wrapper for accessing the shoutcast api
"""
rootUrl = "http://api.shoutcast.com/station/"
nowPlayingUrl = "http://api.shoutcast.com/station/nowplaying"
apiKey = "**api_key**"
header = {'User-agent' : 'meuse'}
def getStationPlayingArtist(self, artist):
"""
returns a list of shoutcast stations playing the given
artist. Each station is a dictionary.
parameters
----------
artist: the name of an artist
return
------
a list of tuples (station name, listen count)
"""
stationDictList = []
stationList = []
args = {"ct" : artist, "f" : "json", "k" : self.apiKey}
try:
request = requests.get(self.nowPlayingUrl, params=args)
data = request.json()
#construct a list with the stations
stationDictList = data['response']['data']['stationlist']['station']
for item in stationDictList:
stationList.append((item['name'], item['lc'], item['id']))
except Exception as e:
print "No artists found OR http request error"
stationList = []
return stationList