-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastlyric.py
52 lines (41 loc) · 1.24 KB
/
lastlyric.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
#!/usr/bin/env python
#coding=utf-8
import re
import json
import gecimi
import requests
__version__ = '0.0.1'
match = re.compile(r'\([^)]*?\)|([^)]*?)')
class LastLyric(object):
api_key = ''
def __init__(self, api_key=None):
self.api_key = self.api_key or api_key
def getLast(self, user):
url = ('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks'
'&user=%s&api_key=%s&format=json' % (user, self.api_key))
try:
r = requests.get(url, verify=False, stream=False, timeout=10)
content = r.content
except Exception, e:
print e
content = '{}'
try:
result = json.loads(content)
song = result['recenttracks']['track'][0]
except Exception, e:
print e
return ''
artist = song['artist']['#text']
title = song['name']
title = match.sub('', title)
if artist:
lyric = gecimi.Lyric(title, artist)
lrc = lyric.get()
else:
lyric = gecimi.Lyric(title)
lrc = lyric.get()
return lrc
if __name__ == '__main__':
api_key = ''
lyric = LastLyric(api_key)
print lyric.getLast('jht360')