From 4881b96df4f5ed8832f0adf303d19057af68fb81 Mon Sep 17 00:00:00 2001 From: melonhead Date: Wed, 3 Mar 2010 18:11:52 +0000 Subject: [PATCH] Wtf hg can't handle symlinks --- plugins/drama.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) mode change 120000 => 100644 plugins/drama.py diff --git a/plugins/drama.py b/plugins/drama.py deleted file mode 120000 index d9279f0..0000000 --- a/plugins/drama.py +++ /dev/null @@ -1 +0,0 @@ -/Dropbox/ed.py \ No newline at end of file diff --git a/plugins/drama.py b/plugins/drama.py new file mode 100644 index 0000000..7236cf6 --- /dev/null +++ b/plugins/drama.py @@ -0,0 +1,53 @@ +'''Searches Encyclopedia Dramatica and returns the first paragraph of the +article''' + +import urllib2 +from util import hook +from util import BeautifulSoup + +api_url = "http://encyclopediadramatica.com/api.php?action=opensearch&search=%s" +ed_url = "http://encyclopediadramatica.com/%s" + +ua_header = ('User-Agent','Skybot/1.0 http://bitbucket.org/Scaevolus/skybot/') + + +def get_article_name(query): + q = api_url % (urllib2.quote(query, safe='')) + request = urllib2.Request(q) + request.add_header(*ua_header) + opener = urllib2.build_opener() + try: + results = eval(opener.open(request).read()) + if isinstance(results,list) and len(results[1]): + return results[1][0].replace(' ','_') + except: + return None + +@hook.command('ed') +@hook.command +def drama(inp): + '''.drama -- gets first paragraph of Encyclopedia Dramatica ''' \ + '''article on ''' + if not inp: + return drama.__doc__ + + article_name = get_article_name(inp) + if not article_name: + return 'no results found' + + url = ed_url % (urllib2.quote(article_name)) + request = urllib2.Request(url) + request.add_header(*ua_header) + opener = urllib2.build_opener() + result = opener.open(request).read() + + bs = BeautifulSoup.BeautifulSoup(result) + content = bs.find('div', {"id":"bodyContent"}) + + for p in content.findAll('p'): + if p.text: + summary = ''.join(''.join(p.findAll(text=True)).splitlines()) + if len(summary) > 300: + summary = summary[:300] + "..." + return '%s -- %s' % (summary, url) + return "error"