Updated picarto to work with rewrite
This commit is contained in:
parent
9caf781289
commit
c1c1a7c20f
1 changed files with 49 additions and 76 deletions
125
cogs/picarto.py
125
cogs/picarto.py
|
@ -17,99 +17,72 @@ BASE_URL = 'https://ptvappapi.picarto.tv'
|
||||||
api_key = '03e26294-b793-11e5-9a41-005056984bd4'
|
api_key = '03e26294-b793-11e5-9a41-005056984bd4'
|
||||||
|
|
||||||
|
|
||||||
async def online_users():
|
|
||||||
try:
|
|
||||||
# Someone from picarto contacted me and told me their database queries are odd
|
|
||||||
# It is more efficent on their end to make a query for all online users, and base checks off that
|
|
||||||
# In place of requesting for /channel and checking if that is online currently, for each channel
|
|
||||||
# This method is in place to just return all online_users
|
|
||||||
url = BASE_URL + '/online/all'
|
|
||||||
payload = {'key': api_key}
|
|
||||||
return await utils.request(url, payload=payload)
|
|
||||||
except:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
|
||||||
def check_online(online_channels, channel):
|
|
||||||
# online_channels is the dictionary of all users online currently
|
|
||||||
# And channel is the name we are checking against that
|
|
||||||
# This creates a list of all users that match this channel name (should only ever be 1)
|
|
||||||
# And returns True as long as it is more than 0
|
|
||||||
matches = [stream for stream in online_channels if stream['channel_name'].lower() == channel.lower()]
|
|
||||||
return len(matches) > 0
|
|
||||||
|
|
||||||
|
|
||||||
class Picarto:
|
class Picarto:
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
|
||||||
|
async def get_online_users(self):
|
||||||
|
# This method is in place to just return all online users so we can compare against it
|
||||||
|
url = BASE_URL + '/online/all'
|
||||||
|
payload = {'key': api_key}
|
||||||
|
self.online_channels = await utils.request(url, payload=payload)
|
||||||
|
|
||||||
|
def channel_online(self, channel):
|
||||||
|
# online_channels is the dictionary of all users online currently
|
||||||
|
# And channel is the name we are checking against that
|
||||||
|
# This creates a list of all users that match this channel name (should only ever be 1)
|
||||||
|
# And returns True as long as it is more than 0
|
||||||
|
matches = [stream for stream in self.online_channels if stream['channel_name'].lower() == channel.lower()]
|
||||||
|
return len(matches) > 0
|
||||||
|
|
||||||
async def check_channels(self):
|
async def check_channels(self):
|
||||||
await self.bot.wait_until_ready()
|
await self.bot.wait_until_ready()
|
||||||
# This is a loop that runs every 30 seconds, checking if anyone has gone online
|
# This is a loop that runs every 30 seconds, checking if anyone has gone online
|
||||||
try:
|
try:
|
||||||
while not self.bot.is_closed:
|
while not self.bot.is_closed():
|
||||||
r_filter = {'notifications_on': 1}
|
picarto = await utils.filter_content('picarto', {'notifications_on': 1})
|
||||||
picarto = await utils.filter_content('picarto', r_filter)
|
for data in picarto:
|
||||||
# Get all online users before looping, so that only one request is needed
|
m_id = int(data['member_id'])
|
||||||
online_users_list = await online_users()
|
url = data['picarto_url']
|
||||||
old_online_users = {data['member_id']: data for data in picarto if data['live']}
|
# Check if they are online
|
||||||
old_offline_users = {data['member_id']: data for data in picarto if not data['live']}
|
online = await self.channel_online(url)
|
||||||
|
# If they're currently online, but saved as not then we'll send our notification
|
||||||
for m_id, result in old_offline_users.items():
|
if online and data['live'] == 0:
|
||||||
m_id = int(m_id)
|
for s_id in data['servers']:
|
||||||
# Get their url and their user based on that url
|
server = self.bot.get_guild(int(s_id))
|
||||||
url = result['picarto_url']
|
if server is None:
|
||||||
user = re.search("(?<=picarto.tv/)(.*)", url).group(1)
|
|
||||||
# Check if they are online right now
|
|
||||||
if check_online(online_users_list, user):
|
|
||||||
for guild_id in result['servers']:
|
|
||||||
guild_id = int(guild_id)
|
|
||||||
# Get the channel to send the message to, based on the saved alert's channel
|
|
||||||
guild = self.bot.get_guild(guild_id)
|
|
||||||
if guild is None:
|
|
||||||
continue
|
continue
|
||||||
guild_alerts = await utils.get_content('server_alerts', {'server_id': guild_id})
|
member = server.get_member(m_id)
|
||||||
try:
|
|
||||||
channel_id = guild_alerts['channel_id']
|
|
||||||
except (IndexError, TypeError):
|
|
||||||
channel_id = guild_id
|
|
||||||
channel = self.bot.get_channel(channel_id)
|
|
||||||
# Get the member that has just gone live
|
|
||||||
member = guild.get_member(m_id)
|
|
||||||
if member is None:
|
if member is None:
|
||||||
continue
|
continue
|
||||||
|
server_settings = await utils.get_content('server_settings', s_id)
|
||||||
fmt = "{} has just gone live! View their stream at {}".format(member.display_name, url)
|
if server_settings is not None:
|
||||||
await channel.send(fmt)
|
channel_id = int(server_settings.get('notification_channel', s_id))
|
||||||
await utils.update_content('picarto', {'live': 1}, {'member_id': m_id})
|
else:
|
||||||
for m_id, result in old_online_users.items():
|
channel_id = int(s_id)
|
||||||
m_id = int(m_id)
|
channel = server.get_channel(channel_id)
|
||||||
# Get their url and their user based on that url
|
await channel.send("{} has just gone live! View their stream at <{}>".format(member.display_name, data['picarto_url']))
|
||||||
url = result['picarto_url']
|
self.bot.loop.create_task(utils.update_content('picarto', {'live': 1}, str(m_id)))
|
||||||
user = re.search("(?<=picarto.tv/)(.*)", url).group(1)
|
elif not online and data['live'] == 1:
|
||||||
# Check if they are online right now
|
for s_id in data['servers']:
|
||||||
if not check_online(online_users_list, user):
|
server = self.bot.get_guild(int(s_id))
|
||||||
for guild_id in result['servers']:
|
if server is None:
|
||||||
guild_id = int(guild_id)
|
|
||||||
# Get the channel to send the message to, based on the saved alert's channel
|
|
||||||
guild = self.bot.get_guild(guild_id)
|
|
||||||
if guild is None:
|
|
||||||
continue
|
continue
|
||||||
guild_alerts = await utils.get_content('server_alerts', {'server_id': guild_id})
|
member = server.get_member(m_id)
|
||||||
try:
|
|
||||||
channel_id = guild_alerts['channel_id']
|
|
||||||
except (IndexError, TypeError):
|
|
||||||
channel_id = guild_id
|
|
||||||
channel = self.bot.get_channel(channel_id)
|
|
||||||
# Get the member that has just gone live
|
|
||||||
member = guild.get_member(m_id)
|
|
||||||
if member is None:
|
if member is None:
|
||||||
continue
|
continue
|
||||||
|
server_settings = await utils.get_content('server_settings', s_id)
|
||||||
fmt = "{} has just gone offline! Catch them next time they stream at {}".format(
|
if server_settings is not None:
|
||||||
member.display_name, url)
|
channel_id = int(server_settings.get('notification_channel', s_id))
|
||||||
await channel.send(fmt)
|
else:
|
||||||
await utils.update_content('picarto', {'live': 0}, {'member_id': m_id})
|
channel_id = int(s_id)
|
||||||
|
channel = server.get_channel(channel_id)
|
||||||
|
await channel.send("{} has just gone offline! View their stream next time at <{}>".format(member.display_name, data['picarto_url']))
|
||||||
|
self.bot.loop.create_task(utils.update_content('picarto', {'live': 0}, str(m_id)))
|
||||||
await asyncio.sleep(30)
|
await asyncio.sleep(30)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
tb = traceback.format_exc()
|
tb = traceback.format_exc()
|
||||||
|
@ -181,7 +154,7 @@ class Picarto:
|
||||||
# Otherwise if it doesn't match, we'll hit an AttributeError due to .group(0)
|
# Otherwise if it doesn't match, we'll hit an AttributeError due to .group(0)
|
||||||
# This means that the url was just given as a user (or something complete invalid)
|
# This means that the url was just given as a user (or something complete invalid)
|
||||||
# So set URL as https://www.picarto.tv/[url]
|
# So set URL as https://www.picarto.tv/[url]
|
||||||
# Even if this was invalid such as https://www.picarto.tv/twitch.tv/user
|
# Even if this was invalid such as https://www.picarto.tv/picarto.tv/user
|
||||||
# For example, our next check handles that
|
# For example, our next check handles that
|
||||||
try:
|
try:
|
||||||
url = re.search("((?<=://)?picarto.tv/)+(.*)", url).group(0)
|
url = re.search("((?<=://)?picarto.tv/)+(.*)", url).group(0)
|
||||||
|
|
Loading…
Reference in a new issue