Commit a75ec5f1 authored by Svetlana Tkachenko's avatar Svetlana Tkachenko
Browse files

Have Wikipedia plugin reply not found if there is no results (for issue #21) (thanks, Nem0)

parent 1c7041ee
......@@ -18,7 +18,6 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import xml.etree.ElementTree
import urllib.request, urllib.parse
......@@ -35,33 +34,31 @@ class Wikipedia(object):
if len(args) < 1:
self.server.doMessage(channel, user + ": Not enough arguments.")
return
url = "http://en.wikipedia.org/w/api.php?action=opensearch&limit=3&namespace=0&format=xml&search="
query = " ".join(args)
search = url + urllib.parse.quote_plus(query, "/")
try:
# get the data
data = urllib.request.urlopen(search).readlines()
# parse the xml to a xpath tree
xml_tree = xml.etree.ElementTree.fromstringlist(data)
# get <Items> tags
# the first element in xml_tree is a <QUERY> tag
# we don't need that. We want the <SECTION> tag, which is the
# second tag, hence the index of 1.
xml_Items = list(xml_tree[1])
# loop and grab all <Description> tags
# and have an array to store the results
results = []
for item in xml_Items:
this = list(item)
results.append(this[1].text.strip('\n') + ' ' + this[2].text.strip('\n'))
if not results: ######## check
self.server.doMessage(channel, "No results for " + query + ".") ######## for
return ######## results
for result in results:
self.server.doMessage(channel, result)
except Exception:
pass
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment