Update of /cvsroot/freevo/freevo/helpers
In directory sc8-pr-cvs1:/tmp/cvs-serv13420/helpers

Added Files:
        musicsqlimport.py 
Log Message:
Ok, I think this is a pretty good preliminary version, and since it doesn't
touch anything else, I think it could go into helpers.

Current issue:

The big one is that it tries to reimport entries even if they already exist,
it'll probably fail when that happens. 

As it stands though, I imported 400 entries of varying tag quality with 
md5 checksums into a new database in around 27s. I'm using the config entries
to figure out which folders to add and where to put the database.


--- NEW FILE: musicsqlimport.py ---
#!/usr/bin/env python

# The basics
import os,string,fnmatch,sys

# The DB stuff
import sqlite

# Metadata tools
import mmpython,fchksum

sys.path.append('./src/')
import config, util

CACHEDIR = config.FREEVO_CACHEDIR
DBFILE  ='freevo.sqlite'
DATABASE = CACHEDIR +"/" + DBFILE

print DATABASE

# Utility functions

def inti(a):
    if a:
        return int(a)
    else:
        return 0

def escape(sql):
    if sql:
        sql = sql.replace('\'','\'\'')
        return sql
    return 'null'

# Only call once... how do we check if the tables exist?


def check_db():
    # verify the database exists
    if not os.path.exists(DATABASE):
        return None
    # verify the table exists
    db = sqlite.connect(DATABASE)
    cursor = db.cursor()
    cursor.execute('SELECT name FROM sqlite_master where name="music" and 
type="table"')
    if not cursor.fetchone()[0] == 'music':
        return None
    return DATABASE

def create_db():
    print DATABASE
    db = sqlite.connect(DATABASE)
    cursor = db.cursor()
    cursor.execute("CREATE TABLE music (id INTEGER PRIMARY KEY, dirtitle VARCHAR(255), 
md5 VARCHAR(33), path VARCHAR(255), filename VARCHAR(255), type VARCHAR(3), artist 
VARCHAR(255), title VARCHAR(255), album VARCHAR(255), year VARCHAR(255), track 
NUMERIC(3), track_total NUMERIC(3), bpm NUMERIC(3), last_play timestamp, play_count 
NUMERIC, start_time VARCHAR, end_time VARCHAR, rating NUMERIC, eq  VARCHAR)")
    db.commit()
    db.close()

def make_query(filename,dirtitle):
    md5 = 'null'

    if not os.path.exists(filename):
        print "File %s does not exist" % (filename)
        return None
    
    a = mmpython.parse(filename)

    # You really, really want fchksum for this, it's about twice as fast as raw python.
    md5 = fchksum.fmd5t(filename)[0]

    # This is ugly, how do I clean it up?
    trackno = -1
    trackof = -1


    if a['trackno']:
        trackno = inti(a['trackno'].split('/')[0])
        if a['trackno'].find('/') != -1:
            trackof = inti(a['trackno'].split('/')[1])

    VALUES = 
"(null,\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',%i,%i,%i,\'%s\',%f,%i,\'%s\',\'%s\',%i,\'%s\')"
 \
        % 
(escape(dirtitle),md5,escape(os.path.dirname(filename)),escape(os.path.basename(filename)),
 \
           
'mp3',escape(a['artist']),escape(a['title']),escape(a['album']),inti(a['date']),trackno,
 \
           trackof, 100,0,0,'0','262',0,'null')

    SQL = 'INSERT INTO music VALUES ' + VALUES

    return SQL

def mainloop(path='/media/Music',dirtitle='',type='*.mp3'):

    db = sqlite.connect(DATABASE,autocommit=0)
    cursor = db.cursor()

    songs = util.recursefolders(path,1,type,1)
    tempvar = ''
    for song in songs:
        cursor.execute(make_query(song,dirtitle))

    # Only call commit after we're done to save time... (autocommit slows down the db 
dramatically)
    db.commit()
    db.close()

if __name__ == '__main__':
    if not check_db():
        print "Creating data file..."
        create_db()
    print "Populating Database with entries"
    for dir in config.DIR_AUDIO:
        print "Scanning %s" % dir[0]
        mainloop(dir[1],dir[0])




-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
_______________________________________________
Freevo-cvslog mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to