#!/usr/local/bin/python import os.path import re import walkgallerysettings, MySQLdb import subprocess #pathToWalk = '/var/www/albums' #pathsToWalk = ['/var/lib/gallery2/g2data/albums', '/home/gregstoll/images/digitalblasphemy/allorig'] pathsToWalk = ['/var/lib/gallery2/g2data/albums'] pathToPutLinksIn = '/home/gregstoll/images/allgallerypictures' db = MySQLdb.connect(user=walkgallerysettings.galleryDbUsername, passwd=walkgallerysettings.galleryDbPassword, db=walkgallerysettings.galleryDb) cursor = db.cursor() cursor.execute("""SELECT g_id FROM g2_ChildEntity WHERE g_parentId=0""") rootAlbum = cursor.fetchone()[0] urlRe = re.compile(r"\[url=.*?\]") def textifyDescription(str): if (str == None): return '' str = str.replace(""", '"') str = str.replace("&", '&') str = str.replace("[b]", '') str = str.replace("[/b]", '') str = str.replace("[i]", '') str = str.replace("[/i]", '') str = str.replace("[u]", '') str = str.replace("[/u]", '') str = str.replace("[/url]", '') urlMatch = urlRe.search(str) while urlMatch != None: str = str[0:urlMatch.start()] + str[urlMatch.end():] urlMatch = urlRe.search(str) return str # g2_Item has description # g2_ChildEntity has parent relationship # g2_FileSystemEntity has path # Cache album id? testJpg = re.compile(r"""^[^.]*\.jpg$""", re.IGNORECASE) def doLinks(startDir, dirName, names): #print "startDir is %s" % startDir #print "dirName is %s" % dirName # Find album id relativeAlbum = dirName[len(startDir)+1:] albumPaths = relativeAlbum.split('/') if len(albumPaths) == 1 and albumPaths[0] == '': albumPaths = [] curAlbum = rootAlbum for albumPath in albumPaths: cursor.execute("""SELECT fse.g_id FROM g2_FileSystemEntity AS fse, g2_ChildEntity AS ce WHERE ce.g_parentId=%s AND ce.g_id = fse.g_id AND fse.g_pathComponent = %s;""", (curAlbum, albumPath)) curAlbum = cursor.fetchone()[0] #print curAlbum # Find the ids for all items in this album. nameToId = {} cursor2 = db.cursor() cursor.execute("""SELECT fse.g_id FROM g2_FileSystemEntity AS fse, g2_ChildEntity AS ce WHERE ce.g_parentId=%s AND ce.g_id = fse.g_id""", (curAlbum,)) for result in cursor: # Find the name of this item and add it to the hash cursor2.execute("""SELECT fse.g_pathComponent FROM g2_FileSystemEntity AS fse WHERE fse.g_id=%s""", (result[0])) nameToId[cursor2.fetchone()[0]] = result[0] #print nameToId del cursor2 for name in names: if (os.path.isfile(os.path.normpath(os.path.join(startDir, dirName, name)))): if (testJpg.match(name)): linkFileName = (dirName[len(startDir)+1:].replace('/', '_').replace(' ', '_')) + '_' + name #print "Analyzing %s" % linkFileName testName = os.path.normpath(os.path.join(pathToPutLinksIn, linkFileName)) if (not os.path.exists(testName)): if name in nameToId: print "Linking %s to %s - id is %d" % (name, testName, nameToId[name]) os.symlink(os.path.join(startDir, dirName, name), testName) # Find the comment for this photo. cursor.execute("""SELECT g_description FROM g2_Item WHERE g_id=%s""", (nameToId[name],)) # strip " [url] [b] [u] [i] comment = textifyDescription(cursor.fetchone()[0]) print "Comment is: %s" % comment if (comment != None and comment.strip() != ''): subprocess.call(['exiv2', '-M', 'add Exif.Photo.UserComment %s' % comment, os.path.join(startDir, dirName, name)]) else: pass #print "%s already exists: skipping..." % testName def main(): for pathToWalk in pathsToWalk: os.path.walk(pathToWalk, doLinks, pathToWalk) if (__name__ == '__main__'): main()