Example #1
0
    def __init__(self):
        
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.parser = Parser()
        self.currentlist = None
        self.addon = None
        
        common.log('SportsDevil initialized')
Example #2
0
    def __init__(self):
        #BASE_RESOURCE_PATH = os.path.join( os.getcwd(), "resources" )
        BASE_RESOURCE_PATH = os.path.join( ptv.getAddonInfo('path'), "resources" )
        BASE_RESOURCE_PATH = os.path.join( ptv2.getAddonInfo('path'), "resources" )

        sys.path.append( os.path.join( BASE_RESOURCE_PATH, "lib" ) )
        sys.path.append( os.path.join( BASE_RESOURCE_PATH, "resources" ) )

        sys.path.append( os.path.join( ptv.getAddonInfo('path'), "host" ) )
        import mrknow_pLog, settings, Parser, mrknow_pCommon

        self.cm = mrknow_pCommon.common()
        self.log = mrknow_pLog.pLog()
        self.settings = settings.TVSettings()
        self.parser = Parser.Parser()
        #self.log.info("DIR " + common.Paths.modulesDir + 'mainMenu.cfg')
        #self.MAIN_MENU_FILE = os.path.join( common.Paths.modulesDir, 'mainMenu.cfg' )
        self.MAIN_MENU_FILE = 'mainMenu.cfg'
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(common.Paths.customModulesDir, common.Paths.customModulesRepo)

        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser2 = Parser2()
        self.currentlist = None

        self.addon = None
        self.log.info('Filmy online www.mrknow.pl')
        common.clearCache()
Example #3
0
    def __init__(self):
        
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(common.Paths.customModulesDir, common.Paths.customModulesRepo)
        
        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser = Parser2()
        self.currentlist = None
        
        self.addon = None
        
        common.log('Filmkodi initialized')
Example #4
0
    def __init__(self):
        
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.parser = Parser()
        self.currentlist = None
        self.addon = None
        
        common.log('SportsDevilNL initialized')
Example #5
0
    def __init__(self):
        #BASE_RESOURCE_PATH = os.path.join( os.getcwd(), "resources" )
        BASE_RESOURCE_PATH = os.path.join(ptv.getAddonInfo('path'),
                                          "resources")
        BASE_RESOURCE_PATH = os.path.join(ptv2.getAddonInfo('path'),
                                          "resources")

        sys.path.append(os.path.join(BASE_RESOURCE_PATH, "lib"))
        sys.path.append(os.path.join(BASE_RESOURCE_PATH, "resources"))

        sys.path.append(os.path.join(ptv.getAddonInfo('path'), "host"))
        import mrknow_pLog, settings, mrknow_Parser, mrknow_pCommon

        self.cm = mrknow_pCommon.common()
        self.log = mrknow_pLog.pLog()
        self.settings = settings.TVSettings()
        self.parser = mrknow_Parser.mrknow_Parser()
        #self.log.info("DIR " + common.Paths.modulesDir + 'mainMenu.cfg')
        if ptv.getSetting('adults') == 'false':
            self.MAIN_MENU_FILE = 'mainMenu.cfg'
        else:
            self.MAIN_MENU_FILE = 'mainMenuAdult.cfg'
        self.SPORT_MENU_FILE = 'sportMenu.cfg'

        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(
            common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(
            common.Paths.customModulesDir, common.Paths.customModulesRepo)

        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser2 = Parser2()
        self.currentlist = None

        self.addon = None
        self.log.info('Filmy online www.mrknow.pl')
        common.clearCache()
Example #6
0
    def __init__(self):
        
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.parser = Parser()
        self.currentlist = None
        self.addon = None
        
        common.log('SportsDevil initialized')
        common.log('Running on Python %s'%(str(sys.version)))
Example #7
0
    def __init__(self):
        
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(common.Paths.customModulesDir, common.Paths.customModulesRepo)
        
        # todo: cache this (due to limited API calls for github)
        self.syncManager = SyncManager()
        #self.syncManager.addSource("Max Mustermann - Catchers", SyncSourceType.CATCHERS, common.Paths.catchersRepo)
        #self.syncManager.addSource("Max Mustermann - Modules", SyncSourceType.MODULES, common.Paths.modulesRepo)
        
        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser = Parser()
        self.currentlist = None
        
        self.addon = None
        
        common.log('PGLiveTV initialized')
    def __init__(self):

        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(common.Paths.customModulesDir, common.Paths.customModulesRepo)

        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser = Parser()
        self.currentlist = None

        self.addon = None

        common.log('ViendoKodiStreaming initialized')
Example #9
0
    def __init__(self):
        self.base = sys.argv[0]
        self.handle = int(sys.argv[1])
        paramstring = urllib.unquote_plus(sys.argv[2])
        
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(common.Paths.customModulesDir, common.Paths.customModulesRepo)
        self.syncManager = SyncManager()
        self.syncManager.addSource("Max Mustermann", SyncSourceType.CATCHERS, common.Paths.catchersRepo)
        
        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser = Parser()
        self.currentlist = None
        common.log('SportsDevil initialized')
        
        self.run(paramstring)
    def __init__(self):
        common.log('Initializing SportsDevil')

        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir)

        self.curr_file = ''
        self.urlList = []
        self.extensionList = []
        self.selectionList = []
        self.currentlist = None
        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)

        self.parser = Parser()

        self.handle = int(sys.argv[1])

        common.log('SportsDevil initialized')

        paramstring = sys.argv[2]
        self.run(paramstring)
Example #11
0
    def __init__(self):
        
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(common.Paths.customModulesDir, common.Paths.customModulesRepo)
        
        # todo: cache this (due to limited API calls for github)
        self.syncManager = SyncManager()
        #self.syncManager.addSource("Max Mustermann - Catchers", SyncSourceType.CATCHERS, common.Paths.catchersRepo)
        #self.syncManager.addSource("Max Mustermann - Modules", SyncSourceType.MODULES, common.Paths.modulesRepo)
        
        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser = Parser()
        self.currentlist = None
        
        self.addon = None
        
        common.log('SportsDevil initialized')
Example #12
0
class MrknowFilms:



    def __init__(self):
        #BASE_RESOURCE_PATH = os.path.join( os.getcwd(), "resources" )
        BASE_RESOURCE_PATH = os.path.join( ptv.getAddonInfo('path'), "resources" )
        #BASE_RESOURCE_PATH = os.path.join( ptv2.getAddonInfo('path'), "resources" )

        sys.path.append( os.path.join( BASE_RESOURCE_PATH, "lib" ) )
        sys.path.append( os.path.join( BASE_RESOURCE_PATH, "resources" ) )

        sys.path.append( os.path.join( ptv.getAddonInfo('path'), "host" ) )
        import mrknow_pLog, settings, mrknow_Parser, mrknow_pCommon

        self.cm = mrknow_pCommon.common()
        self.log = mrknow_pLog.pLog()
        self.settings = settings.TVSettings()
        self.parser = mrknow_Parser.mrknow_Parser()
        #self.log.info("DIR " + common.Paths.modulesDir + 'mainMenu.cfg')
        if ptv.getSetting('adults') == 'false':
            self.MAIN_MENU_FILE = 'mainMenu.cfg'
        else:
            self.MAIN_MENU_FILE = 'mainMenuAdult.cfg'
        self.SPORT_MENU_FILE = 'sportMenu.cfg'

        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(common.Paths.customModulesDir, common.Paths.customModulesRepo)

        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser2 = Parser2()
        self.currentlist = None

        self.addon = None
        self.log.info('Filmy online www.mrknow.pl')
        common.clearCache()


    def _parseParameters(self):
        mode = int(self.addon.queries['mode'])
        queryString = self.addon.queries['item']
        item = ListItem.create()
        if mode in [common.Mode2.CHROME, common.Mode2.ADDTOFAVOURITES, common.Mode2.REMOVEFROMFAVOURITES, common.Mode2.EDITITEM]:
            item.infos = self.addon.parse_query(urllib.unquote(queryString),{})
        else:
            item.infos = self.addon.parse_query(queryString,{})
        item.infos = dict((k.decode('utf8'), v.decode('utf8')) for k, v in item.infos.items())
        return [mode, item]


    def showListOptions(self,argv=None):
        params = self.parser.getParams()
        mode = self.parser.getIntParam(params, "mode")
        name = self.parser.getParam(params, "name")
        service = self.parser.getParam(params, "service")
        self.addon = Addon(scriptID, argv)
        #print("MMMMMMMM",mode,params)
        mymodes = [common.Mode2.VIEW ,common.Mode2.PLAY, common.Mode2.ADDTOFAVOURITES, common.Mode2.EXECUTE]
        mymodes2 = [common.Mode3.VIEW ,common.Mode3.PLAY]

        base = argv[0]
        handle = int(argv[1])
        parameter = argv[2]
        self.base = base
        self.handle = handle

        paramstring = urllib.unquote_plus(parameter)

        self.log.info(paramstring)
        self.log.info('Base: '+ base)
        self.log.info('Handle: '+ str(handle))
        self.log.info('Parameter: '+ parameter)
        self.log.info('LEN: ' + str(len(paramstring)))
        self.log.info('mode: ' + str(mode))


        if mode == None and name == None and service == None:
            self.log.info('Wyświetlam kategorie')
            self.CATEGORIES()
            #self.LIST(MENU_TABLE)
        elif mode == 1:
            self.LIST(TV_ONLINE_TABLE)
        elif mode == 4:
            self.LIST(FUN_ONLINE_TABLE)
        elif mode == 2:
            self.LIST(FILM_ONLINE_TABLE)
        elif mode == 3:
            self.LIST(SERIALE_ONLINE_TABLE)
        elif mode == 5:
            self.LIST(DOC_ONLINE_TABLE)
        elif mode == 6:
            self.LIST(VOD_ONLINE_TABLE)
        elif mode == 7:
            self.LIST(BAJKI_ONLINE_TABLE)



        elif mode == 20:
            self.log.info('Wyświetlam ustawienia')
            self.settings.showSettings()
        elif mode == 30:
            self.LIBRTMP()
        #elif mode == 30 and category=='update':
        #    self.DLLIBRTMP(name,murl)

        elif mode in mymodes:
            #try:

            # if addon is started
            listItemPath = xbmcUtils.getListItemPath()
            if not listItemPath.startswith(self.base):
                if not('mode=' in paramstring and not 'mode=110&' in paramstring):
                    xbmcplugin.setPluginFanart(self.handle, common.Paths.pluginFanart)

                    #if common.getSetting('autoupdate') == 'true':
                    #    self.update()


            # Main Menu
            if len(paramstring) <= 9:
                mainMenu = ListItem.create()
                mainMenu['url'] = self.MAIN_MENU_FILE
                tmpList = self.parseView(mainMenu)
                if tmpList:
                    self.currentlist = tmpList
            else:
                [mode, item] = self._parseParameters()
                #print("MMMMMMMM",mode,mydump(item))


                # switch(mode)
                if mode == common.Mode2.VIEW:
                    tmpList = self.parseView(item)
                    #print("MMMMMMMM",item,vars(item))
                    if tmpList:
                        self.currentlist = tmpList
                        count = len(self.currentlist.items)
                        if count == 1:
                            # Autoplay single video
                            autoplayEnabled = ptv.getSetting('autoplay') == 'true'
                            if autoplayEnabled:
                                videos = self.currentlist.getVideos()
                                if len(videos) == 1:
                                    self.playVideo(videos[0], True)


                elif mode == common.Mode2.ADDITEM:
                    tmp = os.path.normpath(paramstring.split('url=')[1])
                    if tmp:
                        suffix = tmp.split(os.path.sep)[-1]
                        tmp = tmp.replace(suffix,'') + urllib.quote_plus(suffix)
                    if self.favouritesManager.add(tmp):
                        xbmc.executebuiltin('Container.Refresh()')


                elif mode in [common.Mode2.ADDTOFAVOURITES, common.Mode2.REMOVEFROMFAVOURITES, common.Mode2.EDITITEM]:

                    if mode == common.Mode2.ADDTOFAVOURITES:
                        self.favouritesManager.addItem(item)
                    elif mode == common.Mode2.REMOVEFROMFAVOURITES:
                        self.favouritesManager.removeItem(item)
                        xbmc.executebuiltin('Container.Refresh()')
                    elif mode == common.Mode2.EDITITEM:
                        if self.favouritesManager.editItem(item):
                            xbmc.executebuiltin('Container.Refresh()')

                elif mode == common.Mode2.EXECUTE:
                    self.executeItem(item)

                elif mode == common.Mode2.PLAY:
                    self.playVideo(item)

            #except Exception, e:
            #    common.showError('Error running Mrknow')
            #    self.log.info('Error running Mrknow. m1 Reason:' + str(e))

        elif mode in mymodes2:
            try:

                # if addon is started
                listItemPath = xbmcUtils.getListItemPath()
                if not listItemPath.startswith(self.base):
                    if not('mode=' in paramstring and not 'mode=210&' in paramstring):
                        xbmcplugin.setPluginFanart(self.handle, common.Paths.pluginFanart)

                        #if common.getSetting('autoupdate') == 'true':
                        #    self.update()


                # Main Menu
                if len(paramstring) <= 9:
                    mainMenu = ListItem.create()
                    mainMenu['url'] = self.SPORT_MENU_FILE
                    tmpList = self.parseView(mainMenu)
                    if tmpList:
                        self.currentlist = tmpList
                else:
                    [mode, item] = self._parseParameters()
                    #print("MMMMMMMM",mode,mydump(item))


                    # switch(mode)
                    if mode == common.Mode2.VIEW:
                        tmpList = self.parseView(item)
                        #print("MMMMMMMM",item,vars(item))
                        if tmpList:
                            self.currentlist = tmpList
                            count = len(self.currentlist.items)
                            if count == 1:
                                # Autoplay single video
                                autoplayEnabled = ptv.getSetting('autoplay') == 'true'
                                if autoplayEnabled:
                                    videos = self.currentlist.getVideos()
                                    if len(videos) == 1:
                                        self.playVideo(videos[0], True)
            except Exception, e:
                common.showError('Error running Mrknow')
                self.log.info('Error running Mrknow. m2 Reason:' + str(e))



        elif mode == 4500 or service == 'testyonline':
            tv = testyonline.testyonline()
            tv.handleService()
