mirror of
https://github.com/SpiceBot/SpiceBot.git
synced 2026-11-12 06:29:54 -05:00
commit
a8bd276b6f
@ -6,5 +6,4 @@ include *requirements.txt
|
|||||||
recursive-include tests *
|
recursive-include tests *
|
||||||
recursive-exclude * __pycache__
|
recursive-exclude * __pycache__
|
||||||
recursive-exclude * *.py[co]
|
recursive-exclude * *.py[co]
|
||||||
recursive-include sopel_modules/SpiceBot_GifSearch/gifapi *
|
recursive-include sopel_modules/SpiceBot_Configs/* *
|
||||||
recursive-include sopel_modules/SpiceBot_Configs/aiml *
|
|
||||||
|
|||||||
@ -2,3 +2,4 @@ spicemanip>=0.1.6
|
|||||||
gitpython
|
gitpython
|
||||||
pygit2
|
pygit2
|
||||||
fake-useragent
|
fake-useragent
|
||||||
|
google-api-python-client
|
||||||
|
|||||||
236
sopel_modules/SpiceBot/Gif.py
Normal file
236
sopel_modules/SpiceBot/Gif.py
Normal file
@ -0,0 +1,236 @@
|
|||||||
|
# coding=utf8
|
||||||
|
from __future__ import unicode_literals, absolute_import, division, print_function
|
||||||
|
"""How to handle gifs"""
|
||||||
|
|
||||||
|
from sopel.config.types import StaticSection, ValidatedAttribute, ListAttribute
|
||||||
|
import sopel_modules
|
||||||
|
|
||||||
|
from .Config import config as botconfig
|
||||||
|
from .Tools import read_directory_json_to_dict
|
||||||
|
from .Commands import commands as botcommands
|
||||||
|
|
||||||
|
import spicemanip
|
||||||
|
|
||||||
|
import os
|
||||||
|
from fake_useragent import UserAgent
|
||||||
|
import urllib
|
||||||
|
import random
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
class SpiceBot_Gif_MainSection(StaticSection):
|
||||||
|
extra = ListAttribute('extra')
|
||||||
|
nsfw = ValidatedAttribute('nsfw', default=False)
|
||||||
|
|
||||||
|
|
||||||
|
class GifAPISection(StaticSection):
|
||||||
|
apikey = ValidatedAttribute('apikey', default=None)
|
||||||
|
|
||||||
|
|
||||||
|
class BotGif():
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.setup_gif()
|
||||||
|
self.badlinks = []
|
||||||
|
self.header = {'User-Agent': str(UserAgent().chrome)}
|
||||||
|
self.valid_api = {}
|
||||||
|
|
||||||
|
dir_to_scan = []
|
||||||
|
for plugin_dir in set(sopel_modules.__path__):
|
||||||
|
configsdir = os.path.join(plugin_dir, "SpiceBot_Configs")
|
||||||
|
gifcfgdir = os.path.join(configsdir, "gifapi")
|
||||||
|
dir_to_scan.append(gifcfgdir)
|
||||||
|
|
||||||
|
if len(botconfig.SopelGifSearch.extra):
|
||||||
|
for extragifcfgdir in botconfig.SopelGifSearch.extra:
|
||||||
|
dir_to_scan.append(extragifcfgdir)
|
||||||
|
|
||||||
|
valid_gif_api_dict = read_directory_json_to_dict(dir_to_scan, "Gif API", "SpiceBot_Gif")
|
||||||
|
|
||||||
|
for gif_api in list(valid_gif_api_dict.keys()):
|
||||||
|
botconfig.define_section(gif_api, GifAPISection, validate=False)
|
||||||
|
|
||||||
|
self.valid_api[gif_api] = valid_gif_api_dict[gif_api]
|
||||||
|
self.valid_api[gif_api]["apikey"] = None
|
||||||
|
self.valid_api[gif_api]["cache"] = dict()
|
||||||
|
|
||||||
|
apikey = eval("botconfig." + gif_api + ".apikey")
|
||||||
|
if apikey:
|
||||||
|
self.valid_api[gif_api]["apikey"] = apikey
|
||||||
|
|
||||||
|
for validgifapi in list(self.valid_api.keys()):
|
||||||
|
command_dict = {
|
||||||
|
"comtype": "gif_prefix",
|
||||||
|
"validcoms": validgifapi
|
||||||
|
}
|
||||||
|
botcommands.dict['counts'] += 1
|
||||||
|
botcommands.register(command_dict)
|
||||||
|
|
||||||
|
def setup_gif(self):
|
||||||
|
botconfig.define_section("SopelGifSearch", SpiceBot_Gif_MainSection, validate=False)
|
||||||
|
|
||||||
|
def get_gif(self, searchdict):
|
||||||
|
|
||||||
|
# list of defaults
|
||||||
|
query_defaults = {
|
||||||
|
"query": None,
|
||||||
|
"searchnum": 'random',
|
||||||
|
"gifsearch": list(self.valid_api.keys()),
|
||||||
|
"gifsearchremove": ['gifme'], # TODO make a config setting
|
||||||
|
"searchlimit": 'default',
|
||||||
|
"nsfw": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
# set defaults if they don't exist
|
||||||
|
for key in query_defaults:
|
||||||
|
if key not in list(searchdict.keys()):
|
||||||
|
searchdict[key] = query_defaults[key]
|
||||||
|
if key == "gifsearch":
|
||||||
|
for remx in query_defaults["gifsearchremove"]:
|
||||||
|
searchdict["gifsearch"].remove(remx)
|
||||||
|
|
||||||
|
if botconfig.SopelGifSearch.nsfw:
|
||||||
|
query_defaults["nsfw"] = True
|
||||||
|
|
||||||
|
# verify query is there to search
|
||||||
|
if not searchdict["query"]:
|
||||||
|
return {"error": 'No Query to Search'}
|
||||||
|
|
||||||
|
# Replace spaces in search query
|
||||||
|
searchdict["searchquery"] = urllib.request.pathname2url(searchdict["query"])
|
||||||
|
|
||||||
|
# set api usage
|
||||||
|
if not isinstance(searchdict['gifsearch'], list):
|
||||||
|
if str(searchdict['gifsearch']) in list(self.valid_api.keys()):
|
||||||
|
searchdict['gifsearch'] = [searchdict['gifsearch']]
|
||||||
|
else:
|
||||||
|
searchdict['gifsearch'] = list(self.valid_api.keys())
|
||||||
|
for apis in searchdict['gifsearch']:
|
||||||
|
if apis not in list(self.valid_api.keys()):
|
||||||
|
searchdict['gifsearch'].remove(apis)
|
||||||
|
|
||||||
|
# Verify search limit
|
||||||
|
if searchdict['searchlimit'] == 'default' or not isinstance(searchdict['searchlimit'], int):
|
||||||
|
searchdict['searchlimit'] = 50
|
||||||
|
|
||||||
|
# Random handling for searchnum
|
||||||
|
if searchdict["searchnum"] == 'random':
|
||||||
|
searchdict["searchnum"] = random.randint(0, searchdict['searchlimit'])
|
||||||
|
|
||||||
|
# verify valid search parameters
|
||||||
|
if not str(searchdict["searchnum"]).isdigit():
|
||||||
|
return {"error": 'No Search Number or Random Specified'}
|
||||||
|
|
||||||
|
# perform search
|
||||||
|
gifapiresults = []
|
||||||
|
for currentapi in searchdict['gifsearch']:
|
||||||
|
|
||||||
|
# check if query is already cached
|
||||||
|
searchfor = True
|
||||||
|
if searchdict["searchquery"].lower() in list(self.valid_api[currentapi]["cache"].keys()):
|
||||||
|
if len(self.valid_api[currentapi]["cache"][str(searchdict["searchquery"]).lower()]):
|
||||||
|
searchfor = False
|
||||||
|
verifygoodlinks = []
|
||||||
|
for gifresult in self.valid_api[currentapi]["cache"][str(searchdict["searchquery"])]:
|
||||||
|
if gifresult["returnurl"] not in self.badlinks:
|
||||||
|
verifygoodlinks.append(gifresult)
|
||||||
|
self.valid_api[currentapi]["cache"][str(searchdict["searchquery"])] = verifygoodlinks
|
||||||
|
|
||||||
|
if searchfor:
|
||||||
|
self.valid_api[currentapi]["cache"][str(searchdict["searchquery"]).lower()] = []
|
||||||
|
# assemble url
|
||||||
|
url = self.gif_url_assemble(currentapi, str(searchdict["searchquery"]), str(searchdict["searchlimit"]), searchdict['nsfw'])
|
||||||
|
# fetch results
|
||||||
|
self.fetch_gif_results(currentapi, str(searchdict["searchquery"]), url)
|
||||||
|
|
||||||
|
if len(self.valid_api[currentapi]["cache"][searchdict["searchquery"].lower()]):
|
||||||
|
gifapiresults.extend(self.valid_api[currentapi]["cache"][searchdict["searchquery"].lower()])
|
||||||
|
|
||||||
|
if not len(gifapiresults):
|
||||||
|
return {"error": "No Results were found for '" + searchdict["query"] + "' in the " + str(spicemanip.main(searchdict['gifsearch'], 'orlist')) + " api(s)"}
|
||||||
|
|
||||||
|
random.shuffle(gifapiresults)
|
||||||
|
random.shuffle(gifapiresults)
|
||||||
|
randombad = True
|
||||||
|
while randombad:
|
||||||
|
gifdict = spicemanip.main(gifapiresults, "random")
|
||||||
|
|
||||||
|
try:
|
||||||
|
gifpage = requests.get(gifdict["returnurl"], headers=None)
|
||||||
|
except Exception as e:
|
||||||
|
gifpage = str(e)
|
||||||
|
gifpage = None
|
||||||
|
|
||||||
|
if gifpage and not str(gifpage.status_code).startswith(tuple(["4", "5"])):
|
||||||
|
randombad = False
|
||||||
|
else:
|
||||||
|
self.badlinks.append(str(gifdict["returnurl"]))
|
||||||
|
newlist = []
|
||||||
|
for tempdict in gifapiresults:
|
||||||
|
if tempdict["returnurl"] != gifdict["returnurl"]:
|
||||||
|
newlist.append(tempdict)
|
||||||
|
gifapiresults = newlist
|
||||||
|
|
||||||
|
if not len(gifapiresults):
|
||||||
|
return {"error": "No Results were found for '" + searchdict["query"] + "' in the " + str(spicemanip.main(searchdict['gifsearch'], 'orlist')) + " api(s)"}
|
||||||
|
|
||||||
|
# return dict
|
||||||
|
gifdict['error'] = None
|
||||||
|
return gifdict
|
||||||
|
|
||||||
|
def fetch_gif_results(self, currentapi, searchquery, url):
|
||||||
|
try:
|
||||||
|
page = requests.get(url, headers=self.header)
|
||||||
|
except Exception as e:
|
||||||
|
page = e
|
||||||
|
page = None
|
||||||
|
if page and not str(page.status_code).startswith(tuple(["4", "5"])):
|
||||||
|
data = json.loads(urllib.request.urlopen(url).read().decode('utf-8'))
|
||||||
|
results = data[self.valid_api[currentapi]['results']]
|
||||||
|
resultsarray = []
|
||||||
|
for result in results:
|
||||||
|
appendresult = False
|
||||||
|
cururl = result[self.valid_api[currentapi]['cururl']]
|
||||||
|
slashsplit = str(cururl).split("/")
|
||||||
|
fileextension = slashsplit[-1]
|
||||||
|
if not fileextension or fileextension == '':
|
||||||
|
appendresult = True
|
||||||
|
elif str(fileextension).endswith(".gif"):
|
||||||
|
appendresult = True
|
||||||
|
elif "." not in str(fileextension):
|
||||||
|
appendresult = True
|
||||||
|
if appendresult:
|
||||||
|
resultsarray.append(cururl)
|
||||||
|
# make sure there are results
|
||||||
|
if len(resultsarray):
|
||||||
|
# Create Temp dict for every result
|
||||||
|
tempresultnum = 0
|
||||||
|
for tempresult in resultsarray:
|
||||||
|
if tempresult not in self.badlinks:
|
||||||
|
tempresultnum += 1
|
||||||
|
tempdict = {
|
||||||
|
"returnnum": tempresultnum,
|
||||||
|
"returnurl": tempresult,
|
||||||
|
"gifapi": currentapi,
|
||||||
|
}
|
||||||
|
self.valid_api[currentapi]["cache"][str(searchquery).lower()].append(tempdict)
|
||||||
|
|
||||||
|
def gif_url_assemble(self, currentapi, searchquery, searchlimit, searchnsfw):
|
||||||
|
# url base
|
||||||
|
url = str(self.valid_api[currentapi]['url'])
|
||||||
|
# query
|
||||||
|
url += str(self.valid_api[currentapi]['query']) + str(searchquery)
|
||||||
|
# limit
|
||||||
|
url += str(self.valid_api[currentapi]['limit']) + str(searchlimit)
|
||||||
|
# nsfw search?
|
||||||
|
if searchnsfw:
|
||||||
|
url += str(self.valid_api[currentapi]['nsfw'])
|
||||||
|
else:
|
||||||
|
url += str(self.valid_api[currentapi]['sfw'])
|
||||||
|
# api key
|
||||||
|
url += str(self.valid_api[currentapi]['key']) + str(self.valid_api[currentapi]['apikey'])
|
||||||
|
return url
|
||||||
|
|
||||||
|
|
||||||
|
gif = BotGif()
|
||||||
@ -5,45 +5,100 @@ from __future__ import unicode_literals, absolute_import, division, print_functi
|
|||||||
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
|
||||||
|
|
||||||
|
|
||||||
|
class SpiceBot_Google_MainSection(StaticSection):
|
||||||
|
search_api = ValidatedAttribute('search_api', default=None)
|
||||||
|
|
||||||
|
|
||||||
class Google():
|
class Google():
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.header = {'User-Agent': str(UserAgent().chrome)}
|
||||||
|
self.setup_google()
|
||||||
self.cache = {
|
self.cache = {
|
||||||
"info": {},
|
"info": {},
|
||||||
"maps": {}
|
"maps": {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def setup_google(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
|
||||||
searchterm = searchterm.lower()
|
searchterm = searchterm.lower()
|
||||||
|
|
||||||
|
"""
|
||||||
|
safe='off',
|
||||||
|
"""
|
||||||
|
|
||||||
# check cache
|
# check cache
|
||||||
if searchtype not in list(self.cache.keys()):
|
if searchtype not in list(self.cache.keys()):
|
||||||
self.cache[searchtype] = dict()
|
self.cache[searchtype] = dict()
|
||||||
if searchterm in self.cache[searchtype]:
|
if searchterm in self.cache[searchtype]:
|
||||||
return self.cache[searchtype][searchterm]
|
return self.cache[searchtype][searchterm]
|
||||||
|
|
||||||
header = {'User-Agent': str(UserAgent().chrome)}
|
if searchtype == 'maps':
|
||||||
|
returnurl = self.search_maps(searchterm)
|
||||||
|
elif searchtype == 'youtube':
|
||||||
|
returnurl = self.search_youtube(searchterm)
|
||||||
|
else:
|
||||||
|
returnurl = self.search_info(searchterm)
|
||||||
|
|
||||||
|
if returnurl:
|
||||||
|
self.cache[searchtype][searchterm] = str(returnurl)
|
||||||
|
return returnurl
|
||||||
|
|
||||||
|
def search_info(self, searchterm, retry=False):
|
||||||
data = searchterm.replace(' ', '+')
|
data = searchterm.replace(' ', '+')
|
||||||
lookfor = data.replace(':', '%3A')
|
lookfor = data.replace(':', '%3A')
|
||||||
try:
|
if not botconfig.SpiceBot_Google.search_api or retry:
|
||||||
if searchtype == 'maps':
|
try:
|
||||||
var = requests.get(r'http://www.google.com/maps/place/' + lookfor, headers=header)
|
var = requests.get(r'http://www.google.com/search?q=' + lookfor + '&btnI', headers=self.header)
|
||||||
elif searchtype == 'info':
|
except Exception as e:
|
||||||
var = requests.get(r'http://www.google.com/search?q=' + lookfor + '&btnI', headers=header)
|
var = e
|
||||||
else:
|
var = None
|
||||||
var = requests.get(r'http://www.google.com/search?q=' + lookfor + '&btnI', headers=header)
|
if not var or not var.url:
|
||||||
except Exception as e:
|
return None
|
||||||
var = e
|
|
||||||
var = None
|
|
||||||
|
|
||||||
if not var or not var.url:
|
|
||||||
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):
|
||||||
|
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=self.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):
|
||||||
|
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=self.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()
|
||||||
|
|||||||
@ -48,7 +48,7 @@ class BotLogs():
|
|||||||
self.dict["queue"].append(logmessage)
|
self.dict["queue"].append(logmessage)
|
||||||
|
|
||||||
if stdio:
|
if stdio:
|
||||||
self.botstderr(logmessage)
|
self.botstderr(logmessage + "\n")
|
||||||
|
|
||||||
if logtype not in list(self.dict["list"].keys()):
|
if logtype not in list(self.dict["list"].keys()):
|
||||||
self.dict["list"][logtype] = []
|
self.dict["list"][logtype] = []
|
||||||
|
|||||||
@ -23,6 +23,7 @@ from .Channels import *
|
|||||||
from .Users import *
|
from .Users import *
|
||||||
from .AI import *
|
from .AI import *
|
||||||
from .Google import *
|
from .Google import *
|
||||||
|
from .Gif import *
|
||||||
|
|
||||||
|
|
||||||
from .configure import *
|
from .configure import *
|
||||||
|
|||||||
@ -3,8 +3,11 @@ 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 .Gif import SpiceBot_Gif_MainSection
|
||||||
from .Kick import SpiceBot_Kick
|
from .Kick import SpiceBot_Kick
|
||||||
from .osd import SpiceBot_OSD
|
from .osd import SpiceBot_OSD
|
||||||
|
|
||||||
@ -36,3 +39,13 @@ 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')
|
||||||
|
|
||||||
|
config.define_section("SpiceBot_Gif", SpiceBot_Gif_MainSection, validate=False)
|
||||||
|
config.SopelGifSearch.configure_setting('extra', 'SpiceBot_Gif API Extra directory')
|
||||||
|
config.SopelGifSearch.configure_setting('nsfw', 'SpiceBot_Gif API nsfw content')
|
||||||
|
|||||||
@ -13,9 +13,11 @@ 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
|
||||||
|
from .Gif import SpiceBot_Gif_MainSection
|
||||||
from .AI import SpiceBot_AI_MainSection
|
from .AI import SpiceBot_AI_MainSection
|
||||||
|
|
||||||
|
|
||||||
@ -30,6 +32,9 @@ def setup(bot):
|
|||||||
# Commands
|
# Commands
|
||||||
setup_commands(bot)
|
setup_commands(bot)
|
||||||
|
|
||||||
|
# Commands
|
||||||
|
setup_google(bot)
|
||||||
|
|
||||||
# OSD
|
# OSD
|
||||||
setup_osd(bot)
|
setup_osd(bot)
|
||||||
|
|
||||||
@ -48,6 +53,8 @@ def setup(bot):
|
|||||||
# startupmonologue
|
# startupmonologue
|
||||||
setup_startupmonologue(bot)
|
setup_startupmonologue(bot)
|
||||||
|
|
||||||
|
setup_gif(bot)
|
||||||
|
|
||||||
|
|
||||||
def setup_config(bot):
|
def setup_config(bot):
|
||||||
logs.log('SpiceBot_Config', "Setting Up Configuration")
|
logs.log('SpiceBot_Config', "Setting Up Configuration")
|
||||||
@ -75,6 +82,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([
|
||||||
@ -129,3 +141,7 @@ def setup_kick(bot):
|
|||||||
logs.log('SpiceBot_Kick', "Checking for config settings")
|
logs.log('SpiceBot_Kick', "Checking for config settings")
|
||||||
bot.config.define_section("SpiceBot_Kick", SpiceBot_Kick, validate=False)
|
bot.config.define_section("SpiceBot_Kick", SpiceBot_Kick, validate=False)
|
||||||
botconfig.define_section("SpiceBot_Kick", SpiceBot_Kick, validate=False)
|
botconfig.define_section("SpiceBot_Kick", SpiceBot_Kick, validate=False)
|
||||||
|
|
||||||
|
|
||||||
|
def setup_gif(bot):
|
||||||
|
bot.config.define_section("SopelGifSearch", SpiceBot_Gif_MainSection, validate=False)
|
||||||
|
|||||||
@ -1,8 +0,0 @@
|
|||||||
# coding=utf8
|
|
||||||
from __future__ import unicode_literals, absolute_import, division, print_function
|
|
||||||
|
|
||||||
from .AI import *
|
|
||||||
|
|
||||||
__author__ = 'Sam Zick'
|
|
||||||
__email__ = 'sam@deathbybandaid.net'
|
|
||||||
__version__ = '0.1.1'
|
|
||||||
@ -10,7 +10,7 @@ import spicemanip
|
|||||||
|
|
||||||
|
|
||||||
@sopel.module.rule('(.*)')
|
@sopel.module.rule('(.*)')
|
||||||
def bot_command_rule(bot, trigger):
|
def bot_command_rule_ai(bot, trigger):
|
||||||
|
|
||||||
# TODO add config limits
|
# TODO add config limits
|
||||||
# but still allow in privmsg
|
# but still allow in privmsg
|
||||||
@ -11,6 +11,8 @@ import spicemanip
|
|||||||
|
|
||||||
import sopel_modules.SpiceBot as SpiceBot
|
import sopel_modules.SpiceBot as SpiceBot
|
||||||
|
|
||||||
|
# TODO commands sent in privmsg don't need bot.nick
|
||||||
|
|
||||||
|
|
||||||
@SpiceBot.events.check_ready([SpiceBot.events.BOT_COMMANDS])
|
@SpiceBot.events.check_ready([SpiceBot.events.BOT_COMMANDS])
|
||||||
@SpiceBot.prerun.prerun('nickname')
|
@SpiceBot.prerun.prerun('nickname')
|
||||||
|
|||||||
51
sopel_modules/SpiceBot_Core_Commands/Gif.py
Normal file
51
sopel_modules/SpiceBot_Core_Commands/Gif.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
|
||||||
|
from __future__ import unicode_literals, absolute_import, division, print_function
|
||||||
|
|
||||||
|
import sopel.module
|
||||||
|
|
||||||
|
import sopel_modules.SpiceBot as SpiceBot
|
||||||
|
|
||||||
|
import spicemanip
|
||||||
|
|
||||||
|
# TODO custom gif shortcut commands
|
||||||
|
|
||||||
|
|
||||||
|
@SpiceBot.prerun.prerun('gif_prefix')
|
||||||
|
@sopel.module.commands('gif')
|
||||||
|
def gif_trigger(bot, trigger):
|
||||||
|
|
||||||
|
if not len(trigger.sb['args']):
|
||||||
|
return bot.osd("Please present a query to search.")
|
||||||
|
|
||||||
|
query = spicemanip.main(trigger.sb['args'], 0)
|
||||||
|
searchapis = list(SpiceBot.gif.valid_api.keys())
|
||||||
|
searchdict = {"query": query, "gifsearch": searchapis}
|
||||||
|
|
||||||
|
gifdict = SpiceBot.gif.get_gif(searchdict)
|
||||||
|
|
||||||
|
if gifdict["error"]:
|
||||||
|
bot.osd(gifdict["error"])
|
||||||
|
else:
|
||||||
|
bot.osd(str(gifdict['gifapi'].title() + " Result (" + str(query) + " #" + str(gifdict["returnnum"]) + "): " + str(gifdict["returnurl"])))
|
||||||
|
|
||||||
|
|
||||||
|
@SpiceBot.prerun.prerun('gif_prefix')
|
||||||
|
@sopel.module.commands('(.*)')
|
||||||
|
def gifapi_triggers(bot, trigger):
|
||||||
|
|
||||||
|
if trigger.sb['com'] not in list(SpiceBot.gif.valid_api.keys()):
|
||||||
|
return
|
||||||
|
|
||||||
|
if not len(trigger.sb['args']):
|
||||||
|
return bot.osd("Please present a query to search.")
|
||||||
|
|
||||||
|
query = spicemanip.main(trigger.sb['args'], 0)
|
||||||
|
searchdict = {"query": query, "gifsearch": trigger.sb['com']}
|
||||||
|
|
||||||
|
gifdict = SpiceBot.gif.get_gif(searchdict)
|
||||||
|
|
||||||
|
if gifdict["error"]:
|
||||||
|
bot.osd(gifdict["error"])
|
||||||
|
else:
|
||||||
|
bot.osd(str(gifdict['gifapi'].title() + " Result (" + str(query) + " #" + str(gifdict["returnnum"]) + "): " + str(gifdict["returnurl"])))
|
||||||
74
sopel_modules/SpiceBot_Core_Commands/Search.py
Normal file
74
sopel_modules/SpiceBot_Core_Commands/Search.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
# coding=utf8
|
||||||
|
from __future__ import unicode_literals, absolute_import, division, print_function
|
||||||
|
"""
|
||||||
|
This is the SpiceBot search system.
|
||||||
|
"""
|
||||||
|
import sopel
|
||||||
|
|
||||||
|
import spicemanip
|
||||||
|
|
||||||
|
import sopel_modules.SpiceBot as SpiceBot
|
||||||
|
|
||||||
|
|
||||||
|
@SpiceBot.prerun.prerun('module')
|
||||||
|
@sopel.module.commands('search', 'find', 'google', 'lookup')
|
||||||
|
def search_main(bot, trigger):
|
||||||
|
|
||||||
|
searchterm = spicemanip.main(trigger.sb['args'], 0) or None
|
||||||
|
if not searchterm:
|
||||||
|
bot.osd("Not sure what you want me to look for.")
|
||||||
|
return
|
||||||
|
|
||||||
|
searchreturn = SpiceBot.google.search(searchterm)
|
||||||
|
if not searchreturn:
|
||||||
|
bot.osd('I cannot find anything about that')
|
||||||
|
else:
|
||||||
|
bot.osd(["[Information search for '" + str(searchterm) + "']", str(searchreturn)])
|
||||||
|
|
||||||
|
|
||||||
|
@SpiceBot.prerun.prerun('module')
|
||||||
|
@sopel.module.commands("where", "whereis")
|
||||||
|
def search_where(bot, trigger):
|
||||||
|
|
||||||
|
searchterm = spicemanip.main(trigger.sb['args'], 0) or None
|
||||||
|
if not searchterm:
|
||||||
|
bot.osd("Not sure what you want me to look for.")
|
||||||
|
return
|
||||||
|
|
||||||
|
searchreturn = SpiceBot.google.search(searchterm, 'maps')
|
||||||
|
if not searchreturn:
|
||||||
|
bot.osd('I cannot find anything about that')
|
||||||
|
else:
|
||||||
|
bot.osd(["[Location search for " + str(searchterm) + "]", str(searchreturn)])
|
||||||
|
|
||||||
|
|
||||||
|
@SpiceBot.prerun.prerun('module')
|
||||||
|
@sopel.module.commands("what", "whatis")
|
||||||
|
def search_what(bot, trigger):
|
||||||
|
|
||||||
|
searchterm = spicemanip.main(trigger.sb['args'], 0) or None
|
||||||
|
if not searchterm:
|
||||||
|
bot.osd("Not sure what you want me to look for.")
|
||||||
|
return
|
||||||
|
|
||||||
|
searchreturn = SpiceBot.google.search(searchterm)
|
||||||
|
if not searchreturn:
|
||||||
|
bot.osd('I cannot find anything about that')
|
||||||
|
else:
|
||||||
|
bot.osd(["[Information search for '" + str(searchterm) + "']", str(searchreturn)])
|
||||||
|
|
||||||
|
|
||||||
|
@SpiceBot.prerun.prerun('module')
|
||||||
|
@sopel.module.commands("youtube", "video")
|
||||||
|
def search_video(bot, trigger):
|
||||||
|
|
||||||
|
searchterm = spicemanip.main(trigger.sb['args'], 0) or None
|
||||||
|
if not searchterm:
|
||||||
|
bot.osd("Not sure what you want me to look for.")
|
||||||
|
return
|
||||||
|
|
||||||
|
searchreturn = SpiceBot.google.search(searchterm, 'youtube')
|
||||||
|
if not searchreturn:
|
||||||
|
bot.osd('I cannot find anything about that')
|
||||||
|
else:
|
||||||
|
bot.osd(["[Youtube search for '" + str(searchterm) + "']", str(searchreturn)])
|
||||||
@ -16,6 +16,9 @@ from .Runtime_Controls import *
|
|||||||
from .Messaging import *
|
from .Messaging import *
|
||||||
from .Docs import *
|
from .Docs import *
|
||||||
from .BotPrivs import *
|
from .BotPrivs import *
|
||||||
|
from .Search import *
|
||||||
|
from .AI import *
|
||||||
|
from .Gif import *
|
||||||
|
|
||||||
__author__ = 'Sam Zick'
|
__author__ = 'Sam Zick'
|
||||||
__email__ = 'sam@deathbybandaid.net'
|
__email__ = 'sam@deathbybandaid.net'
|
||||||
|
|||||||
@ -1,33 +0,0 @@
|
|||||||
# coding=utf-8
|
|
||||||
|
|
||||||
from __future__ import unicode_literals, absolute_import, division, print_function
|
|
||||||
|
|
||||||
from .gifsearch import getGif
|
|
||||||
|
|
||||||
import sopel.module
|
|
||||||
|
|
||||||
import spicemanip
|
|
||||||
|
|
||||||
import sopel_modules.SpiceBot as SpiceBot
|
|
||||||
|
|
||||||
|
|
||||||
@SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH])
|
|
||||||
@SpiceBot.prerun.prerun('prefix')
|
|
||||||
@sopel.module.commands('(.*)')
|
|
||||||
def gifapi_triggers(bot, trigger):
|
|
||||||
|
|
||||||
if trigger.sb['com'] not in list(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys()):
|
|
||||||
return
|
|
||||||
|
|
||||||
if not len(trigger.sb['args']):
|
|
||||||
return bot.osd("Please present a query to search.")
|
|
||||||
|
|
||||||
query = spicemanip.main(trigger.sb['args'], 0)
|
|
||||||
searchdict = {"query": query, "gifsearch": trigger.sb['com']}
|
|
||||||
|
|
||||||
gifdict = getGif(bot, searchdict)
|
|
||||||
|
|
||||||
if gifdict["error"]:
|
|
||||||
bot.osd(gifdict["error"])
|
|
||||||
else:
|
|
||||||
bot.osd(str(gifdict['gifapi'].title() + " Result (" + str(query) + " #" + str(gifdict["returnnum"]) + "): " + str(gifdict["returnurl"])))
|
|
||||||
@ -1,31 +0,0 @@
|
|||||||
# coding=utf-8
|
|
||||||
|
|
||||||
from __future__ import unicode_literals, absolute_import, division, print_function
|
|
||||||
|
|
||||||
from sopel import module
|
|
||||||
|
|
||||||
from .gifsearch import getGif
|
|
||||||
|
|
||||||
import sopel_modules.SpiceBot as SpiceBot
|
|
||||||
|
|
||||||
import spicemanip
|
|
||||||
|
|
||||||
|
|
||||||
@SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH])
|
|
||||||
@SpiceBot.prerun.prerun('prefix')
|
|
||||||
@module.commands('gif')
|
|
||||||
def gif_trigger(bot, trigger):
|
|
||||||
|
|
||||||
if not len(trigger.sb['args']):
|
|
||||||
return bot.osd("Please present a query to search.")
|
|
||||||
|
|
||||||
query = spicemanip.main(trigger.sb['args'], 0)
|
|
||||||
searchapis = list(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys())
|
|
||||||
searchdict = {"query": query, "gifsearch": searchapis}
|
|
||||||
|
|
||||||
gifdict = getGif(bot, searchdict)
|
|
||||||
|
|
||||||
if gifdict["error"]:
|
|
||||||
bot.osd(gifdict["error"])
|
|
||||||
else:
|
|
||||||
bot.osd(str(gifdict['gifapi'].title() + " Result (" + str(query) + " #" + str(gifdict["returnnum"]) + "): " + str(gifdict["returnurl"])))
|
|
||||||
@ -1,33 +0,0 @@
|
|||||||
# coding=utf-8
|
|
||||||
|
|
||||||
from __future__ import unicode_literals, absolute_import, division, print_function
|
|
||||||
|
|
||||||
from .gifsearch import getGif
|
|
||||||
|
|
||||||
import sopel.module
|
|
||||||
|
|
||||||
import spicemanip
|
|
||||||
|
|
||||||
import sopel_modules.SpiceBot as SpiceBot
|
|
||||||
|
|
||||||
|
|
||||||
@SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH])
|
|
||||||
@SpiceBot.prerun.prerun('prefix')
|
|
||||||
@sopel.module.commands('(.*)')
|
|
||||||
def gifapi_triggers(bot, trigger):
|
|
||||||
|
|
||||||
if trigger.sb['com'] not in list(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys()):
|
|
||||||
return
|
|
||||||
|
|
||||||
if not len(trigger.sb['args']):
|
|
||||||
return bot.osd("Please present a query to search.")
|
|
||||||
|
|
||||||
query = spicemanip.main(trigger.sb['args'], 0)
|
|
||||||
searchdict = {"query": query, "gifsearch": trigger.sb['com']}
|
|
||||||
|
|
||||||
gifdict = getGif(bot, searchdict)
|
|
||||||
|
|
||||||
if gifdict["error"]:
|
|
||||||
bot.osd(gifdict["error"])
|
|
||||||
else:
|
|
||||||
bot.osd(str(gifdict['gifapi'].title() + " Result (" + str(query) + " #" + str(gifdict["returnnum"]) + "): " + str(gifdict["returnurl"])))
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
# coding=utf8
|
|
||||||
"""Sopel Gif Search
|
|
||||||
|
|
||||||
Sopel Gif Search builds a convenient framework for searching for Gifs
|
|
||||||
"""
|
|
||||||
from __future__ import unicode_literals, absolute_import, division, print_function
|
|
||||||
|
|
||||||
from .Gif_Command import *
|
|
||||||
from .Gif_API_Commands import *
|
|
||||||
# from .Gif_Search_Commands import *
|
|
||||||
from .gifsearch import *
|
|
||||||
|
|
||||||
__author__ = 'Sam Zick'
|
|
||||||
__email__ = 'sam@deathbybandaid.net'
|
|
||||||
__version__ = '0.1.1'
|
|
||||||
@ -1,254 +0,0 @@
|
|||||||
# coding=utf-8
|
|
||||||
|
|
||||||
from __future__ import unicode_literals, absolute_import, division, print_function
|
|
||||||
|
|
||||||
import sopel.module
|
|
||||||
from sopel.config.types import StaticSection, ValidatedAttribute
|
|
||||||
|
|
||||||
import sopel_modules.SpiceBot as SpiceBot
|
|
||||||
|
|
||||||
import spicemanip
|
|
||||||
|
|
||||||
import os
|
|
||||||
import urllib
|
|
||||||
import requests
|
|
||||||
import json
|
|
||||||
from fake_useragent import UserAgent
|
|
||||||
import random
|
|
||||||
|
|
||||||
|
|
||||||
class GifAPIMainSection(StaticSection):
|
|
||||||
extra = ValidatedAttribute('extra', default=None)
|
|
||||||
nsfw = ValidatedAttribute('nsfw', default=False)
|
|
||||||
|
|
||||||
|
|
||||||
class GifAPISection(StaticSection):
|
|
||||||
apikey = ValidatedAttribute('apikey', default=None)
|
|
||||||
|
|
||||||
|
|
||||||
def configure(config):
|
|
||||||
moduledir = os.path.dirname(os.path.abspath(__file__))
|
|
||||||
api_dir = os.path.join(moduledir, 'gifapi')
|
|
||||||
valid_gif_api_dict = SpiceBot.read_directory_json_to_dict([api_dir], "Gif API", "SpiceBot_GifSearch", logging=True)
|
|
||||||
|
|
||||||
config.define_section("SopelGifSearch", GifAPIMainSection, validate=False)
|
|
||||||
config.SopelGifSearch.configure_setting('extra', 'SpiceBot_GifSearch API Extra directory')
|
|
||||||
config.SopelGifSearch.configure_setting('nsfw', 'SpiceBot_GifSearch API nsfw content')
|
|
||||||
|
|
||||||
for gif_api in list(valid_gif_api_dict.keys()):
|
|
||||||
config.define_section(gif_api, GifAPISection, validate=False)
|
|
||||||
gif_api_config = eval("config." + gif_api)
|
|
||||||
gif_api_config.configure_setting('apikey', 'GIF API Client ID-' + gif_api)
|
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
|
||||||
SpiceBot.logs.log('SpiceBot_GifSearch', "Starting Setup Procedure")
|
|
||||||
SpiceBot.events.startup_add([SpiceBot.events.BOT_GIFSEARCH])
|
|
||||||
|
|
||||||
if 'SpiceBot_GifSearch' not in bot.memory:
|
|
||||||
bot.memory["SpiceBot_GifSearch"] = {"cache": {}, "badgiflinks": [], 'valid_gif_api_dict': {}}
|
|
||||||
|
|
||||||
dir_to_scan = []
|
|
||||||
|
|
||||||
moduledir = os.path.dirname(os.path.abspath(__file__))
|
|
||||||
api_dir = os.path.join(moduledir, 'gifapi')
|
|
||||||
dir_to_scan.append(api_dir)
|
|
||||||
|
|
||||||
bot.config.define_section("SopelGifSearch", GifAPIMainSection, validate=False)
|
|
||||||
SpiceBot.config.define_section("SopelGifSearch", GifAPIMainSection, validate=False)
|
|
||||||
if bot.config.SopelGifSearch.extra:
|
|
||||||
dir_to_scan.append(bot.config.SopelGifSearch.extra)
|
|
||||||
|
|
||||||
valid_gif_api_dict = SpiceBot.read_directory_json_to_dict(dir_to_scan, "Gif API", "SpiceBot_GifSearch", logging=True)
|
|
||||||
|
|
||||||
for gif_api in list(valid_gif_api_dict.keys()):
|
|
||||||
bot.config.define_section(gif_api, GifAPISection, validate=False)
|
|
||||||
SpiceBot.config.define_section(gif_api, GifAPISection, validate=False)
|
|
||||||
apikey = eval("bot.config." + gif_api + ".apikey")
|
|
||||||
if apikey:
|
|
||||||
valid_gif_api_dict[gif_api]["apikey"] = apikey
|
|
||||||
else:
|
|
||||||
valid_gif_api_dict[gif_api]["apikey"] = None
|
|
||||||
bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][gif_api] = valid_gif_api_dict[gif_api]
|
|
||||||
|
|
||||||
for validgifapi in list(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys()):
|
|
||||||
command_dict = {
|
|
||||||
"comtype": "prefix",
|
|
||||||
"validcoms": validgifapi
|
|
||||||
}
|
|
||||||
SpiceBot.commands.dict['counts'] += 1
|
|
||||||
SpiceBot.commands.register(command_dict)
|
|
||||||
|
|
||||||
if validgifapi not in bot.memory["SpiceBot_GifSearch"]['cache'].keys():
|
|
||||||
bot.memory["SpiceBot_GifSearch"]['cache'][validgifapi] = dict()
|
|
||||||
|
|
||||||
SpiceBot.events.trigger(bot, SpiceBot.events.BOT_GIFSEARCH, "SpiceBot_GifSearch")
|
|
||||||
|
|
||||||
|
|
||||||
def shutdown(bot):
|
|
||||||
if "SpiceBot_GifSearch" in bot.memory:
|
|
||||||
del bot.memory["SpiceBot_GifSearch"]
|
|
||||||
|
|
||||||
|
|
||||||
def getGif(bot, searchdict):
|
|
||||||
|
|
||||||
header = {'User-Agent': str(UserAgent().chrome)}
|
|
||||||
|
|
||||||
# list of defaults
|
|
||||||
query_defaults = {
|
|
||||||
"query": None,
|
|
||||||
"searchnum": 'random',
|
|
||||||
"gifsearch": list(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys()),
|
|
||||||
"gifsearchremove": ['gifme'],
|
|
||||||
"searchlimit": 'default',
|
|
||||||
"nsfw": False,
|
|
||||||
}
|
|
||||||
|
|
||||||
if bot.config.SopelGifSearch.nsfw:
|
|
||||||
query_defaults["nsfw"] = True
|
|
||||||
|
|
||||||
# set defaults if they don't exist
|
|
||||||
for key in query_defaults:
|
|
||||||
if key not in list(searchdict.keys()):
|
|
||||||
searchdict[key] = query_defaults[key]
|
|
||||||
if key == "gifsearch":
|
|
||||||
for remx in query_defaults["gifsearchremove"]:
|
|
||||||
searchdict["gifsearch"].remove(remx)
|
|
||||||
|
|
||||||
# Replace spaces in search query
|
|
||||||
if not searchdict["query"]:
|
|
||||||
return {"error": 'No Query to Search'}
|
|
||||||
# searchdict["searchquery"] = searchdict["query"].replace(' ', '%20')
|
|
||||||
searchdict["searchquery"] = urllib.request.pathname2url(searchdict["query"])
|
|
||||||
|
|
||||||
# set api usage
|
|
||||||
if not isinstance(searchdict['gifsearch'], list):
|
|
||||||
if str(searchdict['gifsearch']) in list(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys()):
|
|
||||||
searchdict['gifsearch'] = [searchdict['gifsearch']]
|
|
||||||
else:
|
|
||||||
searchdict['gifsearch'] = bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys()
|
|
||||||
else:
|
|
||||||
for apis in searchdict['gifsearch']:
|
|
||||||
if apis not in list(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'].keys()):
|
|
||||||
searchdict['gifsearch'].remove(apis)
|
|
||||||
|
|
||||||
# Verify search limit
|
|
||||||
if searchdict['searchlimit'] == 'default' or not isinstance(searchdict['searchlimit'], int):
|
|
||||||
searchdict['searchlimit'] = 50
|
|
||||||
|
|
||||||
# Random handling for searchnum
|
|
||||||
if searchdict["searchnum"] == 'random':
|
|
||||||
searchdict["searchnum"] = random.randint(0, searchdict['searchlimit'])
|
|
||||||
|
|
||||||
# Make sure there is a valid input of query and search number
|
|
||||||
if not searchdict["query"]:
|
|
||||||
return {"error": 'No Query to Search'}
|
|
||||||
|
|
||||||
if not str(searchdict["searchnum"]).isdigit():
|
|
||||||
return {"error": 'No Search Number or Random Specified'}
|
|
||||||
|
|
||||||
gifapiresults = []
|
|
||||||
for currentapi in searchdict['gifsearch']:
|
|
||||||
|
|
||||||
# url base
|
|
||||||
url = str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['url'])
|
|
||||||
# query
|
|
||||||
url += str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['query']) + str(searchdict["searchquery"])
|
|
||||||
# limit
|
|
||||||
url += str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['limit']) + str(searchdict["searchlimit"])
|
|
||||||
# nsfw search?
|
|
||||||
if searchdict['nsfw']:
|
|
||||||
url += str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['nsfw'])
|
|
||||||
else:
|
|
||||||
url += str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['sfw'])
|
|
||||||
# api key
|
|
||||||
url += str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['key']) + str(bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['apikey'])
|
|
||||||
|
|
||||||
if currentapi not in list(bot.memory["SpiceBot_GifSearch"]['cache'].keys()):
|
|
||||||
bot.memory["SpiceBot_GifSearch"]['cache'][currentapi] = dict()
|
|
||||||
|
|
||||||
if str(searchdict["searchquery"]) not in list(bot.memory["SpiceBot_GifSearch"]['cache'][currentapi].keys()):
|
|
||||||
bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] = []
|
|
||||||
|
|
||||||
if not len(bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])]):
|
|
||||||
|
|
||||||
try:
|
|
||||||
page = requests.get(url, headers=header)
|
|
||||||
except Exception as e:
|
|
||||||
page = None
|
|
||||||
|
|
||||||
if page and not str(page.status_code).startswith(tuple(["4", "5"])):
|
|
||||||
|
|
||||||
data = json.loads(urllib.request.urlopen(url).read().decode('utf-8'))
|
|
||||||
|
|
||||||
results = data[bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['results']]
|
|
||||||
resultsarray = []
|
|
||||||
for result in results:
|
|
||||||
appendresult = False
|
|
||||||
cururl = result[bot.memory["SpiceBot_GifSearch"]['valid_gif_api_dict'][currentapi]['cururl']]
|
|
||||||
slashsplit = str(cururl).split("/")
|
|
||||||
fileextension = slashsplit[-1]
|
|
||||||
if not fileextension or fileextension == '':
|
|
||||||
appendresult = True
|
|
||||||
elif str(fileextension).endswith(".gif"):
|
|
||||||
appendresult = True
|
|
||||||
elif "." not in str(fileextension):
|
|
||||||
appendresult = True
|
|
||||||
if appendresult:
|
|
||||||
resultsarray.append(cururl)
|
|
||||||
|
|
||||||
# make sure there are results
|
|
||||||
if len(resultsarray):
|
|
||||||
|
|
||||||
# Create Temp dict for every result
|
|
||||||
tempresultnum = 0
|
|
||||||
for tempresult in resultsarray:
|
|
||||||
if tempresult not in bot.memory["SpiceBot_GifSearch"]["badgiflinks"]:
|
|
||||||
tempresultnum += 1
|
|
||||||
tempdict = dict()
|
|
||||||
tempdict["returnnum"] = tempresultnum
|
|
||||||
tempdict["returnurl"] = tempresult
|
|
||||||
tempdict["gifapi"] = currentapi
|
|
||||||
bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])].append(tempdict)
|
|
||||||
|
|
||||||
else:
|
|
||||||
verifygoodlinks = []
|
|
||||||
for gifresult in bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])]:
|
|
||||||
if gifresult["returnurl"] not in bot.memory["SpiceBot_GifSearch"]["badgiflinks"]:
|
|
||||||
verifygoodlinks.append(gifresult)
|
|
||||||
bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])] = verifygoodlinks
|
|
||||||
|
|
||||||
if len(bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])]):
|
|
||||||
gifapiresults.extend(bot.memory["SpiceBot_GifSearch"]['cache'][currentapi][str(searchdict["searchquery"])])
|
|
||||||
|
|
||||||
if not len(gifapiresults):
|
|
||||||
return {"error": "No Results were found for '" + searchdict["query"] + "' in the " + str(spicemanip.main(searchdict['gifsearch'], 'orlist')) + " api(s)"}
|
|
||||||
|
|
||||||
random.shuffle(gifapiresults)
|
|
||||||
random.shuffle(gifapiresults)
|
|
||||||
randombad = True
|
|
||||||
while randombad:
|
|
||||||
gifdict = spicemanip.main(gifapiresults, "random")
|
|
||||||
|
|
||||||
try:
|
|
||||||
gifpage = requests.get(gifdict["returnurl"], headers=None)
|
|
||||||
except Exception as e:
|
|
||||||
gifpage = str(e)
|
|
||||||
gifpage = None
|
|
||||||
|
|
||||||
if gifpage and not str(gifpage.status_code).startswith(tuple(["4", "5"])):
|
|
||||||
randombad = False
|
|
||||||
else:
|
|
||||||
bot.memory["SpiceBot_GifSearch"]["badgiflinks"].append(str(gifdict["returnurl"]))
|
|
||||||
newlist = []
|
|
||||||
for tempdict in gifapiresults:
|
|
||||||
if tempdict["returnurl"] != gifdict["returnurl"]:
|
|
||||||
newlist.append(tempdict)
|
|
||||||
gifapiresults = newlist
|
|
||||||
|
|
||||||
if not len(gifapiresults):
|
|
||||||
return {"error": "No Results were found for '" + searchdict["query"] + "' in the " + str(spicemanip.main(searchdict['gifsearch'], 'orlist')) + " api(s)"}
|
|
||||||
|
|
||||||
# return dict
|
|
||||||
gifdict['error'] = None
|
|
||||||
return gifdict
|
|
||||||
Loading…
Reference in New Issue
Block a user