Created
September 29, 2011 12:30
-
-
Save astanin/1250635 to your computer and use it in GitHub Desktop.
Embed links to all Flickr photos of a user, separately for every photoset.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
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 | |
# for md5 in flickrapi | |
warnings.filterwarnings("ignore", category=DeprecationWarning) | |
from flickrapi import FlickrAPI | |
USAGE = """embedall.py username | |
Create an HTML index in the local directory for each photoset of the | |
user username. Flickr API keys should be available in ~/.flickrapi_keys file. | |
""" | |
# Create file ~/.flickrapi_keys with keys | |
# ---- | |
# KEY = 12345678901234567890123456789012 | |
# SECRET = 1234567890123456 | |
# ---- | |
# or define them inline. | |
# To get a key, see http://www.flickr.com/services/api/misc.api_keys.html | |
KEY = "" | |
SECRET = "" | |
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 authorize(): | |
"Connect to Flickr and authorize." | |
global KEY, SECRET | |
if not KEY or not SECRET: | |
ls = file(expanduser("~/.flickrapi_keys")).readlines() | |
d = dict([[w.strip(' "') for w in l.strip().split("=",1)] | |
for l in ls ]) | |
KEY = d["KEY"] | |
SECRET = d["SECRET"] | |
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)) | |
return flck | |
def get_photosets(flickr, username): | |
"Fetch a list of username's photosets: [(photoset_id, title, description)]" | |
if "@N" in username: # likely an nsid | |
nsid = username | |
elif "@" in username: # likely an e-mail | |
resp = flickr.people_findByEmail(username=username) | |
nsid = resp.find("user").get("nsid") | |
else: # likely a username | |
resp = flickr.people_findByUsername(username=username) | |
nsid = resp.find("user").get("nsid") | |
resp = flickr.photosets_getList(user_id=nsid) | |
psets = [ (ps.get("id"), ps.findtext(".//title"), ps.findtext(".//description")) | |
for ps in resp.findall(".//photoset") ] | |
return psets | |
def get_largest(resp, szs=["Large","Medium 640","Medium","Small","Thumbnail"]): | |
"Process getSizes' response. Find the largest available size of the image." | |
def is_size(elm, size="Large"): | |
return elm.get("label") == size | |
elms = resp.findall(".//size") | |
for sz in szs: | |
found = [ s for s in elms if is_size(s, sz) ] | |
if found: | |
return found[0] | |
return None | |
def write_index(psets, username): | |
"Write index.html where all photosets are listed." | |
global LINK_HTML, INDEX_HTML | |
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)) | |
with file("index.html", "w") as out: | |
print >>out, index_html.encode("utf8") | |
def write_photoset_index(flickr, pset): | |
"Write photoset_id.html where all set photos are embedded." | |
global PHOTO_HTML, HTML | |
ps_id, ps_title, ps_desc = pset | |
ps_desc = ps_desc.replace("\n", "<br>") | |
resp = flickr.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 = flickr.photos_getSizes(photo_id=p_id) | |
large = get_largest(resp) | |
orig = get_largest(resp, ["Original","Large","Medium 640","Medium"]) | |
resp = flickr.photos_getInfo(photo_id=p_id) | |
desc = resp.findtext(".//description").replace("\n", "<br>") | |
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") | |
def main(): | |
global USAGE | |
if len(argv) != 2 or "-h" in argv or "--help" in argv: | |
print USAGE | |
exit(0) | |
flck = authorize() | |
username = argv[1] | |
psets = get_photosets(flck, username) | |
write_index(psets, username) | |
for pset in psets: | |
print "photoset:", pset[0], pset[1] | |
write_photoset_index(flck, pset) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment