Skip to content

Instantly share code, notes, and snippets.

@johntyree
Created August 30, 2009 00:14
Show Gist options
  • Save johntyree/177789 to your computer and use it in GitHub Desktop.
Save johntyree/177789 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from pysqlite2 import dbapi2 as sqlite
import time, sys, os, tagpy
connection = sqlite.connect(':memory:')
cursor = connection.cursor()
cursor.execute('CREATE TABLE file (path VARCHAR(255) PRIMARY KEY, timestamp DECIMAL(12), size INTEGER(12), copy BOOLEAN)')
cursor.execute('CREATE TABLE track (id INTEGER PRIMARY KEY, name VARCHAR(255), artist_id INTEGER, album_id INTEGER, genre_id INTEGER, length INTEGER)')
cursor.execute('CREATE TABLE album (id INTEGER PRIMARY KEY, name VARCHAR(255), year INTEGER(4))')
cursor.execute('CREATE TABLE artist (id INTEGER PRIMARY KEY, name VARCHAR(255))')
cursor.execute('CREATE TABLE genre (id INTEGER PRIMARY KEY, name VARCHAR(255))')
connection.commit()
rootdir = "/media/disk/Music/0-9"
for root, dirs, files in os.walk(rootdir):
for name in files:
if not '.mp3' in name:
continue
abspath = os.path.join(root, name)
relpath = os.path.relpath(abspath, rootdir)
statinfo = os.stat(abspath)
f = tagpy.FileRef(abspath)
cursor.execute('INSERT INTO file VALUES (?, ?, ?, ?)', (relpath, statinfo.st_mtime, statinfo.st_size, True))
cursor.execute('INSERT INTO artist VALUES (null, ?)', (f.tag().artist))
# cursor.execute('INSERT INTO genre VALUES (null, ?)', (f.tag().genre))
# cursor.execute('INSERT INTO album VALUES (null, ?, ?, ?)', (f.tag().album))
# cursor.execute('INSERT INTO track VALUES (null, ?, ?, ?, ?)', (f.tag().title, artist_id, f.tag().year, True))
connection.commit()
#print name + ":", statinfo.st_mtime, statinfo.st_size
"""os.path.commonprefix(list), shutil.copy2 does metadata, os.stat().st_mtime"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment