diff --git a/clients/twitch.py b/clients/twitch.py index 0c1509f..421219f 100644 --- a/clients/twitch.py +++ b/clients/twitch.py @@ -4,6 +4,7 @@ import re import time import unicodedata +import dateutil.parser import irc.bot from commands import CommandError @@ -105,6 +106,7 @@ class TwitchClient(irc.bot.SingleServerIRCBot): (re.compile(r'^!(find)?clip\s+(?P")?(?P.+)(?(q)")$'), self._do_clip), (re.compile(r'^!roll(\s+(?P")?(?P.+)(?(q)"))?$'), self._do_roll), (re.compile(r'^!giveaway$'), self._do_giveaway), + (re.compile(r'^!nextstream$'), self._do_nextstream), (re.compile(r'^!conv(ert)?\s+(?P")?(?P.+?)(?(q1)")' r'(\s+(?P")?(?P[^\s]+)(?(q2)"))?$'), self._do_convert), (re.compile(r'^!command\s+set\s+(?P")?(?P.+?)(?(q1)")\s+' @@ -344,6 +346,36 @@ class TwitchClient(irc.bot.SingleServerIRCBot): if self.giveaway['command']: send_response('Type {0} to join!'.format(self.giveaway['command'])) + def _do_nextstream(self, tags, send_response, **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: + send_response('Sorry @{0}, {1}'.format(tags['display-name'], e)) + else: + url = 'https://www.twitch.tv/events/{0}'.format(result['id']) + time = dateutil.parser.parse(result['start']) + when = format_delta(time - datetime.datetime.utcnow()) + time = time.strftime('%Y-%m-%d %I:%M %p GMT') + send_response('{0} [{1}] • {2} [{3}] • {4}'.format(result['title'].strip(), + result['description'].strip(), when, time, url)) + def _do_convert(self, tags, send_response, expression, unit, **kwargs): try: result = self.commands.convert(expression, unit)