50f7e1203d
The score system will help determine what users do and don't like. When the user chooses a song or listens to > 75% of a song, we increment the score. If the user selects a different song in the first 1/3 of playback then we decrement the score. Songs with a score >= 0 will always be played. Songs with a score < 0 have a 50% chance of playing.
158 lines
3.4 KiB
Python
158 lines
3.4 KiB
Python
# Bryan Schumaker (8/8/2010)
|
|
|
|
import libsaria
|
|
|
|
import datetime
|
|
timedelta = datetime.timedelta
|
|
|
|
tag = None
|
|
audio = None
|
|
ins_table = None
|
|
ins_index = None
|
|
ins_tree = None
|
|
|
|
class TrackRecord:
|
|
def __init__(self, year, length, lenstr):
|
|
self.length = length
|
|
if lenstr[0] == "0" and lenstr[1] == ":":
|
|
self.lenstr = lenstr[2:]
|
|
else:
|
|
self.lenstr = lenstr
|
|
self.count = 0
|
|
self.year = year
|
|
self.score = 0
|
|
|
|
self.fs = None
|
|
self.tags = None
|
|
|
|
|
|
class Collection:
|
|
def __init__(self, file):
|
|
self.file = file
|
|
self.load()
|
|
self.filtered = False
|
|
self.visible = None
|
|
|
|
def save(self):
|
|
libsaria.data.save(
|
|
(self.fs_tree, self.tag_tree, self.index, self.records,
|
|
self.next_record, self.size),
|
|
self.file, "")
|
|
|
|
def load(self):
|
|
objects = libsaria.data.load(self.file, "")
|
|
if objects == None or len(objects) != 6:
|
|
self.reset()
|
|
return
|
|
(self.fs_tree, self.tag_tree, self.index, self.records,
|
|
self.next_record, self.size) = objects
|
|
|
|
def reset(self):
|
|
from tree import DLTree
|
|
from index import Index
|
|
self.fs_tree = DLTree()
|
|
self.tag_tree = DLTree()
|
|
self.index = Index()
|
|
self.records = dict()
|
|
self.next_record = 0
|
|
self.size = 0
|
|
|
|
def disp(self):
|
|
pass
|
|
#self.fs_tree.disp()
|
|
#print
|
|
#print
|
|
#self.tag_tree.disp()
|
|
|
|
def filter(self, text):
|
|
if len(text) > 0:
|
|
self.visible = self.index.filter(text)
|
|
self.filtered = True
|
|
else:
|
|
self.visible = None
|
|
self.filtered = False
|
|
|
|
def is_visible(self, id):
|
|
if self.filtered == False:
|
|
return True
|
|
return id in self.visible
|
|
|
|
def num_visible(self):
|
|
if self.filtered == True:
|
|
return len(self.visible)
|
|
else:
|
|
return self.size
|
|
|
|
def walk_tags(self):
|
|
for tag in self.tag_tree.walk_forwards():
|
|
rec = self.records[tag[3]]
|
|
yield tag + [rec.year] + [rec.lenstr]
|
|
|
|
def walk_ids(self):
|
|
for tag in self.tag_tree.walk_forwards():
|
|
yield tag[3]
|
|
|
|
def find_id(self, file):
|
|
stripped = file.strip(libsaria.path.sep)
|
|
split = stripped.split(libsaria.path.sep)
|
|
|
|
res = self.fs_tree.walk_path(split)
|
|
if res == False:
|
|
return None
|
|
return res[0]
|
|
|
|
def get_attr(self, id, attr):
|
|
rec = self.records[id]
|
|
if attr == "filepath":
|
|
cmp = rec.fs.walk_backwards()
|
|
return libsaria.path.sep.join([""] + cmp)
|
|
if attr == "playcount":
|
|
return rec.count
|
|
if attr == "year":
|
|
return rec.year
|
|
if attr == "lenstr":
|
|
return rec.lenstr
|
|
if attr == "score":
|
|
return rec.score
|
|
|
|
tags = rec.tags.walk_backwards()
|
|
if attr == "artist":
|
|
return tags[0]
|
|
if attr == "album":
|
|
return tags[1]
|
|
if attr == "title":
|
|
return tags[2]
|
|
|
|
def insert_tags(self, artist, album, title, id):
|
|
tags = self.tag_tree.insert([artist, album, title, id])
|
|
self.index.insert([artist, album, title], id)
|
|
self.size += 1
|
|
|
|
def insert_allocate(self, components, ref):
|
|
t = ref.tag()
|
|
artist = t.artist
|
|
album = t.album
|
|
title = t.title
|
|
audio = ref.audioProperties()
|
|
if artist == "":
|
|
artist = u"Unknown Artist"
|
|
if album == "":
|
|
album = u"Unknown Album"
|
|
if title == "":
|
|
title = u"Unknown Title"
|
|
|
|
len = audio.length
|
|
record = TrackRecord(t.year, len, "%s" % timedelta(seconds=len))
|
|
|
|
id = self.next_record
|
|
self.next_record += 1
|
|
|
|
fs = self.fs_tree.insert(components + [id])
|
|
tags = self.tag_tree.insert([artist, album, title, id])
|
|
self.index.insert([artist, album, title], id)
|
|
|
|
record.fs = fs
|
|
record.tags = tags
|
|
self.records[id] = record
|
|
self.size += 1
|