ocarina/src/core/ct/update.py

91 lines
1.6 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
global total
global scanned
global added
total = 0
added = 0
scanned = 0
def incr(path):
global total
total += 1
def addtrk(path):
global added
global scanned
scanned += 1
try:
f = tagpy.FileRef(path)
t = f.tag()
added += 1
except:
return
artist = t.artist
if artist == "":
artist = "Unknown Artist"
sel = sql.Select("id","artist","name='"+artist.replace("\'","\'\'")+"'")
result = sel.execute().fetchall()
write(result)
if result == []:
ins = sql.Insert("artist",[None,artist])
ins.execute()
def scan((dir,func)):
files = ls(dir)
for file in files:
path = join(dir,file)
if checkDir(path) == True:
scan((path,func))
else:
func(path)
def go(name):
global total
global added
global scanned
total = 0
added = 0
scanned = 0
sel = sql.Select("id,path","library","name='"+name+"'")
result = sel.execute().fetchall()
if result == []:
return
libid = result[0][0]
path = result[0][1]
# 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." )