Corrected issue with wiki searching, that caused wrong articles to be pulled
This commit is contained in:
parent
8919b1210c
commit
71c0d499c6
1 changed files with 35 additions and 28 deletions
|
@ -1,11 +1,13 @@
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from .utils import config
|
from .utils import config
|
||||||
from .utils import checks
|
from .utils import checks
|
||||||
|
|
||||||
|
import discord
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import json
|
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
class Links:
|
class Links:
|
||||||
"""This class contains all the commands that make HTTP requests
|
"""This class contains all the commands that make HTTP requests
|
||||||
In other words, all commands here rely on other URL's to complete their requests"""
|
In other words, all commands here rely on other URL's to complete their requests"""
|
||||||
|
@ -22,7 +24,7 @@ class Links:
|
||||||
"""Pulls the top match for a specific term, and returns the definition"""
|
"""Pulls the top match for a specific term, and returns the definition"""
|
||||||
# All we need to do is search for the term provided, so the action list and format never need to change
|
# All we need to do is search for the term provided, so the action list and format never need to change
|
||||||
base_url = "https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch="
|
base_url = "https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch="
|
||||||
async with self.session.get("{}/{}".format(base_url, query), headers=self.headers) as r:
|
async with self.session.get("{}{}".format(base_url, query.replace(" ", "%20")), headers=self.headers) as r:
|
||||||
data = await r.json()
|
data = await r.json()
|
||||||
if len(data['query']['search']) == 0:
|
if len(data['query']['search']) == 0:
|
||||||
await self.bot.say("I could not find any results with that term, I tried my best :c")
|
await self.bot.say("I could not find any results with that term, I tried my best :c")
|
||||||
|
@ -37,7 +39,9 @@ class Links:
|
||||||
snippet = re.sub('</span>', '', snippet)
|
snippet = re.sub('</span>', '', snippet)
|
||||||
snippet = re.sub('"', '"', snippet)
|
snippet = re.sub('"', '"', snippet)
|
||||||
|
|
||||||
await self.bot.say("Here is the best match I found with the query `{}`:\nURL: {}\nSnippet: \n```\n{}```".format(query, url, snippet))
|
await self.bot.say(
|
||||||
|
"Here is the best match I found with the query `{}`:\nURL: {}\nSnippet: \n```\n{}```".format(query, url,
|
||||||
|
snippet))
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
@checks.custom_perms(send_messages=True)
|
@checks.custom_perms(send_messages=True)
|
||||||
|
@ -85,7 +89,8 @@ class Links:
|
||||||
# Find a random image based on the first page of results.
|
# Find a random image based on the first page of results.
|
||||||
# Currently derpibooru provides no way to change how many results can be shown on one page
|
# Currently derpibooru provides no way to change how many results can be shown on one page
|
||||||
# Nor anyway to see how many pages are returned by a certain query
|
# Nor anyway to see how many pages are returned by a certain query
|
||||||
# Due to the fact that a query may only return one page, we cannot try to check more than one as it might fail
|
# Due to the fact that a query may only return one page
|
||||||
|
# We cannot try to check more than one as it might fail
|
||||||
# So this is the best that we can do at the moment
|
# So this is the best that we can do at the moment
|
||||||
if len(results) > 0:
|
if len(results) > 0:
|
||||||
index = random.SystemRandom().randint(0, len(results) - 1)
|
index = random.SystemRandom().randint(0, len(results) - 1)
|
||||||
|
@ -101,7 +106,6 @@ class Links:
|
||||||
image_link = r.url
|
image_link = r.url
|
||||||
await self.bot.say(image_link)
|
await self.bot.say(image_link)
|
||||||
|
|
||||||
|
|
||||||
@commands.command(pass_context=True)
|
@commands.command(pass_context=True)
|
||||||
@checks.custom_perms(send_messages=True)
|
@checks.custom_perms(send_messages=True)
|
||||||
async def e621(self, ctx, *, tags: str):
|
async def e621(self, ctx, *, tags: str):
|
||||||
|
@ -109,7 +113,8 @@ class Links:
|
||||||
Format for the search terms need to be 'search term 1, search term 2, etc.'
|
Format for the search terms need to be 'search term 1, search term 2, etc.'
|
||||||
If the channel the command is ran in, is registered as a nsfw channel, this image will be explicit"""
|
If the channel the command is ran in, is registered as a nsfw channel, this image will be explicit"""
|
||||||
|
|
||||||
# This changes the formatting for queries, so we don't have to use e621's stupid formatting when using the command
|
# This changes the formatting for queries, so we don't
|
||||||
|
# Have to use e621's stupid formatting when using the command
|
||||||
tags = tags.replace(' ', '_')
|
tags = tags.replace(' ', '_')
|
||||||
tags = tags.replace(',_', '%20')
|
tags = tags.replace(',_', '%20')
|
||||||
url = 'https://e621.net/post/index.json?limit=320&tags={}'.format(tags)
|
url = 'https://e621.net/post/index.json?limit=320&tags={}'.format(tags)
|
||||||
|
@ -119,7 +124,8 @@ class Links:
|
||||||
await self.bot.say("Looking up an image with those tags....")
|
await self.bot.say("Looking up an image with those tags....")
|
||||||
|
|
||||||
nsfw_channels = config.get_content("nsfw_channels") or {}
|
nsfw_channels = config.get_content("nsfw_channels") or {}
|
||||||
# e621 by default does not filter explicit content, so tack on safe/explicit based on if this channel is nsfw or not
|
# e621 by default does not filter explicit content, so tack on
|
||||||
|
# safe/explicit based on if this channel is nsfw or not
|
||||||
if ctx.message.channel.id in nsfw_channels:
|
if ctx.message.channel.id in nsfw_channels:
|
||||||
url += "%20rating:explicit"
|
url += "%20rating:explicit"
|
||||||
else:
|
else:
|
||||||
|
@ -139,5 +145,6 @@ class Links:
|
||||||
rand_image = data[random.SystemRandom().randint(0, len(data) - 1)]['file_url']
|
rand_image = data[random.SystemRandom().randint(0, len(data) - 1)]['file_url']
|
||||||
await self.bot.say(rand_image)
|
await self.bot.say(rand_image)
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
bot.add_cog(Links(bot))
|
bot.add_cog(Links(bot))
|
||||||
|
|
Loading…
Reference in a new issue