-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateMultiAlbum.py
executable file
·153 lines (116 loc) · 4.63 KB
/
generateMultiAlbum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python
import xml.etree.cElementTree as ET
import sys, os, string, re, shutil, hashlib, fnmatch
# TODO: nicer block schema (div around links, block them, center them)
# TODO: fix the first in thumbstrip
# TODO: bigger thumbstrip
highslidePath = '../highslide'
THUMB_SIZE="144x144"
def parseDir(directory, path):
subalbum = {}
subalbum['name'] = directory.translate(string.maketrans("_", " "))
subalbum['dir'] = directory
print("parsing dir " + directory)
thumbDir = os.path.join(path, 'thumbs')
if not os.path.isdir(thumbDir):
os.makedirs(thumbDir)
photos = []
for file in sorted(fnmatch.filter(os.listdir(path), "*.[Jj][Pp][Gg]")):
thumb = os.path.join(thumbDir, file)
if not os.path.isfile(thumb):
os.system("convert " + os.path.join(path, file) + " -resize " + THUMB_SIZE + " " + thumb)
if file == "thumb.jpg":
continue
photos.append({'name' : file})
subalbum['photos'] = photos
if os.path.isfile(os.path.join(path, "thumb.jpg")):
subalbum['thumb'] = 'thumb.jpg'
else:
subalbum['thumb'] = photos[0]['name']
return subalbum
def parseDirs(target):
subalbums = []
for dirname in sorted(os.listdir(target)):
dirPath = os.path.join(target, dirname)
if os.path.isdir(dirPath) and not dirname.startswith('.'):
subalbums.append(parseDir(dirname, dirPath))
return subalbums
def generateSubalbum(f, subalbum):
global source
global target
subalbumDirName = subalbum['name'].translate(string.maketrans(" .", "__"), "'\"")
thumbsDirName = os.path.join(subalbumDirName, "thumbs")
thumbsDir = os.path.join(target, thumbsDirName)
if not os.path.isdir(thumbsDir):
os.makedirs(thumbsDir)
f.write('<span class="highslide-gallery">\n')
subalbumThumbThumb = os.path.join(thumbsDirName, subalbum['thumb'])
subalbumThumb = os.path.join(subalbumDirName, subalbum['thumb'])
group = ", { thumbnailId: '" + subalbumThumb + "', slideshowGroup: '" + subalbum['dir'] + "' }"
f.write('\t<span class="subalbum-link">\n')
# thumbnail with link to first photo & album name
f.write('\t<a class="highslide" id="' + subalbumThumb + '" href="' + subalbumThumb + '" onclick="return hs.expand(this' + group + ')">\n')
f.write('\t\t<img src="' + subalbumThumbThumb + '" title="Click to view this album"/>\n\t</a>\n')
# name (desc) if there is some
f.write('\t<div class="subalbum-name">' + subalbum['name'])
f.write('</div>\n')
f.write('\t</span>\n\n') # subalbum-link
# append hidden container
f.write('\t<span class="hidden-container">\n')
for photo in subalbum['photos']:
#TODO: generate thumbs?
f.write('\t\t<span class="thumb"><a id="' + subalbumDirName + '-' + photo['name'] + '" class="highslide" href="' + os.path.join(subalbumDirName, photo['name']) + '" onclick="return hs.expand(this' + group + ')">\n')
f.write("\t\t\t<img src='" + os.path.join(thumbsDirName, photo['name']) + "'/>\n\t\t</a>\n")
f.write('\t\t</span>\n\n')
f.write('\t</span>\n') # hidden-container
f.write('</span>\n\n') # highslide-gallery
# parse cmdline
if len(sys.argv) < 3:
print "not enough parameters"
print "usage: " + sys.argv[0] + " <source dir> <albumName>"
exit(1)
# basic album info
target = sys.argv[1]
albumName = sys.argv[2]
for i in ['..', '..', '..']:
if os.path.isdir(os.path.join(target,highslidePath)):
break
highslidePath = os.path.join(i, highslidePath)
if not os.path.isdir(os.path.join(target, highslidePath)):
printf("warning: highslide not found")
# iterate albums
subalbums = parseDirs(target)
f = open(os.path.join(target, 'index.html'), 'w')
# create header, put album thumbnail & name(desc) there in center
f.write(
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">\n'
'<html>\n'
'<head>\n'
'\t<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\n'
'\t<title>' + albumName + '</title>\n'
"\n"
'\t<script type="text/javascript" src="' + highslidePath + '/highslide.js"></script>\n'
'\t<link rel="stylesheet" type="text/css" href="' + highslidePath + '/highslide.css"/>\n'
"\n"
'\t<script type="text/javascript">\n'
"\t\ths.graphicsDir = '" + highslidePath + "/graphics/';\n"
'\t</script>\n'
'\t<script type="text/javascript" src="' + highslidePath + '/config.js"></script>\n'
'\t<link rel="stylesheet" type="text/css" href="' + highslidePath + '/global.css"/>\n'
'\n'
'\t<!-- overrides here -->\n'
'</head>\n'
'\n'
'<body>\n'
'\t<div class="album-name">' + albumName + '</div>\n'
)
f.write('\t<div class="album-thumb"><img src="thumb.jpg"/></div>\n')
f.write('\t<div class="thumbnails-wrapper">\n\n')
# iterate album(s)
for subalbum in subalbums:
generateSubalbum(f, subalbum)
# close everything
f.write('\t</div>\n') # thumbnails-wrapper
f.write('</body></html>\n')
f.close()
exit(0)