#!/usr/bin/python

## Nic's OGG hack to update the .meta_storage file with ogg
## data. Could it be simpler?
##
## Cheers to all ogg users around there. I had a profecy this
## morning. A vision of Ogg. He told me: 'We will prevail'.
##
##   ++nicolau 20090810

from pysqlite2 import dbapi2 as sqlite
from mutagen.oggvorbis import OggVorbis

con = sqlite.connect("/home/user/.meta_storage",isolation_level=None)
cur = con.cursor()

cur.execute("select Filename from Metadata where Filename LIKE '%.ogg' AND (Album IS NULL OR Track IS NULL OR Artist IS NULL)")

for k in cur.fetchall():
   print k[0]
   try:
       a=OggVorbis(k[0])
   except:
       print 'fanofau'
       continue

   #print a

   for t in ['artist', 'album', 'title', 'date', 'tracknumber', 'cat']:
       if not t in a.keys():
           a[t]=''

   cons= 'update Metadata set '+\
       '   Artist="'+a['artist'][0]+'"'\
       ', Album="' + a['album'][0]+'"'\
       ', Title="' + a['title'][0]+'"'\
       ', Year="'  + a['date'][0]+'"'\
       ',  Track="' + a['tracknumber'][0]+'"'\
       ', Genre="'+ a['cat'][0]+'"'\
       ' WHERE Filename LIKE "'+k[0]+'"'

   print cons
   con.execute(cons)