Skip to content

Commit

Permalink
DDG: fix code handling, remove regex parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
James Lu committed Dec 16, 2014
1 parent b62249c commit 64c2949
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions DDG/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
# without the i18n module
_ = lambda x:x

import re
try: # Python 3
from urllib.parse import urlencode
except ImportError: # Python 2
Expand All @@ -59,34 +58,32 @@ class DDG(callbacks.Plugin):

def search(self, irc, msg, args, text):
"""<query>
Searches for <query> on DuckDuckGo (web search)."""
url = "https://duckduckgo.com/lite?" + urlencode({"q":text})
try:
data = utils.web.getUrl(url).decode("utf-8")
except utils.web.Error as e:
self.log.info(url)
irc.error(str(e), Raise=True)
# GRR, having to clean up our HTML for the results...
data = re.sub('\t|\r|\n', '', data)
data = re.sub('\s{2,}', ' ', data)
soup = BeautifulSoup(data)
tds = soup.find_all('td')
for t in tds:
for t in soup.find_all('td'):
if "1." in t.text:
res = t.next_sibling.next_sibling
break
try:
# 1) Fetch the result link.
link = res.a.get('href')
# 2) Get a result snippet.
snippet = res.parent.next_sibling.next_sibling.find("td",
class_="result-snippet")
snippet = snippet.text.strip()

s = format("%s - %u", snippet, link)
irc.reply(s)
except AttributeError:
try:
# 1) Get a result snippet.
snippet = res.parent.next_sibling.next_sibling.find("td",
class_="result-snippet")
# 2) Fetch the result link.
link = res.a.get('href')
snippet = snippet.text.strip()

s = format("%s - %u", snippet, link)
irc.reply(s)
return
except (AttributeError, UnboundLocalError):
continue
else:
irc.error("No results found.")
search = wrap(search, ['text'])

Expand Down

0 comments on commit 64c2949

Please sign in to comment.