-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlastfm.py
More file actions
128 lines (93 loc) · 3.61 KB
/
lastfm.py
File metadata and controls
128 lines (93 loc) · 3.61 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
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
127
128
##### coding: utf-8 #####
# Simple last.fm interface for retrieving album art
# (and possibly other information). Scrapped authentication
# and scrobble functionality since we don't need it!
# Noah Roberts :)
# Uncomment & copy & paste this into main file to use this interface:
#######################################################
# import sys
# import os
#
# # Change this path to true path of project directory
# sys.path.append(os.path.abspath('./'))
#
# from env import *
# from lastfm import *
#######################################################
import sys
import os
import requests
import json
import hashlib
import six
import logging
class lastfm():
# constructor
def __init__(self):
self.logger = logging.getLogger(__name__)
self.apiKey = os.environ.get('LastApiKey')
self.secret = os.environ.get('LastSecret')
if self.apiKey == '<YOUR API KEY>' or self.secret == '<YOUR SECRET>':
raise ApiKeyNotSetException()
# returns string holding image url
def getAlbumArt(self, artist, album):
payload = {'method':'album.getInfo',
'artist':artist,
'album':album,
'api_key':self.apiKey}
payload['api_sig'] = self.genApiSig(payload)
payload['format'] = 'json'
response = requests.get('http://ws.audioscrobbler.com/2.0/?', payload)
if response.status_code == 200:
response = response.json()
image = response['album']['image'][2]['#text']
self.logger.debug('Image retrieved. URL: ' + str(image))
return image
else:
self.logger.warn('Failed to get image. Response status code: ' + str(response.status_code))
return ''
# returns a dict with some cool stuff
def getArtistInfo(self, artist):
payload = {'method':'artist.getInfo',
'artist':artist,
'api_key':self.apiKey}
payload['api_sig'] = self.genApiSig(payload)
payload['format'] = 'json'
response = requests.get('http://ws.audioscrobbler.com/2.0/?', payload)
if response.status_code == 200:
response = response.json()
# return an ugly dict (but thats ok for now)
info = {}
info['status'] = 'success'
info['name'] = response['artist']['name']
info['listeners'] = response['artist']['stats']['listeners']
info['playcount'] = response['artist']['stats']['playcount']
info['bio'] = response['artist']['bio']['summary']
self.logger.debug('Artist info retrieved')
self.logger.trace('Info dictionary: ' + str(info))
return info
else:
self.logger.warn('Failed to get artist image. Response status code: ' + str(response.status_code))
return {'status':'failure'}
def genApiSig(self, dataToHash):
sigStr = ''
for key in sorted(dataToHash):
sigStr += key + dataToHash[key]
sigStr += self.secret
return md5(sigStr)
# helper functions
def md5(text):
h = hashlib.md5(formatUnicode(text).encode('utf-8'))
return h.hexdigest()
def formatUnicode(text):
if isinstance(text, six.binary_type):
return six.text_type(text, 'utf-8')
elif isinstance(text, six.text_type):
return text
else:
return six.text_type(text)
# exception
class ApiKeyNotSetException(Exception):
def __init__():
logging.getLogger(__name__).error('Please set your API key and secret (obtained from last.fm) in env.py')
pass