Example #13
0
class Main:

    MAIN_MENU_FILE = 'mainMenu.cfg'

    def __init__(self):

        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(
            common.Paths.favouritesFolder)
        self.parser = Parser()
        self.currentlist = None

        self.addon = None

        common.log('ViendoKodiStreaming initialized')

    def playVideo(self, videoItem, isAutoplay=False):
        if not videoItem:
            return

        listitem = self.createXBMCListItem(videoItem)

        title = videoItem['videoTitle']
        if title:
            listitem.setInfo('video', {'title': title})

        if not isAutoplay:
            xbmcplugin.setResolvedUrl(self.handle, True, listitem)
        else:
            url = urllib.unquote_plus(videoItem['url'])
            xbmc.Player().play(url, listitem)

    def launchChrome(self, url, title):
        action = 'RunPlugin(%s)' % (
            'plugin://plugin.program.chrome.launcher/?kiosk=yes&mode=showSite&stopPlayback=yes&url='
            + url)
        common.log('chrome test:' + str(action))
        xbmc.executebuiltin(action)

    def playWebDriver(self, url, title):
        try:
            import liveremote
            video = liveremote.resolve(url)
            liz = xbmcgui.ListItem(title)
            liz.setPath(video)
            liz.setProperty('IsPlayable', 'true')
            xbmc.Player().play(video, liz)
        except:
            import sys, traceback
            traceback.print_exc(file=sys.stdout)
            common.showInfo('This is not the option you are looking for.')

    def getVideos(self, lItem, dia=None, percent=0, percentSpan=100):
        allitems = []

        if dia and dia.isCanceled():
            return allitems

        currentName = lItem['title']

        if lItem['type'].find('video') != -1:
            if dia:
                dia.update(percent + percentSpan, thirdline=currentName)
            allitems.append(lItem)
        else:
            tmpList = self.parser.parse(lItem).list
            if tmpList and len(tmpList.items) > 0:

                if dia:
                    dia.update(percent, secondline=currentName, thirdline=' ')

                inc = percentSpan / len(tmpList.items)
                for item in tmpList.items:

                    if dia and dia.isCanceled():
                        break

                    children = self.getVideos(item, dia, percent, inc)
                    if children:
                        allitems.extend(children)
                    percent += inc

        return allitems

    def getSearchPhrase(self):
        searchCache = os.path.join(common.Paths.cacheDir, 'search')
        default_phrase = fu.getFileContent(searchCache)
        if not default_phrase:
            default_phrase = ''

        search_phrase = common.showOSK(default_phrase, common.translate(30102))
        if search_phrase == '':
            return None
        xbmc.sleep(10)
        fu.setFileContent(searchCache, search_phrase)

        return search_phrase

    def parseView(self, lItem):
        def endOfDirectory(succeeded=True):
            if self.handle > -1:
                xbmcplugin.endOfDirectory(handle=self.handle,
                                          succeeded=succeeded,
                                          cacheToDisc=True)
            else:
                common.log('Handle -1')

        if not lItem:
            endOfDirectory(False)
            return None

        if lItem['type'] == 'search':
            search_phrase = self.getSearchPhrase()
            if not search_phrase:
                common.log("search canceled")
                endOfDirectory(False)
                return None
            else:
                lItem['type'] = 'rss'
                lItem['url'] = lItem['url'] % (
                    urllib.quote_plus(search_phrase))

        url = lItem['url']

        if url == common.Paths.customModulesFile:
            self.customModulesManager.getCustomModules()

        tmpList = None
        result = self.parser.parse(lItem)
        if result.code == ParsingResult.Code.SUCCESS:
            tmpList = result.list
        elif result.code == ParsingResult.Code.CFGFILE_NOT_FOUND:
            common.showError("No encuentro el fichero Cfg")
            endOfDirectory(False)
            return None
        elif result.code == ParsingResult.Code.CFGSYNTAX_INVALID:
            common.showError("sintaxis invalida en Cfg")
            endOfDirectory(False)
            return None
        elif result.code == ParsingResult.Code.WEBREQUEST_FAILED:
            common.showError("Ha fallado la llamada a la Web")
            if len(result.list.items) > 0:
                tmpList = result.list
            else:
                endOfDirectory(False)
                return None

        # if it's the main menu, add folder 'Favourites' and 'Custom Modules
        if url == self.MAIN_MENU_FILE:
            tmp = ListItem.create()
            tmp['title'] = ' [COLOR blue]ViendoKodi[/COLOR] [COLOR red]Streaming[/COLOR]'
            tmp['type'] = 'say'
            tmp['url'] = ''
            tmp['icon'] = os.path.join(common.Paths.imgDir, 'icon.png')
            tmpList.items.insert(0, tmp)
            tmp = ListItem.create()
            tmp['title'] = '[COLOR red][B] [/B][/COLOR]'
            tmp['type'] = 'say'
            tmp['url'] = ''
            tmp['icon'] = os.path.join(common.Paths.imgDir, 'icon.png')

            tmpList.items.insert(1, tmp)

            #tmp = ListItem.create()
            #tmp['title'] = '[COLOR red]Custom Modules[/COLOR]'
            #tmp['type'] = 'rss'
            #tmp['url'] = os.path.join(common.Paths.customModulesDir, 'custom.cfg')
            #tmpList.items.insert(0, tmp)

        # if it's the favourites menu, add item 'Add item'
        elif url == common.Paths.favouritesFile or url.startswith(
                'favfolders'):

            if url.startswith('favfolders'):
                url = os.path.normpath(
                    os.path.join(common.Paths.favouritesFolder, url))

            tmp = ListItem.create()
            tmp['title'] = 'Add item...'
            tmp['type'] = 'command'
            tmp['icon'] = os.path.join(common.Paths.imgDir, 'bookmark_add.png')
            action = 'RunPlugin(%s)' % (self.base + '?mode=' +
                                        str(Mode.ADDITEM) + '&url=' + url)
            tmp['url'] = action
            tmpList.items.append(tmp)

        # Create menu or play, if it's a single video and autoplay is enabled
        count = len(tmpList.items)
        if (count == 0 and not url.startswith('favfolders')):
            common.showInfo('No stream available')
            #Directory with 0 items
            endOfDirectory(False)
        elif not (common.getSetting('autoplay') == 'true' and count == 1
                  and len(tmpList.getVideos()) == 1):
            # sort methods
            sortKeys = tmpList.sort.split('|')
            setSortMethodsForCurrentXBMCList(self.handle, sortKeys)

            # Add items to XBMC list
            for m in tmpList.items:
                self.addListItem(m, len(tmpList.items))
            #Directory with >1 items
            endOfDirectory(True)
        else:
            #Directory with 0 items
            endOfDirectory(False)
        return tmpList

    def createXBMCListItem(self, item):
        title = item['title']

        m_type = item['type']

        icon = item['icon']

        if icon and not icon.startswith('http'):
            try:
                if not fu.fileExists(icon):
                    tryFile = os.path.join(common.Paths.modulesDir, icon)
                    if not fu.fileExists(tryFile):
                        tryFile = os.path.join(common.Paths.customModulesDir,
                                               icon)
                    if fu.fileExists(tryFile):
                        icon = tryFile
            except:
                pass

        if not icon:
            if m_type == 'video':
                icon = common.Paths.defaultVideoIcon
            else:
                icon = common.Paths.defaultCategoryIcon

        fanart = item['fanart']
        if not fanart:
            fanart = common.Paths.pluginFanart

        liz = xbmcgui.ListItem(title)
        try:
            liz.setArt({'thumb': icon, 'fanart': fanart})
        except:
            liz.setProperty('fanart_image', fanart)
            liz.setThumbnailImage(icon)
            common.log('main.py:374: setThumbnailImage is deprecated')
        """
        General Values that apply to all types:
            count         : integer (12) - can be used to store an id for later, or for sorting purposes
            size          : long (1024) - size in bytes
            date          : string (%d.%m.%Y / 01.01.2009) - file date

        Video Values:
            genre         : string (Comedy)
            year          : integer (2009)
            episode       : integer (4)
            season        : integer (1)
            top250        : integer (192)
            tracknumber   : integer (3)
            rating        : float (6.4) - range is 0..10
            watched       : depreciated - use playcount instead
            playcount     : integer (2) - number of times this item has been played
            overlay       : integer (2) - range is 0..8.  See GUIListItem.h for values
            cast          : list (Michal C. Hall)
            castandrole   : list (Michael C. Hall|Dexter)
            director      : string (Dagur Kari)
            mpaa          : string (PG-13)
            plot          : string (Long Description)
            plotoutline   : string (Short Description)
            title         : string (Big Fan)
            originaltitle : string (Big Fan)
            duration      : string (3:18)
            studio        : string (Warner Bros.)
            tagline       : string (An awesome movie) - short description of movie
            writer        : string (Robert D. Siegel)
            tvshowtitle   : string (Heroes)
            premiered     : string (2005-03-04)
            status        : string (Continuing) - status of a TVshow
            code          : string (tt0110293) - IMDb code
            aired         : string (2008-12-07)
            credits       : string (Andy Kaufman) - writing credits
            lastplayed    : string (%Y-%m-%d %h:%m:%s = 2009-04-05 23:16:04)
            album         : string (The Joshua Tree)
            votes         : string (12345 votes)
            trailer       : string (/home/user/trailer.avi)
        """

        infoLabels = {}
        for video_info_name in item.infos.keys():
            infoLabels[video_info_name] = item[video_info_name]
        infoLabels['title'] = title

        liz.setInfo('video', infoLabels)

        url = urllib.unquote_plus(item['url'])
        liz.setPath(url)

        if m_type == 'video':
            liz.setProperty('IsPlayable', 'true')

        if title.startswith('p2pcast'):
            try:
                liz.setMimeType('application/vnd.apple.mpegurl')
                #liz.setContentLookup(False)
            except:
                common.showError('Update Kodi to 16+ to view this stream')
                return None

        return liz

    def addListItem(self, lItem, totalItems):
        def createContextMenuItem(label, mode, codedItem):
            action = 'XBMC.RunPlugin(%s)' % (self.addon.build_plugin_url(
                {
                    'mode': str(mode),
                    'item': codedItem
                }))
            return (label, action)

        def encoded_dict(in_dict):
            out_dict = {}
            for k, v in in_dict.iteritems():
                if isinstance(v, unicode):
                    v = v.encode('utf8')
                elif isinstance(v, str):
                    # Must be encoded in UTF-8
                    v.decode('utf8')
                out_dict[k] = v
            return urllib.urlencode(out_dict)

        contextMenuItems = []
        definedIn = lItem['definedIn']

        codedItem = urllib.quote(encoded_dict(lItem.infos))

        if definedIn:
            # Queue
            #contextMenuItem = createContextMenuItem('Queue', Mode.QUEUE, codedItem)
            #contextMenuItems.append(contextMenuItem)

            # Favourite
            if definedIn.endswith('favourites.cfg') or definedIn.startswith(
                    "favfolders/"):
                # Remove from favourites
                contextMenuItem = createContextMenuItem(
                    'Remove', Mode.REMOVEFROMFAVOURITES, codedItem)
                contextMenuItems.append(contextMenuItem)

                # Edit label
                contextMenuItem = createContextMenuItem(
                    'Edit', Mode.EDITITEM, codedItem)
                contextMenuItems.append(contextMenuItem)

            else:
                # Custom module
                if definedIn.endswith('custom.cfg'):
                    # Remove from custom modules
                    contextMenuItem = createContextMenuItem(
                        'Remove module', Mode.REMOVEFROMCUSTOMMODULES,
                        codedItem)
                    contextMenuItems.append(contextMenuItem)

                #if lItem['title'] != "Favourites":
                # Add to favourites
                #contextMenuItem = createContextMenuItem('Add to ViendoKodiStreaming favourites', Mode.ADDTOFAVOURITES, codedItem)
                #contextMenuItems.append(contextMenuItem)
                #contextMenuItem = createContextMenuItem('Open with Chrome launcher', Mode.CHROME, codedItem)
                #contextMenuItems.append(contextMenuItem)
                #contextMenuItem = createContextMenuItem('Open with WebDriver', Mode.WEBDRIVER, codedItem)
                #contextMenuItems.append(contextMenuItem)

        liz = self.createXBMCListItem(lItem)

        m_type = lItem['type']
        if not m_type:
            m_type = 'rss'

        if m_type == 'video':
            u = self.base + '?mode=' + str(Mode.PLAY) + '&item=' + codedItem
            if lItem['IsDownloadable']:
                contextMenuItem = createContextMenuItem(
                    'Download', Mode.DOWNLOAD, codedItem)
                contextMenuItems.append(contextMenuItem)
            isFolder = False
        elif m_type == 'say':
            u = self.base + '?mode=' + str(
                Mode.SAY) + '&item=' + " "  #codedItem
            isFolder = False
        elif m_type.find('command') > -1:
            u = self.base + '?mode=' + str(Mode.EXECUTE) + '&item=' + codedItem
            isFolder = False
        else:
            u = self.base + '?mode=' + str(Mode.VIEW) + '&item=' + codedItem
            isFolder = True

        liz.addContextMenuItems(contextMenuItems)
        xbmcplugin.addDirectoryItem(handle=self.handle,
                                    url=u,
                                    listitem=liz,
                                    isFolder=isFolder,
                                    totalItems=totalItems)

    def clearCache(self):
        cacheDir = common.Paths.cacheDir
        if not os.path.exists(cacheDir):
            os.mkdir(cacheDir, 0777)
            common.log('Cache directory created' + str(cacheDir))
        else:
            size, within_limit = fu.checkQuota(cacheDir)
            if not within_limit:
                fu.clearDirectory(cacheDir)
                common.log('Cache directory purged')
            common.log('Cache Usage:' + str(size))

    def queueAllVideos(self, item):
        dia = DialogProgress()
        dia.create('ViendoKodiStreaming', 'Get videos...' + item['title'])
        dia.update(0)

        items = self.getVideos(item, dia)
        if items:
            for it in items:
                item = self.createXBMCListItem(it)
                queries = {
                    'mode': str(Mode.PLAY),
                    'url': self.addon.build_plugin_url(it.infos)
                }
                uc = self.addon.build_plugin_url(queries)
                xbmc.PlayList(xbmc.PLAYLIST_VIDEO).add(uc, item)
            resultLen = len(items)
            msg = 'Queued ' + str(resultLen) + ' video'
            if resultLen > 1:
                msg += 's'
            dia.update(100, msg)
            xbmc.sleep(500)
            dia.update(100, msg, ' ', ' ')
        else:
            dia.update(0, 'No items found', ' ')

        xbmc.sleep(700)
        dia.close()

    def executeItem(self, item):
        url = item['url']
        if '(' in url:
            xbmcCommand = parseText(url, '([^\(]*).*')
            if xbmcCommand.lower() in [
                    'activatewindow', 'runscript', 'runplugin', 'playmedia'
            ]:
                if xbmcCommand.lower() == 'activatewindow':
                    params = parseText(url, '.*\(\s*(.+?)\s*\).*').split(',')
                    for i in range(len(params) - 1, -1, -1):
                        p = params[i]
                        if p == 'return':
                            params.remove(p)
                    path = params[len(params) - 1]
                    xbmc.executebuiltin('Container.Update(' + path + ')')
                    return
                xbmc.executebuiltin(url)

    def _parseParameters(self):
        mode = int(self.addon.queries['mode'])
        queryString = self.addon.queries['item']
        item = ListItem.create()
        if mode in [
                Mode.CHROME, Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES,
                Mode.EDITITEM, Mode.WEBDRIVER
        ]:
            item.infos = self.addon.parse_query(urllib.unquote(queryString),
                                                {})
        else:
            item.infos = self.addon.parse_query(queryString, {})
        item.infos = dict((k.decode('utf8'), v.decode('utf8'))
                          for k, v in item.infos.items())
        return [mode, item]

    def run(self, argv=None):

        self.addon = Addon('plugin.video.ViendoKodiStreaming', argv)

        common.log('ViendoKodiStreaming running')

        base = argv[0]
        handle = int(argv[1])
        parameter = argv[2]
        self.base = base
        self.handle = handle

        paramstring = urllib.unquote_plus(parameter)
        common.log(paramstring)

        try:

            # if addon is started
            listItemPath = xbmcUtils.getListItemPath()
            if not listItemPath.startswith(self.base):
                if not ('mode=' in paramstring
                        and not 'mode=1&' in paramstring):
                    xbmcplugin.setPluginFanart(self.handle,
                                               common.Paths.pluginFanart)
                    self.clearCache()

                    #if common.getSetting('autoupdate') == 'true':
                    #    self.update()

            # Main Menu
            if '98VKS' in paramstring:
                nametorrent = os.path.normpath(paramstring.split('url=')[1])
                if nametorrent == 'quasar':
                    addonTorrent = 'item_info_build=plugin://plugin.video.quasar/play?uri=%s'
                elif nametorrent == 'pulsar':
                    addonTorrent = 'item_info_build=plugin://plugin.video.pulsar/play?uri=%s'
                elif nametorrent == 'kmediatorrent':
                    addonTorrent = 'item_info_build=plugin://plugin.video.kmediatorrent/play/%s'
                elif nametorrent == "torrenter":
                    addonTorrent = 'item_info_build=plugin://plugin.video.torrenter/?action=playSTRM&url=%s&not_download_only=True'
                elif nametorrent == "yatp":
                    addonTorrent = 'item_info_build=plugin://plugin.video.yatp/?action=play&torrent=%s'
                else:
                    addonTorrent = 'item_info_build=plugin://plugin.video.xbmctorrent/play/%s'

                cFichero = common.Paths.catchersDir + '/' + 'torrent.txt'
                outfile = open(cFichero, 'w')  # Indicamos el valor 'w'.
                outfile.write('item_info_name=url\n' +
                              'item_info_from=@PARAM1@\n' + addonTorrent +
                              '\n')
                outfile.close()
                common.showInfo(
                    '[COLOR red]NO INSTALAR[/COLOR] conjuntamente los addon pulsar y quasar.\nYa que tendremos problemas de compatibilidad y no funcionará ninguno. [COLOR lime] \nPara los torrent se utilizará: [/COLOR] '
                    + nametorrent)

            elif len(paramstring) <= 2:
                mainMenu = ListItem.create()
                mainMenu['url'] = self.MAIN_MENU_FILE
                tmpList = self.parseView(mainMenu)
                if tmpList:
                    self.currentlist = tmpList

            else:
                [mode, item] = self._parseParameters()

                # switch(mode)
                if mode == Mode.VIEW:
                    tmpList = self.parseView(item)
                    if tmpList:
                        self.currentlist = tmpList
                        count = len(self.currentlist.items)
                        if count == 1:
                            # Autoplay single video
                            autoplayEnabled = common.getSetting(
                                'autoplay') == 'true'
                            if autoplayEnabled:
                                videos = self.currentlist.getVideos()
                                if len(videos) == 1:
                                    self.playVideo(videos[0], True)

                elif mode == Mode.ADDITEM:
                    tmp = os.path.normpath(paramstring.split('url=')[1])
                    if tmp:
                        suffix = tmp.split(os.path.sep)[-1]
                        tmp = tmp.replace(suffix,
                                          '') + urllib.quote_plus(suffix)
                    if self.favouritesManager.add(tmp):
                        xbmc.executebuiltin('Container.Refresh()')

                elif mode in [
                        Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES,
                        Mode.EDITITEM
                ]:

                    if mode == Mode.ADDTOFAVOURITES:
                        self.favouritesManager.addItem(item)
                    elif mode == Mode.REMOVEFROMFAVOURITES:
                        self.favouritesManager.removeItem(item)
                        xbmc.executebuiltin('Container.Refresh()')
                    elif mode == Mode.EDITITEM:
                        if self.favouritesManager.editItem(item):
                            xbmc.executebuiltin('Container.Refresh()')

                elif mode == Mode.EXECUTE:
                    self.executeItem(item)

                elif mode == Mode.PLAY:
                    self.playVideo(item)

                elif mode == Mode.WEBDRIVER:
                    url = urllib.quote(item['url'])
                    title = item['title']
                    self.playWebDriver(url, title)

                elif mode == Mode.QUEUE:
                    self.queueAllVideos(item)

                elif mode == Mode.CHROME:
                    url = urllib.quote(item['url'])
                    title = item['title']
                    self.launchChrome(url, title)

                elif mode == Mode.SAY:
                    #title = item['title']
                    url = ""

                elif mode == Mode.INSTALLADDON:
                    success = install(item['url'])
                    if success:
                        xbmc.sleep(100)
                        if xbmcUtils.getCurrentWindowXmlFile(
                        ) == 'DialogAddonSettings.xml':
                            # workaround to update settings dialog
                            common.setSetting('', '')

        except Exception, e:
            common.showError('Error running ViendoKodiStreaming')
            common.log('Error running ViendoKodiStreaming. Reason:' + str(e))
