mirror of
https://github.com/SpiceBot/SpiceBot.git
synced 2026-11-10 21:50:59 -05:00
make google search faster with a cache
This commit is contained in:
parent
f04ef8af68
commit
9fdccf64e3
@ -528,7 +528,6 @@ class SpiceDB(object):
|
||||
|
||||
|
||||
class BotDatabase():
|
||||
"""A thread safe database cache"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
|
||||
49
sopel_modules/SpiceBot/Google.py
Normal file
49
sopel_modules/SpiceBot/Google.py
Normal file
@ -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()
|
||||
@ -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"""
|
||||
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@ from .StartupMonologue import *
|
||||
from .Channels import *
|
||||
from .Users import *
|
||||
from .AI import *
|
||||
from .Google import *
|
||||
|
||||
|
||||
from .configure import *
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -13,4 +13,10 @@
|
||||
</category>
|
||||
|
||||
|
||||
<category>
|
||||
<pattern>BOTNICK have you seen</pattern>
|
||||
<template>triggernick, I have seen many things.</template>
|
||||
</category>
|
||||
|
||||
|
||||
</aiml>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user