libsaria: Find attributes for songs not in library
I use the same Track() class that the library does. I have to create an instance of this class each time the function is called, but this is ok since it is not the case I am trying to optimize for.
This commit is contained in:
parent
7c39b9ccd1
commit
6398b3dd9a
|
@ -24,6 +24,12 @@ def load_file(file):
|
|||
unlock_audio()
|
||||
callbacks.load_file(file)
|
||||
|
||||
def get_cur_file():
|
||||
lock_audio()
|
||||
file = pipeline.get_cur_file()
|
||||
unlock_audio()
|
||||
return file
|
||||
|
||||
def get_state():
|
||||
lock_audio()
|
||||
state = pipeline.get_state()
|
||||
|
|
|
@ -23,6 +23,9 @@ def load_file(type, file):
|
|||
cur_file = file
|
||||
player.set_property("uri", "%s://%s" % (type, file))
|
||||
|
||||
def get_cur_file():
|
||||
return cur_file
|
||||
|
||||
def has_file():
|
||||
return cur_file != None
|
||||
|
||||
|
|
|
@ -24,8 +24,7 @@ def open_xml(file):
|
|||
|
||||
def open_music(file):
|
||||
try:
|
||||
from libsaria.sources import file as file_source
|
||||
file_source.load_file(file)
|
||||
libsaria.controls.load(file)
|
||||
except Exception, e:
|
||||
print e
|
||||
|
||||
|
|
|
@ -37,14 +37,35 @@ def inc_count():
|
|||
# This will eventually increment a play count...
|
||||
pass
|
||||
|
||||
def find_attrs(file, *attrs):
|
||||
if file == None:
|
||||
return
|
||||
track = library.track.Track(file)
|
||||
if track == None:
|
||||
return
|
||||
get = track.__dict__.get
|
||||
res = []
|
||||
for attr in attrs:
|
||||
if attr == "id":
|
||||
res.append(library.update.song_id(file))
|
||||
else:
|
||||
res.append(get(attr))
|
||||
return res
|
||||
|
||||
# Function pointer to avoid unnecessary lookups for a common function
|
||||
get_attrs_id = library.get_attrs
|
||||
|
||||
def get_attrs(*attrs):
|
||||
# I should probably get a lock here...
|
||||
res = None
|
||||
if cur_id:
|
||||
return get_attrs_id(cur_id, *attrs)
|
||||
return -1
|
||||
res = get_attrs_id(cur_id, *attrs)
|
||||
if res == None:
|
||||
file = libsaria.audio.get_cur_file()
|
||||
res = find_attrs(file, *attrs)
|
||||
if res == None:
|
||||
res = [None] * len(attrs)
|
||||
return res
|
||||
|
||||
def filter(text):
|
||||
visible = library.filter(text)
|
||||
|
|
|
@ -1,87 +0,0 @@
|
|||
# Bryan Schumaker (12/7/2010)
|
||||
|
||||
import datetime
|
||||
import libsaria
|
||||
path = libsaria.path
|
||||
tagpy = None
|
||||
|
||||
timedelta = datetime.timedelta
|
||||
|
||||
source = None
|
||||
cur_file = None
|
||||
file_ref = None
|
||||
tags = None
|
||||
audio = None
|
||||
|
||||
def init():
|
||||
global source
|
||||
global tagpy
|
||||
|
||||
import tagpy
|
||||
|
||||
source = libsaria.sources.Source()
|
||||
source.get_attrs = get_attrs
|
||||
source.set_attr = set_attr
|
||||
source.get_cur_id = get_cur_file
|
||||
source.save = save
|
||||
|
||||
load_file = source.load_file
|
||||
print load_file
|
||||
|
||||
def load_file(file):
|
||||
global file_ref
|
||||
global tags
|
||||
global audio
|
||||
global cur_file
|
||||
file_ref = tagpy.FileRef(file)
|
||||
tags = file_ref.tag()
|
||||
audio = file_ref.audioProperties()
|
||||
cur_file = file
|
||||
source.load_file(file)
|
||||
|
||||
def get_cur_file():
|
||||
return cur_file
|
||||
|
||||
def get_attrs(file, *attrs):
|
||||
print "Getting attributes for: %s" % file
|
||||
#print " Requested attributes: %s" % attrs
|
||||
|
||||
res = []
|
||||
id = libsaria.path.file_id(file)
|
||||
seconds = audio.length
|
||||
for attr in attrs:
|
||||
if attr == "id":
|
||||
res += [id]
|
||||
elif attr == "filepath":
|
||||
res += [file]
|
||||
elif attr == "art":
|
||||
from libsaria import lastfm
|
||||
res += [id]
|
||||
elif attr == "artist":
|
||||
res += [tags.artist]
|
||||
elif attr == "album":
|
||||
res += [tags.album]
|
||||
elif attr == "title":
|
||||
res += [tags.title]
|
||||
elif attr == "year":
|
||||
res += [tags.year]
|
||||
elif attr == "seconds":
|
||||
res += [seconds]
|
||||
elif attr == "lenstr":
|
||||
lenstr = "%s" % timedelta(seconds=seconds)
|
||||
if lenstr[0] == "0" and lenstr[1] == ":":
|
||||
lenstr = lenstr[2:]
|
||||
res += [lenstr]
|
||||
else:
|
||||
return "???"
|
||||
if len(res) == 1:
|
||||
return res[0]
|
||||
return res
|
||||
|
||||
def set_attr(*args):
|
||||
pass
|
||||
|
||||
def save():
|
||||
pass
|
||||
|
||||
init()
|
Loading…
Reference in New Issue