forked from albertz/music-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preloader.py
70 lines (56 loc) · 1.48 KB
/
preloader.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
PreloadNextN = 10
import sys
Attribs = ["sha1", "gain", "fingerprint_AcoustId", "bmpThumbnail"]
def needUpdate(song):
for attr in Attribs:
if getattr(song, attr, None) is None: return True
return False
def update(song):
import threading
curThread = threading.currentThread()
for attr in Attribs:
if curThread.cancel: return
# This will calculate it and save it.
song.get(
attr,
timeout=None, # wait for it. dont spawn too much tasks async
)
def checkUpdate():
from queue import queue
from State import state
import threading
curThread = threading.currentThread()
checkAgain = True
while checkAgain:
checkAgain = False
songs = []
if state.curSong:
songs += [state.curSong]
songs += queue.peekNextN(PreloadNextN)
for song in songs:
if song is None: continue
if curThread.cancel: return
if needUpdate(song):
update(song)
checkAgain = True
break
def preloaderMain():
try:
# Import itunes module. This might start some background processes
# which load the iTunes DB, etc.
import itunes
except:
sys.excepthook(*sys.exc_info())
try:
checkUpdate()
except:
sys.excepthook(*sys.exc_info())
from State import state
for ev,args,kwargs in state.updates.read():
try:
# actually, the queue thread might not have added songs yet.
# it might make more sense to yield queue updates here.
# not possible atm, though. and this still works good enough.
checkUpdate()
except:
sys.excepthook(*sys.exc_info())