jdic should not crash on no results.
[zzz-dywypi.git] / plugins / WWWJDIC / plugin.py
index cefd09b..5e548a3 100644 (file)
@@ -72,19 +72,33 @@ class WWWJDIC(callbacks.Plugin):
 
         # Even the raw results come wrapped in minimal HTML.  This sucks.
         # They're just in this form though:
-        # <p>
-        # <br>entry 1
-        # <br>entry 2
-        # So grab everything from that paragraph that isn't a tag (<br>) or
-        # blank space and spit it back out.
+        # <pre>
+        # entry 1
+        # entry 2
+        # So grab everything from that pre tag, split by lines, and spit it
+        # back out.
         soup = BeautifulSoup(res)
-        thing_ct = 0
-        for thing in soup.p:
-            if not isinstance(thing, NavigableString):
-                continue
+        if not soup.pre:
+            # Nothing found?
+            reply = u"Hmm, nothing found -- but I only look for exact " \
+                "matches and common words.  Try denshi jisho directly: "
+
+            jisho_url = u"http://jisho.org/words?jap={jap}&eng={eng}&dict=edict"
+            if thing[0] in ('@', '#'):
+                # Prefixes for roomaji
+                reply += jisho_url.format(jap=thing[1:], eng=u'')
+            # wtf why is any() overridden
+            elif filter(lambda c: ord(c) > 256, thing):
+                reply += jisho_url.format(jap=thing, eng=u'')
+            else:
+                reply += jisho_url.format(jap=u'', eng=thing)
+
+            self._reply(irc, reply)
+            return
 
-            # Everything ends with a newline, bleh!
-            entry = unicode(thing).strip()
+        thing_ct = 0
+        for entry in soup.pre.string.splitlines():
+            entry = entry.strip()
             if entry == '':
                 continue