Skip to content

Commit

Permalink
DDG: attempt at simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
GLolol authored and GLolol committed Dec 15, 2014
1 parent ad8931a commit b62249c
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions DDG/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,22 @@ def search(self, irc, msg, args, text):
data = re.sub('\t|\r|\n', '', data)
data = re.sub('\s{2,}', ' ', data)
soup = BeautifulSoup(data)
# DuckDuckGo lite uses tables for everything. Each WEB result is made
# up of 3 <tr> tags:
tables = soup.find_all('table')

# Sometimes there is an extra table for page navigation
tds = soup.find_all('td')
for t in tds:
if "1." in t.text:
res = t.next_sibling.next_sibling
break
try:
webresults = tables[2].find_all('tr')
except IndexError:
webresults = tables[1].find_all('tr')
if webresults:
try:
while 'result-sponsored' in webresults[0]["class"]:
self.log.debug("DDG: stripping 1 sponsored/ad result.")
webresults = webresults[4:]
except KeyError: pass
# 1) The link and title.
link = webresults[0].find('a').get('href')
# 2) A result snippet.
snippet = webresults[1].find("td", class_="result-snippet")
try:
snippet = snippet.text.strip()
except AttributeError:
snippet = webresults[1].td.text.strip()
# 3) The link-text; essentially the same as the link in 1), but with the
# URI (http(s)://) removed. We do not need this section.
# 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)
else:
except AttributeError:
irc.error("No results found.")
search = wrap(search, ['text'])

Expand Down

0 comments on commit b62249c

Please sign in to comment.