From 8531bdd76cb60bc31c704ed4a177da93c1efb51e Mon Sep 17 00:00:00 2001 From: Chris Skalenda Date: Sun, 21 Feb 2010 22:12:55 -0600 Subject: [PATCH 1/2] wikipedia: send a useragent when fetching xml from the wikipedia api. If you don't, it sends 403 a lot. --- plugins/wikipedia.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/wikipedia.py b/plugins/wikipedia.py index 5ddf19f..e6cab30 100644 --- a/plugins/wikipedia.py +++ b/plugins/wikipedia.py @@ -1,7 +1,7 @@ '''Searches wikipedia and returns first sentence of article Scaevolus 2009''' -import urllib +import urllib2 from lxml import etree import re @@ -24,7 +24,12 @@ def wiki(inp): return wiki.__doc__ q = search_url % (urllib.quote(inp, safe='')) - x = etree.parse(q) + + request = urllib2.Request(q) + request.add_header('User-Agent', 'Skybot/1.0 http://bitbucket.org/Scaevolus/skybot/') + opener = urllib2.build_opener() + xml = opener.open(request).read() + x = etree.fromstring(xml) ns = '{http://opensearch.org/searchsuggest2}' items = x.findall(ns + 'Section/' + ns + 'Item') From f493927eb9bca19254771b02b3fef0078c28c6bc Mon Sep 17 00:00:00 2001 From: Chris Skalenda Date: Sun, 21 Feb 2010 22:23:52 -0600 Subject: [PATCH 2/2] hurf durf --- plugins/wikipedia.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/wikipedia.py b/plugins/wikipedia.py index e6cab30..ec6e9f3 100644 --- a/plugins/wikipedia.py +++ b/plugins/wikipedia.py @@ -23,7 +23,7 @@ def wiki(inp): if not inp: return wiki.__doc__ - q = search_url % (urllib.quote(inp, safe='')) + q = search_url % (urllib2.quote(inp, safe='')) request = urllib2.Request(q) request.add_header('User-Agent', 'Skybot/1.0 http://bitbucket.org/Scaevolus/skybot/')