class Main:
    def __init__( self, params ):
        self.client = NineMSNVideo()
        handle = int(sys.argv[1])
        category = params['id'][0]
        
        def get_url(action, id, fullname, season=None):
          url_base = sys.argv[0]
          url_params = {'action': action, 'id': id, 'fullname': fullname}
          if season is not None:
            url_params['season'] = season
          
          return "{0}?{1}".format(url_base, urllib.urlencode(url_params))
        
        for show in self.client.get_shows_for_category(category):
          li = xbmcgui.ListItem("{0} ({1} episodes)".format(show.name, show.episode_count))
          if len(show.seasons) > 1:
            url = get_url('show', show.id, show.name)
          else:
            url = get_url('show_season', show.id, show.name, '')
          xbmcplugin.addDirectoryItem(handle=handle, listitem=li, url=url, isFolder=True)

        xbmcplugin.addSortMethod( handle=handle, sortMethod=xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE )
        xbmcplugin.setContent( handle=handle, content='tvshows' )
        xbmcplugin.endOfDirectory( handle=handle, succeeded=1 )