Use Picarto's new API
This commit is contained in:
parent
6b805faf0f
commit
51f3696308
|
@ -1,8 +1,6 @@
|
||||||
import aiohttp
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import discord
|
import discord
|
||||||
import re
|
import re
|
||||||
import rethinkdb as r
|
|
||||||
import traceback
|
import traceback
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -11,20 +9,21 @@ from discord.ext import commands
|
||||||
from . import utils
|
from . import utils
|
||||||
|
|
||||||
log = logging.getLogger()
|
log = logging.getLogger()
|
||||||
BASE_URL = 'https://ptvappapi.picarto.tv'
|
BASE_URL = 'https://api.picarto.tv/v1'
|
||||||
|
|
||||||
# This is a public key for use, I don't care if this is seen
|
|
||||||
api_key = '03e26294-b793-11e5-9a41-005056984bd4'
|
|
||||||
|
|
||||||
|
|
||||||
class Picarto:
|
class Picarto:
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
|
||||||
|
# noinspection PyAttributeOutsideInit
|
||||||
async def get_online_users(self):
|
async def get_online_users(self):
|
||||||
# This method is in place to just return all online users so we can compare against it
|
# This method is in place to just return all online users so we can compare against it
|
||||||
url = BASE_URL + '/online/all'
|
url = BASE_URL + '/online'
|
||||||
payload = {'key': api_key}
|
payload = {
|
||||||
|
'adult': True,
|
||||||
|
'gaming': True
|
||||||
|
}
|
||||||
self.online_channels = await utils.request(url, payload=payload)
|
self.online_channels = await utils.request(url, payload=payload)
|
||||||
|
|
||||||
def channel_online(self, channel):
|
def channel_online(self, channel):
|
||||||
|
@ -34,8 +33,7 @@ class Picarto:
|
||||||
if not self.online_channels:
|
if not self.online_channels:
|
||||||
return False
|
return False
|
||||||
channel = re.search("(?<=picarto.tv/)(.*)", channel).group(1)
|
channel = re.search("(?<=picarto.tv/)(.*)", channel).group(1)
|
||||||
matches = [stream for stream in self.online_channels if stream['channel_name'].lower() == channel.lower()]
|
return channel in [stream['name'].lower() for stream in self.online_channels]
|
||||||
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()
|
||||||
|
@ -59,7 +57,7 @@ class Picarto:
|
||||||
if member is None:
|
if member is None:
|
||||||
continue
|
continue
|
||||||
channel_id = self.bot.db.load('server_settings', key=s_id,
|
channel_id = self.bot.db.load('server_settings', key=s_id,
|
||||||
pluck='notifications_channel') or int(s_id)
|
pluck='notifications_channel') or int(s_id)
|
||||||
channel = server.get_channel(channel_id)
|
channel = server.get_channel(channel_id)
|
||||||
try:
|
try:
|
||||||
await channel.send(
|
await channel.send(
|
||||||
|
@ -77,7 +75,7 @@ class Picarto:
|
||||||
if member is None:
|
if member is None:
|
||||||
continue
|
continue
|
||||||
channel_id = self.bot.db.load('server_settings', key=s_id,
|
channel_id = self.bot.db.load('server_settings', key=s_id,
|
||||||
pluck='notifications_channel') or int(s_id)
|
pluck='notifications_channel') or int(s_id)
|
||||||
channel = server.get_channel(channel_id)
|
channel = server.get_channel(channel_id)
|
||||||
try:
|
try:
|
||||||
await channel.send(
|
await channel.send(
|
||||||
|
@ -110,19 +108,17 @@ class Picarto:
|
||||||
|
|
||||||
# Use regex to get the actual username so that we can make a request to the API
|
# Use regex to get the actual username so that we can make a request to the API
|
||||||
stream = re.search("(?<=picarto.tv/)(.*)", member_url).group(1)
|
stream = re.search("(?<=picarto.tv/)(.*)", member_url).group(1)
|
||||||
url = BASE_URL + '/channel/{}'.format(stream)
|
url = BASE_URL + '/channel/name/{}'.format(stream)
|
||||||
payload = {'key': api_key}
|
|
||||||
|
|
||||||
data = await utils.request(url, payload=payload)
|
data = await utils.request(url)
|
||||||
if data is None:
|
if data is None:
|
||||||
await ctx.send("I couldn't connect to Picarto!")
|
await ctx.send("I couldn't connect to Picarto!")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Not everyone has all these settings, so use this as a way to print information if it does, otherwise ignore it
|
# Not everyone has all these settings, so use this as a way to print information if it does, otherwise ignore it
|
||||||
things_to_print = ['channel', 'commissions_enabled', 'is_nsfw', 'program', 'tablet', 'followers',
|
things_to_print = ['comissions', 'adult', 'followers', 'category', 'online']
|
||||||
'content_type']
|
|
||||||
|
|
||||||
embed = discord.Embed(title='{}\'s Picarto'.format(data['channel']), url=url)
|
embed = discord.Embed(title='{}\'s Picarto'.format(data['channel']), url=member_url)
|
||||||
if data['avatar_url']:
|
if data['avatar_url']:
|
||||||
embed.set_thumbnail(url=data['avatar_url'])
|
embed.set_thumbnail(url=data['avatar_url'])
|
||||||
|
|
||||||
|
@ -165,10 +161,9 @@ class Picarto:
|
||||||
else:
|
else:
|
||||||
url = "https://www.{}".format(url)
|
url = "https://www.{}".format(url)
|
||||||
channel = re.search("https://www.picarto.tv/(.*)", url).group(1)
|
channel = re.search("https://www.picarto.tv/(.*)", url).group(1)
|
||||||
api_url = BASE_URL + '/channel/{}'.format(channel)
|
api_url = BASE_URL + '/channel/name/{}'.format(channel)
|
||||||
payload = {'key': api_key}
|
|
||||||
|
|
||||||
data = await utils.request(api_url, payload=payload)
|
data = await utils.request(api_url)
|
||||||
if not data:
|
if not data:
|
||||||
await ctx.send("That Picarto user does not exist! What would be the point of adding a nonexistant Picarto "
|
await ctx.send("That Picarto user does not exist! What would be the point of adding a nonexistant Picarto "
|
||||||
"user? Silly")
|
"user? Silly")
|
||||||
|
@ -193,8 +188,8 @@ class Picarto:
|
||||||
}
|
}
|
||||||
self.bot.db.save('picarto', entry)
|
self.bot.db.save('picarto', entry)
|
||||||
await ctx.send(
|
await ctx.send(
|
||||||
"I have just saved your Picarto URL {}, this guild will now be notified when you go live".format(
|
"I have just saved your Picarto URL {}, this guild will now be notified when you go live".format(
|
||||||
ctx.message.author.mention))
|
ctx.message.author.mention))
|
||||||
|
|
||||||
@picarto.command(name='remove', aliases=['delete'])
|
@picarto.command(name='remove', aliases=['delete'])
|
||||||
@utils.custom_perms(send_messages=True)
|
@utils.custom_perms(send_messages=True)
|
||||||
|
|
Loading…
Reference in a new issue