http://redfoot.net/3.0/code#python,
import logging
_logger = logging.getLogger(__uri__)
auth = redfoot.module(URIRef("auth#module", base=redfoot.uri))
bookmark_module = redfoot.module(URIRef("#module", base=__uri__))
from sgmllib import SGMLParser
class BookmarkParser(SGMLParser):
def __init__(self, bookmark, uri):
SGMLParser.__init__(self)
self.bookmark = bookmark
self.uri = uri
self.context = redfoot.get_context(redfoot.context_id(bookmark))
self.data = ""
def reset(self):
SGMLParser.reset(self)
def handle_data(self, data):
self.data += data
def start_title(self, attrs):
self.data = ""
def end_title(self):
c = self.context
if (self.uri, RDFS.label, None) not in c:
c.add((self.uri, RDFS.label, Literal(self.data)))
self.data = ""
def unknown_endtag(self, tag):
pass
import md5
import urlparse
from time import time
from twisted.web.client import getPage
from twisted.web import http
BOOKMARK = redfoot.namespace("http://redfoot.net/3.0/bookmark#")
SERVER = redfoot.namespace("http://redfoot.net/3.0/server#")
SESSION = redfoot.namespace("http://redfoot.net/3.0/session#")
DC_creator = URIRef("http://purl.org/dc/elements/1.1/creator")
DC_created = URIRef("http://purl.org/dc/terms/created")
uri = request.headers.get("referer")
if not auth.allow(request):
response.setStatus(403, "Forbidden")
response.write("Must be logged in to add a bookmark.")
response.finish()
elif uri:
uri = URIRef(uri)
b = URIRef("/bookmark/%s/" % md5.new(uri).hexdigest(), base=request.host).abstract()
c = redfoot.get_context(redfoot.context_id(b))
c.add((b, RDFS.label, Literal("Bookmark")))
c.add((b, RDF.type, BOOKMARK.Bookmark))
c.add((b, BOOKMARK.refersTo, uri))
if (b, DC_created, None) not in redfoot:
c.add((b, DC_created, Literal("%s" % time())))
c.add((b, SERVER.site, request.host))
scheme, netloc, url, params, query, fragment = urlparse.urlparse(uri)
host = URIRef(urlparse.urlunparse((scheme, netloc, "", "", "", "")))
# TODO: d = getPage(host.encode("utf-8")); d.addCallback(updateCategory)
category = URIRef("/bookmarks/category/%s/" % md5.new(host).hexdigest(), base=request.host).abstract()
c.remove((category, RDFS.label, None))
c.add((category, RDFS.label, redfoot.label(host) or Literal(host)))
c.add((category, RDF.type, BOOKMARK.Category))
c.add((b, BOOKMARK.category, category))
if request.session_id:
# TODO: add a uid method to request?
uid = redfoot.value(request.session_id, SESSION.uid)
if uid:
if (b, DC_creator, None) not in redfoot:
c.add((b, DC_creator, uid))
def finish(page):
if page is not None:
import urllib
parser = BookmarkParser(b, uri)
parser.feed(page)
response.setStatus(http.SEE_OTHER, "The Bookmark Page")
response.setHeader("Location", b.concrete())
response.finish()
try:
redfoot.dispatcher.dispatch(bookmark_module.BookmarkCreatedEvent(uri=b))
except Exception, e:
_logger.exception(e)
def error(result):
finish(None)
if host==request.host:
# Don't try to fetch page from ourself... just finish up.
finish(None)
else:
# else some of the bits turn unicode again and cause a twisted
# TypeError("Data must not be unicode")
urlparse.clear_cache()
d = getPage(uri.encode("utf-8"))
d.addCallback(finish)
d.addErrback(error)
else:
response.setStatus(http.PRECONDITION_FAILED, "no referer")
response.write("No referer specified")
response.finish()
Comments regarding http://redfoot.net/3.0/bookmark/add_bookmark_handler
Login to submit a comment.