ocarina/src/core/extract.py

97 lines
1.6 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
# Called when the plugin needs to perform some action
def run(file=None):
if file == None:
return
if validFile(file) == False:
return
try:
f = tagpy.FileRef(file)
t = f.tag()
except:
return
artists = settings.get("artists")
# Find artist information
artist = t.artist
if artist == "":
artist = "Unknown Artist"
# If artist does not exist yet, insert into database
if (artist in artists.keys())==False:
arid = database.count("artist")
database.insert("artist", (arid, artist) )
artists[artist] = arid
else:
arid = artists[artist]
album = t.album
if album == "":
album = "Unknown Album"
alid = database.count("album")
database.insert("album", (alid, album) )