1
# Copyright 2014 Canonical
4
"""The Grooveshark scope."""
10
from urllib2 import urlopen, Request
12
from ..scopes import SimpleApp
14
SEARCH_URI = 'https://api.grooveshark.com/ws3.php?sig='
16
DEFAULT_COUNTRY = {'CC1': 0,
24
CLIENT_IP = '173.194.42.19'
27
class APIError(Exception):
28
""" API errors catcher """
31
def api_call(method, api_utils, client_ip=CLIENT_IP, **parameters):
32
""" Call the Grooveshark API """
34
data['method'] = method
35
data['parameters'] = parameters
36
data['header'] = {'wsKey': api_utils['key']}
37
data_str = json.dumps(data).encode("utf-8")
38
sig = hmac.new(api_utils['secret'], data_str).hexdigest()
39
headers = {'X-Client-IP': client_ip}
40
req = Request(SEARCH_URI+sig, data_str,
42
response = urlopen(req)
43
response_data = response.read()
44
result = json.loads(response_data.decode("utf-8"))
45
if 'errors' in result:
46
error = result.get('errors')[0]
47
raise APIError("Error: %r" % (error,))
50
# Override category renderer
53
'template': {'category-layout': 'grid', 'card-size': 'medium'},
54
'components': {'title': 'title',
56
'subtitle': 'artist'},
63
'render_template': json.dumps(RENDERER_MUSIC),
69
'render_template': json.dumps(RENDERER_MUSIC),
75
"""The Grooveshark app."""
77
def __init__(self, *args, **kwargs):
78
super(App, self).__init__(*args, **kwargs)
79
self.api_utils = {'key': self.config['secrets'].get('api_key'),
80
'secret': self.config['secrets'].get('api_secret')}
82
def search(self, **kwargs):
84
query = kwargs['query']
85
limit = kwargs['limit']
86
form_factor = kwargs['platform']
87
yield dict(category=SONGS_CATEGORY)
88
yield dict(category=ALBUMS_CATEGORY)
89
if form_factor == "phone":
90
base_uri = 'http://html5.grooveshark.com'
92
base_uri = 'http://grooveshark.com'
93
albums = {'result': {'albums': []}}
94
songs = {'result': {'songs': []}}
97
songs = api_call("getPopularSongsMonth",
99
client_ip=CLIENT_IP, limit=limit)
101
albums = api_call("getAlbumSearchResults",
104
query=query, limit=limit)
105
songs = api_call("getSongSearchResults",
108
query=query, limit=10,
109
country=DEFAULT_COUNTRY)
111
for i in albums['result']['albums']:
112
if i['AlbumName'].strip().lower() != query.strip().lower():
114
album_name_parsed = urllib.quote(i['AlbumName'])
115
album_uri = '%(uri)s/#/album/%(aname)s/%(aid)s' \
117
aname=album_name_parsed,
118
aid=str(i['AlbumID']))
119
if i.get('CoverArtFilename') and \
120
len(i.get('CoverArtFilename')) > 0:
121
icon = 'http://beta.grooveshark.com/static/' \
122
'amazonart/m%(cover)s' \
123
% dict(cover=str(i['CoverArtFilename']))
125
icon = 'http://beta.grooveshark.com/static/' \
126
'amazonart/m%(aid)s.jpg' \
127
% dict(aid=str(i['AlbumID']))
128
yield dict(result={'uri': album_uri,
129
'title': i['AlbumName'],
130
'album': i['AlbumName'],
131
'artist': i['ArtistName'],
132
'album_id': str(i['AlbumID']),
133
'album_uri': album_uri,
135
'form_factor': form_factor,
138
for i in songs['result']['songs']:
139
album_name_parsed = urllib.quote(i['AlbumName'])
140
album_uri = '%(uri)s/#/album/%(aname)s/%(aid)s' \
142
aname=album_name_parsed,
143
aid=str(i['AlbumID']))
144
if i.get('CoverArtFilename') and \
145
len(i.get('CoverArtFilename')) > 0:
146
icon = 'http://beta.grooveshark.com/static/' \
147
'amazonart/m%(aid)s' \
148
% dict(aid=str(i['CoverArtFilename']))
150
icon = 'http://beta.grooveshark.com/static/' \
151
'amazonart/m%(aid)s.jpg' \
152
% dict(aid=str(i['AlbumID']))
153
yield dict(result={'uri': album_uri,
154
'title': i['SongName'],
155
'album': i['AlbumName'],
156
'artist': i['ArtistName'],
157
'album_id': str(i['AlbumID']),
158
'song_id': str(i['SongID']),
160
'form_factor': form_factor,
163
def preview(self, **kwargs):
164
"""Do the preview."""
165
result = kwargs['result']
167
song = api_call("getSongURLFromSongID",
169
songID=result['song_id'])
170
if self.result['form_factor'] == "phone":
171
action_uri = song['url'].replace('/grooveshark.com/s',
172
'/html5.grooveshark.com/s')
174
action_uri = result['uri']
176
# yield a 'header' widget
180
'title': result['title'],
181
'art': result['art'],
182
'subtitle': result['artist'],
184
# add an 'actions' widget
188
'actions': [{'label': 'Grooveshark',