Example #14
0
class Main:

    MAIN_MENU_FILE = 'mainMenu.cfg'


    def __init__(self):
        
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(common.Paths.customModulesDir, common.Paths.customModulesRepo)
        
        # todo: cache this (due to limited API calls for github)
        self.syncManager = SyncManager()
        #self.syncManager.addSource("Max Mustermann - Catchers", SyncSourceType.CATCHERS, common.Paths.catchersRepo)
        #self.syncManager.addSource("Max Mustermann - Modules", SyncSourceType.MODULES, common.Paths.modulesRepo)
        
        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser = Parser()
        self.currentlist = None
        
        self.addon = None
        
        common.log('SportsDevil initialized')
        


    def getPlayerType(self):
        sPlayerType = common.getSetting('playerType')
        
        if (sPlayerType == '0'):
            return xbmc.PLAYER_CORE_AUTO
        elif (sPlayerType == '1'):
            return xbmc.PLAYER_CORE_MPLAYER
        elif (sPlayerType == '2'):
            return xbmc.PLAYER_CORE_DVDPLAYER        
        # PLAYER_CORE_AMLPLAYER
        elif (sPlayerType == '3'):
            return 5

        return xbmc.PLAYER_CORE_AUTO


    def playVideo(self, videoItem, isAutoplay = False):
        if not videoItem:
            return

        listitem = self.createXBMCListItem(videoItem)

        title = videoItem['videoTitle']
        if title:
            listitem.setInfo('video', {'title': enc.clean_safe(title)})

        if not isAutoplay:
            xbmcplugin.setResolvedUrl(self.handle, True, listitem)
        else:
            url = urllib.unquote_plus(videoItem['url'])
            xbmc.Player(self.getPlayerType()).play(url, listitem)


    def downloadVideo(self, url, title):
        common.log('Trying to download video ' + str(url))

        # check url
        if url.startswith('plugin'):
            common.log('Video is not downloadable')
            return None

        path = common.getSetting('download_path')
        if not path:
            path = common.browseFolders(common.translate(30017))
            common.setSetting('download_path', path)

        title = getKeyboard(default = fu.cleanFilename(title),heading='SportsDevil')
        if title == None or title == '':
            return None

        downloader = Downloader()
        downloaded_file = downloader.downloadMovie(url, path,  fu.cleanFilename(title), '.flv')

        if downloaded_file == None:
            common.log ('Download cancelled')
        else:
            common.log('Video ' + url + " downloaded to '" + downloaded_file + "'")

        return downloaded_file


    def getVideos(self, lItem, dia = None, percent = 0, percentSpan = 100):
        allitems = []
        
        if dia and dia.isCanceled():
            return allitems
        
        currentName = lItem['title']

        if lItem['type'].find('video') != -1:
            if dia:
                dia.update(percent + percentSpan, thirdline=currentName)
            allitems.append(lItem)
        else:
            tmpList = self.parser.parse(lItem).list
            if tmpList and len(tmpList.items) > 0:
                
                if dia:
                    dia.update(percent, secondline=currentName, thirdline=' ')
                
                inc = percentSpan/len(tmpList.items)
                for item in tmpList.items:
                    
                    if dia and dia.isCanceled():
                        break
                    
                    children = self.getVideos(item, dia, percent, inc)
                    if children:
                        allitems.extend(children)
                    percent += inc

        return allitems


    def getSearchPhrase(self):
        searchCache = os.path.join(common.Paths.cacheDir, 'search')
        default_phrase = fu.getFileContent(searchCache)
        if not default_phrase:
            default_phrase = ''

        search_phrase = common.showOSK(default_phrase, common.translate(30102))
        if search_phrase == '':
            return None
        xbmc.sleep(10)
        fu.setFileContent(searchCache, search_phrase)

        return search_phrase


    def parseView(self, lItem):

        def endOfDirectory(succeeded=True):
            xbmcplugin.endOfDirectory(handle=self.handle, succeeded=succeeded, cacheToDisc=True)

        if not lItem:
            endOfDirectory(False)
            return None

        if lItem['type'] == 'search':
            search_phrase = self.getSearchPhrase()
            if not search_phrase:
                common.log("search canceled")
                endOfDirectory(False)
                return None
            else:
                lItem['type'] = 'rss'
                lItem['url'] =  lItem['url'] % (urllib.quote_plus(search_phrase))

        url = lItem['url']

        if url == common.Paths.customModulesFile: 
            self.customModulesManager.getCustomModules()

        tmpList = None
        result = self.parser.parse(lItem)
        if result.code == ParsingResult.Code.SUCCESS:
            tmpList = result.list
        else:
            if result.code == ParsingResult.Code.CFGFILE_NOT_FOUND:
                common.showError("Cfg file not found")
            elif result.code == ParsingResult.Code.CFGSYNTAX_INVALID:
                common.showError("Cfg syntax invalid")
            elif result.code == ParsingResult.Code.WEBREQUEST_FAILED:
                common.showError("Web request failed")

            endOfDirectory(False)
            return None

        # if it's the main menu, add folder 'Favourites' and 'Custom Modules
        if url == self.MAIN_MENU_FILE:
            tmp = ListItem.create()
            tmp['title'] = '[COLOR green]Favourites[/COLOR]'
            tmp['type'] = 'rss'
            tmp['icon'] = os.path.join(common.Paths.imgDir, 'bookmark.png')
            tmp['url'] = str(common.Paths.favouritesFile)
            tmpList.items.insert(0, tmp)
            
            tmp = ListItem.create()
            tmp['title'] = '[COLOR red]Custom Modules[/COLOR]'
            tmp['type'] = 'rss'
            tmp['url'] = os.path.join(common.Paths.customModulesDir, 'custom.cfg')
            tmpList.items.insert(0, tmp)

        # if it's the favourites menu, add item 'Add item'
        elif url == common.Paths.favouritesFile or url.startswith('favfolders'):
            
            if url.startswith('favfolders'):
                url = os.path.normpath(os.path.join(common.Paths.favouritesFolder, url)) 
            
            tmp = ListItem.create()
            tmp['title'] = 'Add item...'
            tmp['type'] = 'command'
            tmp['icon'] = os.path.join(common.Paths.imgDir, 'bookmark_add.png')
            action = 'RunPlugin(%s)' % (self.base + '?mode=' + str(Mode.ADDITEM) + '&url=' + url)
            tmp['url'] = action
            tmpList.items.append(tmp)
        
        # if it's the custom modules  menu, add item 'more...'
        elif url == common.Paths.customModulesFile:            
            tmp = ListItem.create()
            tmp['title'] = 'more...'
            tmp['type'] = 'command'
            #tmp['icon'] = os.path.join(common.Paths.imgDir, 'bookmark_add.png')
            action = 'RunPlugin(%s)' % (self.base + '?mode=' + str(Mode.DOWNLOADCUSTOMMODULE) + '&url=')
            tmp['url'] = action
            tmpList.items.append(tmp)


        # Create menu or play, if it's a single video and autoplay is enabled
        proceed = False

        count = len(tmpList.items)
        if count == 0:
            if url.startswith('favfolders'):
                proceed = True
            else:
                common.showInfo('No stream available')
        elif count > 0 and not (common.getSetting('autoplay') == 'true' and count == 1 and len(tmpList.getVideos()) == 1):
            # sort methods
            sortKeys = tmpList.sort.split('|')
            setSortMethodsForCurrentXBMCList(self.handle, sortKeys)

            # Add items to XBMC list
            for m in tmpList.items:
                self.addListItem(m, len(tmpList.items))

            proceed = True

        endOfDirectory(proceed)
        common.log('End of directory')
        return tmpList


    def downloadCustomModule(self):
        success = self.customModulesManager.downloadCustomModules()
        if success == True:            
            # refresh container if SportsDevil is active
            currContainer = xbmcUtils.getContainerFolderPath()
            common.showNotification('SportsDevil', 'Download successful', 1000)
            if currContainer.startswith(self.base):                
                xbmc.executebuiltin('Container.Refresh()')
            return True
        elif success == False:        
            common.showNotification('SportsDevil', 'Download failed', 1000)
        return False


    def removeCustomModule(self, item):
        name = urllib.unquote(item["title"])
        success = self.customModulesManager.removeCustomModule(name)
        if success:
            xbmc.executebuiltin('Container.Refresh()')
            

    def createXBMCListItem(self, item):
        title = enc.clean_safe(item['title'])

        m_type = item['type']

        icon = item['icon']

        if icon and not icon.startswith('http'):
            try:
                if not fu.fileExists(icon):
                    tryFile = os.path.join(common.Paths.modulesDir, icon)
                    if not fu.fileExists(tryFile):
                        tryFile = os.path.join(common.Paths.customModulesDir, icon)
                    if fu.fileExists(tryFile):
                        icon = tryFile
            except:
                pass

        if not icon:
            if m_type == 'video':
                icon = common.Paths.defaultVideoIcon
            else:
                icon = common.Paths.defaultCategoryIcon
                
        liz = xbmcgui.ListItem(title, title, iconImage=icon, thumbnailImage=icon)

        fanart = item['fanart']
        if not fanart:
            fanart = common.Paths.pluginFanart
        liz.setProperty('fanart_image', fanart)

        """
        General Values that apply to all types:
            count         : integer (12) - can be used to store an id for later, or for sorting purposes
            size          : long (1024) - size in bytes
            date          : string (%d.%m.%Y / 01.01.2009) - file date

        Video Values:
            genre         : string (Comedy)
            year          : integer (2009)
            episode       : integer (4)
            season        : integer (1)
            top250        : integer (192)
            tracknumber   : integer (3)
            rating        : float (6.4) - range is 0..10
            watched       : depreciated - use playcount instead
            playcount     : integer (2) - number of times this item has been played
            overlay       : integer (2) - range is 0..8.  See GUIListItem.h for values
            cast          : list (Michal C. Hall)
            castandrole   : list (Michael C. Hall|Dexter)
            director      : string (Dagur Kari)
            mpaa          : string (PG-13)
            plot          : string (Long Description)
            plotoutline   : string (Short Description)
            title         : string (Big Fan)
            originaltitle : string (Big Fan)
            duration      : string (3:18)
            studio        : string (Warner Bros.)
            tagline       : string (An awesome movie) - short description of movie
            writer        : string (Robert D. Siegel)
            tvshowtitle   : string (Heroes)
            premiered     : string (2005-03-04)
            status        : string (Continuing) - status of a TVshow
            code          : string (tt0110293) - IMDb code
            aired         : string (2008-12-07)
            credits       : string (Andy Kaufman) - writing credits
            lastplayed    : string (%Y-%m-%d %h:%m:%s = 2009-04-05 23:16:04)
            album         : string (The Joshua Tree)
            votes         : string (12345 votes)
            trailer       : string (/home/user/trailer.avi)
        """

        infoLabels = {}
        for video_info_name in item.infos.keys():
            infoLabels[video_info_name] = enc.clean_safe(item[video_info_name])
        infoLabels['title'] = title

        liz.setInfo('video', infoLabels)

        url = urllib.unquote_plus(item['url'])
        liz.setPath(url)

        if m_type == 'video':
            liz.setProperty('IsPlayable','true')

        return liz


    def addListItem(self, lItem, totalItems):
        def createContextMenuItem(label, mode, codedItem):
            action = 'XBMC.RunPlugin(%s)' % (self.addon.build_plugin_url({'mode': str(mode), 'item': codedItem}))
            return (label, action)

        def encoded_dict(in_dict):
            out_dict = {}
            for k, v in in_dict.iteritems():
                if isinstance(v, unicode):
                    v = v.encode('utf8')
                elif isinstance(v, str):
                    # Must be encoded in UTF-8
                    v.decode('utf8')
                out_dict[k] = v
            return urllib.urlencode(out_dict)
        
        contextMenuItems = []
        definedIn = lItem['definedIn']

        codedItem = urllib.quote(encoded_dict(lItem.infos))

        if definedIn:
            # Queue
            contextMenuItem = createContextMenuItem('Queue', Mode.QUEUE, codedItem)
            contextMenuItems.append(contextMenuItem)

            # Favourite
            if definedIn.endswith('favourites.cfg') or definedIn.startswith("favfolders/"):
                # Remove from favourites
                contextMenuItem = createContextMenuItem('Remove', Mode.REMOVEFROMFAVOURITES, codedItem)
                contextMenuItems.append(contextMenuItem)

                # Edit label
                contextMenuItem = createContextMenuItem('Edit', Mode.EDITITEM, codedItem)
                contextMenuItems.append(contextMenuItem)

            else:
                # Custom module
                if definedIn.endswith('custom.cfg'):
                    # Remove from custom modules
                    contextMenuItem = createContextMenuItem('Remove module', Mode.REMOVEFROMCUSTOMMODULES, codedItem)
                    contextMenuItems.append(contextMenuItem)
    
                if lItem['title'] != "Favourites":
                        # Add to favourites
                        contextMenuItem = createContextMenuItem('Add to SportsDevil favourites', Mode.ADDTOFAVOURITES, codedItem)
                        contextMenuItems.append(contextMenuItem)

        liz = self.createXBMCListItem(lItem)

        m_type = lItem['type']
        if not m_type:
            m_type = 'rss'
        
        if m_type == 'video':
            u = self.base + '?mode=' + str(Mode.PLAY) + '&item=' + codedItem
            if lItem['IsDownloadable']:
                contextMenuItem = createContextMenuItem('Download', Mode.DOWNLOAD, codedItem)
                contextMenuItems.append(contextMenuItem)
            isFolder = False
        elif m_type.find('command') > -1:
            u = self.base + '?mode=' + str(Mode.EXECUTE) + '&item=' + codedItem
            isFolder = False
        else:
            u = self.base + '?mode=' + str(Mode.VIEW) + '&item=' + codedItem
            isFolder = True

        liz.addContextMenuItems(contextMenuItems)
        xbmcplugin.addDirectoryItem(handle = self.handle, url = u, listitem = liz, isFolder = isFolder, totalItems = totalItems)


    def clearCache(self):
        cacheDir = common.Paths.cacheDir
        if not os.path.exists(cacheDir):
            os.mkdir(cacheDir, 0777)
            common.log('Cache directory created' + str(cacheDir))
        else:
            fu.clearDirectory(cacheDir)
            common.log('Cache directory purged')


    def update(self):
        
        def checkForUpdates():
            updates = {}          
            common.showNotification('SportsDevil', common.translate(30275))
            xbmcUtils.showBusyAnimation()
                 
            catchersUpdates = self.syncManager.getUpdates(SyncSourceType.CATCHERS, common.Paths.catchersDir)
            if len(catchersUpdates) > 0:
                updates["Catchers"] = catchersUpdates    
            modulesUpdates = self.syncManager.getUpdates(SyncSourceType.MODULES, common.Paths.modulesDir)
            if len(modulesUpdates) > 0:
                updates["Modules"] = modulesUpdates
            
            xbmcUtils.hideBusyAnimation()
            return updates
        
        def doUpdates(typeName, updates):
            count = len(updates)
            
            head = "SportsDevil Updates - %s" % typeName
            
            msg = common.translate(30277)
            if count == 1:
                msg = common.translate(30276)
                
            question = ("%s %s: " % (count, msg)) + ', '.join(map(lambda u: u.split('/')[-1], updates.keys())) + '\n'
            question += common.translate(30278)
            
            updates = updates.values()
            
            countFailed = 0
    
            dlg = DialogQuestion()
            dlg.head = head
            if dlg.ask(question):
                dlg = DialogProgress()
                firstline = common.translate(30279)
                dlg.create(head, firstline, " ")
           
                for i in range(0, count):
                    update = updates[i]
                    percent = int((i+1.0)*100/count)
                    dlg.update(percent, firstline, update.name)
                    if not update.do():
                        countFailed += 1
                
                msg = " "
                if countFailed > 0:
                    msg = "%s %s" % (countFailed, common.translate(30280))
                    
                dlg.update(100, msg, " ")
                xbmc.sleep(500)
                dlg.close()
            
        allupdates = checkForUpdates()
        count = len(allupdates)
        if count == 0:
            common.showNotification('SportsDevil', common.translate(30273))
            return
        else:
            for key, value in allupdates.items():
                doUpdates(key, value)


    def queueAllVideos(self, item):
        dia = DialogProgress()
        dia.create('SportsDevil', 'Get videos...' + item['title'])
        dia.update(0)

        items = self.getVideos(item, dia)
        if items:
            for it in items:
                item = self.createXBMCListItem(it)
                queries = {'mode': str(Mode.PLAY), 'url': self.addon.build_plugin_url(it.infos)}
                uc = self.addon.build_plugin_url(queries)
                xbmc.PlayList(xbmc.PLAYLIST_VIDEO).add(uc, item)
            resultLen = len(items)
            msg = 'Queued ' + str(resultLen) + ' video'
            if resultLen > 1:
                msg += 's'
            dia.update(100, msg)
            xbmc.sleep(500)
            dia.update(100, msg,' ',' ')
        else:
            dia.update(0, 'No items found',' ')

        xbmc.sleep(700)
        dia.close()        
                

    def executeItem(self, item):
        url = item['url']
        if '(' in url:
            xbmcCommand = parseText(url,'([^\(]*).*')
            if xbmcCommand.lower() in ['activatewindow', 'runscript', 'runplugin', 'playmedia']:
                if xbmcCommand.lower() == 'activatewindow':
                    params = parseText(url, '.*\(\s*(.+?)\s*\).*').split(',')
                    for i in range(len(params)-1,-1,-1):
                        p = params[i]
                        if p == 'return':
                            params.remove(p)
                    path = enc.unescape(params[len(params)-1])
                    xbmc.executebuiltin('Container.Update(' + path + ')')
                    return
                xbmc.executebuiltin(enc.unescape(url))


    def _parseParameters(self):
        mode = int(self.addon.queries['mode'])
        queryString = self.addon.queries['item']
        item = ListItem.create()
	if mode in [Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES, Mode.EDITITEM]:
        	item.infos = self.addon.parse_query(urllib.unquote(queryString),{})
	else:
		item.infos = self.addon.parse_query(queryString,{})
        return [mode, item]


    def run(self, argv=None):
        
        self.addon = Addon('plugin.video.SportsDevil', argv)

        common.log('SportsDevil running')
        
        base = argv[0]
        handle = int(argv[1])
        parameter = argv[2]
        self.base = base
        self.handle = handle
        
        paramstring = urllib.unquote_plus(parameter)
        
        try:
            
            # if addon is started
            listItemPath = xbmcUtils.getListItemPath()
            if not listItemPath.startswith(self.base):
                if not('mode=' in paramstring and not 'mode=1&' in paramstring):   
                    xbmcplugin.setPluginFanart(self.handle, common.Paths.pluginFanart)
                    self.clearCache()                    
                     
                    #if common.getSetting('autoupdate') == 'true':    
                    #    self.update()
            
            
            # Main Menu
            if len(paramstring) <= 2:                
                mainMenu = ListItem.create()
                mainMenu['url'] = self.MAIN_MENU_FILE
                tmpList = self.parseView(mainMenu)
                if tmpList:
                    self.currentlist = tmpList
                
            else:
                [mode, item] = self._parseParameters()

                # switch(mode)
                if mode == Mode.VIEW:
                    tmpList = self.parseView(item)
                    if tmpList:
                        self.currentlist = tmpList
                        count = len(self.currentlist.items)
                        if count == 1:
                            # Autoplay single video
                            autoplayEnabled = common.getSetting('autoplay') == 'true'
                            if autoplayEnabled:
                                videos = self.currentlist.getVideos()
                                if len(videos) == 1:
                                    self.playVideo(videos[0], True)


                elif mode == Mode.ADDITEM:
                    tmp = os.path.normpath(paramstring.split('url=')[1])
                    if tmp:
                        suffix = tmp.split(os.path.sep)[-1]
                        tmp = tmp.replace(suffix,'') + urllib.quote_plus(suffix)
                    if self.favouritesManager.add(tmp):
                        xbmc.executebuiltin('Container.Refresh()')


                elif mode in [Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES, Mode.EDITITEM]:

                    if mode == Mode.ADDTOFAVOURITES:
                        self.favouritesManager.addItem(item)
                    elif mode == Mode.REMOVEFROMFAVOURITES:
                        self.favouritesManager.removeItem(item)
                        xbmc.executebuiltin('Container.Refresh()')
                    elif mode == Mode.EDITITEM:
                        if self.favouritesManager.editItem(item):
                            xbmc.executebuiltin('Container.Refresh()')


                elif mode == Mode.EXECUTE:
                    self.executeItem(item)

                elif mode == Mode.PLAY:
                    self.playVideo(item)

                elif mode == Mode.QUEUE:
                    self.queueAllVideos(item)

                elif mode == Mode.DOWNLOAD:
                    url = urllib.unquote(item['url'])
                    title = item['title']
                    self.downloadVideo(url, title)
                
                elif mode == Mode.REMOVEFROMCUSTOMMODULES:
                    self.removeCustomModule(item)
                
                #elif mode == Mode.UPDATE:
                #    self.update()
                    
                elif mode == Mode.DOWNLOADCUSTOMMODULE:
                    self.downloadCustomModule()
                
                elif mode == Mode.INSTALLADDON:
                    success = install(item['url'])
                    if success:
                        xbmc.sleep(100)
                        if xbmcUtils.getCurrentWindowXmlFile() == 'DialogAddonSettings.xml':
                            # workaround to update settings dialog
                            common.setSetting('', '')
                            

        except Exception, e:
            common.showError('Error running SportsDevil')
            common.log('Error running SportsDevil. Reason:' + str(e))
