LIl things.

This commit is contained in:
Jason A. Donenfeld 2011-05-06 22:48:09 -04:00
parent 2e017d0df3
commit 8bd9d1519e
7 changed files with 109 additions and 28 deletions

View File

@ -14,7 +14,7 @@ def trim_base_custom(path, base):
def trim_base(path): def trim_base(path):
return trim_base_custom(path, trim_base.base) return trim_base_custom(path, trim_base.base)
def cache_base(path): def cache_base(path):
path = trim_base(path).replace('/', '-').replace(' ', '_').replace('(', '').replace(')', '').replace('#', '').replace('[', '').replace(']', '').replace('"', '').replace("'", '').replace('_-_', '-').lower() path = trim_base(path).replace('/', '-').replace(' ', '_').replace('(', '').replace('&', '').replace(',', '').replace(')', '').replace('#', '').replace('[', '').replace(']', '').replace('"', '').replace("'", '').replace('_-_', '-').lower()
while path.find("--") != -1: while path.find("--") != -1:
path = path.replace("--", "-") path = path.replace("--", "-")
while path.find("__") != -1: while path.find("__") != -1:

View File

@ -52,6 +52,17 @@ class Album(object):
if not self._albums_sorted: if not self._albums_sorted:
self._albums.sort() self._albums.sort()
self._albums_sorted = True self._albums_sorted = True
@property
def empty(self):
if len(self._photos) != 0:
return False
if len(self._albums) == 0:
return True
for album in self._albums:
if not album.empty:
return False
return True
def cache(self, base_dir): def cache(self, base_dir):
self._sort() self._sort()
fp = open(os.path.join(base_dir, self.cache_path), 'w') fp = open(os.path.join(base_dir, self.cache_path), 'w')
@ -75,10 +86,15 @@ class Album(object):
return album return album
def to_dict(self, cripple=True): def to_dict(self, cripple=True):
self._sort() self._sort()
subalbums = []
if cripple: if cripple:
subalbums = [ { "path": trim_base_custom(sub.path, self._path), "date": sub.date } for sub in self._albums ] for sub in self._albums:
if not sub.empty:
subalbums.append({ "path": trim_base_custom(sub.path, self._path), "date": sub.date })
else: else:
subalbums = self._albums for sub in self._albums:
if not sub.empty:
subalbums.append(sub)
return { "path": self.path, "date": self.date, "albums": subalbums, "photos": self._photos } return { "path": self.path, "date": self.date, "albums": subalbums, "photos": self._photos }
def photo_from_path(self, path): def photo_from_path(self, path):
for photo in self._photos: for photo in self._photos:

View File

@ -49,9 +49,12 @@ class TreeWalker:
if photo.is_valid: if photo.is_valid:
self.all_photos.append(photo) self.all_photos.append(photo)
album.add_photo(photo) album.add_photo(photo)
if not album.empty:
print "Writing cache of %s" % album.cache_path print "Writing cache of %s" % album.cache_path
album.cache(self.cache_path) album.cache(self.cache_path)
self.all_albums.append(album) self.all_albums.append(album)
else:
print "Not writing cache of %s because it's empty" % album.cache_path
return album return album
def big_lists(self): def big_lists(self):
photo_list = [] photo_list = []
@ -69,19 +72,15 @@ class TreeWalker:
fp.close() fp.close()
def remove_stale(self): def remove_stale(self):
print "Removing stale cache entries." print "Building list of all cache entries."
for cache in os.listdir(self.cache_path): all_cache_entries = { "all_photos.json": True, "latest_photos.json": True }
match = False
for album in self.all_albums: for album in self.all_albums:
if cache == album.cache_path: all_cache_entries[album.cache_path] = True
match = True
break
if match:
continue
for photo in self.all_photos: for photo in self.all_photos:
if cache in photo.image_caches: for entry in photo.image_caches:
match = True all_cache_entries[entry] = True
break print "Searching stale cache entries."
if not match: for cache in os.listdir(self.cache_path):
if cache not in all_cache_entries:
print "Removing stale cache %s" % cache print "Removing stale cache %s" % cache
os.unlink(os.path.join(self.cache_path, cache)) os.unlink(os.path.join(self.cache_path, cache))

