class Main:
    def __init__( self, params ): 
        self.client = NineMSNVideo()
        
        if not 'uuid' in params:
          # Perform the lookup using page_url
          page_url = params['page_url'][0]
          uuid = self.client.get_uuid_from_url(page_url)
        else:
          uuid = params['uuid'][0]
          page_url = params['page_url'][0]
        
        media = self.client.get_media_for_video(uuid)
        utils.log('Found media renditions for video: %s' % repr(media.items))

        # Blindly go for the highest bitrate for now. Later versions could include a customisable setting of which stream to use
        media_sorted = sorted(media.items, key=lambda m: m.encodingRate, reverse=True)
        media = media_sorted[0]
        path = media.defaultURL
        if path.startswith('rtmp'):
            path = path.replace('&mp4:', ' playpath=mp4:')
            path += ' swfVfy=true swfUrl=%s pageUrl=%s' % (SWF_URL, page_url)
        utils.log('Using rendition: %s with url: %s' % (media, path))

        listitem = xbmcgui.ListItem(path=path)
        if hasattr(listitem, 'addStreamInfo'):
            listitem.addStreamInfo('video', {'codec': media.videoCodec, 'width': media.frameWidth, 'height': media.frameHeight})
        # make sure that the original ListItem is set to IsPlayable otherwise you will get handle errors here
        xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=listitem)
    def __init__( self, params ):
        self.client = NineMSNVideo()
        handle = int(sys.argv[1])
        section = params['id'][0]
        page = 1
        if 'page' in params:
          page = int(params['page'][0])
        
        def get_play_url(url):
          url_base = sys.argv[0]
          url_params = {'action': 'play', 'page_url': url}
          
          return "{0}?{1}".format(url_base, urllib.urlencode(url_params))
        
        def get_page_url(page):
          url_base = sys.argv[0]
          url_params = {'action': 'section', 'id': section, 'page': page}
          
          return "{0}?{1}".format(url_base, urllib.urlencode(url_params))
        
        for video in self.client.get_videos_for_section(section, page):
          li = xbmcgui.ListItem("{0} - {1}".format(video.show, video.name), thumbnailImage=video.image)
          li.setProperty('IsPlayable', 'true')
          # Set the image if we have one
          xbmcplugin.addDirectoryItem(handle=handle, listitem=li, url=get_play_url(video.url))

        # And tack on the "Next page" option all the time; should really check if there *is* a next page first
        # Instead, let's just clamp it to 10 pages
        if section != 'mostpopular' and page != 10:
          li = xbmcgui.ListItem("Next Page")
          xbmcplugin.addDirectoryItem(handle=handle, listitem=li, url=get_page_url(page+1), isFolder=True)
        
        xbmcplugin.addSortMethod( handle=handle, sortMethod=xbmcplugin.SORT_METHOD_NONE )
        xbmcplugin.setContent( handle=handle, content='tvshows' )
        xbmcplugin.endOfDirectory( handle=handle, succeeded=1 )
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 )
class Main:
    def __init__( self, params ):
        self.client = NineMSNVideo()
        handle = int(sys.argv[1])
        
        def get_url(action, id):
          url_base = sys.argv[0]
          url_params = {'action': action, 'id': id}
          
          return "{0}?{1}".format(url_base, urllib.urlencode(url_params))
        
        for section in self.client.get_sections():
          li = xbmcgui.ListItem(section.name)
          xbmcplugin.addDirectoryItem(handle=handle, listitem=li, url=get_url('section', section.id), isFolder=True)
        
        for category in self.client.get_categories():
          li = xbmcgui.ListItem(category.name)
          xbmcplugin.addDirectoryItem(handle=handle, listitem=li, url=get_url('category', category.id), isFolder=True)

        # xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE ) # Needed?
        xbmcplugin.setContent( handle=handle, content='tvshows' )
        xbmcplugin.endOfDirectory( handle=handle, succeeded=1 )
    def __init__( self, params ):
        self.client = NineMSNVideo()
        handle = int(sys.argv[1])
        show = params['id'][0]
        show_name = params['fullname'][0]
        
        def get_url(action, show, fullname, season):
          url_base = sys.argv[0]
          url_params = {'action': action, 'id': show, 'fullname': fullname, 'season': season}
          
          return "{0}?{1}".format(url_base, urllib.urlencode(url_params))
        
        for season in self.client.get_seasons_for_show(show):
          li = xbmcgui.ListItem("{0} {1}".format(show_name, season.name))
          xbmcplugin.addDirectoryItem(handle=handle, listitem=li, url=get_url('show_season', show, "{0} {1}".format(show_name, season.name), season.id), isFolder=True)

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