mirror of
https://github.com/SpiceBot/SpiceBot.git
synced 2026-11-13 06:59:53 -05:00
test
This commit is contained in:
parent
d99e96386f
commit
bb33c328af
@ -2,20 +2,33 @@
|
|||||||
from __future__ import unicode_literals, absolute_import, division, print_function
|
from __future__ import unicode_literals, absolute_import, division, print_function
|
||||||
"""A way to search google"""
|
"""A way to search google"""
|
||||||
|
|
||||||
|
import urllib
|
||||||
import requests
|
import requests
|
||||||
from fake_useragent import UserAgent
|
from fake_useragent import UserAgent
|
||||||
|
|
||||||
|
from sopel.config.types import StaticSection, ValidatedAttribute
|
||||||
|
|
||||||
|
from .Config import config as botconfig
|
||||||
|
|
||||||
# TODO add a cache flush
|
# TODO add a cache flush
|
||||||
|
|
||||||
|
|
||||||
|
class SpiceBot_Google_MainSection(StaticSection):
|
||||||
|
search_api = ValidatedAttribute('search_api', default=None)
|
||||||
|
|
||||||
|
|
||||||
class Google():
|
class Google():
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.setup_google()
|
||||||
self.cache = {
|
self.cache = {
|
||||||
"info": {},
|
"info": {},
|
||||||
"maps": {}
|
"maps": {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def setup_commands(self):
|
||||||
|
botconfig.define_section("SpiceBot_Google", SpiceBot_Google_MainSection, validate=False)
|
||||||
|
|
||||||
def search(self, searchterm, searchtype="info"):
|
def search(self, searchterm, searchtype="info"):
|
||||||
|
|
||||||
# losercase searching
|
# losercase searching
|
||||||
@ -31,27 +44,64 @@ class Google():
|
|||||||
if searchterm in self.cache[searchtype]:
|
if searchterm in self.cache[searchtype]:
|
||||||
return self.cache[searchtype][searchterm]
|
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)}
|
header = {'User-Agent': str(UserAgent().chrome)}
|
||||||
data = searchterm.replace(' ', '+')
|
data = searchterm.replace(' ', '+')
|
||||||
lookfor = data.replace(':', '%3A')
|
lookfor = data.replace(':', '%3A')
|
||||||
|
if not botconfig.SpiceBot_Google.search_api or retry:
|
||||||
try:
|
try:
|
||||||
if searchtype == 'info':
|
|
||||||
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)
|
var = requests.get(r'http://www.google.com/search?q=' + lookfor + '&btnI', headers=header)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
var = e
|
var = e
|
||||||
var = None
|
var = None
|
||||||
|
|
||||||
if not var or not var.url:
|
if not var or not var.url:
|
||||||
return None
|
return None
|
||||||
else:
|
|
||||||
self.cache[searchtype][searchterm] = str(var.url)
|
|
||||||
return var.url
|
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()
|
google = Google()
|
||||||
|
|||||||
@ -3,6 +3,8 @@ from __future__ import unicode_literals, absolute_import, division, print_functi
|
|||||||
"""Configuration for SpiceBot"""
|
"""Configuration for SpiceBot"""
|
||||||
|
|
||||||
from .Channels import SpiceBot_Channels_MainSection
|
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 .AI import SpiceBot_AI_MainSection
|
||||||
from .Update import SpiceBot_Update_MainSection
|
from .Update import SpiceBot_Update_MainSection
|
||||||
from .Kick import SpiceBot_Kick
|
from .Kick import SpiceBot_Kick
|
||||||
@ -36,3 +38,9 @@ def configure(config):
|
|||||||
config.define_section("SpiceBot_AI", SpiceBot_AI_MainSection, validate=False)
|
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('extra', 'extra aiml directories to import matches from')
|
||||||
config.SpiceBot_AI.configure_setting('gender', 'bot gender')
|
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')
|
||||||
|
|||||||
@ -13,6 +13,7 @@ from .Events import events
|
|||||||
|
|
||||||
from .Channels import SpiceBot_Channels_MainSection
|
from .Channels import SpiceBot_Channels_MainSection
|
||||||
from .Commands import SpiceBot_Commands_MainSection
|
from .Commands import SpiceBot_Commands_MainSection
|
||||||
|
from .Google import SpiceBot_Google_MainSection
|
||||||
from .Update import SpiceBot_Update_MainSection
|
from .Update import SpiceBot_Update_MainSection
|
||||||
from .osd import SopelWrapperOSD, ToolsOSD, SopelOSD, SpiceBot_OSD
|
from .osd import SopelWrapperOSD, ToolsOSD, SopelOSD, SpiceBot_OSD
|
||||||
from .Kick import SopelWrapperKICK, SopelKICK, SpiceBot_Kick
|
from .Kick import SopelWrapperKICK, SopelKICK, SpiceBot_Kick
|
||||||
@ -30,6 +31,9 @@ def setup(bot):
|
|||||||
# Commands
|
# Commands
|
||||||
setup_commands(bot)
|
setup_commands(bot)
|
||||||
|
|
||||||
|
# Commands
|
||||||
|
setup_google(bot)
|
||||||
|
|
||||||
# OSD
|
# OSD
|
||||||
setup_osd(bot)
|
setup_osd(bot)
|
||||||
|
|
||||||
@ -75,6 +79,11 @@ def setup_commands(bot):
|
|||||||
bot.config.core.prefix_list.extend(bot.config.SpiceBot_Commands.query_prefix)
|
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):
|
def setup_startupmonologue(bot):
|
||||||
logs.log('SpiceBot_StartupMonologue', "Initial Setup processing")
|
logs.log('SpiceBot_StartupMonologue', "Initial Setup processing")
|
||||||
events.startup_add([
|
events.startup_add([
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user