173 lines
3.1 KiB
Python
173 lines
3.1 KiB
Python
#! /usr/bin/python
|
|
|
|
# To change this template, choose Tools | Templates
|
|
# and open the template in the editor.
|
|
|
|
__author__="bjschuma"
|
|
__date__ ="$Jan 30, 2010 3:57:53 PM$"
|
|
|
|
|
|
from bt import sql
|
|
from bt import needle
|
|
from bt.file import *
|
|
from bt.message import write
|
|
|
|
import tagpy
|
|
import re
|
|
|
|
global total
|
|
global scanned
|
|
global added
|
|
global root
|
|
global goodFiles
|
|
global search
|
|
|
|
global libid
|
|
|
|
total = 0
|
|
added = 0
|
|
scanned = 0
|
|
root = ""
|
|
goodFiles = ["ogg", "mp3"]
|
|
|
|
|
|
# Generate search string (allows adding to goodFiles later)
|
|
def genSearch():
|
|
global goodFiles
|
|
global search
|
|
search = ".*\.("
|
|
for index,file in enumerate(goodFiles):
|
|
if index > 0:
|
|
search += "|"
|
|
search += file
|
|
search += ")"
|
|
|
|
genSearch()
|
|
|
|
|
|
|
|
# Test if the file extension is in goodFiles
|
|
def testPath(path):
|
|
global search
|
|
match = re.search(search,path,re.IGNORECASE)
|
|
if match == None:
|
|
return False
|
|
return True
|
|
|
|
|
|
|
|
def incr(path):
|
|
global total
|
|
total += 1
|
|
|
|
|
|
# Call to insert either an artist or an album
|
|
def insartalb(table,value):
|
|
if value == "":
|
|
value = "Unknown "+table.title()
|
|
#value = value.replace("\'","\'\'")
|
|
sel = sql.Select("id",table,'name="' + value + '"')
|
|
result = sel.execute().fetchall()
|
|
if result == []:
|
|
ins = sql.Insert(table,[None,value])
|
|
ins.execute()
|
|
result = sel.execute().fetchall()
|
|
return result[0][0]
|
|
|
|
|
|
# Call to insert a new track
|
|
def instrk(arid, alid, title, length, path):
|
|
global root
|
|
global libid
|
|
if title == "":
|
|
title = "Unknown Title"
|
|
|
|
path = path[len(root):]
|
|
|
|
sel = sql.Select("id","track",'path="'+path+'"')
|
|
result = sel.execute().fetchall()
|
|
|
|
if result == []:
|
|
ins = sql.Insert("track", [None, arid, alid, 0, length, title, path] )
|
|
ins.execute()
|
|
result = sel.execute().fetchall()
|
|
|
|
trid = str(result[0][0])
|
|
|
|
where = "library='"+libid+"' AND track='"+trid+"'"
|
|
sel = sql.Select("*","libtrack",where)
|
|
result = sel.execute().fetchall()
|
|
if result == []:
|
|
ins = sql.Insert("libtrack",[int(libid),int(trid)])
|
|
ins.execute()
|
|
|
|
|
|
|
|
def addtrk(path):
|
|
global added
|
|
global scanned
|
|
|
|
scanned += 1
|
|
try:
|
|
f = tagpy.FileRef(path)
|
|
t = f.tag()
|
|
added += 1
|
|
except:
|
|
return
|
|
|
|
try:
|
|
arid = insartalb("artist", t.artist)
|
|
alid = insartalb("album", t.album)
|
|
|
|
a = f.audioProperties()
|
|
instrk(arid, alid, t.title, a.length, path)
|
|
except:
|
|
write("Error adding: "+path)
|
|
#pass
|
|
|
|
|
|
|
|
def scan((dir,func)):
|
|
files = ls(dir)
|
|
for file in files:
|
|
path = join(dir,file)
|
|
if checkDir(path) == True:
|
|
scan((path,func))
|
|
else:
|
|
if testPath(path) == True:
|
|
func(path)
|
|
|
|
|
|
def go(name):
|
|
global total
|
|
global added
|
|
global scanned
|
|
global root
|
|
global libid
|
|
total = 0
|
|
added = 0
|
|
scanned = 0
|
|
|
|
sel = sql.Select("id,path","library","name='"+name+"'")
|
|
result = sel.execute().fetchall()
|
|
if result == []:
|
|
return
|
|
libid = str(result[0][0])
|
|
path = result[0][1]
|
|
root = path
|
|
|
|
# Start a thread to count the total number of files to scan
|
|
totthr = needle.Needle(scan,(path,incr))
|
|
totthr.start()
|
|
|
|
# Start a thread to actually add tracks to the db
|
|
scthr = needle.Needle(scan,(path,addtrk))
|
|
scthr.start()
|
|
|
|
|
|
def prcnt():
|
|
global total
|
|
global scanned
|
|
global added
|
|
write( str(scanned) + " / " + str(total) )
|
|
write( "Added " + str(added) + " tracks." ) |