123 lines
2.5 KiB
Python
123 lines
2.5 KiB
Python
# Bryan Schumaker (11/07/2010)
|
|
|
|
import libsaria
|
|
from libsaria.sources import library
|
|
import playlist
|
|
import random
|
|
|
|
filtered = False
|
|
visible = None
|
|
recent = []
|
|
recent_msg = "Skipping %s by %s because it has played recently"
|
|
skip_msg = "Skipping %s by %s because I don't think you like it"
|
|
cur_index = None
|
|
|
|
# Function pointers for convenience
|
|
add_ids = playlist.add_ids
|
|
rm_ids = playlist.rm_ids
|
|
reset = playlist.reset
|
|
|
|
playlist.load()
|
|
|
|
def list():
|
|
for id in playlist.song_list:
|
|
yield id
|
|
|
|
def filter(text):
|
|
global visible
|
|
global song_set
|
|
global filtered
|
|
if len(text) > 0:
|
|
visible = playlist.as_set().intersection(library.test_filter(text))
|
|
filtered = True
|
|
else:
|
|
visible = playlist.as_set()
|
|
filtered = False
|
|
|
|
def is_visible(id):
|
|
global visible
|
|
global filtered
|
|
if filtered == True:
|
|
return id in visible
|
|
return True
|
|
|
|
def num_visible():
|
|
if filtered == True:
|
|
return len(visible)
|
|
return len(playlist.song_list)
|
|
|
|
def get_cur_id():
|
|
return playlist.song_list[cur_index]
|
|
|
|
def seq_next():
|
|
global cur_index
|
|
cur_index += 1
|
|
max_index = len(playlist.song_list)
|
|
if cur_index == max_index:
|
|
cur_index = 0
|
|
while is_visible(playlist.song_list[cur_index]) == False:
|
|
cur_index += 1
|
|
if cur_index == max_index:
|
|
cur_index = 0
|
|
return playlist.song_list[cur_index]
|
|
|
|
def rand_candidate(list, max):
|
|
index = random.randint(0, max-1)
|
|
return list[index]
|
|
|
|
def rand_next():
|
|
global cur_index
|
|
n = num_visible()
|
|
if n == 0:
|
|
return
|
|
get_attrs = library.get_attrs
|
|
|
|
if filtered == False:
|
|
selection = playlist.song_list
|
|
else:
|
|
selection = list(visible)
|
|
|
|
for i in xrange(15):
|
|
id = rand_candidate(selection, n)
|
|
(artist, title, like) = get_attrs(id, "artist", "title", "like")
|
|
if (artist, title) in recent:
|
|
print recent_msg % (artist, title)
|
|
continue
|
|
if like == False:
|
|
print skip_msg % (artist, title)
|
|
continue
|
|
if i > 0:
|
|
print "Picking a song took %s iterations" % i
|
|
cur_index = playlist.song_list.index(id)
|
|
return id
|
|
return id
|
|
|
|
def load_id(id):
|
|
global cur_index
|
|
cur_index = playlist.song_list.index(id)
|
|
save()
|
|
path = library.get_attrs(id, "filepath")
|
|
return source.load_file(path)
|
|
|
|
def play_id(id):
|
|
if load_id(id) == True:
|
|
print "playing..."
|
|
libsaria.controls.play()
|
|
return True
|
|
return False
|
|
|
|
def next():
|
|
id = None
|
|
if len(playlist.song_list) == 0:
|
|
return
|
|
if libsaria.prefs.get("libsaria.random") == True:
|
|
id = rand_next()
|
|
else:
|
|
id = seq_next()
|
|
if id != None:
|
|
artist, title, file = library.get_attrs(id, "artist", "title", "filepath")
|
|
recent.append((artist, title))
|
|
if len(recent) > 50:
|
|
recent.pop(0)
|
|
return id
|