Example #15
0
class Main:

    MAIN_MENU_FILE = 'mainMenu.cfg'

    def __init__(self):

        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(
            common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(
            common.Paths.customModulesDir, common.Paths.customModulesRepo)

        # todo: cache this (due to limited API calls for github)
        self.syncManager = SyncManager()
        #self.syncManager.addSource("Max Mustermann - Catchers", SyncSourceType.CATCHERS, common.Paths.catchersRepo)
        #self.syncManager.addSource("Max Mustermann - Modules", SyncSourceType.MODULES, common.Paths.modulesRepo)

        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser = Parser()
        self.currentlist = None

        self.addon = None

        common.log('SportsDevil initialized')

    def getPlayerType(self):
        sPlayerType = common.getSetting('playerType')

        if (sPlayerType == '0'):
            return xbmc.PLAYER_CORE_AUTO
        elif (sPlayerType == '1'):
            return xbmc.PLAYER_CORE_MPLAYER
        elif (sPlayerType == '2'):
            return xbmc.PLAYER_CORE_DVDPLAYER
        # PLAYER_CORE_AMLPLAYER
        elif (sPlayerType == '3'):
            return 5

        return xbmc.PLAYER_CORE_AUTO

    def playVideo(self, videoItem, isAutoplay=False):
        if not videoItem:
            return

        listitem = self.createXBMCListItem(videoItem)

        title = videoItem['videoTitle']
        if title:
            listitem.setInfo('video', {'title': enc.clean_safe(title)})

        if not isAutoplay:
            xbmcplugin.setResolvedUrl(self.handle, True, listitem)
        else:
            url = urllib.unquote_plus(videoItem['url'])
            xbmc.Player(self.getPlayerType()).play(url, listitem)

    def launchChrome(self, url, title):
        action = 'RunPlugin(%s)' % (
            'plugin://plugin.program.chrome.launcher/?kiosk=yes&mode=showSite&stopPlayback=yes&url='
            + url)
        common.log('chrome test:' + str(action))
        xbmc.executebuiltin(enc.unescape(action))

    def downloadVideo(self, url, title):
        common.log('Trying to download video ' + str(url))

        # check url
        if url.startswith('plugin'):
            common.log('Video is not downloadable')
            return None

        path = common.getSetting('download_path')
        if not path:
            path = common.browseFolders(common.translate(30017))
            common.setSetting('download_path', path)

        title = getKeyboard(default=fu.cleanFilename(title),
                            heading='SportsDevil')
        if title == None or title == '':
            return None

        downloader = Downloader()
        downloaded_file = downloader.downloadMovie(url, path,
                                                   fu.cleanFilename(title),
                                                   '.flv')

        if downloaded_file == None:
            common.log('Download cancelled')
        else:
            common.log('Video ' + url + " downloaded to '" + downloaded_file +
                       "'")

        return downloaded_file

    def getVideos(self, lItem, dia=None, percent=0, percentSpan=100):
        allitems = []

        if dia and dia.isCanceled():
            return allitems

        currentName = lItem['title']

        if lItem['type'].find('video') != -1:
            if dia:
                dia.update(percent + percentSpan, thirdline=currentName)
            allitems.append(lItem)
        else:
            tmpList = self.parser.parse(lItem).list
            if tmpList and len(tmpList.items) > 0:

                if dia:
                    dia.update(percent, secondline=currentName, thirdline=' ')

                inc = percentSpan / len(tmpList.items)
                for item in tmpList.items:

                    if dia and dia.isCanceled():
                        break

                    children = self.getVideos(item, dia, percent, inc)
                    if children:
                        allitems.extend(children)
                    percent += inc

        return allitems

    def getSearchPhrase(self):
        searchCache = os.path.join(common.Paths.cacheDir, 'search')
        default_phrase = fu.getFileContent(searchCache)
        if not default_phrase:
            default_phrase = ''

        search_phrase = common.showOSK(default_phrase, common.translate(30102))
        if search_phrase == '':
            return None
        xbmc.sleep(10)
        fu.setFileContent(searchCache, search_phrase)

        return search_phrase

    def parseView(self, lItem):
        def endOfDirectory(succeeded=True):
            xbmcplugin.endOfDirectory(handle=self.handle,
                                      succeeded=succeeded,
                                      cacheToDisc=True)

        if not lItem:
            endOfDirectory(False)
            return None

        if lItem['type'] == 'search':
            search_phrase = self.getSearchPhrase()
            if not search_phrase:
                common.log("search canceled")
                endOfDirectory(False)
                return None
            else:
                lItem['type'] = 'rss'
                lItem['url'] = lItem['url'] % (
                    urllib.quote_plus(search_phrase))

        url = lItem['url']

        if url == common.Paths.customModulesFile:
            self.customModulesManager.getCustomModules()

        tmpList = None
        result = self.parser.parse(lItem)
        if result.code == ParsingResult.Code.SUCCESS:
            tmpList = result.list
        else:
            if result.code == ParsingResult.Code.CFGFILE_NOT_FOUND:
                common.showError("Cfg file not found")
            elif result.code == ParsingResult.Code.CFGSYNTAX_INVALID:
                common.showError("Cfg syntax invalid")
            elif result.code == ParsingResult.Code.WEBREQUEST_FAILED:
                common.showError("Web request failed")

            endOfDirectory(False)
            return None

        # if it's the main menu, add folder 'Favourites' and 'Custom Modules
        if url == self.MAIN_MENU_FILE:
            tmp = ListItem.create()
            tmp['title'] = 'Favourites'
            tmp['type'] = 'rss'
            tmp['icon'] = os.path.join(common.Paths.imgDir, 'bookmark.png')
            tmp['url'] = str(common.Paths.favouritesFile)
            tmpList.items.insert(0, tmp)

            tmp = ListItem.create()
            tmp['title'] = '[COLOR red]Custom Modules[/COLOR]'
            tmp['type'] = 'rss'
            tmp['url'] = os.path.join(common.Paths.customModulesDir,
                                      'custom.cfg')
            tmpList.items.insert(0, tmp)

        # if it's the favourites menu, add item 'Add item'
        elif url == common.Paths.favouritesFile or url.startswith(
                'favfolders'):

            if url.startswith('favfolders'):
                url = os.path.normpath(
                    os.path.join(common.Paths.favouritesFolder, url))

            tmp = ListItem.create()
            tmp['title'] = 'Add item...'
            tmp['type'] = 'command'
            tmp['icon'] = os.path.join(common.Paths.imgDir, 'bookmark_add.png')
            action = 'RunPlugin(%s)' % (self.base + '?mode=' +
                                        str(Mode.ADDITEM) + '&url=' + url)
            tmp['url'] = action
            tmpList.items.append(tmp)

        # if it's the custom modules  menu, add item 'more...'
        elif url == common.Paths.customModulesFile:
            tmp = ListItem.create()
            tmp['title'] = 'more...'
            tmp['type'] = 'command'
            #tmp['icon'] = os.path.join(common.Paths.imgDir, 'bookmark_add.png')
            action = 'RunPlugin(%s)' % (self.base + '?mode=' + str(
                Mode.DOWNLOADCUSTOMMODULE) + '&url=')
            tmp['url'] = action
            tmpList.items.append(tmp)

        # Create menu or play, if it's a single video and autoplay is enabled
        proceed = False

        count = len(tmpList.items)
        if count == 0:
            if url.startswith('favfolders'):
                proceed = True
            else:
                common.showInfo('No stream available')
        elif count > 0 and not (common.getSetting('autoplay') == 'true' and
                                count == 1 and len(tmpList.getVideos()) == 1):
            # sort methods
            sortKeys = tmpList.sort.split('|')
            setSortMethodsForCurrentXBMCList(self.handle, sortKeys)

            # Add items to XBMC list
            for m in tmpList.items:
                self.addListItem(m, len(tmpList.items))

            proceed = True

        endOfDirectory(proceed)
        common.log('End of directory')
        return tmpList

    def downloadCustomModule(self):
        success = self.customModulesManager.downloadCustomModules()
        if success == True:
            # refresh container if SportsDevil is active
            currContainer = xbmcUtils.getContainerFolderPath()
            common.showNotification('SportsDevil', 'Download successful', 1000)
            if currContainer.startswith(self.base):
                xbmc.executebuiltin('Container.Refresh()')
            return True
        elif success == False:
            common.showNotification('SportsDevil', 'Download failed', 1000)
        return False

    def removeCustomModule(self, item):
        name = urllib.unquote(item["title"])
        success = self.customModulesManager.removeCustomModule(name)
        if success:
            xbmc.executebuiltin('Container.Refresh()')

    def createXBMCListItem(self, item):
        title = enc.clean_safe(item['title'])

        m_type = item['type']

        icon = item['icon']

        if icon and not icon.startswith('http'):
            try:
                if not fu.fileExists(icon):
                    tryFile = os.path.join(common.Paths.modulesDir, icon)
                    if not fu.fileExists(tryFile):
                        tryFile = os.path.join(common.Paths.customModulesDir,
                                               icon)
                    if fu.fileExists(tryFile):
                        icon = tryFile
            except:
                pass

        if not icon:
            if m_type == 'video':
                icon = common.Paths.defaultVideoIcon
            else:
                icon = common.Paths.defaultCategoryIcon

        liz = xbmcgui.ListItem(title,
                               title,
                               iconImage=icon,
                               thumbnailImage=icon)

        fanart = item['fanart']
        if not fanart:
            fanart = common.Paths.pluginFanart
        liz.setProperty('fanart_image', fanart)
        """
        General Values that apply to all types:
            count         : integer (12) - can be used to store an id for later, or for sorting purposes
            size          : long (1024) - size in bytes
            date          : string (%d.%m.%Y / 01.01.2009) - file date

        Video Values:
            genre         : string (Comedy)
            year          : integer (2009)
            episode       : integer (4)
            season        : integer (1)
            top250        : integer (192)
            tracknumber   : integer (3)
            rating        : float (6.4) - range is 0..10
            watched       : depreciated - use playcount instead
            playcount     : integer (2) - number of times this item has been played
            overlay       : integer (2) - range is 0..8.  See GUIListItem.h for values
            cast          : list (Michal C. Hall)
            castandrole   : list (Michael C. Hall|Dexter)
            director      : string (Dagur Kari)
            mpaa          : string (PG-13)
            plot          : string (Long Description)
            plotoutline   : string (Short Description)
            title         : string (Big Fan)
            originaltitle : string (Big Fan)
            duration      : string (3:18)
            studio        : string (Warner Bros.)
            tagline       : string (An awesome movie) - short description of movie
            writer        : string (Robert D. Siegel)
            tvshowtitle   : string (Heroes)
            premiered     : string (2005-03-04)
            status        : string (Continuing) - status of a TVshow
            code          : string (tt0110293) - IMDb code
            aired         : string (2008-12-07)
            credits       : string (Andy Kaufman) - writing credits
            lastplayed    : string (%Y-%m-%d %h:%m:%s = 2009-04-05 23:16:04)
            album         : string (The Joshua Tree)
            votes         : string (12345 votes)
            trailer       : string (/home/user/trailer.avi)
        """

        infoLabels = {}
        for video_info_name in item.infos.keys():
            infoLabels[video_info_name] = enc.clean_safe(item[video_info_name])
        infoLabels['title'] = title

        liz.setInfo('video', infoLabels)

        url = urllib.unquote_plus(item['url'])
        liz.setPath(url)

        if m_type == 'video':
            liz.setProperty('IsPlayable', 'true')

        return liz

    def addListItem(self, lItem, totalItems):
        def createContextMenuItem(label, mode, codedItem):
            action = 'XBMC.RunPlugin(%s)' % (self.addon.build_plugin_url(
                {
                    'mode': str(mode),
                    'item': codedItem
                }))
            return (label, action)

        def encoded_dict(in_dict):
            out_dict = {}
            for k, v in in_dict.iteritems():
                if isinstance(v, unicode):
                    v = v.encode('utf8')
                elif isinstance(v, str):
                    # Must be encoded in UTF-8
                    v.decode('utf8')
                out_dict[k] = v
            return urllib.urlencode(out_dict)

        contextMenuItems = []
        definedIn = lItem['definedIn']

        codedItem = urllib.quote(encoded_dict(lItem.infos))

        if definedIn:
            # Queue
            contextMenuItem = createContextMenuItem('Queue', Mode.QUEUE,
                                                    codedItem)
            contextMenuItems.append(contextMenuItem)

            # Favourite
            if definedIn.endswith('favourites.cfg') or definedIn.startswith(
                    "favfolders/"):
                # Remove from favourites
                contextMenuItem = createContextMenuItem(
                    'Remove', Mode.REMOVEFROMFAVOURITES, codedItem)
                contextMenuItems.append(contextMenuItem)

                # Edit label
                contextMenuItem = createContextMenuItem(
                    'Edit', Mode.EDITITEM, codedItem)
                contextMenuItems.append(contextMenuItem)

            else:
                # Custom module
                if definedIn.endswith('custom.cfg'):
                    # Remove from custom modules
                    contextMenuItem = createContextMenuItem(
                        'Remove module', Mode.REMOVEFROMCUSTOMMODULES,
                        codedItem)
                    contextMenuItems.append(contextMenuItem)

                if lItem['title'] != "Favourites":
                    # Add to favourites
                    contextMenuItem = createContextMenuItem(
                        'Add to SportsDevil favourites', Mode.ADDTOFAVOURITES,
                        codedItem)
                    contextMenuItems.append(contextMenuItem)
                contextMenuItem = createContextMenuItem(
                    'Open with Chrome launcher', Mode.CHROME, codedItem)
                contextMenuItems.append(contextMenuItem)

        liz = self.createXBMCListItem(lItem)

        m_type = lItem['type']
        if not m_type:
            m_type = 'rss'

        if m_type == 'video':
            u = self.base + '?mode=' + str(Mode.PLAY) + '&item=' + codedItem
            if lItem['IsDownloadable']:
                contextMenuItem = createContextMenuItem(
                    'Download', Mode.DOWNLOAD, codedItem)
                contextMenuItems.append(contextMenuItem)
            isFolder = False
        elif m_type.find('command') > -1:
            u = self.base + '?mode=' + str(Mode.EXECUTE) + '&item=' + codedItem
            isFolder = False
        else:
            u = self.base + '?mode=' + str(Mode.VIEW) + '&item=' + codedItem
            isFolder = True

        liz.addContextMenuItems(contextMenuItems)
        xbmcplugin.addDirectoryItem(handle=self.handle,
                                    url=u,
                                    listitem=liz,
                                    isFolder=isFolder,
                                    totalItems=totalItems)

    def clearCache(self):
        cacheDir = common.Paths.cacheDir
        if not os.path.exists(cacheDir):
            os.mkdir(cacheDir, 0777)
            common.log('Cache directory created' + str(cacheDir))
        else:
            fu.clearDirectory(cacheDir)
            common.log('Cache directory purged')

    def update(self):
        def checkForUpdates():
            updates = {}
            common.showNotification('SportsDevil', common.translate(30275))
            xbmcUtils.showBusyAnimation()

            catchersUpdates = self.syncManager.getUpdates(
                SyncSourceType.CATCHERS, common.Paths.catchersDir)
            if len(catchersUpdates) > 0:
                updates["Catchers"] = catchersUpdates
            modulesUpdates = self.syncManager.getUpdates(
                SyncSourceType.MODULES, common.Paths.modulesDir)
            if len(modulesUpdates) > 0:
                updates["Modules"] = modulesUpdates

            xbmcUtils.hideBusyAnimation()
            return updates

        def doUpdates(typeName, updates):
            count = len(updates)

            head = "SportsDevil Updates - %s" % typeName

            msg = common.translate(30277)
            if count == 1:
                msg = common.translate(30276)

            question = ("%s %s: " % (count, msg)) + ', '.join(
                map(lambda u: u.split('/')[-1], updates.keys())) + '\n'
            question += common.translate(30278)

            updates = updates.values()

            countFailed = 0

            dlg = DialogQuestion()
            dlg.head = head
            if dlg.ask(question):
                dlg = DialogProgress()
                firstline = common.translate(30279)
                dlg.create(head, firstline, " ")

                for i in range(0, count):
                    update = updates[i]
                    percent = int((i + 1.0) * 100 / count)
                    dlg.update(percent, firstline, update.name)
                    if not update.do():
                        countFailed += 1

                msg = " "
                if countFailed > 0:
                    msg = "%s %s" % (countFailed, common.translate(30280))

                dlg.update(100, msg, " ")
                xbmc.sleep(500)
                dlg.close()

        allupdates = checkForUpdates()
        count = len(allupdates)
        if count == 0:
            common.showNotification('SportsDevil', common.translate(30273))
            return
        else:
            for key, value in allupdates.items():
                doUpdates(key, value)

    def queueAllVideos(self, item):
        dia = DialogProgress()
        dia.create('SportsDevil', 'Get videos...' + item['title'])
        dia.update(0)

        items = self.getVideos(item, dia)
        if items:
            for it in items:
                item = self.createXBMCListItem(it)
                queries = {
                    'mode': str(Mode.PLAY),
                    'url': self.addon.build_plugin_url(it.infos)
                }
                uc = self.addon.build_plugin_url(queries)
                xbmc.PlayList(xbmc.PLAYLIST_VIDEO).add(uc, item)
            resultLen = len(items)
            msg = 'Queued ' + str(resultLen) + ' video'
            if resultLen > 1:
                msg += 's'
            dia.update(100, msg)
            xbmc.sleep(500)
            dia.update(100, msg, ' ', ' ')
        else:
            dia.update(0, 'No items found', ' ')

        xbmc.sleep(700)
        dia.close()

    def executeItem(self, item):
        url = item['url']
        if '(' in url:
            xbmcCommand = parseText(url, '([^\(]*).*')
            if xbmcCommand.lower() in [
                    'activatewindow', 'runscript', 'runplugin', 'playmedia'
            ]:
                if xbmcCommand.lower() == 'activatewindow':
                    params = parseText(url, '.*\(\s*(.+?)\s*\).*').split(',')
                    for i in range(len(params) - 1, -1, -1):
                        p = params[i]
                        if p == 'return':
                            params.remove(p)
                    path = enc.unescape(params[len(params) - 1])
                    xbmc.executebuiltin('Container.Update(' + path + ')')
                    return
                xbmc.executebuiltin(enc.unescape(url))

    def _parseParameters(self):
        mode = int(self.addon.queries['mode'])
        queryString = self.addon.queries['item']
        item = ListItem.create()
        if mode in [
                Mode.CHROME, Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES,
                Mode.EDITITEM
        ]:
            item.infos = self.addon.parse_query(urllib.unquote(queryString),
                                                {})
        else:
            item.infos = self.addon.parse_query(queryString, {})
        return [mode, item]

    def run(self, argv=None):

        self.addon = Addon('plugin.video.SportsDevil', argv)

        common.log('SportsDevil running')

        base = argv[0]
        handle = int(argv[1])
        parameter = argv[2]
        self.base = base
        self.handle = handle

        paramstring = urllib.unquote_plus(parameter)

        try:

            # if addon is started
            listItemPath = xbmcUtils.getListItemPath()
            if not listItemPath.startswith(self.base):
                if not ('mode=' in paramstring
                        and not 'mode=1&' in paramstring):
                    xbmcplugin.setPluginFanart(self.handle,
                                               common.Paths.pluginFanart)
                    self.clearCache()

                    #if common.getSetting('autoupdate') == 'true':
                    #    self.update()

            # Main Menu
            if len(paramstring) <= 2:
                mainMenu = ListItem.create()
                mainMenu['url'] = self.MAIN_MENU_FILE
                tmpList = self.parseView(mainMenu)
                if tmpList:
                    self.currentlist = tmpList

            else:
                [mode, item] = self._parseParameters()

                # switch(mode)
                if mode == Mode.VIEW:
                    tmpList = self.parseView(item)
                    if tmpList:
                        self.currentlist = tmpList
                        count = len(self.currentlist.items)
                        if count == 1:
                            # Autoplay single video
                            autoplayEnabled = common.getSetting(
                                'autoplay') == 'true'
                            if autoplayEnabled:
                                videos = self.currentlist.getVideos()
                                if len(videos) == 1:
                                    self.playVideo(videos[0], True)

                elif mode == Mode.ADDITEM:
                    tmp = os.path.normpath(paramstring.split('url=')[1])
                    if tmp:
                        suffix = tmp.split(os.path.sep)[-1]
                        tmp = tmp.replace(suffix,
                                          '') + urllib.quote_plus(suffix)
                    if self.favouritesManager.add(tmp):
                        xbmc.executebuiltin('Container.Refresh()')

                elif mode in [
                        Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES,
                        Mode.EDITITEM
                ]:

                    if mode == Mode.ADDTOFAVOURITES:
                        self.favouritesManager.addItem(item)
                    elif mode == Mode.REMOVEFROMFAVOURITES:
                        self.favouritesManager.removeItem(item)
                        xbmc.executebuiltin('Container.Refresh()')
                    elif mode == Mode.EDITITEM:
                        if self.favouritesManager.editItem(item):
                            xbmc.executebuiltin('Container.Refresh()')

                elif mode == Mode.EXECUTE:
                    self.executeItem(item)

                elif mode == Mode.PLAY:
                    self.playVideo(item)

                elif mode == Mode.QUEUE:
                    self.queueAllVideos(item)

                elif mode == Mode.DOWNLOAD:
                    url = urllib.unquote(item['url'])
                    title = item['title']
                    self.downloadVideo(url, title)

                elif mode == Mode.CHROME:
                    url = urllib.quote(item['url'])
                    title = item['title']
                    self.launchChrome(url, title)

                elif mode == Mode.REMOVEFROMCUSTOMMODULES:
                    self.removeCustomModule(item)

                #elif mode == Mode.UPDATE:
                #    self.update()

                elif mode == Mode.DOWNLOADCUSTOMMODULE:
                    self.downloadCustomModule()

                elif mode == Mode.INSTALLADDON:
                    success = install(item['url'])
                    if success:
                        xbmc.sleep(100)
                        if xbmcUtils.getCurrentWindowXmlFile(
                        ) == 'DialogAddonSettings.xml':
                            # workaround to update settings dialog
                            common.setSetting('', '')

        except Exception, e:
            common.showError('Error running SportsDevil')
            common.log('Error running SportsDevil. Reason:' + str(e))
Example #16
0
class Main:

    MAIN_MENU_FILE = "mainMenu.cfg"

    def __init__(self):

        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(common.Paths.customModulesDir, common.Paths.customModulesRepo)

        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser = Parser()
        self.currentlist = None

        self.addon = None

        common.log("SportsDevil initialized")

    def getPlayerType(self):
        sPlayerType = common.getSetting("playerType")

        if sPlayerType == "0":
            return xbmc.PLAYER_CORE_AUTO
        elif sPlayerType == "1":
            return xbmc.PLAYER_CORE_MPLAYER
        elif sPlayerType == "2":
            return xbmc.PLAYER_CORE_DVDPLAYER
        # PLAYER_CORE_AMLPLAYER
        elif sPlayerType == "3":
            return 5

        return xbmc.PLAYER_CORE_AUTO

    def playVideo(self, videoItem, isAutoplay=False):
        if not videoItem:
            return

        listitem = self.createXBMCListItem(videoItem)

        title = videoItem["videoTitle"]
        if title:
            listitem.setInfo("video", {"title": title})

        if not isAutoplay:
            xbmcplugin.setResolvedUrl(self.handle, True, listitem)
        else:
            url = urllib.unquote_plus(videoItem["url"])
            # xbmc.Player(self.getPlayerType()).play(url, listitem)
            xbmc.Player().play(url, listitem)

    def launchChrome(self, url, title):
        action = "RunPlugin(%s)" % (
            "plugin://plugin.program.chrome.launcher/?kiosk=yes&mode=showSite&stopPlayback=yes&url=" + url
        )
        common.log("chrome test:" + str(action))
        xbmc.executebuiltin(action)

    def playWebDriver(self, url, title):
        try:
            import liveremote

            video = liveremote.resolve(url)
            liz = xbmcgui.ListItem(title)
            liz.setPath(video)
            liz.setProperty("IsPlayable", "true")
            # xbmc.Player(self.getPlayerType()).play(video, liz)
            xbmc.Player().play(video, liz)
        except:
            import sys, traceback

            traceback.print_exc(file=sys.stdout)
            common.showInfo("This is not the option you are looking for.")

    def downloadVideo(self, url, title):
        common.log("Trying to download video " + str(url))

        # check url
        if url.startswith("plugin"):
            common.log("Video is not downloadable")
            return None

        path = common.getSetting("download_path")
        if not path:
            path = common.browseFolders(common.translate(30017))
            common.setSetting("download_path", path)

        title = getKeyboard(default=fu.cleanFilename(title), heading="SportsDevil")
        if title == None or title == "":
            return None

        downloader = Downloader()
        downloaded_file = downloader.downloadMovie(url, path, fu.cleanFilename(title), ".flv")

        if downloaded_file == None:
            common.log("Download cancelled")
        else:
            common.log("Video " + url + " downloaded to '" + downloaded_file + "'")

        return downloaded_file

    def getVideos(self, lItem, dia=None, percent=0, percentSpan=100):
        allitems = []

        if dia and dia.isCanceled():
            return allitems

        currentName = lItem["title"]

        if lItem["type"].find("video") != -1:
            if dia:
                dia.update(percent + percentSpan, thirdline=currentName)
            allitems.append(lItem)
        else:
            tmpList = self.parser.parse(lItem).list
            if tmpList and len(tmpList.items) > 0:

                if dia:
                    dia.update(percent, secondline=currentName, thirdline=" ")

                inc = percentSpan / len(tmpList.items)
                for item in tmpList.items:

                    if dia and dia.isCanceled():
                        break

                    children = self.getVideos(item, dia, percent, inc)
                    if children:
                        allitems.extend(children)
                    percent += inc

        return allitems

    def getSearchPhrase(self):
        searchCache = os.path.join(common.Paths.cacheDir, "search")
        default_phrase = fu.getFileContent(searchCache)
        if not default_phrase:
            default_phrase = ""

        search_phrase = common.showOSK(default_phrase, common.translate(30102))
        if search_phrase == "":
            return None
        xbmc.sleep(10)
        fu.setFileContent(searchCache, search_phrase)

        return search_phrase

    def parseView(self, lItem):
        def endOfDirectory(succeeded=True):
            xbmcplugin.endOfDirectory(handle=self.handle, succeeded=succeeded, cacheToDisc=True)

        if not lItem:
            endOfDirectory(False)
            return None

        if lItem["type"] == "search":
            search_phrase = self.getSearchPhrase()
            if not search_phrase:
                common.log("search canceled")
                endOfDirectory(False)
                return None
            else:
                lItem["type"] = "rss"
                lItem["url"] = lItem["url"] % (urllib.quote_plus(search_phrase))

        url = lItem["url"]

        if url == common.Paths.customModulesFile:
            self.customModulesManager.getCustomModules()

        tmpList = None
        result = self.parser.parse(lItem)
        if result.code == ParsingResult.Code.SUCCESS:
            tmpList = result.list
        elif result.code == ParsingResult.Code.CFGFILE_NOT_FOUND:
            common.showError("Cfg file not found")
            endOfDirectory(False)
            return None
        elif result.code == ParsingResult.Code.CFGSYNTAX_INVALID:
            common.showError("Cfg syntax invalid")
            endOfDirectory(False)
            return None
        elif result.code == ParsingResult.Code.WEBREQUEST_FAILED:
            common.showError("Web request failed")
            if len(result.list.items) > 0:
                tmpList = result.list
            else:
                endOfDirectory(False)
                return None

        # if it's the main menu, add folder 'Favourites' and 'Custom Modules
        if url == self.MAIN_MENU_FILE:
            tmp = ListItem.create()
            tmp["title"] = "Favourites"
            tmp["type"] = "rss"
            tmp["icon"] = os.path.join(common.Paths.imgDir, "bookmark.png")
            tmp["url"] = str(common.Paths.favouritesFile)
            tmpList.items.insert(0, tmp)

            # tmp = ListItem.create()
            # tmp['title'] = '[COLOR red]Custom Modules[/COLOR]'
            # tmp['type'] = 'rss'
            # tmp['url'] = os.path.join(common.Paths.customModulesDir, 'custom.cfg')
            # tmpList.items.insert(0, tmp)

        # if it's the favourites menu, add item 'Add item'
        elif url == common.Paths.favouritesFile or url.startswith("favfolders"):

            if url.startswith("favfolders"):
                url = os.path.normpath(os.path.join(common.Paths.favouritesFolder, url))

            tmp = ListItem.create()
            tmp["title"] = "Add item..."
            tmp["type"] = "command"
            tmp["icon"] = os.path.join(common.Paths.imgDir, "bookmark_add.png")
            action = "RunPlugin(%s)" % (self.base + "?mode=" + str(Mode.ADDITEM) + "&url=" + url)
            tmp["url"] = action
            tmpList.items.append(tmp)

        # if it's the custom modules  menu, add item 'more...'
        elif url == common.Paths.customModulesFile:
            tmp = ListItem.create()
            tmp["title"] = "more..."
            tmp["type"] = "command"
            # tmp['icon'] = os.path.join(common.Paths.imgDir, 'bookmark_add.png')
            action = "RunPlugin(%s)" % (self.base + "?mode=" + str(Mode.DOWNLOADCUSTOMMODULE) + "&url=")
            tmp["url"] = action
            tmpList.items.append(tmp)

        # Create menu or play, if it's a single video and autoplay is enabled
        proceed = False

        count = len(tmpList.items)
        if count == 0:
            if url.startswith("favfolders"):
                proceed = True
            else:
                common.showInfo("No stream available")
        elif count > 0 and not (
            common.getSetting("autoplay") == "true" and count == 1 and len(tmpList.getVideos()) == 1
        ):
            # sort methods
            sortKeys = tmpList.sort.split("|")
            setSortMethodsForCurrentXBMCList(self.handle, sortKeys)

            # Add items to XBMC list
            for m in tmpList.items:
                self.addListItem(m, len(tmpList.items))

            proceed = True

        endOfDirectory(proceed)
        common.log("End of directory")
        return tmpList

    def downloadCustomModule(self):
        success = self.customModulesManager.downloadCustomModules()
        if success == True:
            # refresh container if SportsDevil is active
            currContainer = xbmcUtils.getContainerFolderPath()
            common.showNotification("SportsDevil", "Download successful", 1000)
            if currContainer.startswith(self.base):
                xbmc.executebuiltin("Container.Refresh()")
            return True
        elif success == False:
            common.showNotification("SportsDevil", "Download failed", 1000)
        return False

    def removeCustomModule(self, item):
        name = urllib.unquote(item["title"])
        success = self.customModulesManager.removeCustomModule(name)
        if success:
            xbmc.executebuiltin("Container.Refresh()")

    def createXBMCListItem(self, item):
        title = item["title"]

        m_type = item["type"]

        icon = item["icon"]

        if icon and not icon.startswith("http"):
            try:
                if not fu.fileExists(icon):
                    tryFile = os.path.join(common.Paths.modulesDir, icon)
                    if not fu.fileExists(tryFile):
                        tryFile = os.path.join(common.Paths.customModulesDir, icon)
                    if fu.fileExists(tryFile):
                        icon = tryFile
            except:
                pass

        if not icon:
            if m_type == "video":
                icon = common.Paths.defaultVideoIcon
            else:
                icon = common.Paths.defaultCategoryIcon

        liz = xbmcgui.ListItem(title, title, iconImage=icon, thumbnailImage=icon)

        fanart = item["fanart"]
        if not fanart:
            fanart = common.Paths.pluginFanart
        liz.setProperty("fanart_image", fanart)

        """
        General Values that apply to all types:
            count         : integer (12) - can be used to store an id for later, or for sorting purposes
            size          : long (1024) - size in bytes
            date          : string (%d.%m.%Y / 01.01.2009) - file date

        Video Values:
            genre         : string (Comedy)
            year          : integer (2009)
            episode       : integer (4)
            season        : integer (1)
            top250        : integer (192)
            tracknumber   : integer (3)
            rating        : float (6.4) - range is 0..10
            watched       : depreciated - use playcount instead
            playcount     : integer (2) - number of times this item has been played
            overlay       : integer (2) - range is 0..8.  See GUIListItem.h for values
            cast          : list (Michal C. Hall)
            castandrole   : list (Michael C. Hall|Dexter)
            director      : string (Dagur Kari)
            mpaa          : string (PG-13)
            plot          : string (Long Description)
            plotoutline   : string (Short Description)
            title         : string (Big Fan)
            originaltitle : string (Big Fan)
            duration      : string (3:18)
            studio        : string (Warner Bros.)
            tagline       : string (An awesome movie) - short description of movie
            writer        : string (Robert D. Siegel)
            tvshowtitle   : string (Heroes)
            premiered     : string (2005-03-04)
            status        : string (Continuing) - status of a TVshow
            code          : string (tt0110293) - IMDb code
            aired         : string (2008-12-07)
            credits       : string (Andy Kaufman) - writing credits
            lastplayed    : string (%Y-%m-%d %h:%m:%s = 2009-04-05 23:16:04)
            album         : string (The Joshua Tree)
            votes         : string (12345 votes)
            trailer       : string (/home/user/trailer.avi)
        """

        infoLabels = {}
        for video_info_name in item.infos.keys():
            infoLabels[video_info_name] = item[video_info_name]
        infoLabels["title"] = title

        liz.setInfo("video", infoLabels)

        url = urllib.unquote_plus(item["url"])
        liz.setPath(url)

        if m_type == "video":
            liz.setProperty("IsPlayable", "true")
            # liz.setMimeType('text')

        return liz

    def addListItem(self, lItem, totalItems):
        def createContextMenuItem(label, mode, codedItem):
            action = "XBMC.RunPlugin(%s)" % (self.addon.build_plugin_url({"mode": str(mode), "item": codedItem}))
            return (label, action)

        def encoded_dict(in_dict):
            out_dict = {}
            for k, v in in_dict.iteritems():
                if isinstance(v, unicode):
                    v = v.encode("utf8")
                elif isinstance(v, str):
                    # Must be encoded in UTF-8
                    v.decode("utf8")
                out_dict[k] = v
            return urllib.urlencode(out_dict)

        contextMenuItems = []
        definedIn = lItem["definedIn"]

        codedItem = urllib.quote(encoded_dict(lItem.infos))

        if definedIn:
            # Queue
            contextMenuItem = createContextMenuItem("Queue", Mode.QUEUE, codedItem)
            contextMenuItems.append(contextMenuItem)

            # Favourite
            if definedIn.endswith("favourites.cfg") or definedIn.startswith("favfolders/"):
                # Remove from favourites
                contextMenuItem = createContextMenuItem("Remove", Mode.REMOVEFROMFAVOURITES, codedItem)
                contextMenuItems.append(contextMenuItem)

                # Edit label
                contextMenuItem = createContextMenuItem("Edit", Mode.EDITITEM, codedItem)
                contextMenuItems.append(contextMenuItem)

            else:
                # Custom module
                if definedIn.endswith("custom.cfg"):
                    # Remove from custom modules
                    contextMenuItem = createContextMenuItem("Remove module", Mode.REMOVEFROMCUSTOMMODULES, codedItem)
                    contextMenuItems.append(contextMenuItem)

                if lItem["title"] != "Favourites":
                    # Add to favourites
                    contextMenuItem = createContextMenuItem(
                        "Add to SportsDevil favourites", Mode.ADDTOFAVOURITES, codedItem
                    )
                    contextMenuItems.append(contextMenuItem)
                contextMenuItem = createContextMenuItem("Open with Chrome launcher", Mode.CHROME, codedItem)
                contextMenuItems.append(contextMenuItem)
                # contextMenuItem = createContextMenuItem('Open with WebDriver', Mode.WEBDRIVER, codedItem)
                # contextMenuItems.append(contextMenuItem)

        liz = self.createXBMCListItem(lItem)

        m_type = lItem["type"]
        if not m_type:
            m_type = "rss"

        if m_type == "video":
            u = self.base + "?mode=" + str(Mode.PLAY) + "&item=" + codedItem
            if lItem["IsDownloadable"]:
                contextMenuItem = createContextMenuItem("Download", Mode.DOWNLOAD, codedItem)
                contextMenuItems.append(contextMenuItem)
            isFolder = False
        elif m_type.find("command") > -1:
            u = self.base + "?mode=" + str(Mode.EXECUTE) + "&item=" + codedItem
            isFolder = False
        else:
            u = self.base + "?mode=" + str(Mode.VIEW) + "&item=" + codedItem
            isFolder = True

        liz.addContextMenuItems(contextMenuItems)
        xbmcplugin.addDirectoryItem(handle=self.handle, url=u, listitem=liz, isFolder=isFolder, totalItems=totalItems)

    def clearCache(self):
        cacheDir = common.Paths.cacheDir
        if not os.path.exists(cacheDir):
            os.mkdir(cacheDir, 0777)
            common.log("Cache directory created" + str(cacheDir))
        else:
            fu.clearDirectory(cacheDir)
            common.log("Cache directory purged")

    def update(self):
        def checkForUpdates():
            return None

        def doUpdates(typeName, updates):
            count = len(updates)

            head = "SportsDevil Updates - %s" % typeName

            msg = common.translate(30277)
            if count == 1:
                msg = common.translate(30276)

            question = ("%s %s: " % (count, msg)) + ", ".join(map(lambda u: u.split("/")[-1], updates.keys())) + "\n"
            question += common.translate(30278)

            updates = updates.values()

            countFailed = 0

            dlg = DialogQuestion()
            dlg.head = head
            if dlg.ask(question):
                dlg = DialogProgress()
                firstline = common.translate(30279)
                dlg.create(head, firstline, " ")

                for i in range(0, count):
                    update = updates[i]
                    percent = int((i + 1.0) * 100 / count)
                    dlg.update(percent, firstline, update.name)
                    if not update.do():
                        countFailed += 1

                msg = " "
                if countFailed > 0:
                    msg = "%s %s" % (countFailed, common.translate(30280))

                dlg.update(100, msg, " ")
                xbmc.sleep(500)
                dlg.close()

        allupdates = checkForUpdates()
        count = len(allupdates)
        if count == 0:
            common.showNotification("SportsDevil", common.translate(30273))
            return
        else:
            for key, value in allupdates.items():
                doUpdates(key, value)

    def queueAllVideos(self, item):
        dia = DialogProgress()
        dia.create("SportsDevil", "Get videos..." + item["title"])
        dia.update(0)

        items = self.getVideos(item, dia)
        if items:
            for it in items:
                item = self.createXBMCListItem(it)
                queries = {"mode": str(Mode.PLAY), "url": self.addon.build_plugin_url(it.infos)}
                uc = self.addon.build_plugin_url(queries)
                xbmc.PlayList(xbmc.PLAYLIST_VIDEO).add(uc, item)
            resultLen = len(items)
            msg = "Queued " + str(resultLen) + " video"
            if resultLen > 1:
                msg += "s"
            dia.update(100, msg)
            xbmc.sleep(500)
            dia.update(100, msg, " ", " ")
        else:
            dia.update(0, "No items found", " ")

        xbmc.sleep(700)
        dia.close()

    def executeItem(self, item):
        url = item["url"]
        if "(" in url:
            xbmcCommand = parseText(url, "([^\(]*).*")
            if xbmcCommand.lower() in ["activatewindow", "runscript", "runplugin", "playmedia"]:
                if xbmcCommand.lower() == "activatewindow":
                    params = parseText(url, ".*\(\s*(.+?)\s*\).*").split(",")
                    for i in range(len(params) - 1, -1, -1):
                        p = params[i]
                        if p == "return":
                            params.remove(p)
                    path = params[len(params) - 1]
                    xbmc.executebuiltin("Container.Update(" + path + ")")
                    return
                xbmc.executebuiltin(url)

    def _parseParameters(self):
        mode = int(self.addon.queries["mode"])
        queryString = self.addon.queries["item"]
        item = ListItem.create()
        if mode in [Mode.CHROME, Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES, Mode.EDITITEM, Mode.WEBDRIVER]:
            item.infos = self.addon.parse_query(urllib.unquote(queryString), {})
        else:
            item.infos = self.addon.parse_query(queryString, {})
        item.infos = dict((k.decode("utf8"), v.decode("utf8")) for k, v in item.infos.items())
        return [mode, item]

    def run(self, argv=None):

        self.addon = Addon("plugin.video.SportsDevil", argv)

        common.log("SportsDevil running")

        base = argv[0]
        handle = int(argv[1])
        parameter = argv[2]
        self.base = base
        self.handle = handle

        paramstring = urllib.unquote_plus(parameter)
        common.log(paramstring)

        try:

            # if addon is started
            listItemPath = xbmcUtils.getListItemPath()
            if not listItemPath.startswith(self.base):
                if not ("mode=" in paramstring and not "mode=1&" in paramstring):
                    xbmcplugin.setPluginFanart(self.handle, common.Paths.pluginFanart)
                    self.clearCache()

                    # if common.getSetting('autoupdate') == 'true':
                    #    self.update()

            # Main Menu
            if len(paramstring) <= 2:
                mainMenu = ListItem.create()
                mainMenu["url"] = self.MAIN_MENU_FILE
                tmpList = self.parseView(mainMenu)
                if tmpList:
                    self.currentlist = tmpList

            else:
                [mode, item] = self._parseParameters()

                # switch(mode)
                if mode == Mode.VIEW:
                    tmpList = self.parseView(item)
                    if tmpList:
                        self.currentlist = tmpList
                        count = len(self.currentlist.items)
                        if count == 1:
                            # Autoplay single video
                            autoplayEnabled = common.getSetting("autoplay") == "true"
                            if autoplayEnabled:
                                videos = self.currentlist.getVideos()
                                if len(videos) == 1:
                                    self.playVideo(videos[0], True)

                elif mode == Mode.ADDITEM:
                    tmp = os.path.normpath(paramstring.split("url=")[1])
                    if tmp:
                        suffix = tmp.split(os.path.sep)[-1]
                        tmp = tmp.replace(suffix, "") + urllib.quote_plus(suffix)
                    if self.favouritesManager.add(tmp):
                        xbmc.executebuiltin("Container.Refresh()")

                elif mode in [Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES, Mode.EDITITEM]:

                    if mode == Mode.ADDTOFAVOURITES:
                        self.favouritesManager.addItem(item)
                    elif mode == Mode.REMOVEFROMFAVOURITES:
                        self.favouritesManager.removeItem(item)
                        xbmc.executebuiltin("Container.Refresh()")
                    elif mode == Mode.EDITITEM:
                        if self.favouritesManager.editItem(item):
                            xbmc.executebuiltin("Container.Refresh()")

                elif mode == Mode.EXECUTE:
                    self.executeItem(item)

                elif mode == Mode.PLAY:
                    self.playVideo(item)

                elif mode == Mode.WEBDRIVER:
                    url = urllib.quote(item["url"])
                    title = item["title"]
                    self.playWebDriver(url, title)

                elif mode == Mode.QUEUE:
                    self.queueAllVideos(item)

                elif mode == Mode.DOWNLOAD:
                    url = urllib.unquote(item["url"])
                    title = item["title"]
                    self.downloadVideo(url, title)

                elif mode == Mode.CHROME:
                    url = urllib.quote(item["url"])
                    title = item["title"]
                    self.launchChrome(url, title)

                elif mode == Mode.REMOVEFROMCUSTOMMODULES:
                    self.removeCustomModule(item)

                # elif mode == Mode.UPDATE:
                #    self.update()

                elif mode == Mode.DOWNLOADCUSTOMMODULE:
                    self.downloadCustomModule()

                elif mode == Mode.INSTALLADDON:
                    success = install(item["url"])
                    if success:
                        xbmc.sleep(100)
                        if xbmcUtils.getCurrentWindowXmlFile() == "DialogAddonSettings.xml":
                            # workaround to update settings dialog
                            common.setSetting("", "")

        except Exception, e:
            common.showError("Error running SportsDevil")
            common.log("Error running SportsDevil. Reason:" + str(e))
class Main:

    MAIN_MENU_FILE = 'mainMenu.cfg'



    def __init__(self):
        common.log('Initializing SportsDevil')

        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir)

        self.curr_file = ''
        self.urlList = []
        self.extensionList = []
        self.selectionList = []
        self.currentlist = None
        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)

        self.parser = Parser()

        self.handle = int(sys.argv[1])

        common.log('SportsDevil initialized')

        paramstring = sys.argv[2]
        self.run(paramstring)


    def playVideo(self, videoItem, isAutoplay = False):
        if not videoItem:
            return

        url = urllib.unquote_plus(videoItem['url'])

        title = videoItem['videoTitle']
        if not title:
            title = videoItem['title']
            if not title:
                title = 'unknown'

        try:
            icon = videoItem['icon']
        except:
            icon = common.Paths.defaultVideoIcon

        listitem = xbmcgui.ListItem(title, title, icon, icon)
        listitem.setInfo('video', {'Title':title})

        for video_info_name in videoItem.infos_names:
            try:
                listitem.setInfo(type = 'Video', infoLabels = {video_info_name: videoItem[video_info_name]})
            except:
                pass

        listitem.setPath(url)

        if not isAutoplay:
            xbmcplugin.setResolvedUrl(self.handle, True, listitem)
        else:
            xbmc.Player(xbmc.PLAYER_CORE_AUTO).play(url, listitem)


    def downloadVideo(self, url, title):
        common.log('Trying to download video ' + str(url))

        # check url
        if url.startswith('plugin'):
            common.log('Video is not downloadable')
            return None

        path = common.getSetting('download_path')
        if not path:
            path = common.browseFolders(common.translate(30017))
            common.setSetting('download_path', path)

        title = getKeyboard(default = fu.cleanFilename(title),heading='SportsDevil')
        if title == None or title == '':
            return None

        downloader = Downloader()
        downloaded_file = downloader.downloadMovie(url, path,  fu.cleanFilename(title), '.flv')

        if downloaded_file == None:
            common.log ('Download cancelled')
        else:
            common.log('Video ' + url + ' downloaded to ' + downloaded_file)

        return downloaded_file


    def getVideos(self, lItem, dia = None, percent = 0, percentSpan = 100):
        allitems = []
        currentName = lItem['title']

        if lItem['type'].find('video') != -1:
            if dia:
                dia.update(percent + percentSpan, thirdline=currentName)
            allitems.append(lItem)
        else:
            tmpList = self.parser.parse(lItem)
            if tmpList and len(tmpList.items) > 0:
                inc = percentSpan/len(tmpList.items)
                dia.update(percent, secondline=currentName, thirdline=' ')
                for item in tmpList.items:
                    if dia.isCanceled():
                        break
                    children = self.getVideos(item, dia, percent, inc)
                    if children:
                        allitems.extend(children)
                    percent += inc

        return allitems

    def getSearchPhrase(self):
        searchCache = os.path.join(common.Paths.cacheDir, 'search')
        try:
            curr_phrase = fu.getFileContent(searchCache)
        except:
            curr_phrase = ''
        search_phrase = getKeyboard(default = curr_phrase, heading = common.translate(30102))
        if search_phrase == '':
            return None
        xbmc.sleep(10)
        fu.setFileContent(searchCache, search_phrase)

        return search_phrase

    def __endOfDirectory(self, succeeded=True):
        xbmcplugin.endOfDirectory(handle=self.handle, succeeded=succeeded, cacheToDisc=True)


    def parseView(self, url):

        lItem = decodeUrl(url)
        if lItem['type'] == 'search':
            search_phrase = self.getSearchPhrase()
            if not search_phrase:
                common.log("search canceled")
                self.__endOfDirectory(False)
                return None
            lItem['type'] = 'rss'
            lItem['url'] =  lItem['url'] % (urllib.quote_plus(search_phrase))

        url = lItem['url']

        tmpList = self.parser.parse(lItem)
        if not tmpList:
            common.showError("Parsing failed")
            self.__endOfDirectory(False)
            return None

        # Remove duplicates
        urls = []
        for i in range(len(tmpList.items)-1,-1,-1):
            item = tmpList.items[i]
            tmpUrl = item['url']
            tmpCfg = item['cfg']
            if not tmpCfg:
                tmpCfg = ''
            if not urls.__contains__(tmpUrl + '|' + tmpCfg):
                urls.append(tmpUrl + '|' + tmpCfg)
            else:
                tmpList.items.remove(item)

        # SHOW ITEMS IN GUI
        # if it's the main menu, add folder 'Favourites'
        if url == self.MAIN_MENU_FILE:
            # Add Favourites
            tmp = CListItem()
            tmp['title'] = 'Favourites'
            tmp['type'] = 'rss'
            tmp['url'] = str(common.Paths.favouritesFile)
            tmpList.items.insert(0,tmp)

        # if it's the favourites menu, add item 'Add item'
        elif url == common.Paths.favouritesFile:
            tmp = CListItem()
            tmp['title'] = 'Add item...'
            tmp['type'] = 'command'
            action = 'RunPlugin(%s)' % (sys.argv[0] + '?mode=' + str(Mode.ADDITEM) + '&url=')
            tmp['url'] = action
            tmpList.items.append(tmp)

        count = len(tmpList.items)
        if count > 0 and not (common.getSetting('autoplay') == 'true' and count == 1 and len(tmpList.getVideos()) == 1):
            # sort methods
            sortKeys = tmpList.sort.split('|')
            setSortMethodsForCurrentXBMCList(self.handle, sortKeys)

            # Add items to XBMC list
            for m in tmpList.items:
                self.addListItem(m, len(tmpList.items))

            self.__endOfDirectory()
            common.log('End of directory')
        else:
            self.__endOfDirectory(False)
        return tmpList


    def createXBMCListItem(self, item):
        liz = None
        title = enc.clean_safe(item['title'])

        icon = item['icon']
        if icon:
            liz = xbmcgui.ListItem(title, iconImage=icon, thumbnailImage=icon)
        else:
            liz = xbmcgui.ListItem(title)

        fanart = item['fanart']
        if not fanart:
            fanart = common.Paths.pluginFanart
        liz.setProperty('fanart_image', fanart)

        for video_info_name in item.infos_names:
            if video_info_name.find('context.') != -1:
                try:
                    cItem = item
                    cItem['type'] = 'rss'
                    cItem['url'] = item[video_info_name]
                    action = 'XBMC.RunPlugin(%s)' % (sys.argv[0] + '?url=' + codeUrl(cItem))
                    liz.addContextMenuItems([(video_info_name[video_info_name.find('.') + 1:], action)])
                except:
                    pass
            if video_info_name not in ['url', 'title', 'icon', 'type', 'extension'] and video_info_name.find('.tmp') == -1 and video_info_name.find('.append') == -1 and video_info_name.find('context.') == -1:
                try:
                    info_value = item[video_info_name]
                    infoLabels = {}
                    if video_info_name.find('.int') != -1:
                        infoLabels = {capitalize(video_info_name[:video_info_name.find('.int')]): int(info_value)}
                    elif video_info_name.find('.tmp') != -1:
                        infoLabels = {capitalize(video_info_name[:video_info_name.find('.tmp')]): info_value}
                    else:
                        infoLabels = {capitalize(video_info_name): info_value}
                    liz.setInfo(type = 'Video', infoLabels = infoLabels)
                except:
                    pass
        return liz





    def addListItem(self, lItem, totalItems):
        def createContextMenuItem(label, mode, codedItem):
            action = 'XBMC.RunPlugin(%s)' % (sys.argv[0] + '?mode=' + str(mode) + '&url=' + codedItem)
            return (label, action)

        contextMenuItems = []
        definedIn = lItem['definedIn']

        codedItem = codeUrl(lItem)

        # Jump to MainMenu
