-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLManager.py
53 lines (51 loc) · 1.36 KB
/
DLManager.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
#!/usr/bin/env python2
#--coding:UTF-8
import download
import getPlaylist
import thread
from time import sleep
import os
TEMP_PATH = "/tmp/doubanFM/"
if not os.path.isdir(TEMP_PATH):
os.mkdir(TEMP_PATH)
class DLManager:
def __init__(self):
self.songs = []
self.playlist = getPlaylist.Playlist()
def dl(self):
lock1 = thread.allocate_lock()
flag1 = thread.allocate_lock()
flag2 = thread.allocate_lock()
songinfo = ''
picpath = ''
while True:
if len(self.songs)>2 or lock1.locked():
#print "self.song:",len(self.songs)
#print "lock1.locked():",lock1.locked()
sleep(1)
else:
if flag1.locked() and songinfo != '':
if flag2.locked():
songinfo['picture']=picpath
self.songs.append(songinfo)
print "new song"
songinfo = self.playlist.next()
musicname = songinfo['url'].split("/")[-1]
picname = songinfo['picture'].split("/")[-1]
picpath = TEMP_PATH+picname
savepath = TEMP_PATH+musicname
lock1.acquire()
if flag1.locked():
flag1.release()
if flag2.locked():
flag2.release()
flag1.acquire()
flag2.acquire()
print songinfo['picture']
thread.start_new_thread(download.download,(lock1,flag1,songinfo['url'],savepath,flag2,songinfo['picture'],picpath))
songinfo['path']=savepath
def getSong(self):
if len(self.songs) == 0:
return -1
else:
return self.songs.pop(0)