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)