116 lines
2.3 KiB
Python
116 lines
2.3 KiB
Python
# Bryan Schumaker (11/07/2010)
|
|
|
|
import random as rand
|
|
|
|
import libsaria
|
|
from libsaria.collection import library
|
|
call = libsaria.event.call
|
|
|
|
song_list = None
|
|
song_set = None
|
|
|
|
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 = -1
|
|
|
|
def add_id(id):
|
|
global song_list
|
|
global song_set
|
|
song_list.append(id)
|
|
song_set.add(id)
|
|
|
|
def reset():
|
|
global song_list
|
|
global song_set
|
|
song_list = []
|
|
song_set = set()
|
|
|
|
def load():
|
|
global song_list
|
|
global song_set
|
|
global visible
|
|
song_list = libsaria.data.load("playlist", ".list")
|
|
if song_list == None:
|
|
reset()
|
|
return
|
|
song_set = set(song_list)
|
|
|
|
def save():
|
|
libsaria.data.save(song_list, "playlist", ".list")
|
|
|
|
def walk():
|
|
global song_list
|
|
for id in song_list:
|
|
yield id
|
|
|
|
def filter(text):
|
|
global visible
|
|
global song_set
|
|
global filtered
|
|
if len(text) > 0:
|
|
visible = song_set.intersection(library.test_filter(text))
|
|
filtered = True
|
|
else:
|
|
visible = song_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(song_list)
|
|
|
|
def seq_next():
|
|
global cur_index
|
|
global song_list
|
|
cur_index += 1
|
|
while is_visible(song_list[cur_index]) == False:
|
|
cur_index += 1
|
|
return song_list[cur_index]
|
|
|
|
def rand_candidate(max):
|
|
index = rand.randint(0, max-1)
|
|
if filtered == False:
|
|
return song_list[index]
|
|
return list(visible)[index]
|
|
|
|
def rand_next():
|
|
n = num_visible()
|
|
if n == 0:
|
|
return
|
|
get_attrs = library.get_attrs
|
|
for i in xrange(15):
|
|
id = rand_candidate(n)
|
|
(artist, title, score) = get_attrs(id, "artist", "title", "score")
|
|
if (artist, title) in recent:
|
|
print recent_msg % (artist, title)
|
|
continue
|
|
if score < 0:
|
|
do_play = rand.randint(0, 100)
|
|
if do_play < ((20 * score) + 100):
|
|
print skip_msg % (artist, title)
|
|
continue
|
|
recent.append((artist, title))
|
|
if len(recent) > 50:
|
|
recent.pop(0)
|
|
if i > 0:
|
|
print "Picking a song took %s iterations" % i
|
|
return id
|
|
|
|
def next():
|
|
if libsaria.prefs["random"] == True:
|
|
id = rand_next()
|
|
else:
|
|
id = seq_next()
|
|
if id != None:
|
|
return call("NEXT", library.play_id, id)
|