-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLastFmApi.py
executable file
·126 lines (115 loc) · 5.23 KB
/
LastFmApi.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
119
120
121
122
123
124
125
126
import Request
from collections import OrderedDict
#Last.Fm
Api_Key = 'c513f3a2a2dad1d1a07021e181df1b1f'
ApiUrl = 'http://ws.audioscrobbler.com/2.0/?method={0}&api_key=' + Api_Key + '&format=json'
requestHandler = Request.requestHandler()
class Api:
Method_Artist_GetTopAlbums = 'artist.getTopAlbums'
Method_Album_GetInfo = 'album.getInfo'
Method_Track_Search = 'track.search'
def getUrlTopAlbumsByArtist(self,artist):
url = ApiUrl.replace('{0}',self.Method_Artist_GetTopAlbums) + '&artist=' + artist
return url
def getUrlTracksByArtistAndAlbum(self,artist,album):
url = ApiUrl.replace('{0}',self.Method_Album_GetInfo) + '&artist=' + artist + '&album=' + album
return url
def getUrlTracks(self,track):
url = ApiUrl.replace('{0}',self.Method_Track_Search) + '&track=' + track
return url
#Tracks
def getBysearch(self,search):
tracksDict = []
if search != "":
response = requestHandler.request(self.getUrlTracks(search))
jsonObject = requestHandler.encodeJson(response);
tracks = jsonObject['results']['trackmatches']['track']
for track in tracks:
if 'image' in track:
cover_url_medium = ''
cover_url_large = ''
cover_url_extralarge = ''
for image in track['image']:
if image['size'] == 'medium' and image['#text'] != '':
cover_url_medium = image['#text']
pass
elif image['size'] == 'large' and image['#text'] != '':
cover_url_large = image['#text']
pass
elif image['size'] == 'extralarge' and image['#text'] != '':
cover_url_extralarge = image['#text']
pass
pass
tracksDict.append(OrderedDict([('title',track['name'].encode('ascii')), ('artist',track['artist'].encode('ascii')), ('cover_url_medium',cover_url_medium), ('cover_url_large',cover_url_large), ('cover_url_extralarge',cover_url_extralarge)]))
pass
return tracksDict
#Albums by artist
def getAlbumsByArtist(self,artist):
tracksDict = []
if artist != "":
response = requestHandler.request(self.getUrlTopAlbumsByArtist(artist))
jsonObject = requestHandler.encodeJson(response);
albums = jsonObject['topalbums']['album']
for album in albums:
if 'image' in album:
cover_url_medium = ''
cover_url_large = ''
cover_url_extralarge = ''
for image in album['image']:
if image['size'] == 'medium' and image['#text'] != '':
cover_url_medium = image['#text']
pass
elif image['size'] == 'large' and image['#text'] != '':
cover_url_large = image['#text']
pass
elif image['size'] == 'extralarge' and image['#text'] != '':
cover_url_extralarge = image['#text']
pass
pass
tracksDict.append(OrderedDict([('artist',album['artist']['name'].encode('ascii')), ('album',album['name'].encode('ascii')), ('cover_url_medium',cover_url_medium), ('cover_url_large',cover_url_large), ('cover_url_extralarge',cover_url_extralarge)]))
pass
return tracksDict
#Tracks from an album and artist
def getTracksByArtistAndAlbum(self,artist,album):
tracksDict = []
if artist != "" and album != "":
response = requestHandler.request(self.getUrlTracksByArtistAndAlbum(artist,album))
jsonObject = requestHandler.encodeJson(response);
albumObj = jsonObject['album']
tracks = albumObj['tracks']['track']
cover_url_medium = ''
cover_url_large = ''
cover_url_extralarge = ''
if 'image' in albumObj:
for image in albumObj['image']:
if image['size'] == 'medium' and image['#text'] != '':
cover_url_medium = image['#text']
pass
elif image['size'] == 'large' and image['#text'] != '':
cover_url_large = image['#text']
pass
elif image['size'] == 'extralarge' and image['#text'] != '':
cover_url_extralarge = image['#text']
pass
pass
if 'name' in tracks:#Single Object
tracksDict.append(OrderedDict([('artist',albumObj['artist'].encode('ascii')), ('album',albumObj['name'].encode('ascii')), ('track',tracks['name'].encode('ascii')), ('cover_url_medium',cover_url_medium), ('cover_url_large',cover_url_large), ('cover_url_extralarge',cover_url_extralarge)]))
pass
else:
for track in tracks:
tracksDict.append(OrderedDict([('artist',albumObj['artist'].encode('ascii')), ('album',albumObj['name'].encode('ascii')), ('track',track['name'].encode('ascii')), ('cover_url_medium',cover_url_medium), ('cover_url_large',cover_url_large), ('cover_url_extralarge',cover_url_extralarge)]))
pass
pass
return tracksDict
#Tracks from albums by artist
def getTracksOfAlbumsbyArtist(self,artist):
tracksDict = []
if artist != "":
response = requestHandler.request(self.getUrlTopAlbumsByArtist(artist))
jsonObject = requestHandler.encodeJson(response);
albums = jsonObject['topalbums']['album']
for album in albums:
tracks = self.getTracksByArtistAndAlbum(artist,requestHandler.encode(album['name']))
tracksDict.append(tracks)
pass
return tracksDict