1
0
Fork 0
mirror of synced 2024-06-28 11:10:27 +12:00

Corrected all issues with the da RSS feed

This commit is contained in:
phxntxm 2016-10-08 02:45:24 -05:00
parent 0ab813f834
commit e529bb8804

View file

@ -1,33 +1,33 @@
import aiohttp import aiohttp
import asyncio import asyncio
import discord import discord
import re
import rethinkdb as r
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
class Deviantart: class Deviantart:
def __init__(self, bot): def __init__(self, bot):
self.base_url = "https://www.deviantart.com/api/v1/oauth2/gallery/all" self.base_url = "https://www.deviantart.com/api/v1/oauth2/gallery/all"
self.bot = bot self.bot = bot
self.headers = {"User-Agent": "Bonfire/1.0.0"} self.headers = {"User-Agent": "Bonfire/1.0.0"}
self.session = aiohttp.ClientSession() self.session = aiohttp.ClientSession()
self.token = None
self.params = None
bot.loop.create_task(self.token_task()) bot.loop.create_task(self.token_task())
bot.loop.create_task(self.post_task()) bot.loop.create_task(self.post_task())
async def token_task(self): async def token_task(self):
while(True): while True:
expires_in = await self.get_token() expires_in = await self.get_token()
await asyncio.sleep(expires_in) await asyncio.sleep(expires_in)
async def post_task(self): async def post_task(self):
await asyncio.sleep(5) await asyncio.sleep(5)
# Lets start the task a few seconds after, to ensure our token gets set # Lets start the task a few seconds after, to ensure our token gets set
while(True): while True:
expires_in = await self.check_posts() await self.check_posts()
await asyncio.sleep(300) await asyncio.sleep(300)
async def get_token(self): async def get_token(self):
@ -55,7 +55,8 @@ class Deviantart:
for entry in content: for entry in content:
user = discord.utils.get(self.bot.get_all_members(), id=entry['member_id']) user = discord.utils.get(self.bot.get_all_members(), id=entry['member_id'])
# If we're sharded, we might not be able to find this user. If the bot is not in the server with the member either # If we're sharded, we might not be able to find this user.
# If the bot is not in the server with the member either
if user is None: if user is None:
continue continue
@ -78,14 +79,15 @@ class Deviantart:
# This means that our last update to this user, for this author, is not the same # This means that our last update to this user, for this author, is not the same
if last_updated_id != result['deviationid']: if last_updated_id != result['deviationid']:
# First lets check if the last updated ID was None, if so...then we haven't alerted them yet # First lets check if the last updated ID was None, if so...then we haven't alerted them yet
# We don't want to alert them, we just want to act like the artist's most recent update was the last notified # We don't want to alert them in this case
# We just want to act like the artist's most recent update was the last notified
# So just notify the user if this is not None # So just notify the user if this is not None
if last_updated_id is not None: if last_updated_id is not None:
fmt = "**Title:** {}\n**User:** {}\n**Category:** {}\n**URL:** {}".format( fmt = "There has been a new post by an artist you are subscribed to!\n\n" \
title = result['title'], "**Title:** {}\n**User:** {}\n**URL:** {}".format(
artist = result['author']['username'], result['title'],
url = result['url'], result['author']['username'],
category = result['category']) result['url'])
await self.bot.send_message(user, fmt) await self.bot.send_message(user, fmt)
# Now we can update the user's last updated for this DA # Now we can update the user's last updated for this DA
# We want to do this whether or not our last if statement was met # We want to do this whether or not our last if statement was met
@ -93,8 +95,6 @@ class Deviantart:
update = {'last_updated': {da_name: result['deviationid']}} update = {'last_updated': {da_name: result['deviationid']}}
await config.update_content('deviantart', update, r_filter) await config.update_content('deviantart', update, r_filter)
@commands.group() @commands.group()
@checks.custom_perms(send_messages=True) @checks.custom_perms(send_messages=True)
async def da(self): async def da(self):
@ -112,14 +112,15 @@ class Deviantart:
# TODO: Ensure the user provided is a real user # TODO: Ensure the user provided is a real user
if content is None: if content is None:
entry = {'member_id': ctx.message.author.id, 'subbed': [username], 'last_entry': {}} entry = {'member_id': ctx.message.author.id, 'subbed': [username], 'last_updated': {}}
await config.add_content('deviantart', entry, r_filter) await config.add_content('deviantart', entry, r_filter)
await self.bot.say("You have just subscribed to {}!".format(username)) await self.bot.say("You have just subscribed to {}!".format(username))
elif content[0]['subbed'] is None or username not in content[0]['subbed']: elif content[0]['subbed'] is None or username not in content[0]['subbed']:
if content[0]['subbed'] is None: if content[0]['subbed'] is None:
sub_list = [username] sub_list = [username]
else: else:
sub_list = content[0]['subbed'].append(username) content[0]['subbed'].append(username)
sub_list = content[0]['subbed']
await config.update_content('deviantart', {'subbed': sub_list}, r_filter) await config.update_content('deviantart', {'subbed': sub_list}, r_filter)
await self.bot.say("You have just subscribed to {}!".format(username)) await self.bot.say("You have just subscribed to {}!".format(username))
else: else:
@ -135,11 +136,12 @@ class Deviantart:
if content is None or content[0]['subbed'] is None: if content is None or content[0]['subbed'] is None:
await self.bot.say("You are not subscribed to anyone at the moment!") await self.bot.say("You are not subscribed to anyone at the moment!")
elif username in content[0]['subbed']: elif username in content[0]['subbed']:
sub_list = content[0]['subbed'].remove(username) content[0]['subbed'].remove(username)
await config.update_content('deviantart', {'subbed': sub_list}, r_filter) await config.update_content('deviantart', {'subbed': content[0]['subbed']}, r_filter)
await self.bot.say("You have just unsubscribed from {}!".format(username)) await self.bot.say("You have just unsubscribed from {}!".format(username))
else: else:
await self.bot.say("You are not subscribed to that user!") await self.bot.say("You are not subscribed to that user!")
def setup(bot): def setup(bot):
bot.add_cog(Deviantart(bot)) bot.add_cog(Deviantart(bot))