#        if definedIn and definedIn != self.MAIN_MENU_FILE:
#            action = 'Container.Update(%s, replace)' % (sys.argv[0])
#            contextMenuItem = ('Jump to Mainmenu', action)
#            contextMenuItems.append(contextMenuItem)



        # Queue
        if definedIn:
            contextMenuItem = createContextMenuItem('Queue', Mode.QUEUE, codedItem)
            contextMenuItems.append(contextMenuItem)

            if definedIn.endswith('favourites.cfg') or definedIn.startswith("favfolders/"):
                # Remove from favourites
                contextMenuItem = createContextMenuItem('Remove', Mode.REMOVEFROMFAVOURITES, codedItem)
                contextMenuItems.append(contextMenuItem)

                # Edit label
                contextMenuItem = createContextMenuItem('Edit', Mode.EDITITEM, codedItem)
                contextMenuItems.append(contextMenuItem)

            elif lItem['title'] != "Favourites":
                    # Add to favourites
                    contextMenuItem = createContextMenuItem('Add to SportsDevil favourites', Mode.ADDTOFAVOURITES, codedItem)
                    contextMenuItems.append(contextMenuItem)


        liz = self.createXBMCListItem(lItem)

        m_type = lItem['type']
        if m_type == 'video':
            u = sys.argv[0] + '?mode=' + str(Mode.PLAY) + '&url=' + codedItem
            if lItem['IsDownloadable']:
                contextMenuItem = createContextMenuItem('Download', Mode.DOWNLOAD, codedItem)
                contextMenuItems.append(contextMenuItem)
            liz.setProperty('IsPlayable','true')
            isFolder = False
        elif m_type.find('command') > -1:
            u = sys.argv[0] + '?mode=' + str(Mode.EXECUTE) + '&url=' + codedItem
            isFolder = False
        else:
            u = sys.argv[0] + '?mode=' + str(Mode.VIEW) + '&url=' + codedItem
            isFolder = True

        liz.addContextMenuItems(contextMenuItems)
        xbmcplugin.addDirectoryItem(handle = self.handle, url = u, listitem = liz, isFolder = isFolder, totalItems = totalItems)


    def clearCache(self):
        cacheDir = common.Paths.cacheDir
        if not os.path.exists(cacheDir):
            common.log('Creating cache directory ' + str(cacheDir))
            os.mkdir(cacheDir)
            common.log('Cache directory created')
        else:
            common.log('Purging cache directory')
            for root, dirs, files in os.walk(cacheDir , topdown = False):
                for name in files:
                    if not name == 'cookies.lwp':
                        os.remove(os.path.join(root, name))
            common.log('Cache directory purged')


    def run(self, paramstring):
        common.log('SportsDevil running')
        try:
            # Main Menu
            if len(paramstring) <= 2:
                # Set fanart
                xbmcplugin.setPluginFanart(self.handle, common.Paths.pluginFanart)

                # Clear cache
                self.clearCache()

                # Show Main Menu
                tmpList = self.parseView(self.MAIN_MENU_FILE)
                if tmpList:
                    self.currentlist = tmpList
                    self.curr_file = tmpList.cfg

            else:
                params = paramstring
                mode, codedItem = params.split('&',1)
                mode = int(mode.split('=')[1])

                codedItem = codedItem[4:]
                item = decodeUrl(codedItem)

                # switch(mode)
                if mode == Mode.VIEW:
                    tmpList = self.parseView(codedItem)
                    if tmpList:
                        self.currentlist = tmpList
                        self.curr_file = tmpList.cfg
                        count = len(self.currentlist.items)
                        if count == 0:
                            common.showInfo('No stream available')
                        elif count == 1:
                            # Autoplay single video
                            autoplayEnabled = common.getSetting('autoplay') == 'true'
                            if autoplayEnabled:
                                videos = self.currentlist.getVideos()
                                if len(videos) == 1:
                                    self.playVideo(videos[0], True)


                elif mode == Mode.ADDITEM:
                    if self.favouritesManager.addItem():
                        xbmc.executebuiltin('Container.Refresh()')

                elif mode in [Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES, Mode.EDITITEM]:

                    if mode == Mode.ADDTOFAVOURITES:
                        self.favouritesManager.addToFavourites(item)
                    elif mode == Mode.REMOVEFROMFAVOURITES:
                        self.favouritesManager.removeItem(item)
                        xbmc.executebuiltin('Container.Refresh()')
                    elif mode == Mode.EDITITEM:
                        if self.favouritesManager.editItem(item):
                            xbmc.executebuiltin('Container.Refresh()')

                elif mode == Mode.EXECUTE:
                    url = item['url']
                    if url.find('(') > -1:
                        xbmcCommand = parseText(url,'([^\(]*).*')
                        if xbmcCommand.lower() in ['activatewindow', 'runscript', 'runplugin', 'playmedia']:
                            if xbmcCommand.lower() == 'activatewindow':
                                params = parseText(url, '.*\(\s*(.+?)\s*\).*').split(',')
                                for i in range(len(params)-1,-1,-1):
                                    p = params[i]
                                    if p == 'return':
                                        params.remove(p)
                                path = enc.unescape(params[len(params)-1])
                                xbmc.executebuiltin('Container.Update(' + path + ')')
                                return
                            xbmc.executebuiltin(enc.unescape(url))

                elif mode == Mode.PLAY:
                    self.playVideo(item)

                elif mode == Mode.QUEUE:
                    dia = DialogProgress()
                    dia.create('SportsDevil', 'Get videos...' + item['title'])
                    dia.update(0)

                    items = self.getVideos(item, dia)
                    if items:
                        for it in items:
                            item = self.createXBMCListItem(it)
                            uc = sys.argv[0] + '?mode=' + str(Mode.PLAY) + '&url=' + codeUrl(it)
                            item.setProperty('IsPlayable', 'true')
                            item.setProperty('IsFolder','false')
                            xbmc.PlayList(1).add(uc, item)
                        resultLen = len(items)
                        msg = 'Queued ' + str(resultLen) + ' video'
                        if resultLen > 1:
                            msg += 's'
                        dia.update(100, msg)
                        xbmc.sleep(500)
                        dia.update(100, msg,' ',' ')
                    else:
                        dia.update(0, 'No items found',' ')

                    xbmc.sleep(700)
                    dia.close()

                elif mode == Mode.DOWNLOAD:
                    url = urllib.unquote(item['url'])
                    title = item['title']
                    self.downloadVideo(url, title)

        except Exception, e:
            if common.enable_debug:
                traceback.print_exc(file = sys.stdout)
            common.showError('Error running SportsDevil.\n\nReason:\n' + str(e))
