/
search.py
107 lines (72 loc) · 2.34 KB
/
search.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
"""pysonic library.
"""
import re
from pysonic import pretty
__author__ = 'Ziling Zhao <zilingzhao@gmail.coM>'
def makeArray(obj):
if type(obj) is not list:
return [obj,]
return obj
class Search(object):
def __init__(self, api):
self.api = api
def _getList(self, item, key):
if type(item) is dict:
return item[key]
return []
def search_song(self, options):
qs = [options.song,]
if options.artist:
qs.append(options.artist)
if options.album:
qs.append(options.album)
query = {
'query': " ".join(qs)
}
result = self.api.call_search2(query=query)
songs = SongList(self._getList(result['searchResult2'], 'song'))
return songs
def search_artist(self, options):
query = {
'query': options.artist
}
result = self.api.call_search2(query=query)
artists = ArtistList(self._getList(result['searchResult2'], 'artist'))
return artists
unicode_re = re.compile(r'&#[0-9]{1,7};')
class SearchList(object):
def __init__(self, entries):
self.entries = makeArray(entries)
self.length = len(self.entries)
def entryString(self, entry):
return unicode(entry)
def __str__(self):
return self.__unicode__()
def __unicode__(self):
fmt = u"%%%dd. %%s" % (len(str(self.length)))
cnt = 0
strings = []
def replacer(s):
word = s.group()
return unichr(int(word[2:-1]))
for entry in self.entries:
cnt += 1
encoded_str = unicode_re.sub(
replacer,
unicode(self.entryString(entry))
)
strings.append(fmt % (cnt, encoded_str))
return u"\n".join(strings).encode('utf-8')
def __repr__(self):
return pretty(self.entries)
def __len__(self):
return len(self.entries)
def __getitem__(self, key):
return self.entries[key]
class ArtistList(SearchList):
def entryString(self, entry):
return unicode(entry['name'])
class SongList(SearchList):
def entryString(self, entry):
return u"%s - %s - %s - %s" % (entry['album'], entry['track'],
entry['artist'], entry['title'])