1
0
Fork 0
mirror of synced 2024-06-28 19:20:34 +12:00
Bonfire/cogs/links.py

105 lines
4.3 KiB
Python
Raw Normal View History

2016-07-23 23:59:02 +12:00
from discord.ext import commands
from .utils import config
from .utils import checks
2016-07-25 01:49:05 +12:00
import aiohttp
2016-07-23 23:59:02 +12:00
import json
import random
2016-07-23 23:59:02 +12:00
class Links:
"""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"""
def __init__(self, bot):
2016-07-24 00:00:21 +12:00
self.bot = bot
2016-07-23 23:59:02 +12:00
@commands.command()
@checks.custom_perms(send_messages=True)
2016-07-23 23:59:02 +12:00
async def urban(self, *msg: str):
"""Pulls the top urbandictionary.com definition for a term"""
2016-07-26 02:30:20 +12:00
url = "http://api.urbandictionary.com/v0/define?term={}".format('+'.join(msg))
with aiohttp.ClientSession() as s:
async with s.get(url) as r:
response = await r.text()
data = json.loads(response)
2016-07-25 01:49:05 +12:00
2016-07-25 02:02:50 +12:00
try:
2016-07-23 23:59:02 +12:00
if len(data['list']) == 0:
await self.bot.say("No result with that term!")
else:
await self.bot.say(data['list'][0]['definition'])
except discord.HTTPException:
await self.bot.say('```Error: Definition is too long for me to send```')
@commands.command(pass_context=True)
@checks.custom_perms(send_messages=True)
2016-07-23 23:59:02 +12:00
async def derpi(self, ctx, *search: str):
"""Provides a random image from the first page of derpibooru.org for the following term"""
if len(search) > 0:
# This sets the url as url?q=search+terms
url = 'https://derpibooru.org/search.json?q={}'.format('+'.join(search))
nsfw_channels = config.get_content("nsfw_channels") or {}
if ctx.message.channel.id in nsfw_channels:
2016-07-23 23:59:02 +12:00
url += ",+explicit&filter_id=95938"
2016-07-28 23:50:05 +12:00
# Get the response from derpibooru and parse the 'search' result from it
2016-07-25 01:51:04 +12:00
with aiohttp.ClientSession() as s:
2016-07-25 01:49:05 +12:00
async with s.get(url) as r:
response = await r.text()
data = json.loads(response)
try:
results = data['search']
except KeyError:
await self.bot.say("No results with that search term, {0}!".format(ctx.message.author.mention))
return
2016-07-23 23:59:02 +12:00
# Get the link if it exists, if not return saying no results found
if len(results) > 0:
index = random.SystemRandom().randint(0, len(results) - 1)
2016-07-23 23:59:02 +12:00
imageLink = 'http://{}'.format(results[index].get('representations').get('full')[2:].strip())
else:
await self.bot.say("No results with that search term, {0}!".format(ctx.message.author.mention))
return
else:
# If no search term was provided, search for a random image
with aiohttp.ClientSession() as s:
async with s.get('https://derpibooru.org/images/random') as r:
imageLink = r.url
2016-07-23 23:59:02 +12:00
await self.bot.say(imageLink)
@commands.command(pass_context=True)
@checks.custom_perms(send_messages=True)
2016-07-23 23:59:02 +12:00
async def e621(self, ctx, *, tags: str):
"""Searches for a random image from e621.net
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"""
tags = tags.replace(' ', '_')
tags = tags.replace(',_', '%20')
url = 'https://e621.net/post/index.json?limit=320&tags={}'.format(tags)
await self.bot.say("Looking up an image with those tags....")
nsfw_channels = config.get_content("nsfw_channels") or {}
if ctx.message.channel.id in nsfw_channels:
2016-07-23 23:59:02 +12:00
url += "%20rating:explicit"
else:
url += "%20rating:safe"
2016-07-25 01:50:19 +12:00
2016-07-25 01:51:04 +12:00
with aiohttp.ClientSession() as s:
2016-07-25 01:49:05 +12:00
async with s.get(url) as r:
response = await r.text()
data = json.loads(response)
if len(data) == 0:
await self.bot.say("No results with that image {}".format(ctx.message.author.mention))
return
else:
2016-07-28 23:50:05 +12:00
if len(data) == 1:
rand_image = data[0]['file_url']
else:
rand_image = data[random.SystemRandom().randint(0, len(data)-1)]['file_url']
2016-07-25 01:49:05 +12:00
await self.bot.say(rand_image)
2016-07-24 00:01:16 +12:00
def setup(bot):
bot.add_cog(Links(bot))