User:Simon/Library of Contingencies

From Media Design: Networked & Lens-Based wiki
Jump to navigation Jump to search

About the Library of Contingencies

a card within the library, both obverse and reverse sides

The Library of Contingencies is a digital collection of the cards produced for the text Tasks of the Contingent Librarian. The interface consists of a website with two iframes, into which an "obverse" and a "reverse" of each card is displayed. On the obverse is a task and its description, and on the reverse are images and related snippets. It follows a similar logic and offers similar affordances as the printed cards, but with the addition of snippets makes reference to concrete examples of the practice I have been developing with the bootleg library.

The content for the site is pulled from the pziwiki, with the "obverse" listed in my namespace of the wiki (User:Simon), and the "reverse" in the discussion section (User_talk:Simon).

All the obverse cards are transcluded in the Tasks of the Contingent Librarian page, and all the reverse cards are listed in Category: Library Snippets

To pull the content from the wiki, I'm using a python script called "pulltasks.py", which looks for any pages and images in these sections of the wiki, converting the text from mediawiki format to HTML, and rewriting links to images for thumbnail and full size versions:

  1 import argparse, os, sys
  2 from mwclient import Site
  3 from urllib.parse import quote as urlquote
  4 import html5lib
  5 from xml.etree import ElementTree as ET
  6 
  7 
  8 THUMB_SIZE = 320
  9 FULL_SIZE = 640
 10 
 11 NS_CATEGORY = 14
 12 
 13 p = argparse.ArgumentParser(description="Dump wiki files to html")
 14 p.add_argument("--host",  metavar='', default="pzwiki.wdka.nl", help='wiki host')
 15 p.add_argument("--path", metavar='', default="/mw-mediadesign/", help="Wiki path. Should end with /")
 16 p.add_argument("--output", default="pages", help="Output path for pages")
 17 p.add_argument("--category", default="Tasks of the Contingent Librarian", help="Category to query")
 18 
 19 args = p.parse_args()
 20 # print(args)
 21 # site and login
 22 
 23 def catmembers (c):
 24     prefix = c.get_prefix('cm', True)
 25     kwargs = dict(c.generate_kwargs(prefix, prop='ids|title', namespace=None,
 26                                            sort='sortkey', dir='asc', start=None, end=None,
 27                                            title=c.name, type="page"))
 28     return c.get_list(True)(c.site, 'categorymembers', 'cm', **kwargs)
 29 
 30 NS_MAIN = 0
 31 NS_TALK = 1
 32 NS_USER = 2
 33 NS_USER_TALK = 3
 34 
 35 def path4page(p):
 36     """ REturns the local path for a page """
 37     ret = p.page_title
 38     if "/" in ret:
 39         ret = ret.split("/")[-1]
 40     ret = ret.replace(" ", "_")
 41     if p.namespace == NS_USER_TALK:
 42         ret = ret + "_rvrs"
 43     return ret + ".html"
 44 
 45 def href4page(p):
 46     p = path4path(p)
 47     ret = urlquote(p)
 48 
 49 def filenameforlink(href):
 50     """ todo: deal with namespaces? """
 51     path = href
 52     if "/" in href:
 53         path = path.split("/")[-1]
 54     path = path+".html"
 55     return path
 56 
 57 def rewriteimagelink(a):
 58     href = a.attrib.get("href")
 59     path = href
 60     if "/" in href:
 61         path = path.split("/")[-1]
 62     print ("rewriteimagelink", path)
 63 
 64     r = site.api("query", prop="imageinfo", titles=path, iiprop="url", iiurlwidth=str(THUMB_SIZE), formatversion=2)
 65     iinfo = r['query']['pages'][0]['imageinfo'][0]
 66     thumburl = iinfo['thumburl']
 67     #fullsizeurl = iinfo['url']
 68     #filepageurl = iinfo['descriptionurl']
 69 
 70     r = site.api("query", prop="imageinfo", titles=path, iiprop="url", iiurlwidth=str(FULL_SIZE), formatversion=2)
 71     iinfo = r['query']['pages'][0]['imageinfo'][0]
 72     fullsizeurl = iinfo['thumburl']
 73     #fullsizeurl = iinfo['url']
 74     #filepageurl = iinfo['descriptionurl']
 75 
 76     a.attrib['href'] = fullsizeurl
 77     img = a.find("img")
 78     img.attrib['src'] = thumburl
 79     if "width" in img.attrib:
 80         del img.attrib["width"]
 81     if "height" in img.attrib:
 82         del img.attrib["height"]
 83     if "srcset" in img.attrib:
 84         del img.attrib["srcset"]
 85     print ("rewriteimagelink", thumburl, fullsizeurl)
 86 
 87 def rewritelinks(html):
 88     t = html5lib.parseFragment(html, treebuilder = "etree", namespaceHTMLElements = False)
 89     for a in t.findall(".//*[@href]"):
 90         linkclass = a.attrib.get("class", "")
 91         href = a.attrib.get("href")
 92         if "external" in linkclass:
 93             # leave external links alone
 94             continue
 95         # print ("LINK", href)
 96         if linkclass == "image":
 97             # link to presentation version of image
 98             # change img.src to a thumbnail
 99             rewriteimagelink(a)
100         elif href.startswith("/mediadesign/"):
101             new_href = filenameforlink(href)
102             # print ("Rewriting link {} to {}".format(href, new_href), file=sys.stderr)
103             a.attrib['href'] = new_href
104     html = ET.tostring(t, method="html", encoding="unicode")
105     return html
106 
107 
108 site = Site(host=args.host, path=args.path)
109 tasks=site.Categories[args.category]
110 # FOR EVERY CARD
111 for card in catmembers(tasks):
112     # FRONT
113     cardfilename = path4page(card)
114     cardpath = os.path.join(args.output, cardfilename)
115     print ("Saving page to {}".format(cardpath))
116     
117     with open(cardpath, "w") as f:
118         print ("""<!DOCTYPE html>
119 <!DOCTYPE html>
120 <html>
121 <head>
122     <meta charset="utf-8">
123     <title>Tasks of the Contingent Librarian</title>
124 <link rel="stylesheet" type="text/css" href="tasks.css">
125 <script src="tasks.js"></script>
126 </head>
127 <body>
128 """, file=f)
129         htmlsrc = site.parse(page=card.name)['text']['*']
130         htmlsrc = rewritelinks(htmlsrc)
131         print ("""<div class="card">{}</div>""".format(htmlsrc), file=f)
132         print ("""
133 </body>
134 </html>""", file=f)
135 
136     # BACK
137     talk = site.pages["User_talk:"+card.page_title]
138     if talk.exists:
139         print ("OUTPUTTING CARD BACK {}".format(talk.page_title))
140 
141         talkfilename = path4page(talk)
142         talkpath = os.path.join(args.output, talkfilename)
143         with open(talkpath, "w") as f:
144             print ("""<!DOCTYPE html>
145 <!DOCTYPE html>
146 <html>
147 <head>
148     <meta charset="utf-8">
149     <title>Tasks of the Contingent Librarian</title>
150 <link rel="stylesheet" type="text/css" href="tasks.css">
151 <script src="tasks.js"></script>
152 </head>
153 <body>
154 """, file=f)
155             htmlsrc = site.parse(page=talk.name)['text']['*']
156             htmlsrc = rewritelinks(htmlsrc)
157             print ("""<div class="cardback">{}</div>""".format(htmlsrc), file=f)
158             print ("""
159 </body>
160 </html>""", file=f)