
from datetime import datetime, timedelta
import tvdb_api
import tvdb_exceptions

def command_ep(bot, user, channel, args):
    t = tvdb_api.Tvdb()
    now = datetime.now()

    try:
        series = t[args]
    except tvdb_exceptions.tvdb_shownotfound:
        bot.say(channel, "Series '%s' not found" % args)
        return

    latest_season = series[series.keys()[-1]]

    for episode_no, episode in latest_season.items():
        firstaired = episode['firstaired']

        if not firstaired:
            break

        airdate = datetime.strptime(firstaired, "%Y-%m-%d")
        td = airdate - now
        # find the next unaired episode
        if td > timedelta(0, 0, 0):
            msg = "Next episode of %s '%s' airs %s (%d days)" % (series.data['seriesname'], episode['episodename'], episode['firstaired'], td.days)
            bot.say(channel, msg.encode("UTF-8"))
            return

    msg = "No new episode airdates found for %s" % series.data['seriesname']
    bot.say(channel, msg.encode("UTF-8"))



from datetime import datetime, timedelta
import tvdb_api
import tvdb_exceptions

def command_ep(bot, user, channel, args):
    t = tvdb_api.Tvdb()
    now = datetime.now()

    try:
        series = t[args]
    except tvdb_exceptions.tvdb_shownotfound:
        bot.say(channel, "Series '%s' not found" % args)
        return

    episodes = []

    # find all episodes with airdate > now
    for season_no, season in series.items():
        for episode_no, episode in season.items():
            firstaired = episode['firstaired']
            if not firstaired:
                continue
            airdate = datetime.strptime(firstaired, "%Y-%m-%d")
            td = airdate - now
            # find the next unaired episode
            if td > timedelta(0, 0, 0):
                episodes.append(episode)

    # if any episodes were found, find out the one with airdate closest to now
    if episodes:
        # sort the list just in case
        episodes = sorted(episodes, key=firstaired)
        episode = episodes[0]
        td = datetime.strptime(episode['firstaired'], "%Y-%m-%d") - now

        msg = "Next episode of %s '%s' airs %s (%d days)" % (series.data['seriesname'], episode['episodename'], episode['firstaired'], td.days)
        bot.say(channel, msg.encode("UTF-8"))
    else:
        msg = "No new episode airdates found for %s" % series.data['seriesname']
        bot.say(channel, msg.encode("UTF-8"))


Fix episode finding logic to handle specials and cases where episodes are out of order in tvdb api result
Fix episode finding logic to handle specials and cases where episodes are out of order in tvdb api result


git-svn-id: 056f9092885898c4775d98c479d2d33d00273e45@374 dda364a1-ef19-0410-af65-756c83048fb2

  
  from datetime import datetime, timedelta
  import tvdb_api
  import tvdb_exceptions
  
  def command_ep(bot, user, channel, args):
      t = tvdb_api.Tvdb()
      now = datetime.now()
  
      try:
          series = t[args]
      except tvdb_exceptions.tvdb_shownotfound:
          bot.say(channel, "Series '%s' not found" % args)
          return
  
-     latest_season = series[series.keys()[-1]]
+     episodes = []
  
+     # find all episodes with airdate > now
+     for season_no, season in series.items():
-     for episode_no, episode in latest_season.items():
+         for episode_no, episode in season.items():
-         firstaired = episode['firstaired']
+             firstaired = episode['firstaired']
+             if not firstaired:
+                 continue
+             airdate = datetime.strptime(firstaired, "%Y-%m-%d")
+             td = airdate - now
+             # find the next unaired episode
+             if td > timedelta(0, 0, 0):
+                 episodes.append(episode)
  
-         if not firstaired:
-             break
+     # if any episodes were found, find out the one with airdate closest to now
+     if episodes:
+         # sort the list just in case
+         episodes = sorted(episodes, key=firstaired)
+         episode = episodes[0]
+         td = datetime.strptime(episode['firstaired'], "%Y-%m-%d") - now
  
-         airdate = datetime.strptime(firstaired, "%Y-%m-%d")
-         td = airdate - now
-         # find the next unaired episode
-         if td > timedelta(0, 0, 0):
-             msg = "Next episode of %s '%s' airs %s (%d days)" % (series.data['seriesname'], episode['episodename'], episode['firstaired'], td.days)
+         msg = "Next episode of %s '%s' airs %s (%d days)" % (series.data['seriesname'], episode['episodename'], episode['firstaired'], td.days)
-             bot.say(channel, msg.encode("UTF-8"))
+         bot.say(channel, msg.encode("UTF-8"))
+     else:
-             return
- 
-     msg = "No new episode airdates found for %s" % series.data['seriesname']
+         msg = "No new episode airdates found for %s" % series.data['seriesname']
-     bot.say(channel, msg.encode("UTF-8"))
+         bot.say(channel, msg.encode("UTF-8"))
  
  
Fix episode finding logic to handle specials and cases where episodes are out of order in tvdb api result
## Code Before:

from datetime import datetime, timedelta
import tvdb_api
import tvdb_exceptions

def command_ep(bot, user, channel, args):
    t = tvdb_api.Tvdb()
    now = datetime.now()

    try:
        series = t[args]
    except tvdb_exceptions.tvdb_shownotfound:
        bot.say(channel, "Series '%s' not found" % args)
        return

    latest_season = series[series.keys()[-1]]

    for episode_no, episode in latest_season.items():
        firstaired = episode['firstaired']

        if not firstaired:
            break

        airdate = datetime.strptime(firstaired, "%Y-%m-%d")
        td = airdate - now
        # find the next unaired episode
        if td > timedelta(0, 0, 0):
            msg = "Next episode of %s '%s' airs %s (%d days)" % (series.data['seriesname'], episode['episodename'], episode['firstaired'], td.days)
            bot.say(channel, msg.encode("UTF-8"))
            return

    msg = "No new episode airdates found for %s" % series.data['seriesname']
    bot.say(channel, msg.encode("UTF-8"))


## Instruction:
Fix episode finding logic to handle specials and cases where episodes are out of order in tvdb api result
## Code After:

from datetime import datetime, timedelta
import tvdb_api
import tvdb_exceptions

def command_ep(bot, user, channel, args):
    t = tvdb_api.Tvdb()
    now = datetime.now()

    try:
        series = t[args]
    except tvdb_exceptions.tvdb_shownotfound:
        bot.say(channel, "Series '%s' not found" % args)
        return

    episodes = []

    # find all episodes with airdate > now
    for season_no, season in series.items():
        for episode_no, episode in season.items():
            firstaired = episode['firstaired']
            if not firstaired:
                continue
            airdate = datetime.strptime(firstaired, "%Y-%m-%d")
            td = airdate - now
            # find the next unaired episode
            if td > timedelta(0, 0, 0):
                episodes.append(episode)

    # if any episodes were found, find out the one with airdate closest to now
    if episodes:
        # sort the list just in case
        episodes = sorted(episodes, key=firstaired)
        episode = episodes[0]
        td = datetime.strptime(episode['firstaired'], "%Y-%m-%d") - now

        msg = "Next episode of %s '%s' airs %s (%d days)" % (series.data['seriesname'], episode['episodename'], episode['firstaired'], td.days)
        bot.say(channel, msg.encode("UTF-8"))
    else:
        msg = "No new episode airdates found for %s" % series.data['seriesname']
        bot.say(channel, msg.encode("UTF-8"))

