From 9fdccf64e3787a8b004f238ae2d201f2011b472d Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Fri, 14 Jun 2019 09:24:32 -0400 Subject: [PATCH] make google search faster with a cache --- sopel_modules/SpiceBot/Database.py | 1 - sopel_modules/SpiceBot/Google.py | 49 +++++++++++++++++++ sopel_modules/SpiceBot/Tools.py | 21 -------- sopel_modules/SpiceBot/__init__.py | 1 + sopel_modules/SpiceBot_AI/AI.py | 4 +- .../aiml/nickname_canyousee.aiml | 6 +++ 6 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 sopel_modules/SpiceBot/Google.py diff --git a/sopel_modules/SpiceBot/Database.py b/sopel_modules/SpiceBot/Database.py index 5cdbb66..b514026 100644 --- a/sopel_modules/SpiceBot/Database.py +++ b/sopel_modules/SpiceBot/Database.py @@ -528,7 +528,6 @@ class SpiceDB(object): class BotDatabase(): - """A thread safe database cache""" def __init__(self): diff --git a/sopel_modules/SpiceBot/Google.py b/sopel_modules/SpiceBot/Google.py new file mode 100644 index 0000000..8d29968 --- /dev/null +++ b/sopel_modules/SpiceBot/Google.py @@ -0,0 +1,49 @@ +# coding=utf8 +from __future__ import unicode_literals, absolute_import, division, print_function +"""A way to search google""" + +import requests +from fake_useragent import UserAgent + + +class Google(): + + def __init__(self): + self.cache = { + "info": {}, + "maps": {} + } + + def search(self, searchterm, searchtype="info"): + + # losercase searching + searchterm = searchterm.lower() + + # check cache + if searchtype not in list(self.cache.keys()): + self.cache[searchtype] = dict() + if searchterm in self.cache[searchtype]: + return self.cache[searchtype][searchterm] + + header = {'User-Agent': str(UserAgent().chrome)} + data = searchterm.replace(' ', '+') + lookfor = data.replace(':', '%3A') + try: + if searchtype == 'maps': + var = requests.get(r'http://www.google.com/maps/place/' + lookfor, headers=header) + elif searchtype == 'info': + var = requests.get(r'http://www.google.com/search?q=' + lookfor + '&btnI', headers=header) + else: + var = requests.get(r'http://www.google.com/search?q=' + lookfor + '&btnI', headers=header) + except Exception as e: + var = e + var = None + + if not var or not var.url: + return None + else: + self.cache[searchtype][searchterm] = str(var.url) + return var.url + + +google = Google() diff --git a/sopel_modules/SpiceBot/Tools.py b/sopel_modules/SpiceBot/Tools.py index 4f44c47..637d16f 100644 --- a/sopel_modules/SpiceBot/Tools.py +++ b/sopel_modules/SpiceBot/Tools.py @@ -10,8 +10,6 @@ import collections import os import sys import codecs -import requests -from fake_useragent import UserAgent from difflib import SequenceMatcher from operator import itemgetter from collections import abc @@ -129,25 +127,6 @@ def humanized_time(countdownseconds): """Online Information Requests""" -def googlesearch(searchterm, searchtype=None): - header = {'User-Agent': str(UserAgent().chrome)} - data = searchterm.replace(' ', '+') - lookfor = data.replace(':', '%3A') - try: - if searchtype == 'maps': - var = requests.get(r'http://www.google.com/maps/place/' + lookfor, headers=header) - else: - var = requests.get(r'http://www.google.com/search?q=' + lookfor + '&btnI', headers=header) - except Exception as e: - var = e - var = None - - if not var or not var.url: - return None - else: - return var.url - - """List Manipulation Functions""" diff --git a/sopel_modules/SpiceBot/__init__.py b/sopel_modules/SpiceBot/__init__.py index 239248b..bc0397a 100644 --- a/sopel_modules/SpiceBot/__init__.py +++ b/sopel_modules/SpiceBot/__init__.py @@ -22,6 +22,7 @@ from .StartupMonologue import * from .Channels import * from .Users import * from .AI import * +from .Google import * from .configure import * diff --git a/sopel_modules/SpiceBot_AI/AI.py b/sopel_modules/SpiceBot_AI/AI.py index e3ce058..27e0628 100644 --- a/sopel_modules/SpiceBot_AI/AI.py +++ b/sopel_modules/SpiceBot_AI/AI.py @@ -96,9 +96,9 @@ def bot_command_rule(bot, trigger): searchterm = spicemanip.main(trigger_args, "3+") or None if searchterm: if trigger_args[0].lower() == "where": - searchreturn = SpiceBot.googlesearch(searchterm, 'maps') + searchreturn = SpiceBot.google.search(searchterm, 'maps') else: - searchreturn = SpiceBot.googlesearch(searchterm) + searchreturn = SpiceBot.google.search(searchterm) if not searchreturn: bot.osd('I cannot find anything about that') else: diff --git a/sopel_modules/SpiceBot_Configs/aiml/nickname_canyousee.aiml b/sopel_modules/SpiceBot_Configs/aiml/nickname_canyousee.aiml index 088609a..ee1d90d 100644 --- a/sopel_modules/SpiceBot_Configs/aiml/nickname_canyousee.aiml +++ b/sopel_modules/SpiceBot_Configs/aiml/nickname_canyousee.aiml @@ -13,4 +13,10 @@ + + BOTNICK have you seen + + + +