150 lines
3.6 KiB
Python
150 lines
3.6 KiB
Python
#! /usr/bin/python
|
|
|
|
# To change this template, choose Tools | Templates
|
|
# and open the template in the editor.
|
|
|
|
__author__="bjschuma"
|
|
__date__ ="$Jan 27, 2010 6:21:27 PM$"
|
|
|
|
|
|
from bt.message import write
|
|
from sqlite3 import *
|
|
from bt import sql
|
|
import settings
|
|
|
|
|
|
def init():
|
|
if sql.dbexists() == True:
|
|
return
|
|
|
|
table = sql.CTable("library")
|
|
table.addcol("id","INTEGER","PRIMARY KEY AUTOINCREMENT")
|
|
table.addcol("name","TEXT","UNIQUE")
|
|
table.addcol("path","TEXT","UNIQUE")
|
|
table.execute()
|
|
|
|
table = sql.CTable("artist")
|
|
table.addcol("id","INTEGER","PRIMARY KEY AUTOINCREMENT")
|
|
table.addcol("name","TEXT","UNIQUE")
|
|
table.execute()
|
|
|
|
table = sql.CTable("album")
|
|
table.addcol("id","INTEGER","PRIMARY KEY AUTOINCREMENT")
|
|
table.addcol("name","TEXT","UNIQUE")
|
|
table.execute()
|
|
|
|
table = sql.CTable("track")
|
|
table.addcol("id","INTEGER","PRIMARY KEY AUTOINCREMENT")
|
|
table.addcol("artist","INTEGER")
|
|
table.addcol("album","INTEGER")
|
|
table.addcol("count","INTEGER")
|
|
table.addcol("length","INTEGER")
|
|
table.addcol("name","TEXT")
|
|
table.addcol("path","TEXT","UNIQUE")
|
|
table.execute()
|
|
|
|
table = sql.CTable("libtrack")
|
|
table.addcol("library","INTEGER")
|
|
table.addcol("track","INTEGER")
|
|
table.execute()
|
|
|
|
|
|
|
|
def newlib(name,path):
|
|
try:
|
|
ins = sql.Insert('library',[None,name,path])
|
|
ins.execute()
|
|
except:
|
|
pass
|
|
|
|
|
|
def rmtrk(trid):
|
|
# Get the artist and album of the track
|
|
sel = sql.Select("artist,album","track","id="+str(trid))
|
|
(arid,alid) = sel.execute().fetchone()
|
|
|
|
rm = sql.Remove("track","id="+str(trid))
|
|
rm.execute()
|
|
|
|
# Remove artist if no other tracks are using this artist
|
|
sel = sql.Select("count(*)","track","artist="+str(arid))
|
|
count = sel.execute().fetchone()[0]
|
|
if count == 0:
|
|
rm = sql.Remove("artist","id="+str(arid))
|
|
rm.execute()
|
|
|
|
# Remove album if no other tracks are using this album
|
|
sel = sql.Select("count(*)","track","album="+str(alid))
|
|
count = sel.execute().fetchone()[0]
|
|
if count == 0:
|
|
rm = sql.Remove("album","id="+str(alid))
|
|
rm.execute()
|
|
|
|
|
|
def rmlib(name):
|
|
sel = sql.Select("id,name","library","name='"+name+"'")
|
|
result = sel.execute().fetchone()
|
|
if result == []:
|
|
return
|
|
libid = result[0]
|
|
name = result[1]
|
|
|
|
sel = sql.Select("track","libtrack","library="+str(libid))
|
|
result = sel.execute().fetchall()
|
|
|
|
for track in result:
|
|
where = "track="+str(track[0])+" AND library!="+str(libid)
|
|
sel = sql.Select("track,library","libtrack",where )
|
|
r = sel.execute().fetchall()
|
|
# If track is not in any other library, it can be removed
|
|
if r == []:
|
|
rmtrk(track[0])
|
|
|
|
rm = sql.Remove("library","name='"+name+"'")
|
|
rm.execute()
|
|
|
|
rm = sql.Remove("libtrack","library="+str(libid))
|
|
rm.execute()
|
|
write("Removed library: "+name)
|
|
|
|
|
|
def libid(libname):
|
|
sel = sql.Select("id","library","name='"+libname+"'")
|
|
result = sel.execute().fetchone()
|
|
if result == None:
|
|
return None
|
|
return result[0]
|
|
|
|
|
|
def countlib(libid):
|
|
lib = str( libid )
|
|
sel = sql.Select("count(*)","libtrack","library="+lib)
|
|
count = sel.execute().fetchone()[0]
|
|
return count
|
|
|
|
|
|
def listlib():
|
|
sel = sql.Select("*","library")
|
|
result = sel.execute().fetchall()
|
|
id = settings.get("curlib")
|
|
curname = ""
|
|
write("Id Name Count Path")
|
|
write("---------------------------")
|
|
for row in result:
|
|
if row[0] == id:
|
|
curname = row[1]
|
|
count = str( countlib(row[0]) )
|
|
write( str(row[0]) + " " +
|
|
row[1] + " " + count + " " + row[2] )
|
|
write("Current: "+curname)
|
|
|
|
|
|
def getpath(dbid):
|
|
curlib = str( settings.get("curlib") )
|
|
|
|
selr = sql.Select("path","library","id="+curlib)
|
|
root = selr.execute().fetchone()[0]
|
|
|
|
selt = sql.Select("path","track","id="+str(dbid))
|
|
track = selt.execute().fetchone()[0]
|
|
return root+track |