diff --git a/clients/discord.py b/clients/discord.py index 4259aea..04b3f1b 100644 --- a/clients/discord.py +++ b/clients/discord.py @@ -63,6 +63,7 @@ class DiscordClient(discord.Client): (re.compile(r'^!yt\s+(?P")?(?P.+)(?(q)")$'), self._do_yt), (re.compile(r'^!clip\s+(?P")?(?P.+)(?(q)")$'), self._do_clip), (re.compile(r'^!cheese\s+(?P")?(?P.+)(?(q)")$'), self._do_cheese), + (re.compile(r'^!nextstream$'), self._do_nextstream), ] super(DiscordClient, self).__init__() @@ -245,3 +246,38 @@ class DiscordClient(discord.Client): name, value = 'Base', point embed.add_field(name=name.strip(), value=value.strip(), inline=True) await self.send_message(message.channel, embed=embed) + + async def _do_nextstream(self, server, user, message, **kwargs): + def format_delta(d): + if d.total_seconds() <= 0: + return 'In progress' + days = d.days + hours, rem = divmod(d.seconds, 60 * 60) + mins, secs = divmod(rem, 60) + result = '' + if days > 0: + result += '{0} day{1}'.format(days, 's' if days > 1 else '') + if hours > 0: + if result: + result += ', ' + result += '{0} hour{1}'.format(hours, 's' if hours > 1 else '') + if result: + result += ', ' + result += '{0} minute{1}'.format(mins, 's' if mins > 1 else '') + return 'In ' + result + try: + result = self.commands.next_stream() + except CommandError as e: + await self.send_message(message.channel, 'Sorry {0}, {1}'.format(message.author.mention, e)) + else: + embed = discord.Embed(title=result['title'], + url='https://www.twitch.tv/events/{0}'.format(result['id']), + description=result['description'], color=0x4b367c) + embed.set_thumbnail(url=result['cover_image_url'].format(width=128, height=72)) + embed.set_image(url=result['game_box_large']) + embed.set_author(name='lilialil', url='https://twitch.tv/lilialil', + icon_url='https://static-cdn.jtvnw.net/jtv_user_pictures/lilialil-profile_image-7601a69cf14adae1-300x300.png') + time = dateutil.parser.parse(result['start']) + embed.add_field(name=format_delta(time - datetime.datetime.utcnow()), + value=time.strftime('%Y-%m-%d %I:%M %p GMT'), inline=False) + await self.send_message(message.channel, embed=embed) diff --git a/commands.py b/commands.py index e07a905..3658464 100644 --- a/commands.py +++ b/commands.py @@ -177,6 +177,19 @@ class Commands(object): else: return result + def next_stream(self): + try: + events = self._get_events(dict( + sort_by='start', + sort_order='asc', + newer_than=datetime.datetime.utcnow().isoformat(), + page_size=1)).json() + event = events.pop(0) + except (requests.exceptions.HTTPError, IndexError): + raise CommandError('no events found') + else: + return event + def _get_instagram_media(self, params): api_url = self.config['Instagram'].get('api_url') r = requests.get('{0}/media'.format(api_url), params=params) @@ -261,3 +274,9 @@ class Commands(object): r = requests.get('{0}/clips'.format(api_url), params=params) r.raise_for_status() return r + + def _get_events(self, params): + api_url = self.config['Twitch'].get('cache_api_url') + r = requests.get('{0}/events'.format(api_url), params=params) + r.raise_for_status() + return r