-
Notifications
You must be signed in to change notification settings - Fork 4
/
search.py
122 lines (97 loc) · 3.14 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# coding=utf8
"""
search.py - Sopel Web Search Module
Copyright 2008-9, Sean B. Palmer, inamidst.com
Copyright 2012, Edward Powell, embolalia.net
Licensed under the Eiffel Forum License 2.
http://sopel.dftba.net
"""
from __future__ import unicode_literals
import re
from sopel import web
from sopel.module import commands, example
import json
def formatnumber(n):
"""Format a number with beautiful commas."""
parts = list(str(n))
for i in range((len(parts) - 3), 0, -3):
parts.insert(i, ',')
return ''.join(parts)
r_bing = re.compile(r'<h3><a href="([^"]+)"')
def bing_search(query, lang='en-GB'):
base = 'http://www.bing.com/search?mkt=%s&q=' % lang
bytes = web.get(base + query)
m = r_bing.search(bytes)
if m:
return m.group(1)
r_duck = re.compile(r'nofollow" class="[^"]+" href="(.*?)">')
def duck_search(query):
query = query.replace('!', '')
uri = 'http://duckduckgo.com/html/?q=%s&kl=uk-en' % query
bytes = web.get(uri)
if 'web-result"' in bytes: # filter out the adds on top of the page
bytes = bytes.split('web-result"')[1]
m = r_duck.search(bytes)
if m:
return web.decode(m.group(1))
# Alias google_search to duck_search
google_search = duck_search
def duck_api(query):
if '!bang' in query.lower():
return 'https://duckduckgo.com/bang.html'
uri = 'http://api.duckduckgo.com/?q=%s&format=json&no_html=1&no_redirect=1' % query
results = json.loads(web.get(uri))
if results['Redirect']:
return results['Redirect']
else:
return None
@commands('duck', 'ddg', 'g')
@example('.duck privacy or .duck !mcwiki obsidian')
def duck(bot, trigger):
"""Queries Duck Duck Go for the specified input."""
query = trigger.group(2)
if not query:
return bot.reply('.ddg what?')
# If the API gives us something, say it and stop
result = duck_api(query)
if result:
bot.reply(result)
return
# Otherwise, look it up on the HTMl version
uri = duck_search(query)
if uri:
bot.reply(uri)
if 'last_seen_url' in bot.memory:
bot.memory['last_seen_url'][trigger.sender] = uri
else:
bot.reply("No results found for '%s'." % query)
@commands('search')
@example('.search nerdfighter')
def search(bot, trigger):
"""Searches Bing and Duck Duck Go."""
if not trigger.group(2):
return bot.reply('.search for what?')
query = trigger.group(2)
bu = bing_search(query) or '-'
du = duck_search(query) or '-'
if bu == du:
result = '%s (b, d)' % gu
else:
if len(bu) > 150:
bu = '(extremely long link)'
if len(du) > 150:
du = '(extremely long link)'
result = '%s (b), %s (d)' % (bu, du)
bot.reply(result)
@commands('suggest')
def suggest(bot, trigger):
"""Suggest terms starting with given input"""
if not trigger.group(2):
return bot.reply("No query term.")
query = trigger.group(2)
uri = 'http://websitedev.de/temp-bin/suggest.pl?q='
answer = web.get(uri + query.replace('+', '%2B'))
if answer:
bot.say(answer)
else:
bot.reply('Sorry, no result.')