1
0
Fork 0
mirror of synced 2024-05-19 03:52:25 +12:00

Move tasks into their own methods, to try to gc better

This commit is contained in:
phxntxm 2018-09-22 11:17:11 -05:00
parent 259da83950
commit c7742a8023
5 changed files with 172 additions and 140 deletions

View file

@ -1,18 +1,22 @@
import discord
import pendulum
import asyncio
import traceback
from pendulum.parsing.exceptions import ParserError
from discord.ext import commands
from . import utils
class Birthday:
def __init__(self, bot):
self.bot = bot
self.bot.loop.create_task(self.birthday_task())
tzmap = {
'us-central': pendulum.timezone('US/Central'),
'eu-central': pendulum.timezone('Europe/Paris'),
'hongkong': pendulum.timezone('Hongkong'),
def sort_birthdays(self, bds):
}
def sort_birthdays(bds):
# First sort the birthdays based on the comparison of the actual date
bds = sorted(bds, key=lambda x: x['birthday'])
# We want to split this into birthdays after and before todays date
@ -31,6 +35,12 @@ class Birthday:
# So all we need to do is put them in order all of "laters" then all of "befores"
return later_bds + previous_bds
class Birthday:
def __init__(self, bot):
self.bot = bot
self.task = self.bot.loop.create_task(self.birthday_task())
def get_birthdays_for_server(self, server, today=False):
bds = self.bot.db.load('birthdays')
# Get a list of the ID's to compare against
@ -46,6 +56,7 @@ class Birthday:
continue
day = pendulum.parse(bd['birthday'])
# tz = tzmap.get(server.region)
# Check if it's today, and we want to only get todays birthdays
if (today and day.date() == pendulum.today().date()) or not today:
# If so, get the member and add them to the entry
@ -55,11 +66,17 @@ class Birthday:
'member': member
})
return self.sort_birthdays(_entries)
return sort_birthdays(_entries)
async def birthday_task(self):
while True:
try:
await self.notify_birthdays()
except Exception as error:
with open("error_log", 'a') as f:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
finally:
# Every 12 hours, this is not something that needs to happen often
await asyncio.sleep(60 * 60 * 12)

View file

@ -2,20 +2,18 @@ import asyncio
import discord
import re
import traceback
import logging
from discord.ext import commands
from . import utils
log = logging.getLogger()
BASE_URL = 'https://api.picarto.tv/v1'
class Picarto:
def __init__(self, bot):
self.bot = bot
self.bot.loop.create_task(self.check_channels())
self.task = self.bot.loop.create_task(self.picarto_task())
# noinspection PyAttributeOutsideInit
async def get_online_users(self):
@ -69,11 +67,19 @@ class Picarto:
channel = re.search("(?<=picarto.tv/)(.*)", channel).group(1)
return channel.lower() in [stream['name'].lower() for stream in self.online_channels]
async def check_channels(self):
await self.bot.wait_until_ready()
# This is a loop that runs every 30 seconds, checking if anyone has gone online
async def picarto_task(self):
try:
await self.bot.wait_until_ready()
while not self.bot.is_closed():
await self.check_channels()
except Exception as error:
with open("error_log", 'a') as f:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
finally:
await asyncio.sleep(30)
async def check_channels(self):
await self.get_online_users()
picarto = await self.bot.db.actual_load('picarto', table_filter={'notifications_on': 1})
for data in picarto:
@ -122,12 +128,6 @@ class Picarto:
except (discord.Forbidden, discord.HTTPException, AttributeError):
pass
await asyncio.sleep(30)
except Exception as e:
tb = traceback.format_exc()
fmt = "{1}\n{0.__class__.__name__}: {0}".format(tb, e)
log.error(fmt)
@commands.group(invoke_without_command=True)
@utils.custom_perms(send_messages=True)
@utils.check_restricted()

View file

@ -23,6 +23,7 @@ class Raffle:
with open("error_log", 'a') as f:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
finally:
await asyncio.sleep(60)
async def check_raffles(self):

View file

@ -1,5 +1,6 @@
import asyncio
import aiohttp
import traceback
from discord.ext import commands
from base64 import urlsafe_b64encode
@ -20,9 +21,21 @@ class Spotify:
urlsafe_b64encode(self._authorization.encode()).decode()
)
}
self.bot.loop.create_task(self.api_token_task())
self.task = self.bot.loop.create_task(self.api_token_task())
async def api_token_task(self):
while True:
try:
delay = await self.get_api_token()
except Exception as error:
with open("error_log", 'a') as f:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
delay = 2400
finally:
await asyncio.sleep(delay)
async def get_api_token(self):
url = "https://accounts.spotify.com/api/token"
opts = {"grant_type": "client_credentials"}
while True:
@ -30,6 +43,7 @@ class Spotify:
response = await session.post(url, data=opts)
data = await response.json()
self._token = data.get("access_token")
return data.get("expires_in")
await asyncio.sleep(data.get("expires_in", 2400))

View file

@ -2,11 +2,9 @@ from discord.ext import commands
from . import utils
import aiohttp
import asyncio
import discord
import re
import rethinkdb as r
import traceback
import logging
@ -23,6 +21,8 @@ class Twitch:
self.key = utils.twitch_key
self.params = {'client_id': self.key}
self.task = bot.loop.create_task(self.check_channels())
def _form_embed(self, data):
if not data:
return None
@ -82,11 +82,19 @@ class Twitch:
data = await utils.request(url, payload=self.params)
return self._form_embed(data)
async def check_channels(self):
async def twitch_task(self):
await self.bot.wait_until_ready()
# This is a loop that runs every 30 seconds, checking if anyone has gone online
try:
while not self.bot.is_closed():
try:
await self.check_channels()
except Exception as error:
with open("error_log", 'a') as f:
traceback.print_tb(error.__traceback__, file=f)
print('{0.__class__.__name__}: {0}'.format(error), file=f)
finally:
await asyncio.sleep(30)
async def check_channels(self):
twitch = await self.bot.db.actual_load('twitch', table_filter={'notifications_on': 1})
for data in twitch:
m_id = int(data['member_id'])
@ -134,12 +142,6 @@ class Twitch:
except (discord.Forbidden, discord.HTTPException, AttributeError):
pass
await asyncio.sleep(30)
except Exception as e:
tb = traceback.format_exc()
fmt = "{1}\n{0.__class__.__name__}: {0}".format(tb, e)
log.error(fmt)
@commands.group(invoke_without_command=True)
@commands.guild_only()
@utils.custom_perms(send_messages=True)
@ -329,6 +331,4 @@ class Twitch:
def setup(bot):
t = Twitch(bot)
bot.loop.create_task(t.check_channels())
bot.add_cog(Twitch(bot))