Example #18
0
class Main:

    MAIN_MENU_FILE = 'mainMenu.cfg'


    def __init__(self):
        
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.parser = Parser()
        self.currentlist = None
        self.addon = None
        
        common.log('SportsDevilNL initialized')
        
    def playVideo(self, videoItem, isAutoplay = False):
        if not videoItem:
            return

        listitem = self.createXBMCListItem(videoItem)

        title = videoItem['videoTitle']
        if title:
            listitem.setInfo('video', {'title': title})

        if not isAutoplay:
            xbmcplugin.setResolvedUrl(self.handle, True, listitem)
        else:
            url = urllib.unquote_plus(videoItem['url'])
            xbmc.Player().play(url, listitem)

    def launchChrome(self, url, title):
        action = 'RunPlugin(%s)' % ('plugin://plugin.program.chrome.launcher/?kiosk=yes&mode=showSite&stopPlayback=yes&url=' + url)
        common.log('chrome test:' + str(action))
        xbmc.executebuiltin(action)
        
    def playWebDriver(self, url, title):
        try:
            import liveremote
            video = liveremote.resolve(url)
            liz = xbmcgui.ListItem(title)
            liz.setPath(video)
            liz.setProperty('IsPlayable','true')
            xbmc.Player().play(video, liz)
        except:
            import sys,traceback
            traceback.print_exc(file = sys.stdout)
            common.showInfo('This is not the option you are looking for.')

    def getVideos(self, lItem, dia = None, percent = 0, percentSpan = 100):
        allitems = []
        
        if dia and dia.isCanceled():
            return allitems
        
        currentName = lItem['title']

        if lItem['type'].find('video') != -1:
            if dia:
                dia.update(percent + percentSpan, thirdline=currentName)
            allitems.append(lItem)
        else:
            tmpList = self.parser.parse(lItem).list
            if tmpList and len(tmpList.items) > 0:
                
                if dia:
                    dia.update(percent, secondline=currentName, thirdline=' ')
                
                inc = percentSpan/len(tmpList.items)
                for item in tmpList.items:
                    
                    if dia and dia.isCanceled():
                        break
                    
                    children = self.getVideos(item, dia, percent, inc)
                    if children:
                        allitems.extend(children)
                    percent += inc

        return allitems


    def getSearchPhrase(self):
        searchCache = os.path.join(common.Paths.cacheDir, 'search')
        default_phrase = fu.getFileContent(searchCache)
        if not default_phrase:
            default_phrase = ''

        search_phrase = common.showOSK(default_phrase, common.translate(30102))
        if search_phrase == '':
            return None
        xbmc.sleep(10)
        fu.setFileContent(searchCache, search_phrase)

        return search_phrase


    def parseView(self, lItem):

        def endOfDirectory(succeeded=True):
            if self.handle > -1:
                xbmcplugin.endOfDirectory(handle=self.handle, succeeded=succeeded, cacheToDisc=True)
            else:
                common.log('Handle -1')

        if not lItem:
            endOfDirectory(False)
            return None

        if lItem['type'] == 'search':
            search_phrase = self.getSearchPhrase()
            if not search_phrase:
                common.log("search canceled")
                endOfDirectory(False)
                return None
            else:
                lItem['type'] = 'rss'
                lItem['url'] =  lItem['url'] % (urllib.quote_plus(search_phrase))

        url = lItem['url']

        if url == common.Paths.customModulesFile: 
            self.customModulesManager.getCustomModules()

        tmpList = None
        result = self.parser.parse(lItem)
        if result.code == ParsingResult.Code.SUCCESS:
            tmpList = result.list
        elif result.code == ParsingResult.Code.CFGFILE_NOT_FOUND:
            common.showError("Cfg file not found")
            endOfDirectory(False)
            return None
        elif result.code == ParsingResult.Code.CFGSYNTAX_INVALID:
            common.showError("Cfg syntax invalid")
            endOfDirectory(False)
            return None
        elif result.code == ParsingResult.Code.WEBREQUEST_FAILED:
            common.showError("Web request failed")
            if len(result.list.items) > 0:
                tmpList = result.list
            else:
                endOfDirectory(False)
                return None

        # if it's the main menu, add folder 'Favourites'
        if url == self.MAIN_MENU_FILE:
            tmp = ListItem.create()
            tmp['title'] = 'Favourites'
            tmp['type'] = 'rss'
            tmp['icon'] = os.path.join(common.Paths.imgDir, 'bookmark.png')
            tmp['url'] = str(common.Paths.favouritesFile)
            tmpList.items.insert(0, tmp)
            
        # if it's the favourites menu, add item 'Add item'
        elif url == common.Paths.favouritesFile or url.startswith('favfolders'):
            
            if url.startswith('favfolders'):
                url = os.path.normpath(os.path.join(common.Paths.favouritesFolder, url)) 
            
            tmp = ListItem.create()
            tmp['title'] = 'Add item...'
            tmp['type'] = 'command'
            tmp['icon'] = os.path.join(common.Paths.imgDir, 'bookmark_add.png')
            action = 'RunPlugin(%s)' % (self.base + '?mode=' + str(Mode.ADDITEM) + '&url=' + url)
            tmp['url'] = action
            tmpList.items.append(tmp)
        
        # Create menu or play, if it's a single video and autoplay is enabled
        count = len(tmpList.items)
        if (count == 0 and not url.startswith('favfolders')):
            common.showInfo('No stream available')
            #Directory with 0 items
            endOfDirectory(False)
        elif not (common.getSetting('autoplay') == 'true' and count == 1 and len(tmpList.getVideos()) == 1):
            # sort methods
            sortKeys = tmpList.sort.split('|')
            setSortMethodsForCurrentXBMCList(self.handle, sortKeys)
            # Add items to XBMC list
            for m in tmpList.items:
                self.addListItem(m, len(tmpList.items))
            #Directory with >1 items
            endOfDirectory(True)
        else:
            #Directory with 0 items
            endOfDirectory(False)
            
        return tmpList

    def createXBMCListItem(self, item):
        title = item['title']
        m_type = item['type']
        icon = item['icon']

        if icon and not icon.startswith('http'):
            try:
                if not fu.fileExists(icon):
                    tryFile = os.path.join(common.Paths.modulesDir, icon)
                    if not fu.fileExists(tryFile):
                        tryFile = os.path.join(common.Paths.customModulesDir, icon)
                    if fu.fileExists(tryFile):
                        icon = tryFile
            except:
                pass

        if not icon:
            if m_type == 'video':
                icon = common.Paths.defaultVideoIcon
            else:
                icon = common.Paths.defaultCategoryIcon

        fanart = item['fanart']
        if not fanart:
            fanart = common.Paths.pluginFanart

        liz = xbmcgui.ListItem(title)
        try:
            liz.setArt({'thumb': icon, 'fanart': fanart})
        except:
            liz.setProperty('fanart_image', fanart)
            liz.setThumbnailImage(icon)
            common.log('main.py:374: setThumbnailImage is deprecated')

        """
        General Values that apply to all types:
            count         : integer (12) - can be used to store an id for later, or for sorting purposes
            size          : long (1024) - size in bytes
            date          : string (%d.%m.%Y / 01.01.2009) - file date

        Video Values:
            genre         : string (Comedy)
            year          : integer (2009)
            episode       : integer (4)
            season        : integer (1)
            top250        : integer (192)
            tracknumber   : integer (3)
            rating        : float (6.4) - range is 0..10
            watched       : depreciated - use playcount instead
            playcount     : integer (2) - number of times this item has been played
            overlay       : integer (2) - range is 0..8.  See GUIListItem.h for values
            cast          : list (Michal C. Hall)
            castandrole   : list (Michael C. Hall|Dexter)
            director      : string (Dagur Kari)
            mpaa          : string (PG-13)
            plot          : string (Long Description)
            plotoutline   : string (Short Description)
            title         : string (Big Fan)
            originaltitle : string (Big Fan)
            duration      : string (3:18)
            studio        : string (Warner Bros.)
            tagline       : string (An awesome movie) - short description of movie
            writer        : string (Robert D. Siegel)
            tvshowtitle   : string (Heroes)
            premiered     : string (2005-03-04)
            status        : string (Continuing) - status of a TVshow
            code          : string (tt0110293) - IMDb code
            aired         : string (2008-12-07)
            credits       : string (Andy Kaufman) - writing credits
            lastplayed    : string (%Y-%m-%d %h:%m:%s = 2009-04-05 23:16:04)
            album         : string (The Joshua Tree)
            votes         : string (12345 votes)
            trailer       : string (/home/user/trailer.avi)
        """

        infoLabels = {}
        for video_info_name in item.infos.keys():
            infoLabels[video_info_name] = item[video_info_name]
        infoLabels['title'] = title

        liz.setInfo('video', infoLabels)

        url = urllib.unquote_plus(item['url'])
        liz.setPath(url)

        if m_type == 'video':
            liz.setProperty('IsPlayable','true')

        if title.startswith('castflash'):
            try:
                liz.setMimeType('application/vnd.apple.mpegurl')
                liz.setContentLookup(False)
            except:
                common.showError('Update Kodi to 16+ to view this stream')
                return None

        return liz


    def addListItem(self, lItem, totalItems):
        def createContextMenuItem(label, mode, codedItem):
            action = 'XBMC.RunPlugin(%s)' % (self.addon.build_plugin_url({'mode': str(mode), 'item': codedItem}))
            return (label, action)

        def encoded_dict(in_dict):
            out_dict = {}
            for k, v in in_dict.iteritems():
                if isinstance(v, unicode):
                    v = v.encode('utf8')
                elif isinstance(v, str):
                    # Must be encoded in UTF-8
                    v.decode('utf8')
                out_dict[k] = v
            return urllib.urlencode(out_dict)
        
        contextMenuItems = []
        definedIn = lItem['definedIn']

        codedItem = urllib.quote(encoded_dict(lItem.infos))

        if definedIn:
            # Queue
            #contextMenuItem = createContextMenuItem('Queue', Mode.QUEUE, codedItem)
            #contextMenuItems.append(contextMenuItem)

            # Favourite
            if definedIn.endswith('favourites.cfg') or definedIn.startswith("favfolders/"):
                # Remove from favourites
                contextMenuItem = createContextMenuItem('Remove', Mode.REMOVEFROMFAVOURITES, codedItem)
                contextMenuItems.append(contextMenuItem)

                # Edit label
                contextMenuItem = createContextMenuItem('Edit', Mode.EDITITEM, codedItem)
                contextMenuItems.append(contextMenuItem)

            else:
                # Custom module
                if definedIn.endswith('custom.cfg'):
                    # Remove from custom modules
                    contextMenuItem = createContextMenuItem('Remove module', Mode.REMOVEFROMCUSTOMMODULES, codedItem)
                    contextMenuItems.append(contextMenuItem)
    
                if lItem['title'] != "Favourites":
                        # Add to favourites
                        contextMenuItem = createContextMenuItem('Add to SportsDevilNL favourites', Mode.ADDTOFAVOURITES, codedItem)
                        contextMenuItems.append(contextMenuItem)
                contextMenuItem = createContextMenuItem('Open with Chrome launcher', Mode.CHROME, codedItem)
                contextMenuItems.append(contextMenuItem)
                #contextMenuItem = createContextMenuItem('Open with WebDriver', Mode.WEBDRIVER, codedItem)
                #contextMenuItems.append(contextMenuItem)

        liz = self.createXBMCListItem(lItem)

        m_type = lItem['type']
        if not m_type:
            m_type = 'rss'
        
        if m_type == 'video':
            u = self.base + '?mode=' + str(Mode.PLAY) + '&item=' + codedItem
            if lItem['IsDownloadable']:
                contextMenuItem = createContextMenuItem('Download', Mode.DOWNLOAD, codedItem)
                contextMenuItems.append(contextMenuItem)
            isFolder = False
        elif m_type.find('command') > -1:
            u = self.base + '?mode=' + str(Mode.EXECUTE) + '&item=' + codedItem
            isFolder = False
        else:
            u = self.base + '?mode=' + str(Mode.VIEW) + '&item=' + codedItem
            isFolder = True

        liz.addContextMenuItems(contextMenuItems)
        xbmcplugin.addDirectoryItem(handle = self.handle, url = u, listitem = liz, isFolder = isFolder, totalItems = totalItems)


    def clearCache(self):
        cacheDir = common.Paths.cacheDir
        if not os.path.exists(cacheDir):
            os.mkdir(cacheDir, 0777)
            common.log('Cache directory created' + str(cacheDir))
        else:
            size, within_limit = fu.checkQuota(cacheDir)
            if not within_limit:
                fu.clearDirectory(cacheDir)
                common.log('Cache directory purged')
            common.log('Cache Usage:' + str(size))

    def queueAllVideos(self, item):
        dia = DialogProgress()
        dia.create('SportsDevilNL', 'Get videos...' + item['title'])
        dia.update(0)

        items = self.getVideos(item, dia)
        if items:
            for it in items:
                item = self.createXBMCListItem(it)
                queries = {'mode': str(Mode.PLAY), 'url': self.addon.build_plugin_url(it.infos)}
                uc = self.addon.build_plugin_url(queries)
                xbmc.PlayList(xbmc.PLAYLIST_VIDEO).add(uc, item)
            resultLen = len(items)
            msg = 'Queued ' + str(resultLen) + ' video'
            if resultLen > 1:
                msg += 's'
            dia.update(100, msg)
            xbmc.sleep(500)
            dia.update(100, msg,' ',' ')
        else:
            dia.update(0, 'No items found',' ')

        xbmc.sleep(700)
        dia.close()


    def executeItem(self, item):
        url = item['url']
        if '(' in url:
            xbmcCommand = parseText(url,'([^\(]*).*')
            if xbmcCommand.lower() in ['activatewindow', 'runscript', 'runplugin', 'playmedia']:
                if xbmcCommand.lower() == 'activatewindow':
                    params = parseText(url, '.*\(\s*(.+?)\s*\).*').split(',')
                    for i in range(len(params)-1,-1,-1):
                        p = params[i]
                        if p == 'return':
                            params.remove(p)
                    path = params[len(params)-1]
                    xbmc.executebuiltin('Container.Update(' + path + ')')
                    return
                xbmc.executebuiltin(url)


    def _parseParameters(self):
        mode = int(self.addon.queries['mode'])
        queryString = self.addon.queries['item']
        item = ListItem.create()
        if mode in [Mode.CHROME, Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES, Mode.EDITITEM, Mode.WEBDRIVER]:
            item.infos = self.addon.parse_query(urllib.unquote(queryString),{})
        else:
            item.infos = self.addon.parse_query(queryString,{})
        item.infos = dict((k.decode('utf8'), v.decode('utf8')) for k, v in item.infos.items())
        return [mode, item]


    def run(self, argv=None):
        self.addon = Addon('plugin.video.SportsDevilNL', argv)
        common.log('SportsDevilNL running')
        
        base = argv[0]
        handle = int(argv[1])
        parameter = argv[2]
        self.base = base
        self.handle = handle
        
        paramstring = urllib.unquote_plus(parameter)
        common.log(paramstring)
        
        try:
            
            # if addon is started
            listItemPath = xbmcUtils.getListItemPath()
            if not listItemPath.startswith(self.base):
                if not('mode=' in paramstring and not 'mode=1&' in paramstring):   
                    xbmcplugin.setPluginFanart(self.handle, common.Paths.pluginFanart)
                    self.clearCache()
            
            # Main Menu
            if len(paramstring) <= 2:                
                mainMenu = ListItem.create()
                mainMenu['url'] = self.MAIN_MENU_FILE
                tmpList = self.parseView(mainMenu)
                if tmpList:
                    self.currentlist = tmpList
                
            else:
                [mode, item] = self._parseParameters()

                # switch(mode)
                if mode == Mode.VIEW:
                    tmpList = self.parseView(item)
                    if tmpList:
                        self.currentlist = tmpList
                        count = len(self.currentlist.items)
                        if count == 1:
                            # Autoplay single video
                            autoplayEnabled = common.getSetting('autoplay') == 'true'
                            if autoplayEnabled:
                                videos = self.currentlist.getVideos()
                                if len(videos) == 1:
                                    self.playVideo(videos[0], True)
                                    


                elif mode == Mode.ADDITEM:
                    tmp = os.path.normpath(paramstring.split('url=')[1])
                    if tmp:
                        suffix = tmp.split(os.path.sep)[-1]
                        tmp = tmp.replace(suffix,'') + urllib.quote_plus(suffix)
                    if self.favouritesManager.add(tmp):
                        xbmc.executebuiltin('Container.Refresh()')


                elif mode in [Mode.ADDTOFAVOURITES, Mode.REMOVEFROMFAVOURITES, Mode.EDITITEM]:

                    if mode == Mode.ADDTOFAVOURITES:
                        self.favouritesManager.addItem(item)
                    elif mode == Mode.REMOVEFROMFAVOURITES:
                        self.favouritesManager.removeItem(item)
                        xbmc.executebuiltin('Container.Refresh()')
                    elif mode == Mode.EDITITEM:
                        if self.favouritesManager.editItem(item):
                            xbmc.executebuiltin('Container.Refresh()')


                elif mode == Mode.EXECUTE:
                    self.executeItem(item)

                elif mode == Mode.PLAY:
                    self.playVideo(item)
                
                elif mode == Mode.WEBDRIVER:
                    url = urllib.quote(item['url'])
                    title = item['title']
                    self.playWebDriver(url, title)

                elif mode == Mode.QUEUE:
                    self.queueAllVideos(item)

                elif mode == Mode.CHROME:
                    url = urllib.quote(item['url'])
                    title = item['title']
                    self.launchChrome(url, title)
                
                elif mode == Mode.INSTALLADDON:
                    success = install(item['url'])
                    if success:
                        xbmc.sleep(100)
                        if xbmcUtils.getCurrentWindowXmlFile() == 'DialogAddonSettings.xml':
                            # workaround to update settings dialog
                            common.setSetting('', '')
                            

        except Exception, e:
            common.showError('Error running SportsDevilNL')
            common.log('Error running SportsDevilNL. Reason:' + str(e))
