import logging
log = logging.getLogger(__name__)

import datetime
import dateutil.parser
import requests

import botologist.plugin



def get_next_episode_info(show):
	query = {'q': '+'.join(show.split()), 'embed': 'nextepisode'}
	try:
		data = requests.get('http://api.tvmaze.com/singlesearch/shows', query).json()
	except (requests.exceptions.RequestException, ValueError):
		log.warning('TVMaze request caused an exception', exc_info=True)
		return None

	info = data['name']
	nextepisode = data.get('_embedded', {}).get('nextepisode')
	if nextepisode:
		log.debug('next episode data: %r', nextepisode)
		dt = dateutil.parser.parse(nextepisode['airstamp'])
		info += ' - season %d, episode %d airs at %s' % (
			nextepisode['season'],
			nextepisode['number'],
			dt.strftime('%Y-%m-%d %H:%I %Z'),
		)
		now = datetime.datetime.now(dt.tzinfo)
		if dt > now:
			time_left = dt - now
			if time_left.days > 0:
				time_left_str = '%dd %dh' % (
					time_left.days,
					round(time_left.seconds / 3600),
				)
			elif time_left.seconds > 3600:
				time_left_str = '%dh %dm' % (
					round(time_left.seconds / 3600),
					round(time_left.seconds / 60),
				)
			else:
				time_left_str = '%dm' % round(time_left.seconds / 60)
			log.debug('time left: %r (%s)', time_left, time_left_str)
			info += ' (in %s)' % time_left_str
	else:
		info += ' - no next episode :('
	return info


class TvseriesPlugin(botologist.plugin.Plugin):
	@botologist.plugin.command('nextepisode')
	def nextepisode(self, msg):
		return get_next_episode_info(' '.join(msg.args))

@@ -1112,32 +1112,33 @@
 00),%0A%09%09%09%09%09round(
+(
 time_left.second
@@ -1134,24 +1134,32 @@
 left.seconds
+ %25 3600)
  / 60),%0A%09%09%09%09

fix minutes calculation