2016-08-08 08:20:43 +12:00
|
|
|
import aiohttp
|
|
|
|
import json
|
|
|
|
import asyncio
|
2016-08-09 09:27:46 +12:00
|
|
|
import discord
|
2016-08-09 09:35:51 +12:00
|
|
|
import re
|
2016-08-08 08:20:43 +12:00
|
|
|
|
2016-08-09 09:27:46 +12:00
|
|
|
from discord.ext import commands
|
2016-08-08 08:20:43 +12:00
|
|
|
from .utils import config
|
2016-08-09 09:27:46 +12:00
|
|
|
from .utils import checks
|
2016-08-08 08:20:43 +12:00
|
|
|
|
|
|
|
base_url = 'https://ptvappapi.picarto.tv'
|
2016-08-09 09:27:46 +12:00
|
|
|
|
2016-08-09 11:32:33 +12:00
|
|
|
# This is a public key for use, I don't care if this is seen
|
2016-08-08 08:20:43 +12:00
|
|
|
key = '03e26294-b793-11e5-9a41-005056984bd4'
|
|
|
|
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-08 08:20:43 +12:00
|
|
|
async def check_online(stream):
|
|
|
|
try:
|
2016-08-09 11:32:33 +12:00
|
|
|
url = '{}/channel/{}?key={}'.format(base_url, stream, key)
|
2016-08-08 08:20:43 +12:00
|
|
|
with aiohttp.ClientSession(headers={"User-Agent": "Bonfire/1.0.0"}) as s:
|
|
|
|
async with s.get(url) as r:
|
|
|
|
response = await r.text()
|
|
|
|
return json.loads(response).get('is_online')
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-08 08:20:43 +12:00
|
|
|
class Picarto:
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-08 08:20:43 +12:00
|
|
|
async def check_channels(self):
|
2016-08-09 11:43:18 +12:00
|
|
|
await self.bot.wait_until_ready()
|
|
|
|
while not self.bot.is_closed:
|
|
|
|
picarto = config.getContent('picarto') or {}
|
|
|
|
for m_id, r in picarto.items():
|
|
|
|
url = r['picarto_url']
|
|
|
|
live = r['live']
|
|
|
|
notify = r['notifications_on']
|
|
|
|
user = re.search("(?<=picarto.tv/)(.*)", url).group(1)
|
|
|
|
online = await check_online(user)
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-09 11:43:18 +12:00
|
|
|
if not live and notify and online:
|
2016-08-12 07:00:24 +12:00
|
|
|
for server_id in r['servers'].items():
|
2016-08-10 16:46:28 +12:00
|
|
|
server = self.bot.get_server(server_id)
|
2016-08-12 07:00:24 +12:00
|
|
|
server_alerts = config.getContent('server_alerts') or {}
|
|
|
|
channel_id = server_alerts.get(server_id) or server_id
|
2016-08-10 16:46:28 +12:00
|
|
|
channel = self.bot.get_channel(channel_id)
|
|
|
|
member = discord.utils.find(lambda m: m.id == m_id, server.members)
|
|
|
|
|
|
|
|
picarto[m_id]['live'] = 1
|
|
|
|
fmt = "{} has just gone live! View their stream at {}".format(member.display_name, url)
|
|
|
|
config.saveContent('picarto', picarto)
|
2016-08-11 03:20:58 +12:00
|
|
|
await self.bot.send_message(channel, fmt)
|
2016-08-09 11:43:18 +12:00
|
|
|
elif live and not online:
|
2016-08-10 16:46:28 +12:00
|
|
|
for server_id, channel_id in r['servers'].items():
|
2016-08-11 03:23:15 +12:00
|
|
|
server = self.bot.get_server(server_id)
|
2016-08-12 07:00:24 +12:00
|
|
|
server_alerts = config.getContent('server_alerts') or {}
|
|
|
|
channel_id = server_alerts.get(server_id) or server_id
|
2016-08-10 16:46:28 +12:00
|
|
|
channel = self.bot.get_channel(channel_id)
|
|
|
|
member = discord.utils.find(lambda m: m.id == m_id, server.members)
|
|
|
|
|
|
|
|
picarto[m_id]['live'] = 0
|
|
|
|
fmt = "{} has just gone offline! Catch them next time they stream at {}".format(
|
|
|
|
member.display_name,
|
|
|
|
url)
|
|
|
|
config.saveContent('picarto', picarto)
|
2016-08-11 03:20:58 +12:00
|
|
|
await self.bot.send_message(channel, fmt)
|
2016-08-09 11:43:18 +12:00
|
|
|
await asyncio.sleep(30)
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-09 09:27:46 +12:00
|
|
|
@commands.group(pass_context=True, invoke_without_command=True)
|
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-08-09 11:32:33 +12:00
|
|
|
async def picarto(self, ctx, member: discord.Member = None):
|
2016-08-09 09:27:46 +12:00
|
|
|
"""This command can be used to view Picarto stats about a certain member"""
|
|
|
|
member = member or ctx.message.author
|
|
|
|
picarto_urls = config.getContent('picarto') or {}
|
|
|
|
member_url = picarto_urls.get(member.id)
|
|
|
|
if not member_url:
|
|
|
|
await self.bot.say("That user does not have a picarto url setup!")
|
|
|
|
return
|
2016-08-09 09:37:44 +12:00
|
|
|
member_url = member_url['picarto_url']
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-09 09:40:26 +12:00
|
|
|
stream = re.search("(?<=picarto.tv/)(.*)", member_url).group(1)
|
2016-08-09 11:32:33 +12:00
|
|
|
url = '{}/channel/{}?key={}'.format(base_url, stream, key)
|
2016-08-09 09:27:46 +12:00
|
|
|
with aiohttp.ClientSession(headers={"User-Agent": "Bonfire/1.0.0"}) as s:
|
2016-08-09 09:39:50 +12:00
|
|
|
async with s.get(url) as r:
|
2016-08-09 09:27:46 +12:00
|
|
|
response = await r.text()
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-09 09:27:46 +12:00
|
|
|
data = json.loads(response)
|
2016-08-09 11:32:33 +12:00
|
|
|
things_to_print = ['channel', 'commissions_enabled', 'is_nsfw', 'program', 'tablet', 'followers',
|
|
|
|
'content_type']
|
|
|
|
fmt = "\n".join(
|
|
|
|
"{}: {}".format(i.title().replace("_", " "), r) for i, r in data.items() if i in things_to_print)
|
2016-08-09 09:44:58 +12:00
|
|
|
social_links = data.get('social_urls')
|
|
|
|
if social_links:
|
2016-08-09 11:32:33 +12:00
|
|
|
fmt2 = "\n".join("\t{}: {}".format(i.title().replace("_", " "), r) for i, r in social_links.items())
|
2016-08-09 09:44:58 +12:00
|
|
|
fmt = "{}\nSocial Links:\n{}".format(fmt, fmt2)
|
2016-08-09 09:46:02 +12:00
|
|
|
await self.bot.say("Picarto stats for {}: ```\n{}```".format(member.display_name, fmt))
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-09 09:27:46 +12:00
|
|
|
@picarto.command(name='add', pass_context=True, no_pm=True)
|
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
|
|
|
async def add_picarto_url(self, ctx, url: str):
|
|
|
|
"""Saves your user's picarto URL"""
|
|
|
|
try:
|
|
|
|
url = re.search("((?<=://)?picarto.tv/)+(.*)", url).group(0)
|
|
|
|
except AttributeError:
|
|
|
|
url = "https://www.picarto.tv/{}".format(url)
|
|
|
|
else:
|
|
|
|
url = "https://www.{}".format(url)
|
2016-08-09 11:32:33 +12:00
|
|
|
|
|
|
|
api_url = '{}/channel/{}?key={}'.format(base_url, re.search("https://www.picarto.tv/(.*)", url).group(1), key)
|
|
|
|
|
2016-08-09 09:27:46 +12:00
|
|
|
with aiohttp.ClientSession() as s:
|
|
|
|
async with s.get(api_url) as r:
|
|
|
|
if not r.status == 200:
|
|
|
|
await self.bot.say("That Picarto user does not exist! "
|
|
|
|
"What would be the point of adding a nonexistant Picarto user? Silly")
|
|
|
|
return
|
|
|
|
|
|
|
|
picarto_urls = config.getContent('picarto') or {}
|
|
|
|
result = picarto_urls.get(ctx.message.author.id)
|
|
|
|
|
|
|
|
if result is not None:
|
|
|
|
picarto_urls[ctx.message.author.id]['picarto_url'] = url
|
|
|
|
else:
|
2016-08-10 16:46:28 +12:00
|
|
|
picarto_urls[ctx.message.author.id] = {'picarto_url': url,
|
|
|
|
'servers': {ctx.message.server.id: ctx.message.channel.id},
|
|
|
|
'notifications_on': 1, 'live': 0}
|
2016-08-09 09:27:46 +12:00
|
|
|
config.saveContent('picarto', picarto_urls)
|
2016-08-09 11:32:33 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"I have just saved your Picarto url {}, this channel will now send a notification when you go live".format(
|
|
|
|
ctx.message.author.mention))
|
|
|
|
|
2016-08-09 09:48:59 +12:00
|
|
|
@picarto.command(name='remove', aliases=['delete'], pass_context=True, no_pm=True)
|
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
|
|
|
async def remove_picarto_url(self, ctx):
|
|
|
|
"""Removes your picarto URL"""
|
2016-08-09 10:10:07 +12:00
|
|
|
picarto = config.getContent('picarto') or {}
|
2016-08-09 09:48:59 +12:00
|
|
|
if picarto.get(ctx.message.author.id) is not None:
|
|
|
|
del picarto[ctx.message.author.id]
|
|
|
|
config.saveContent('picarto', picarto)
|
|
|
|
await self.bot.say("I am no longer saving your picarto URL {}".format(ctx.message.author.mention))
|
|
|
|
else:
|
|
|
|
await self.bot.say(
|
|
|
|
"I do not have your picarto URL added {}. You can save your picarto url with !picarto add".format(
|
|
|
|
ctx.message.author.mention))
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-09 09:48:59 +12:00
|
|
|
@picarto.group(pass_context=True, no_pm=True, invoke_without_command=True)
|
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-08-09 11:32:33 +12:00
|
|
|
async def notify(self, ctx, channel: discord.Channel = None):
|
2016-08-09 10:15:17 +12:00
|
|
|
"""This can be used to turn picarto notifications on or off
|
|
|
|
Call this command by itself, with a channel name, to change which one has the notification sent to it"""
|
2016-08-09 10:10:07 +12:00
|
|
|
channel = channel or ctx.message.channel
|
|
|
|
member = ctx.message.author
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-09 10:10:07 +12:00
|
|
|
picarto = config.getContent('picarto') or {}
|
|
|
|
result = picarto.get(member.id)
|
|
|
|
if result is None:
|
|
|
|
await self.bot.say(
|
|
|
|
"I do not have your picarto URL added {}. You can save your picarto url with !picarto add".format(
|
|
|
|
member.mention))
|
2016-08-09 11:32:33 +12:00
|
|
|
|
2016-08-10 16:46:28 +12:00
|
|
|
picarto[member.id]['servers'][ctx.message.server.id] = channel.id
|
2016-08-09 10:10:07 +12:00
|
|
|
config.saveContent('picarto', picarto)
|
2016-08-09 11:32:33 +12:00
|
|
|
await self.bot.say(
|
2016-08-10 16:46:28 +12:00
|
|
|
"I have just changed which channel will be notified when you go live, to `{}`".format(channel.name))
|
2016-08-09 09:48:59 +12:00
|
|
|
|
|
|
|
@notify.command(name='on', aliases=['start,yes'], pass_context=True, no_pm=True)
|
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
|
|
|
async def notify_on(self, ctx):
|
|
|
|
"""Turns picarto notifications on"""
|
2016-08-09 10:10:07 +12:00
|
|
|
picarto = config.getContent('picarto') or {}
|
2016-08-09 09:48:59 +12:00
|
|
|
result = picarto.get(ctx.message.author.id)
|
|
|
|
if result is None:
|
|
|
|
await self.bot.say(
|
|
|
|
"I do not have your picarto URL added {}. You can save your picarto url with !picarto add".format(
|
|
|
|
ctx.message.author.mention))
|
|
|
|
elif result['notifications_on']:
|
|
|
|
await self.bot.say("What do you want me to do, send two notifications? Not gonna happen {}".format(
|
|
|
|
ctx.message.author.mention))
|
|
|
|
else:
|
|
|
|
picarto[ctx.message.author.id]['notifications_on'] = 1
|
|
|
|
config.saveContent('picarto', picarto)
|
|
|
|
await self.bot.say("I will notify if you go live {}, you'll get a bajillion followers I promise c:".format(
|
|
|
|
ctx.message.author.mention))
|
|
|
|
|
|
|
|
@notify.command(name='off', aliases=['stop,no'], pass_context=True, no_pm=True)
|
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
|
|
|
async def notify_off(self, ctx):
|
|
|
|
"""Turns picarto notifications off"""
|
2016-08-09 10:10:07 +12:00
|
|
|
picarto = config.getContent('picarto') or {}
|
2016-08-09 09:48:59 +12:00
|
|
|
if picarto.get(ctx.message.author.id) is None:
|
|
|
|
await self.bot.say(
|
|
|
|
"I do not have your picarto URL added {}. You can save your picarto url with !picarto add".format(
|
|
|
|
ctx.message.author.mention))
|
|
|
|
elif not picarto.get(ctx.message.author.id)['notifications_on']:
|
|
|
|
await self.bot.say("I am already set to not notify if you go live! Pay attention brah {}".format(
|
|
|
|
ctx.message.author.mention))
|
|
|
|
else:
|
|
|
|
picarto[ctx.message.author.id]['notifications_on'] = 0
|
2016-08-09 09:50:29 +12:00
|
|
|
config.saveContent('picarto', picarto)
|
2016-08-09 09:48:59 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"I will not notify if you go live anymore {}, "
|
|
|
|
"are you going to stream some lewd stuff you don't want people to see?~".format(
|
|
|
|
ctx.message.author.mention))
|
2016-08-09 11:32:33 +12:00
|
|
|
|
|
|
|
|
2016-08-08 08:20:43 +12:00
|
|
|
def setup(bot):
|
|
|
|
p = Picarto(bot)
|
2016-08-09 10:26:44 +12:00
|
|
|
config.loop.create_task(p.check_channels())
|
2016-08-08 08:20:43 +12:00
|
|
|
bot.add_cog(Picarto(bot))
|