View File

@ -17,7 +17,7 @@ a:hover {
top: 0; top: 0;
padding: 0.4em; padding: 0.4em;
font-weight: bold; font-weight: bold;
font-size: 1.2em; font-size: 1.15em;
} }
#loading { #loading {
display: none; display: none;
@ -82,6 +82,33 @@ a:hover {
right: 0; right: 0;
text-align: center; text-align: center;
} }
#photo-box {
display: inline;
}
#photo-links {
background-color: #000000;
font-weight: bold;
height: 10px;
font-size: 10px;
line-height: 7px;
padding-top: 3px;
padding-bottom: 3px;
padding-right: 10px;
padding-left: 10px;
display: none;
border-top-right-radius: 5px;
border-top-left-radius: 5px;
-moz-border-top-right-radius: 5px;
-moz-border-top-left-radius: 5px;
-webkit-border-top-right-radius: 5px;
-webkit-border-top-left-radius: 5px;
}
#photo-bar {
position: absolute;
bottom: 0;
width: 100%;
text-align: center;
}
.photo-view-container { .photo-view-container {
position: absolute; position: absolute;
height: 150px; height: 150px;
@ -94,3 +121,12 @@ a:hover {
padding: 0 !important; padding: 0 !important;
text-align: center; text-align: center;
} }
#error {
display: none;
text-align: center;
padding-top: 20%;
font-size: 4em;
font-weight: bold;
font-style: italic;
}

View File

@ -3,12 +3,12 @@
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>JasonDonenfeld.com &#8211; Colors</title> <title>JasonDonenfeld.com &#8211; Photos</title>
<link href="css/styles.min.css" rel="stylesheet" type="text/css" /> <link href="css/styles.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/scripts.min.js"></script> <script type="text/javascript" src="js/scripts.min.js"></script>
</head> </head>
<body> <body>
<div id="title">Colors</div> <div id="title">Photos</div>
<div id="album-view"> <div id="album-view">
<div id="thumbs"> <div id="thumbs">
<div id="loading">Loading...</div> <div id="loading">Loading...</div>
@ -16,10 +16,20 @@
<div id="subalbums"></div> <div id="subalbums"></div>
</div> </div>
<div id="photo-view"> <div id="photo-view">
<div id="photo-box">
<a id="next-photo"><img id="photo" /></a> <a id="next-photo"><img id="photo" /></a>
<div id="photo-bar">
<div id="photo-links">
<a id="metadata-link">metadata</a> | <a id="original-link" target="_blank">download original</a>
</div>
</div>
</div>
<a id="back">&larr;</a> <a id="back">&larr;</a>
<a id="next">&rarr;</a> <a id="next">&rarr;</a>
</div> </div>
<div id="error">Forgot my camera...</div>
</body> </body>
</html> </html>

View File

