Update of /cvsroot/freevo/freevo/WIP/Aubin
In directory sc8-pr-cvs1:/tmp/cvs-serv8302
Added Files:
makeplaylist.py mp3dir.py musicsqlimport.py player.py
sqlmusic_view.py
Log Message:
Restoring my old sqlite stuff... going to work on doing it properly...
Also, mp3dir is a function that, given a directory will return:
Artist
Album
Playtime (m:s)
Year
which are things I'd like to have in the info display, maybe under the cover image
or something.
(It's not amazing, but it'll know to put "Various" if the artist varies, or the
album title varies.)
Aubin
--- NEW FILE: makeplaylist.py ---
#!/usr/bin/env python
#if 0 /*
# -----------------------------------------------------------------------
# makeplaylist.py - create a playlist from the sqlite database
# -----------------------------------------------------------------------
# $Id: makeplaylist.py,v 1.1 2004/01/09 22:44:58 outlyer Exp $
#
# Notes:
# A little program to demonstrate what can be done with sqlite
# It only outputs static playlists in .m3u format now, but you can
# see what can be done.
#
# Todo:
#
# -----------------------------------------------------------------------
# $Log: makeplaylist.py,v $
# Revision 1.1 2004/01/09 22:44:58 outlyer
# Restoring my old sqlite stuff... going to work on doing it properly...
# Also, mp3dir is a function that, given a directory will return:
#
# Artist
# Album
# Playtime (m:s)
# Year
#
# which are things I'd like to have in the info display, maybe under the cover image
# or something.
#
# (It's not amazing, but it'll know to put "Various" if the artist varies, or the
# album title varies.)
#
# Aubin
#
# Revision 1.2 2003/08/23 12:51:42 dischi
# removed some old CVS log messages
#
# Revision 1.1 2003/08/23 09:09:18 dischi
# moved some helpers to src/helpers
#
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2002 Krister Lagerstrom, et al.
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------------------------------------------------------- */
#endif
import sqlite
import os, sys
import config, util
import musicsqlimport as fdb
base = 'SELECT path, filename FROM music '
topten = base + 'ORDER BY play_count DESC'
notrecent = base + 'ORDER BY last_play'
nineties = base + 'WHERE year like "199%"'
oldsongs = base + 'WHERE year < 1990'
queries = [('Favourite Songs (Most Played)',topten),
('Songs not listened to in awhile', notrecent),
('Songs from the 1990s',nineties),
('Songs before the 1990s',oldsongs) ]
if __name__ == '__main__':
if not fdb.check_db():
print "Database missing or empty, please run 'freevo musicsqlimport'"
print "before running this program"
sys.exit(1)
if not len(sys.argv) > 1 or sys.argv[1] == '--help':
print 'Usage: '
print '\tfreevo makeplaylist <query type> <number of songs> <output m3u file>'
print
print '\tThe following queries are available.'
print
m = 0
for query in queries:
print '\t%i - %s' % (m, query[0])
m = m + 1
else:
output = None
db = sqlite.connect(fdb.DATABASE,autocommit=0)
cursor = db.cursor()
#cursor.execute(queries[sys.argv[1]][1])
q = int(sys.argv[1])
sql = queries[q][1]
if len(sys.argv) > 2:
sql = sql + ' LIMIT %i' % (int(sys.argv[2]))
if len(sys.argv) > 3:
# write to file or stdout?
output = '%s.m3u' % (sys.argv[3])
print "Writing to output file: %s..." % (output)
f = open(output,'w+')
cursor.execute(sql)
for row in cursor.fetchall():
line = os.path.join(row['path'],row['filename'])
if output:
f.write(line+'\n')
else:
print line
--- NEW FILE: mp3dir.py ---
#!/usr/bin/env python
#
import sys, os, string, fnmatch
import mmpython
def recursefolders(root, recurse=0, pattern='*', return_folders=0):
# initialize
result = []
# must have at least root folder
try:
names = os.listdir(root)
except os.error:
return result
# expand pattern
pattern = pattern or '*'
pat_list = string.splitfields( pattern , ';' )
# check each file
for name in names:
fullname = os.path.normpath(os.path.join(root, name))
# grab if it matches our pattern and entry type
for pat in pat_list:
if fnmatch.fnmatch(name, pat):
if os.path.isfile(fullname) or \
(return_folders and vfs.isdir(fullname)):
result.append(fullname)
continue
# recursively scan other folders, appending results
if recurse:
result = result + recursefolders( fullname, recurse,
pattern, return_folders )
return result
if sys.argv[1]:
songs = recursefolders(sys.argv[1], pattern='*.mp3',recurse=1)
dirtime = 0
artist = ''
album = ''
for song in songs:
try:
data = mmpython.parse(song)
myartist = data['artist']
myalbum = data['album']
if myartist != artist and artist != '': artist = 'Various'
else: artist = myartist
if myalbum != album and album != '': album = 'Various'
else: album = myalbum
total = data['length']
dirtime = dirtime + total
except:
pass
print artist
print album
print dirtime
--- NEW FILE: musicsqlimport.py ---
#!/usr/bin/env python
#if 0 /*
# -----------------------------------------------------------------------
# musicsqlimport.py - import all music files into sql database
# -----------------------------------------------------------------------
# $Id: musicsqlimport.py,v 1.1 2004/01/09 22:44:58 outlyer Exp $
#
# Notes:
#
# Todo:
#
# -----------------------------------------------------------------------
# $Log: musicsqlimport.py,v $
# Revision 1.1 2004/01/09 22:44:58 outlyer
# Restoring my old sqlite stuff... going to work on doing it properly...
# Also, mp3dir is a function that, given a directory will return:
#
# Artist
# Album
# Playtime (m:s)
# Year
#
# which are things I'd like to have in the info display, maybe under the cover image
# or something.
#
# (It's not amazing, but it'll know to put "Various" if the artist varies, or the
# album title varies.)
#
# Aubin
#
# Revision 1.2 2003/08/23 12:51:42 dischi
# removed some old CVS log messages
#
#
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2002 Krister Lagerstrom, et al.
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------------------------------------------------------- */
#endif
# The basics
import os,string,fnmatch,sys
# The DB stuff
import sqlite
# Metadata tools
import mmpython
import config, util
CACHEDIR = config.FREEVO_CACHEDIR
DBFILE ='freevo.sqlite'
DATABASE = os.path.join(config.FREEVO_CACHEDIR, DBFILE)
mmpython.use_cache('%s/mmpython' % (config.FREEVO_CACHEDIR))
# Utility functions
def inti(a):
if a:
return int(a)
else:
return 0
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():
db = sqlite.connect(DATABASE)
cursor = db.cursor()
cursor.execute("CREATE TABLE music (id INTEGER PRIMARY KEY, dirtitle VARCHAR(255),
md5 VARCHAR(32) UNIQUE, 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 float, play_count
NUMERIC, start_time NUMERIC, end_time NUMERIC, 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
mmpython.cache_dir(os.path.dirname(filename))
a = mmpython.parse(filename)
md5 = util.md5file(filename)
# 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\')"
\
%
(util.escape(dirtitle),md5,util.escape(os.path.dirname(filename)),util.escape(os.path.basename(filename)),
\
'mp3',util.escape(a['artist']),util.escape(a['title']),util.escape(a['album']),inti(a['date']),trackno,
\
trackof, 100,0,0,'0',inti(a['length']),0,'null')
SQL = 'INSERT OR IGNORE 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)
cursor.execute('SELECT path, filename FROM music')
count = 0
for row in cursor.fetchall():
try:
songs.remove(os.path.join(row['path'],row['filename']))
count = count + 1
except ValueError:
# Why doesn't it just give a return code
pass
if count > 0:
print "Skipped %i songs already in the database..." % (count)
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 len(sys.argv)>1 and sys.argv[1] == '--help':
print 'Freevo musicsqlimport helper'
print 'adds all mp3 files into the database'
sys.exit(0)
if not check_db():
print "Creating data file..."
create_db()
if len(sys.argv) > 1:
print "Adding %s to %s..." % (sys.argv[1],config.DIR_AUDIO[0][0])
mainloop(sys.argv[1],config.DIR_AUDIO[0][0])
else:
print "Populating Database with entries"
for dir in config.DIR_AUDIO:
print "Scanning %s" % dir[0]
mainloop(dir[1],dir[0])
--- NEW FILE: sqlmusic_view.py ---
#if 0 /*
# -----------------------------------------------------------------------
# sqlmusic_view.py - Plugin for displaying information stored in sqlite
# -----------------------------------------------------------------------
#
# Notes: Info plugin.
# You can show music information from the sqlite database.
# Activate with: plugin.activate('audio.sqlmusic_view')
# You can also bind it to a key (in this case key 2):
# EVENTS['menu']['2'] = Event(MENU_CALL_ITEM_ACTION, arg='sqlmusic_view')
#
# Currently, this serves little purpose unless you want to work on the sql
# music stuff, in which case it's a convenient way to make sure stuff is working.
#
# -----------------------------------------------------------------------
# $Log
#
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2002 Krister Lagerstrom, et al.
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------------------------------------------------------- */
#endif
import os
import menu
import config
import plugin
import re
import time
import sqlite,util
from gui.AlertBox import AlertBox
class PluginInterface(plugin.ItemPlugin):
def actions(self, item):
self.item = item
if item.type == 'audio':
return [ ( self.info_show, _('Show database information for this file'),
'info_showdata') ]
return []
def info_show(self, arg=None, menuw=None):
"""
show info for this item
"""
file = self.item.filename
db = sqlite.connect('%s/freevo.sqlite' % (config.FREEVO_CACHEDIR))
cursor = db.cursor()
sql = 'SELECT md5, last_play,play_count,rating FROM music' + \
' WHERE path = \'%s\' and filename = \'%s\'' %
(util.escape(os.path.dirname(file)),
util.escape(os.path.basename(file)))
cursor.execute(sql)
md5,last_play,play_count,rating = cursor.fetchone()
if last_play != 0:
last = str(time.strftime("%a, %d %b %Y %I:%M:%P",
time.localtime(last_play)))
else:
last = 'Never'
box = AlertBox(width=550, height=400, text='MD5: %s\nLast Play: %s\nPlay
Count: %s\nRating: %s\n'
% (str(md5),last,str(play_count),str(rating)))
box.show()
db.close()
return
-------------------------------------------------------
This SF.net email is sponsored by: Perforce Software.
Perforce is the Fast Software Configuration Management System offering
advanced branching capabilities and atomic changes on 50+ platforms.
Free Eval! http://www.perforce.com/perforce/loadprog.html
_______________________________________________
Freevo-cvslog mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog