Commits

Sergey Astanin  committed da531ee

Initial script: create HTML indices of all sets of a Flickr user.

  • Participants

Comments (0)

Files changed (1)

+#!/usr/bin/env python2
+from __future__ import with_statement
+
+from cgi import escape
+from os.path import expanduser
+from string import Template
+from sys import argv, version_info, exit
+from xml.etree import ElementTree as ET
+import pickle
+import warnings
+
+# warnings.filterwarnings("ignore",".*DeprecationWarning:.*")
+from flickrapi import FlickrAPI
+
+# create file ~/.flickrapi_keys with two variables or define them here
+KEYS = dict([[w.strip(' "').rstrip(' "') for w in l.strip().split("=",1)]
+             for l in file(expanduser("~/.flickrapi_keys")).readlines()])
+KEY = KEYS["KEY"]
+SECRET = KEYS["SECRET"]
+USERNAME = argv[1]
+
+flck = FlickrAPI(KEY, SECRET)
+(token, frob) = flck.get_token_part_one(perms='read')
+if not token: raw_input("Press ENTER after you authorized this program")
+flck.get_token_part_two((token, frob))
+
+resp = flck.people_findByUsername(username=USERNAME)
+nsid = resp.find("user").get("nsid")
+
+resp = flck.photosets_getList(user_id=nsid)
+psets = [ (ps.get("id"), ps.findtext(".//title"), ps.findtext(".//description"))
+          for ps in resp.findall(".//photoset") ]
+pickle.dump(psets, file("resp.pkl", "w"))
+
+INDEX_HTML=Template(u"""<!DOCTYPE html>
+<html><head><title>Photosets of $user</title></head>
+<body><h1>Photosets of $user</h1>
+<ul>
+$links
+</ul>
+</body>
+</html>""")
+
+LINK_HTML=Template(u"""<li><a href="$url">$title</a></li>""")
+
+HTML=Template(u"""<!DOCTYPE html>
+<html>
+<head><title>$title</title></head>
+<body>
+<section><h1>$title</h1>
+    <div class="description">$desc</div>
+    <div class="photos">
+    $photos
+    </div>
+</section></body></html>""")
+
+PHOTO_HTML=Template(u"""<figure>
+    <a href="$orig_src" target="_blank">
+    <img src="$src" alt="$alt" title="$title" width="$width" height="$height">
+    </a>
+    <figcaption><div class="title"><strong>$caption</strong></p>
+    <div class="description">$desc</div>
+    </figcaption>
+</figure>""")
+
+def is_large(elm, label="Large"):
+    return elm.get("label") == label
+def is_orig(elm):
+    return is_large(elm, label="Original")
+
+links = [ LINK_HTML.substitute(url=id + ".html", title=ttl)
+          for id,ttl,desc in psets ]
+index_html = INDEX_HTML.substitute(user=USERNAME,links="\n".join(links))
+print >>file("index.html", "w"), index_html.encode("utf8")
+
+for ps_id, ps_title, ps_desc in psets:
+    print "photoset:", ps_id
+    ps_desc = ps_desc.replace("\n", "<br>")
+    resp = flck.photosets_getPhotos(photoset_id=ps_id)
+    photos = [ (p.get("id"), p.get("title")) for p in resp.findall(".//photo") ]
+    def gen_photo_html((p_id, p_title)):
+        resp = flck.photos_getSizes(photo_id=p_id)
+        large = filter(is_large,resp.findall(".//size"))[0]
+        orig = filter(is_orig,resp.findall(".//size"))[0]
+        resp = flck.photos_getInfo(photo_id=p_id)
+        desc = resp.findtext(".//description")
+        p_html = PHOTO_HTML.substitute(src=large.get("source"),
+                                       alt=p_title, title=p_title,
+                                       width=large.get("width"),
+                                       height=large.get("height"),
+                                       caption=p_title, desc=desc,
+                                       orig_src=orig.get("source"))
+        return p_html
+    photos_html = u"\n\n".join(map(gen_photo_html, photos))
+    with file(str(ps_id) + ".html", "w") as out:
+        html = HTML.substitute(title=ps_title,desc=ps_desc,photos=photos_html)
+        print >>out, html.encode("utf8")
+
+