This commit is contained in:
deathbybandaid 2019-06-14 11:52:26 -04:00
parent d99e96386f
commit bb33c328af
3 changed files with 83 additions and 16 deletions

View File

@ -2,20 +2,33 @@
from __future__ import unicode_literals, absolute_import, division, print_function
"""A way to search google"""
import urllib
import requests
from fake_useragent import UserAgent
from sopel.config.types import StaticSection, ValidatedAttribute
from .Config import config as botconfig
# TODO add a cache flush
class SpiceBot_Google_MainSection(StaticSection):
search_api = ValidatedAttribute('search_api', default=None)
class Google():
def __init__(self):
self.setup_google()
self.cache = {
"info": {},
"maps": {}
}
def setup_commands(self):
botconfig.define_section("SpiceBot_Google", SpiceBot_Google_MainSection, validate=False)
def search(self, searchterm, searchtype="info"):
# losercase searching
@ -31,27 +44,64 @@ class Google():
if searchterm in self.cache[searchtype]:
return self.cache[searchtype][searchterm]
if searchtype == 'maps':
returnurl = self.search_maps(searchterm)
elif searchtype == 'youtube':
returnurl = self.search_youtube(searchterm)
else:
returnurl = self.search_info(self, searchterm)
if returnurl:
self.cache[searchtype][searchterm] = str(returnurl)
return returnurl
def search_info(self, searchterm, retry=False):
header = {'User-Agent': str(UserAgent().chrome)}
data = searchterm.replace(' ', '+')
lookfor = data.replace(':', '%3A')
try:
if searchtype == 'info':
if not botconfig.SpiceBot_Google.search_api or retry:
try:
var = requests.get(r'http://www.google.com/search?q=' + lookfor + '&btnI', headers=header)
elif searchtype == 'maps':
var = requests.get(r'http://www.google.com/maps/place/' + lookfor, headers=header)
elif searchtype == 'youtube':
var = requests.get(r'https://www.youtube.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)
except Exception as e:
var = e
var = None
if not var or not var.url:
return None
return var.url
else:
return self.search_info(searchterm, retry=True)
def search_maps(self, searchterm, retry=False):
header = {'User-Agent': str(UserAgent().chrome)}
data = searchterm.replace(' ', '+')
lookfor = data.replace(':', '%3A')
if not botconfig.SpiceBot_Google.search_api or retry:
try:
var = requests.get(r'http://www.google.com/maps/place/' + lookfor, headers=header)
except Exception as e:
var = e
var = None
if not var or not var.url:
return None
return var.url
else:
return self.search_maps(searchterm, retry=True)
def search_youtube(self, searchterm, retry=False):
header = {'User-Agent': str(UserAgent().chrome)}
data = searchterm.replace(' ', '+')
lookfor = data.replace(':', '%3A')
if not botconfig.SpiceBot_Google.search_api or retry:
try:
var = requests.get(r'https://www.youtube.com/search?q=' + lookfor + '&btnI', headers=header)
except Exception as e:
var = e
var = None
if not var or not var.url:
return None
return var.url
else:
return self.search_youtube(searchterm, retry=True)
google = Google()

View File

@ -3,6 +3,8 @@ from __future__ import unicode_literals, absolute_import, division, print_functi
"""Configuration for SpiceBot"""
from .Channels import SpiceBot_Channels_MainSection
from .Commands import SpiceBot_Commands_MainSection
from .Google import SpiceBot_Google_MainSection
from .AI import SpiceBot_AI_MainSection
from .Update import SpiceBot_Update_MainSection
from .Kick import SpiceBot_Kick
@ -36,3 +38,9 @@ def configure(config):
config.define_section("SpiceBot_AI", SpiceBot_AI_MainSection, validate=False)
config.SpiceBot_AI.configure_setting('extra', 'extra aiml directories to import matches from')
config.SpiceBot_AI.configure_setting('gender', 'bot gender')
config.define_section("SpiceBot_Commands", SpiceBot_Commands_MainSection, validate=False)
config.SpiceBot_Commands.configure_setting('query_prefix', 'query prefix')
config.define_section("SpiceBot_Google", SpiceBot_Google_MainSection, validate=False)
config.SpiceBot_Google.configure_setting('search_api', 'api key for google searching')

View File

@ -13,6 +13,7 @@ from .Events import events
from .Channels import SpiceBot_Channels_MainSection
from .Commands import SpiceBot_Commands_MainSection
from .Google import SpiceBot_Google_MainSection
from .Update import SpiceBot_Update_MainSection
from .osd import SopelWrapperOSD, ToolsOSD, SopelOSD, SpiceBot_OSD
from .Kick import SopelWrapperKICK, SopelKICK, SpiceBot_Kick
@ -30,6 +31,9 @@ def setup(bot):
# Commands
setup_commands(bot)
# Commands
setup_google(bot)
# OSD
setup_osd(bot)
@ -75,6 +79,11 @@ def setup_commands(bot):
bot.config.core.prefix_list.extend(bot.config.SpiceBot_Commands.query_prefix)
def setup_google(bot):
logs.log('SpiceBot_Google', "Setting Up Configuration")
bot.config.define_section("SpiceBot_Google", SpiceBot_Google_MainSection, validate=False)
def setup_startupmonologue(bot):
logs.log('SpiceBot_StartupMonologue', "Initial Setup processing")
events.startup_add([