@ -10,6 +10,8 @@ $(document).ready(function() {
.replace(/\(/g, "") .replace(/\(/g, "")
.replace(/\)/g, "") .replace(/\)/g, "")
.replace(/#/g, "") .replace(/#/g, "")
.replace(/&/g, "")
.replace(/,/g, "")
.replace(/\[/g, "") .replace(/\[/g, "")
.replace(/\]/g, "") .replace(/\]/g, "")
.replace(/"/g, "") .replace(/"/g, "")
@ -42,7 +44,7 @@ $(document).ready(function() {
$.ajax({ $.ajax({
type: "GET", type: "GET",
url: "cache/" + current_album_cache + ".json", url: "cache/" + current_album_cache + ".json",
error: function() { $(document.body).html("Couldn't fetch it."); }, error: die,
success: albumLoaded success: albumLoaded
}); });
} }
@ -147,6 +149,8 @@ $(document).ready(function() {
$("#back").attr("href", "#" + current_album_cache + "/" + cachePath(current_album.photos[ $("#back").attr("href", "#" + current_album_cache + "/" + cachePath(current_album.photos[
(current_photo_index - 1 < 0) ? (current_album.photos.length - 1) : (current_photo_index - 1) (current_photo_index - 1 < 0) ? (current_album.photos.length - 1) : (current_photo_index - 1)
].name)); ].name));
$("#original-link").attr("target", "_blank").attr("href", "albums/" + current_album.path + "/" + current_photo.name);
$("#metadata-link").attr("href", "javascript:alert('Coming soon...')");
$("#album-view").addClass("photo-view-container"); $("#album-view").addClass("photo-view-container");
$("#subalbums").hide(); $("#subalbums").hide();
@ -188,13 +192,19 @@ $(document).ready(function() {
$.ajax({ $.ajax({
type: "GET", type: "GET",
url: "cache/" + album + ".json", url: "cache/" + album + ".json",
error: function() { $(document.body).html("Couldn't fetch it."); }, error: die,
success: callback success: callback
}); });
} }
function albumThumbFinder(album, callback) { function albumThumbFinder(album, callback) {
fetchAlbumForThumb(album, function(fetchedAlbum) { albumForThumbIteration(fetchedAlbum, callback); }); fetchAlbumForThumb(album, function(fetchedAlbum) { albumForThumbIteration(fetchedAlbum, callback); });
} }
function die() {
$("#album-view").hide();
$("#photo-view").hide();
$("#title").hide();
$("#error").fadeIn(5000);
}
var current_album_cache = null; var current_album_cache = null;
var current_photo_cache = null; var current_photo_cache = null;
@ -237,4 +247,10 @@ $(document).ready(function() {
} }
return true; return true;
}); });
$("#photo-box").mouseenter(function() {
$("#photo-links").stop().fadeTo("slow", 0.50).css("display", "inline");
});
$("#photo-box").mouseleave(function() {
$("#photo-links").stop().fadeOut("slow");
});
}); });

View File

@ -4,7 +4,7 @@ function cachePath($path) {
$path = substr($path, 1); $path = substr($path, 1);
if ($path[strlen($path) - 1] == '/') if ($path[strlen($path) - 1] == '/')
$path = substr($path, 0, strlen($path) - 1); $path = substr($path, 0, strlen($path) - 1);
$path = str_replace('_-_', '-', str_replace('/', '-', str_replace(' ', '_', str_replace('(', '', str_replace(')', '', str_replace('#', '', str_replace('[', '', str_replace(']', '', str_replace('"', '', str_replace("'", '', strtolower($path))))))))))); $path = str_replace('_-_', '-', str_replace('/', '-', str_replace(' ', '_', str_replace('(', '', str_replace(')', '', str_replace('#', '', str_replace('[', '', str_replace(']', '', str_replace('&', '', str_replace(',', '', str_replace('"', '', str_replace("'", '', strtolower($path)))))))))))));
while (strpos($path, "--") !== false) while (strpos($path, "--") !== false)
$path = str_replace("--", "-", $path); $path = str_replace("--", "-", $path);
while (strpos($path, "__") !== false) while (strpos($path, "__") !== false)
@ -23,9 +23,13 @@ if (strpos(strtolower($url), ".php") == strlen($url) - 4) {
$url = substr($url, 0, strlen($url) - 4); $url = substr($url, 0, strlen($url) - 4);
$index = strrpos($url, "/"); $index = strrpos($url, "/");
$redirect = "/#".cachePath(substr($url, 0, $index))."/".cachePath(substr($url, $index)); $redirect = "/#".cachePath(substr($url, 0, $index))."/".cachePath(substr($url, $index));
} else if (strpos($url, "/cache/") === 0 || strpos($url, "/albums/") === 0 || strpos($url, "/img/") === 0 || strpos($url, "/img/") === 0 || strpos($url, "/js/") === 0 || strpos($url, "/css/") === 0) {
header("HTTP/1.1 404 Not Found");
exit();
} else } else
$redirect = "/#".cachePath($url); $redirect = "/#".cachePath($url);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $redirect"); header("Location: $redirect");
?> ?>