Example #19
0
class MrknowFilms:

    MAIN_MENU_FILE = 'mainMenu.cfg'


    def __init__(self):
        #BASE_RESOURCE_PATH = os.path.join( os.getcwd(), "resources" )
        BASE_RESOURCE_PATH = os.path.join( ptv.getAddonInfo('path'), "resources" )
        BASE_RESOURCE_PATH = os.path.join( ptv2.getAddonInfo('path'), "resources" )

        sys.path.append( os.path.join( BASE_RESOURCE_PATH, "lib" ) )
        sys.path.append( os.path.join( BASE_RESOURCE_PATH, "resources" ) )

        sys.path.append( os.path.join( ptv.getAddonInfo('path'), "host" ) )
        import mrknow_pLog, settings, Parser, mrknow_pCommon

        self.cm = mrknow_pCommon.common()
        self.log = mrknow_pLog.pLog()
        self.settings = settings.TVSettings()
        self.parser = Parser.Parser()
        #self.log.info("DIR " + common.Paths.modulesDir + 'mainMenu.cfg')
        #self.MAIN_MENU_FILE = os.path.join( common.Paths.modulesDir, 'mainMenu.cfg' )
        self.MAIN_MENU_FILE = 'mainMenu.cfg'
        if not os.path.exists(common.Paths.pluginDataDir):
            os.makedirs(common.Paths.pluginDataDir, 0777)

        self.favouritesManager = FavouritesManager(common.Paths.favouritesFolder)
        self.customModulesManager = CustomModulesManager(common.Paths.customModulesDir, common.Paths.customModulesRepo)

        if not os.path.exists(common.Paths.customModulesDir):
            os.makedirs(common.Paths.customModulesDir, 0777)

        self.parser2 = Parser2()
        self.currentlist = None

        self.addon = None
        self.log.info('Filmy online www.mrknow.pl')
        common.clearCache()


    def _parseParameters(self):
        mode = int(self.addon.queries['mode'])
        queryString = self.addon.queries['item']
        item = ListItem.create()
        if mode in [common.Mode2.CHROME, common.Mode2.ADDTOFAVOURITES, common.Mode2.REMOVEFROMFAVOURITES, common.Mode2.EDITITEM]:
            item.infos = self.addon.parse_query(urllib.unquote(queryString),{})
        else:
            item.infos = self.addon.parse_query(queryString,{})
        item.infos = dict((k.decode('utf8'), v.decode('utf8')) for k, v in item.infos.items())
        return [mode, item]


    def showListOptions(self,argv=None):
        params = self.parser.getParams()
        mode = self.parser.getIntParam(params, "mode")
        name = self.parser.getParam(params, "name")
        service = self.parser.getParam(params, "service")
        self.addon = Addon(scriptID, argv)
        print("MMMMMMMM",mode,params)
        mymodes = [common.Mode2.VIEW ,common.Mode2.PLAY, common.Mode2.ADDTOFAVOURITES, common.Mode2.EXECUTE]


        base = argv[0]
        handle = int(argv[1])
        parameter = argv[2]
        self.base = base
        self.handle = handle

        paramstring = urllib.unquote_plus(parameter)
        #self.log.info(paramstring)
        #self.log.info('Base: '+ base)
        #self.log.info('Handle: '+ str(handle))
        #self.log.info('Parameter: '+ parameter)
        #self.log.info('LEN: ' + str(len(paramstring)))
        #self.log.info('mode: ' + str(mode))

        if mode == None and name == None and service == None:
            self.log.info('Wyświetlam kategorie')
            self.CATEGORIES()
            #self.LIST(MENU_TABLE)
        elif mode == 1:
            self.LIST(TV_ONLINE_TABLE)
        elif mode == 4:
            self.LIST(FUN_ONLINE_TABLE)
        elif mode == 19:
            self.LIST(SPORT_ONLINE_TABLE)
        elif mode == 2:
            self.LIST(FILM_ONLINE_TABLE)
        elif mode == 3:
            self.LIST(SERIALE_ONLINE_TABLE)
        elif mode == 20:
            self.log.info('Wyświetlam ustawienia')
            self.settings.showSettings()
        elif mode == 30:
            self.LIBRTMP()
        #elif mode == 30 and category=='update':
        #    self.DLLIBRTMP(name,murl)

        elif mode in mymodes:
            try:

                # if addon is started
                listItemPath = xbmcUtils.getListItemPath()
                if not listItemPath.startswith(self.base):
                    if not('mode=' in paramstring and not 'mode=110&' in paramstring):
                        xbmcplugin.setPluginFanart(self.handle, common.Paths.pluginFanart)

                        #if common.getSetting('autoupdate') == 'true':
                        #    self.update()


                # Main Menu
                if len(paramstring) <= 9:
                    mainMenu = ListItem.create()
                    mainMenu['url'] = self.MAIN_MENU_FILE
                    tmpList = self.parseView(mainMenu)
                    if tmpList:
                        self.currentlist = tmpList
                else:
                    [mode, item] = self._parseParameters()
                    print("MMMMMMMM",mode,mydump(item))


                    # switch(mode)
                    if mode == common.Mode2.VIEW:
                        tmpList = self.parseView(item)
                        print("MMMMMMMM",item,vars(item))
                        if tmpList:
                            self.currentlist = tmpList
                            count = len(self.currentlist.items)
                            if count == 1:
                                # Autoplay single video
                                autoplayEnabled = ptv.getSetting('autoplay') == 'true'
                                if autoplayEnabled:
                                    videos = self.currentlist.getVideos()
                                    if len(videos) == 1:
                                        self.playVideo(videos[0], True)


                    elif mode == common.Mode2.ADDITEM:
                        tmp = os.path.normpath(paramstring.split('url=')[1])
                        if tmp:
                            suffix = tmp.split(os.path.sep)[-1]
                            tmp = tmp.replace(suffix,'') + urllib.quote_plus(suffix)
                        if self.favouritesManager.add(tmp):
                            xbmc.executebuiltin('Container.Refresh()')


                    elif mode in [common.Mode2.ADDTOFAVOURITES, common.Mode2.REMOVEFROMFAVOURITES, common.Mode2.EDITITEM]:

                        if mode == common.Mode2.ADDTOFAVOURITES:
                            self.favouritesManager.addItem(item)
                        elif mode == common.Mode2.REMOVEFROMFAVOURITES:
                            self.favouritesManager.removeItem(item)
                            xbmc.executebuiltin('Container.Refresh()')
                        elif mode == common.Mode2.EDITITEM:
                            if self.favouritesManager.editItem(item):
                                xbmc.executebuiltin('Container.Refresh()')

                    elif mode == common.Mode2.PLAY:
                        self.playVideo(item)

            except Exception, e:
                common.showError('Error running Mrknow')
                self.log.info('Error running Mrknow. Reason:' + str(e))


        elif mode == 8000 or service == 'kinoliveseriale':
            tv = kinoliveseriale.kinoliveseriale()
            tv.handleService()