mirror of
https://github.com/SpiceBot/SpiceBot.git
synced 2026-11-10 21:50:59 -05:00
first test of gif class system
This commit is contained in:
parent
1e2098ceb8
commit
383ee1a26b
@ -6,5 +6,4 @@ include *requirements.txt
|
||||
recursive-include tests *
|
||||
recursive-exclude * __pycache__
|
||||
recursive-exclude * *.py[co]
|
||||
recursive-include sopel_modules/SpiceBot_GifSearch/gifapi *
|
||||
recursive-include sopel_modules/SpiceBot_Configs/aiml *
|
||||
recursive-include sopel_modules/SpiceBot_Configs/* *
|
||||
|
||||
233
sopel_modules/SpiceBot/Gif.py
Normal file
233
sopel_modules/SpiceBot/Gif.py
Normal file
@ -0,0 +1,233 @@
|
||||
# coding=utf8
|
||||
from __future__ import unicode_literals, absolute_import, division, print_function
|
||||
"""How to handle gifs"""
|
||||
|
||||
from sopel.config.types import StaticSection, ValidatedAttribute
|
||||
import sopel_modules
|
||||
|
||||
from .Config import config as botconfig
|
||||
from .Tools import read_directory_json_to_dict
|
||||
from .Commands import config 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 = ValidatedAttribute('extra', default=None)
|
||||
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)
|
||||
gif_api_config = eval("botconfig." + gif_api)
|
||||
gif_api_config.configure_setting('apikey', 'GIF API Client ID-' + gif_api)
|
||||
|
||||
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
|
||||
if searchdict["searchquery"].lower() in list(self.valid_api[currentapi]["cache"].keys()):
|
||||
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
|
||||
else:
|
||||
# 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()
|
||||
@ -19,6 +19,7 @@ class SpiceBot_Google_MainSection(StaticSection):
|
||||
class Google():
|
||||
|
||||
def __init__(self):
|
||||
self.header = {'User-Agent': str(UserAgent().chrome)}
|
||||
self.setup_google()
|
||||
self.cache = {
|
||||
"info": {},
|
||||
@ -55,12 +56,11 @@ class Google():
|
||||
return returnurl
|
||||
|
||||
def search_info(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/search?q=' + lookfor + '&btnI', headers=header)
|
||||
var = requests.get(r'http://www.google.com/search?q=' + lookfor + '&btnI', headers=self.header)
|
||||
except Exception as e:
|
||||
var = e
|
||||
var = None
|
||||
@ -71,12 +71,11 @@ class Google():
|
||||
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)
|
||||
var = requests.get(r'http://www.google.com/maps/place/' + lookfor, headers=self.header)
|
||||
except Exception as e:
|
||||
var = e
|
||||
var = None
|
||||
@ -87,12 +86,11 @@ class Google():
|
||||
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)
|
||||
var = requests.get(r'https://www.youtube.com/search?q=' + lookfor + '&btnI', headers=self.header)
|
||||
except Exception as e:
|
||||
var = e
|
||||
var = None
|
||||
|
||||
@ -23,6 +23,7 @@ from .Channels import *
|
||||
from .Users import *
|
||||
from .AI import *
|
||||
from .Google import *
|
||||
from .Gif import *
|
||||
|
||||
|
||||
from .configure import *
|
||||
|
||||
@ -7,6 +7,7 @@ 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 .Gif import SpiceBot_Gif_MainSection
|
||||
from .Kick import SpiceBot_Kick
|
||||
from .osd import SpiceBot_OSD
|
||||
|
||||
@ -44,3 +45,7 @@ def configure(config):
|
||||
|
||||
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')
|
||||
|
||||
@ -17,6 +17,7 @@ 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
|
||||
from .Gif import SpiceBot_Gif_MainSection
|
||||
from .AI import SpiceBot_AI_MainSection
|
||||
|
||||
|
||||
@ -52,6 +53,8 @@ def setup(bot):
|
||||
# startupmonologue
|
||||
setup_startupmonologue(bot)
|
||||
|
||||
setup_gif(bot)
|
||||
|
||||
|
||||
def setup_config(bot):
|
||||
logs.log('SpiceBot_Config', "Setting Up Configuration")
|
||||
@ -138,3 +141,7 @@ def setup_kick(bot):
|
||||
logs.log('SpiceBot_Kick', "Checking for config settings")
|
||||
bot.config.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)
|
||||
|
||||
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"])))
|
||||
@ -18,6 +18,7 @@ from .Docs import *
|
||||
from .BotPrivs import *
|
||||
from .Search import *
|
||||
from .AI import *
|
||||
from .Gif import *
|
||||
|
||||
__author__ = 'Sam Zick'
|
||||
__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