From 6ecbe4ca18af294cf5538d392950215ca4fd9366 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Sat, 23 Jun 2012 21:55:37 -0700 Subject: [PATCH] wiki: handle json parsing error --- modules/test/test_archwiki.py | 8 ++++++++ modules/test/test_vtluugwiki.py | 8 ++++++++ modules/test/test_wikipedia.py | 8 ++++++++ wiki.py | 9 ++++++--- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/modules/test/test_archwiki.py b/modules/test/test_archwiki.py index 54ef6db..cd83565 100644 --- a/modules/test/test_archwiki.py +++ b/modules/test/test_archwiki.py @@ -22,6 +22,14 @@ class TestArchwiki(unittest.TestCase): out, flags=re.UNICODE) self.assertTrue(m) + def test_awik_invalid(self): + term = "KVM#Enabling_KSM" + input = Mock(groups=lambda: ['', term]) + archwiki.awik(self.phenny, input) + + self.phenny.say.assert_called_once_with( "Can't find anything in "\ + "the ArchWiki for \"{0}\".".format(term)) + def test_awik_none(self): term = "Ajgoajh" input = Mock(groups=lambda: ['', term]) diff --git a/modules/test/test_vtluugwiki.py b/modules/test/test_vtluugwiki.py index b5be482..a1e1121 100644 --- a/modules/test/test_vtluugwiki.py +++ b/modules/test/test_vtluugwiki.py @@ -22,6 +22,14 @@ class TestVtluugwiki(unittest.TestCase): out, flags=re.UNICODE) self.assertTrue(m) + def test_vtluug_invalid(self): + term = "EAP-TLS#netcfg" + input = Mock(groups=lambda: ['', term]) + vtluugwiki.vtluug(self.phenny, input) + + self.phenny.say.assert_called_once_with( "Can't find anything in "\ + "the VTLUUG Wiki for \"{0}\".".format(term)) + def test_vtluug_none(self): term = "Ajgoajh" input = Mock(groups=lambda: ['', term]) diff --git a/modules/test/test_wikipedia.py b/modules/test/test_wikipedia.py index a3900ff..8a6fb26 100644 --- a/modules/test/test_wikipedia.py +++ b/modules/test/test_wikipedia.py @@ -22,6 +22,14 @@ class TestWikipedia(unittest.TestCase): out, flags=re.UNICODE) self.assertTrue(m) + def test_wik_invalid(self): + term = "New York City#Climate" + input = Mock(groups=lambda: ['', term]) + wikipedia.wik(self.phenny, input) + + self.phenny.say.assert_called_once_with( "Can't find anything in "\ + "Wikipedia for \"{0}\".".format(term)) + def test_wik_none(self): term = "Ajgoajh" input = Mock(groups=lambda: ['', term]) diff --git a/wiki.py b/wiki.py index f86a7a9..25aa12d 100644 --- a/wiki.py +++ b/wiki.py @@ -43,9 +43,12 @@ class Wiki(object): def search(self, term, last=False): url = self.api.format(term) bytes = web.get(url) - result = json.loads(bytes) - result = result['query']['search'] - if len(result) <= 0: + try: + result = json.loads(bytes) + result = result['query']['search'] + if len(result) <= 0: + return None + except ValueError: return None term = result[0]['title'] term = term.replace(' ', '_')