#!/usr/bin/env python """ FOAFCite CGI - Get summary information on people Author: Sean B. Palmer, inamidst.com License: GPL 2; share and enjoy! """ import sys, re, cgi, urllib import cgitb; cgitb.enable() form = cgi.FieldStorage() def call(s): try: return form[s].value except KeyError: return None form.__call__ = call mkquery = 'select %s where %s%s%s' r_word = re.compile(r'[A-Za-z0-9]+') r_line = re.compile(r'^[a-z]+: ') class Grab(urllib.URLopener): def __init__(self, *args): self.version = 'Mozilla/5.0 (foafcite)' urllib.URLopener.__init__(self, *args) def http_error_default(self, url, fp, errcode, errmsg, headers): return urllib.addinfourl(fp, [headers, errcode], "http:" + url) urllib._urlopener = Grab() using = ('using foaf for , ' + 'rdfs for ') using = '' julie = 'http://crschmidt.net/rdfpython/redlandbot.py?machine=1&cmd=%5Eq+' def mknamebel(name): name = ''.join(r_word.findall(name)) if not name: return 'Person' if name[0] in '0123456789': name = 'n' + name return name def term(t): if t.startswith('_'): return t[2:] elif t.startswith('<'): return t[1:-1].decode('utf-8') else: return t[1:-1].decode('unicode-escape') def parse(s): # Depends on crschmidt's format being regular results = [] for bindings in s.split('\n\n'): results.append({}) for line in bindings.splitlines(): if r_line.match(line): var, t = line.split(': ', 1) results[-1][var] = term(t) return filter(lambda b: len(b), results) def query(q): u = urllib.urlopen(julie + urllib.quote(q)) s = u.read() u.close() return parse(s) def serve(status, body): sys.stdout.write("Status: %s\r\n" % status) sys.stdout.write("Content-Type: text/html; charset=utf-8\r\n\r\n") sys.stdout.write(body) if not body.endswith('\n'): sys.stdout.write('\n') sys.exit() def barf(msg): serve(200, "Error: " + msg) def homepage(): serve(200, """ FOAFCite: Lookup People's Details Fast!

FOAFCite Query

Name: Nick:
Details: { mbox_sha1sum: } { homepage: } { foaffile: }
RDF/XML: Notation3:

Ever wanted to add a person to your FOAF file but didn't want to have to go through the rigmorale of looking up their foaf:mbox_sha1sum, foaf:homepage, and the location of their possibly numerous FOAF files? Rigmorale no more! This service queries Christopher Schmidt's excellent redlandbot service. Want to know how this works? Check out the foafcite source code.

Tip: if the results returned are inaccurate, try using both a name and a nickname for greater accuracy. Both name and nickname are case sensitive.

Sean B. Palmer
""") def main(): name, nick = form('name'), form('nick') if not (name or nick): homepage() # barf("You must include either a name or a nickname!") ident = '' if name: ident += '(?p foaf:name "%s") ' % name if nick: ident += '(?p foaf:nick "%s") ' % nick namebel = mknamebel(name or nick) format = form('format') if format not in ('rdfxml', 'n3'): barf("Format must be one of: rdfxml, n3.") print 'Content-Type: text/plain; charset=utf-8' print if format == 'rdfxml': print '' print '' % namebel else: print '@prefix foaf: .' print '@prefix rdfs: .' print print ('_:%s' % namebel), if name: if format == 'rdfxml': print ' %s' % name else: print 'foaf:name "%s"; ' % name if nick: # @@ elif nick? if format == 'rdfxml': print ' %s' % nick else: print ' foaf:nick "%s"; ' % nick if form('sha1sum'): q = mkquery % ('?sha', ident, '(?p foaf:mbox_sha1sum ?sha) ', using) for result in query(q): sha = result['sha'] if format == 'rdfxml': print ' %s' % sha else: print ' foaf:mbox_sha1sum "%s"; ' % sha if form('homepage'): q = mkquery % ('?home', ident, '(?p foaf:homepage ?home) ', using) for result in query(q): home = result['home'] if format == 'rdfxml': print ' ' % home else: print ' foaf:homepage <%s>; ' % home if form('foaffile'): foafy = 'and ?foaf =~ /foaf/ ' q1 = mkquery % ('?foaf', ident, '(?p foaf:made ?foaf) '+foafy, using) q2 = mkquery % ('?foaf', ident, '(?foaf foaf:maker ?p) '+foafy, using) q3 = mkquery % ('?foaf', ident, '(?p rdfs:seeAlso ?foaf) '+foafy, using) q1r = query(q1) if q1r: for result in q1r: foaf = result['foaf'] if format == 'rdfxml': print ' ' % foaf else: print ' rdfs:seeAlso <%s>; ' % foaf else: q2r = query(q2) if q2r: for result in q2r: foaf = result['foaf'] if format == 'rdfxml': print ' ' % foaf else: print ' rdfs:seeAlso <%s>; ' % foaf else: q3r = query(q3) for result in q3r: foaf = result['foaf'] if format == 'rdfxml': print ' ' % foaf else: print ' rdfs:seeAlso <%s>; ' % foaf if format == 'rdfxml': print '' print '' else: print '.' if __name__=="__main__": main()