From 8bc5b9e25aed1bbe9b2365a3978f4bf86d2971df Mon Sep 17 00:00:00 2001 From: bjschuma Date: Wed, 30 Dec 2009 00:28:25 -0500 Subject: [PATCH] Created an artists table, pull out artist information from each song --- src/core/extract.py | 85 ++++++++++++++++++++++++++++++++++++++ src/core/scan.py | 11 +++-- src/core/tools/database.py | 30 ++++++++++++-- 3 files changed, 119 insertions(+), 7 deletions(-) create mode 100644 src/core/extract.py diff --git a/src/core/extract.py b/src/core/extract.py new file mode 100644 index 00000000..992ee607 --- /dev/null +++ b/src/core/extract.py @@ -0,0 +1,85 @@ +# 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 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 + + # Add an artist + artist = None + if t.artist=="": + artist = (None,"Unknown",) + else: + artist = (None,t.artist,) + #write(t.artist) + database.insert("artist", artist ) + #database.insert("files",(file,)) + #except: + # pass diff --git a/src/core/scan.py b/src/core/scan.py index 6295ca79..2c254178 100644 --- a/src/core/scan.py +++ b/src/core/scan.py @@ -11,9 +11,12 @@ type = "core" path = "" opt = [] +import tagpy + from bt.message import write from bt.file import * +from manager import manager #from library import * from tools import database #from library.scan import scan @@ -38,7 +41,8 @@ def scan(dir): scan(path) else: write(path,True) - database.insert(path) + manager.run("extract", path) + #database.insert(path) #self.files += [path] @@ -48,10 +52,11 @@ def run(args=None): return space = ' ' path = expandPath( space.join(args) ) - #database.open() + database.open() scan(path) - count = str(database.count("files")) + #count = str(database.count("files")) + count = str(database.count("artist")) write("Library contains "+count+" files.") database.close() diff --git a/src/core/tools/database.py b/src/core/tools/database.py index 2a15b4a8..cfa62436 100644 --- a/src/core/tools/database.py +++ b/src/core/tools/database.py @@ -22,8 +22,10 @@ import sqlite3 #c = None global db global conn +global vals db = None conn = None +vals = dict() # Collect all information for what to insert @@ -34,7 +36,26 @@ conn = None def create(): global conn - conn.execute("create table files (path text)") + conn.executescript(""" + CREATE TABLE files + ( + path TEXT PRIMARY KEY + ); + + CREATE TABLE artist + ( + arid INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT UNIQUE + ); + + UPDATE SQLITE_SEQUENCE SET seq = 1 WHERE name = 'artist'; + + """) + + global vals + vals["files"] = "?" + vals["artist"]= "?,?" + def open(): @@ -60,13 +81,14 @@ def init(): close() -def insert(file): +def insert(table, values): global conn if conn == None: open() + global vals #c = conn.cursor() - t = (file,) - conn.execute('insert into files values(?)',t) + #t = (file,) + conn.execute('INSERT OR IGNORE INTO ' + table + ' VALUES(' + vals[table] + ')',values) #c.close()