import re import urllib import os, time, stat import Image debug = 1 days = range(1,32) months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'] def parse(uri, tag=[]): uri = uri.split('/') output = {} output['tag'] = [] for i in uri: if re.search(r'^\d\d\d\d$', i) and 'year' not in output: output['year'] = int(i) elif i in tag and i not in output['tag']: output['tag'].append(i) elif i == 'pending' and 'pending' not in output: output['pending'] = 1 elif i == 's' and 'slideshow' not in output: output['slideshow'] = 1 elif i == 'l' and 'list' not in output: output['list'] = 1 elif re.search(r'^\d{1,4}x\d{1,4}$', i) and 'res' not in output: output['res'] = i elif re.search(r'^\d{1,2}$', i) and 'day' not in output: i = int(i) if i in days: output['day'] = i elif re.search(r'^\d\d\:\d\d\:\d\d$', i) and 'time' not in output: output['time'] = i elif re.search(r'^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)$', i, re.I) and 'month' not in output: output['month'] = months.index(i.lower()) + 1 elif re.search(r'^r\d{1,3}', i) and 'rotate' not in output: output['rotate'] = int(i[1:]) else: if 'comment' in output: raise ValueError if i: output["comment"] = i return output def getcomment(absfilename): im = Image.open(absfilename) if hasattr(im, 'app'): if 'COM' in im.app: return im.app['COM'] return '' def make(base, repo, filename): # repo, filename # http://pictures.natalian.org/kodak/2004/jun/23/12:32:23 if debug: print repo if debug: print filename for k, v in repo.iteritems(): if v in filename: tag = k if debug: print tag year, month, day, hour, min, sec, wday, yday, isdst = time.localtime(os.stat(filename)[stat.ST_MTIME]) date = '/'.join([str(year), str(months[month-1]), str(int(day))]) if debug: print date xtime = time.strftime('%X', time.localtime(os.stat(filename)[stat.ST_MTIME])) if debug: print xtime #urllib.quote_plus(comment) return base + '/'.join([tag, date, xtime, getcomment(filename)]) if __name__ == '__main__': picturebase = 'http://pictures.natalian.org/' #print make(picturebase, {'bab': '/home/hendry/pictures/bab', 'konica': '/home/hendry/pictures/100konic', 'olympia': '/home/hendry/pictures/100olymp'}, '/home/hendry/pictures/kodak/DCP_2758.JPG') print make(picturebase, {'bab': '/home/hendry/pictures/bab'}, '/home/hendry/pictures/bab/DCP_1179.JPG')