-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
67 lines (54 loc) · 1.66 KB
/
api.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
import re
import urllib
from util import url_open
from memo import Memoized
HOST = "http://www.musicuo.com/"
SEARCH_URL = HOST + "search?&q=%s"
SONG_URL = HOST + 'include/mp3/get'
RE_SONG = re.compile('m.playlist.push\({id: "(?P<id>[0-9]*?)", '\
'icon: "", titulo: "(?P<title>.*?)", artista: "(?P<artist>.*?)", '\
'album: "(?P<album>.*?)", genero: "(?P<gener>.*?)"')
class Song(object):
def __init__(self, song=None, id=None):
if song:
for key, value in song.iteritems():
setattr(self, key, value)
elif id:
self.id = id
@property
def url(self):
params = {'file': self.id,
'token': get_token()}
return url_open(SONG_URL, data=params)
@property
def data(self):
fd = url_open(self.url, handle=True)
data = fd.read(1024*5)
while True:
yield data
data = fd.read(1024)
if not data:
break
def get_token():
# TODO: should implement cookies to disk too
#try:
# with open('.token') as fd:
# token = fd.read()
#except Exception, error:
# data = url_open(HOST)
# token = data.split('var ptk = "')[1].split('";')[0]
# with open('.token', 'w') as fd:
# fd.write(token)
data = url_open(HOST)
token = data.split('var ptk = "')[1].split('";')[0]
return token
@Memoized
def search(query):
query = urllib.quote(query)
data = url_open(SEARCH_URL % query)
songs = []
for song in RE_SONG.finditer(data):
song_obj = Song(song.groupdict())
songs.append(song_obj)
#yield song_obj
return songs