ocarina/src/core/extract.py

130 lines
2.5 KiB
Python

# This is a simple test plugin, to make sure everything is working
__author__="bjschuma"
__date__ ="$Dec 28, 2009 10:21:06 PM$"
global name, app, type, path, opt
name = "extract"
app = "ocarina"
type = "core"
path = ""
opt = []
from bt.message import write
from tools import database
import settings
import re
import tagpy
global goodFiles
goodFiles = []
global search
# Generate the search string
def genSearch():
global goodFiles
global search
search = ".*\.("
for index,file in enumerate(goodFiles):
if index > 0:
search += "|"
search += file
search += ")"
# Return true if the file is valid and false if it is not
def validFile(filename):
global search
match = re.search(search,filename,re.IGNORECASE)
if match==None:
write(filename)
return False
return True
# Called every time the plugin is enabled
def open():
#goodFile = ".*\.(mp3|ogg)"
goodFiles = ["mp3", "ogg"]
genSearch()
# Called every time the plugin is stopped
def close():
pass
def findId(table,name):
result = database.select("id",table,"name=\'"+name.replace("\'","\'\'")+"\'").fetchone()
if result == None:
return database.count(table),False
return result[0],True
def insLibTrk(trid):
libid = settings.get("curlib")
lib = str(libid)
trk = str(trid)
result = database.select("*","libtrack","track="+trk+" and library="+lib).fetchone()
if result == None:
return True,libid
return False,-1
# Called when the plugin needs to perform some action
def run(file=None):
if file == None:
return
if len(file) < 2:
return
root = file[0]
file = file[1]
if validFile(file) == False:
return
try:
f = tagpy.FileRef(file)
t = f.tag()
except:
return
# Find artist information
artist = t.artist
if artist == "":
artist = "Unknown Artist"
# Insert the artist of it is new
arid,inserted = findId("artist",artist)
if inserted == False:
database.insert("artist", (arid, artist) )
# Find album information
album = t.album
if album == "":
album = "Unknown Album"
# Insert the album if it is new
alid,inserted = findId("album",album)
if inserted == False:
database.insert("album", (alid, album) )
# Find the track information
title = t.title
if title == "":
title = "Unknown Title"
# Insert the track if it is new
trid,inserted = findId("track",title)
if inserted == False:
a = f.audioProperties()
database.insert("track", (trid,arid,alid,0,a.length,title,file[len(root):]))
ins,libid = insLibTrk(trid)
if ins == True:
database.insert("libtrack", (libid,trid))