Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DuckDuckGo Plugin #9

Merged
merged 8 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
*.pyc
*.swp
/.history
/bothome/
/build/
/cache/
/dist/
dropin.cache
/.history
/ibid.db
/Ibid.egg-info/
/local.ini
/logs/
dropin.cache
*.pyc
/pyenv/
*.swp
4 changes: 2 additions & 2 deletions ibid/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def load(self, name):
expanduser(uri.replace('sqlite:///', '', 1))),
self.sqlite_synchronous),
encoding='utf-8', convert_unicode=True,
assert_unicode=True, echo=echo
echo=echo
)

elif uri.startswith(u'mysql://'):
Expand All @@ -362,7 +362,7 @@ def load(self, name):
uri += u'&use_unicode=0'

engine = create_engine(uri, encoding='utf-8',
convert_unicode=True, assert_unicode=True, echo=echo,
convert_unicode=True, echo=echo,
# MySQL closes 8hr old connections:
pool_recycle=3600)

Expand Down
20 changes: 10 additions & 10 deletions ibid/db/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@
def monkey_patch():
import sqlalchemy.dialects.postgresql
sqlalchemy.dialects.postgresql.dialect.ischema_names['citext'] = IbidUnicodeText
def postgres_visit_IBID_VARCHAR(self, type_):
def postgres_visit_IBID_VARCHAR(self, type_, **kw):
if type_.case_insensitive:
return 'CITEXT'
else:
return self.visit_VARCHAR(type_)
return self.visit_VARCHAR(type_, **kw)
sqlalchemy.dialects.postgresql.dialect.type_compiler.visit_IBID_VARCHAR = postgres_visit_IBID_VARCHAR
def postgres_visit_IBID_TEXT(self, type_):
def postgres_visit_IBID_TEXT(self, type_, **kw):
if type_.case_insensitive:
return 'CITEXT'
else:
return self.visit_TEXT(type_)
sqlalchemy.dialects.postgresql.dialect.type_compiler.visit_IBID_TEXT = postgres_visit_IBID_TEXT

import sqlalchemy.dialects.sqlite
def sqlite_visit_IBID_VARCHAR(self, type_):
def sqlite_visit_IBID_VARCHAR(self, type_, **kw):
if type_.case_insensitive:
collation = 'NOCASE'
else:
collation = 'BINARY'
return self.visit_VARCHAR(type_) + ' COLLATE ' + collation
return self.visit_VARCHAR(type_, **kw) + ' COLLATE ' + collation
sqlalchemy.dialects.sqlite.dialect.type_compiler.visit_IBID_VARCHAR = sqlite_visit_IBID_VARCHAR
def sqlite_visit_IBID_TEXT(self, type_):
def sqlite_visit_IBID_TEXT(self, type_, **kw):
if type_.case_insensitive:
collation = 'NOCASE'
else:
Expand All @@ -38,19 +38,19 @@ def sqlite_visit_IBID_TEXT(self, type_):
sqlalchemy.dialects.sqlite.dialect.type_compiler.visit_IBID_TEXT = sqlite_visit_IBID_TEXT

import sqlalchemy.dialects.mysql
def mysql_visit_IBID_VARCHAR(self, type_):
def mysql_visit_IBID_VARCHAR(self, type_, **kw):
if type_.case_insensitive:
collation = 'utf8_general_ci'
else:
collation = 'utf8_bin'
return self.visit_VARCHAR(type_) + ' COLLATE ' + collation
return self.visit_VARCHAR(type_, **kw) + ' COLLATE ' + collation
sqlalchemy.dialects.mysql.dialect.type_compiler.visit_IBID_VARCHAR = sqlite_visit_IBID_VARCHAR
def mysql_visit_IBID_TEXT(self, type_):
def mysql_visit_IBID_TEXT(self, type_, **kw):
if type_.case_insensitive:
collation = 'utf8_general_ci'
else:
collation = 'utf8_bin'
return self.visit_TEXT(type_) + ' COLLATE ' + collation
return self.visit_TEXT(type_, **kw) + ' COLLATE ' + collation
sqlalchemy.dialects.mysql.dialect.type_compiler.visit_IBID_TEXT = sqlite_visit_IBID_TEXT

class IbidUnicode(_Unicode):
Expand Down
54 changes: 54 additions & 0 deletions ibid/plugins/ddg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (c) 2016, Kyle Robbertze
# Released under terms of the MIT/X/Expat Licence. See COPYING for details.

from httplib import BadStatusLine

from ibid.config import Option
from ibid.plugins import Processor, match
from ibid.utils import decode_htmlentities, json_webservice

features = {'duckduckgo': {
'description': u'Retrieves results from DuckDuckGo',
'categories': ('lookup', 'web', 'calculate', ),
}}

class DDGAPISearch(Processor):
usage = u"""ddg[.<tld>] [for] <term>"""

features = ('duckduckgo',)
def _ddg_api_search(self, query, resultsize="large", country=None):
params = {
'q': query,
't': 'ibid',
'format': 'json',
}
if country is not None:
params['gl'] = country

return json_webservice('https://api.duckduckgo.com', params)

@match(r'^ddg(?:\.com?)?(?:\.([a-z]{2}))?\s+(?:for\s+)?(.+?)$')
def search(self, event, key, country, query):
try:
items = self._ddg_api_search(query, country=country)
except BadStatusLine:
event.addresponse(u'DuckDuckGo appears to be broken (or more likely, my connection to it)')
return

results = []
topic = 'Results'
for item in items[topic]:
title = item['Text']
url = item['FirstURL']
results.append(u'"%s" %s' % (title, url))
topic = 'RelatedTopics'
for i in range(max(5, len(items[topic]))):
title = items[topic][i]['Text']
url = items[topic][i]['FirstURL']
results.append(u'"%s" %s' % (title, url))

if results:
event.addresponse(u' :: '.join(results) + "(Results from DuckDuckGo)")
else:
event.addresponse(u'Uhh... DuckDuckGo has no Instant Answer on that')
# vi: set et sta sw=4 ts=4: