Commits

Sergey Astanin  committed b3952d7

Embed single sets. --link=WHAT and --embeddable.

  • Participants
  • Parent commits 6a46911

Comments (0)

Files changed (1)

 warnings.filterwarnings("ignore", category=DeprecationWarning)
 from flickrapi import FlickrAPI
 
-USAGE = """embedall.py username
+USAGE = """embedall.py saveall [options] username
+embedall.py photoset [options] photoset_id
 
 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.
+user username or only for the specified photoset.
+
+Options:
+  -e | --embeddable    generate embeddable HTML (no head, body, etc.)
+  --link=WHAT          link images to "original" (default), "page", "nothing"
+
+Flickr API keys should be available in ~/.flickrapi_keys file.
 """
 
 # Create file ~/.flickrapi_keys with keys
 <html>
 <head><title>$title</title></head>
 <body>
-<section><h1>$title</h1>
-    <div class="description">$desc</div>
-    <div class="photos">
-    $photos
-    </div>
+<section><h1>$title</h1>$desc
+<div class="photos">
+$photos
+</div>
 </section></body></html>""")
 
-PHOTO_HTML=Template(u"""<figure>
-    <a href="$orig_src" target="_blank">
+HTML_EMBEDDABLE=Template(u"""<h1>$title</h1>$desc
+<div class="photos">
+$photos
+</div>""")
+
+PHOTO_HTML=Template(u"""
+<figure>
+    <a href="$link" 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><div class="title"><strong>$caption</strong></div>$desc
     </figcaption>
 </figure>""")
 
+PHOTO_HTML_NOLINK=Template(u"""
+<figure>
+    <img src="$src" alt="$alt" title="$title" width="$width" height="$height">
+    <figcaption><div class="title"><strong>$caption</strong></div>$desc</figcaption>
+</figure>""")
+
+DESC_HTML=Template(u"""
+    <div class="description">$desc</div>
+""")
+
 def authorize():
     "Connect to Flickr and authorize."
     global KEY, SECRET
     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>")
+def write_photoset_index(flickr, pset, embeddable=False, linkwhat="original"):
+    """Write photoset_id.html where all set photos are embedded.
+
+    Arguments:
+      flickr      Flickr API object
+      pset        (photoset_id, set_title, set_description) or just photoset_id
+      embeddable  if True, generate embeddable HTML; standalone otherwise
+      linkwhat    one of "original", "nothing", "page"
+
+    Return filename.
+    """
+    global PHOTO_HTML, HTML, HTML_EMBEDDABLE
+    if hasattr(pset, "__iter__"):
+        ps_id, ps_title, ps_desc = pset
+    else:
+        ps_id = pset
+        resp = flickr.photosets_getInfo(photoset_id=ps_id)
+        ps_title = resp.findtext(".//title")
+        ps_desc = resp.findtext(".//description")
+    ps_desc = DESC_HTML.substitute(desc=ps_desc.replace("\n", "<br>")) if ps_desc else ""
     resp = flickr.photosets_getPhotos(photoset_id=ps_id)
     photos = [ (p.get("id"), p.get("title")) for p in resp.findall(".//photo") ]
+    ptmpl = PHOTO_HTML if linkwhat != "nothing" else PHOTO_HTML_NOLINK
     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_sz = flickr.photos_getSizes(photo_id=p_id)
+        large = get_largest(resp_sz)
         resp = flickr.photos_getInfo(photo_id=p_id)
+        if linkwhat == "original":
+            link = get_largest(resp_sz, ["Original","Large","Medium 640","Medium"])
+            link = link.get("source")
+        elif linkwhat == "page":
+            # workaround for lack of ".//element[@attr='value']" in etree < 1.3
+            link = [ p for p in resp.findall(".//url") if p.get("type") == "photopage" ]
+            link = link[0].text
+        else:
+            link = ""
         desc = resp.findtext(".//description").replace("\n", "<br>")
-        p_html = PHOTO_HTML.substitute(src=large.get("source"),
+        desc_html = DESC_HTML.substitute(desc=desc) if desc else ""
+        p_html = ptmpl.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"))
+                   height=large.get("height"), caption=p_title, desc=desc_html,
+                   link=link)
         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)
+    filename = str(ps_id) + ".html"
+    tmpl = HTML_EMBEDDABLE if embeddable else HTML
+    with file(filename, "w") as out:
+        html = tmpl.substitute(title=ps_title,desc=ps_desc,photos=photos_html)
         print >>out, html.encode("utf8")
+    return filename
 
 def main():
     global USAGE
-    if len(argv) != 2 or "-h" in argv or "--help" in argv:
+    args = [ a for a in argv if not a.startswith("-") ][1:]
+    opts = [ a for a in argv if a.startswith("-") ]
+
+    if "-h" in argv or "--help" in opts or len(args) != 2:
         print USAGE
         exit(0)
 
+    embeddable = "--embeddable" not in opts or "-e" not in opts
+    if "--link=nothing" in opts or "--nolinks" in opts:
+        linkwhat = "nothing"
+    elif "--link=page" in opts or "--link=flickr" in opts:
+        linkwhat = "page"
+    else:
+        linkwhat = "original"
+
     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 args[0] == "all" or args[0] == "saveall":
+        username = args[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, embeddable, linkwhat)
+    elif args[0] == "photoset" or args[0] == "set":
+        pset_id = args[1]
+        fn = write_photoset_index(flck, pset_id, embeddable, linkwhat)
+        print file(fn).read()
+    else:
+        print USAGE
+        exit(1)
 
 if __name__ == "__main__":
     main()