Mercurial > hg > wordstream
diff wordstream/handlers.py @ 3:e21f53582267
adding final dissociation stuff
author | k0s <k0scist@gmail.com> |
---|---|
date | Fri, 12 Feb 2010 01:37:38 -0500 |
parents | 8af3412e907a |
children | c576f5877459 |
line wrap: on
line diff
--- a/wordstream/handlers.py Fri Feb 12 00:38:25 2010 -0500 +++ b/wordstream/handlers.py Fri Feb 12 01:37:38 2010 -0500 @@ -3,10 +3,14 @@ these are instantiated for every request, then called """ +import urllib2 + from pprint import pprint from urlparse import urlparse from webob import Response, exc from StringIO import StringIO +from wordstream.api import Corpus +from wordstream.dissociate import dissociate class HandlerMatchException(Exception): """the handler doesn't match the request""" @@ -125,3 +129,50 @@ def __call__(self): self.app.corpus.feed_stream(self.request.environ['path']) return exc.HTTPOk() + + +class Dissociate(GenshiHandler): + template = 'post.html' + methods = set(['GET', 'POST']) + + + @classmethod + def match(cls, app, request): + + # check the method + if request.method not in cls.methods: + return None + + return cls(app, request) + + def Get(self): + if 'url' in self.request.GET: + contents = self.url_contents(self.request.GET['url']) + dissociation = self.dissociation(contents) + return Response(content_type='text/html', + body='<html><body>%s</body></html>' % dissociation) + + + return GenshiHandler.Get(self) + + def url_contents(self, url): + return urllib2.urlopen(url).read() + + def dissociation(self, contents): + corpus = Corpus() + corpus.feed_stream(contents) + corpus.scramble() + buffer = StringIO() + dissociate(corpus, buffer) + return buffer.getvalue() + + def Post(self): + if 'url' in self.request.POST: + contents = self.url_contents(self.request.POST['url']) + elif 'text' in self.request.POST: + contents = self.request.POST['text'] + elif 'file' in self.request.POST: + contents = self.request.POST['file'].file.read() + dissociation = self.dissociation(contents) + return Response(content_type='text/html', + body='<html><body>%s</body></html>' % dissociation)