#!/usr/bin/python

import MySQLdb, os, re

#+--------------+--------------+------+-----+---------+----------------+
#| Field        | Type         | Null | Key | Default | Extra          |
#+--------------+--------------+------+-----+---------+----------------+
#| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
#| year         | varchar(30)  | NO   |     | NULL    |                |
#| title        | varchar(255) | NO   |     | NULL    |                |
#| date_created | datetime     | NO   |     | NULL    |                |
#+--------------+--------------+------+-----+---------+----------------+

db    = MySQLdb.Connect( user="website", passwd="t3ntacl3=", db="website_pics", use_unicode=True, charset="latin1" )
cur   = db.cursor()

for file in os.listdir( "." ):

	if ( re.search( "\.index$", file ) ):

		fh = open ( file )
		lines = fh.readlines()
		year = file.replace( ".index", "" )
		print "Checking %s" % year

		for line in lines:

			try:
				( path, title ) = line.split( "\t" )
				if not re.match("#", path):
					path = path.replace( "/$", "" )
					( null, rest ) = path.split("/")
					date = rest[0:10]
					stmnt = """INSERT INTO pics_photoalbum values ( 0, "%s", "%s", "%s", "%s 08:00:00" ); """ % ( year, path, title, date  )
					cur.execute( stmnt )
					print stmnt

			except Exception, err:
				print err

