-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapplication.py
86 lines (73 loc) · 2.25 KB
/
application.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
from threading import RLock
from audiojack import AudioJack
class AudioJackApplication(object):
def __init__(self):
self.lock = RLock()
self.observers = []
self.requests = [] # Used to record all previous searches, could be used for some features
def add_observer(self, observer):
self.observers.append(observer)
@property
def last_search(self):
try:
self.lock.acquire()
if len(self.requests) > 0:
return self.requests[-1]
else:
return None
finally:
self.lock.release()
def search(self, url):
search = SearchRequest(url)
try:
self.lock.acquire()
self.requests.append(search)
finally:
self.lock.release()
self.notify()
def select(self, index):
try:
self.lock.acquire()
self.last_search.select(index)
finally:
self.lock.release()
self.notify()
def notify(self):
for observer in self.observers:
observer.notify()
class SearchRequest(object):
def __init__(self, url=None):
self.error = 0
self.results = None
self.selection = None
self.file = None
self.audiojack = AudioJack(small_cover_art=True)
self.url = url
if url:
self.search(url)
def search(self, url):
try:
self.results = self.audiojack.get_results(url)
except AttributeError as e:
print(e)
self.error = 1
def select(self, index, path=None):
self.selection = self.results[index]
try:
self.file = self.audiojack.select(self.selection, path=path)
except AttributeError as e:
print(e)
self.error = 1
def custom(self, title, artist, album, path=None):
try:
self.file = self.audiojack.select({
'url': self.url,
'title': title,
'artist': artist,
'album': album
}, path=path)
except AttributeError as e:
print(e)
self.error = 1
def cut_file(self, start, end):
self.audiojack.cut_file(self.file, start, end)