def do_config(custom=False):
     if set_content("movie", True, custom) and set_content(
             "tvshow", True, custom):
         platformtools.dialog_ok(config.get_localized_string(80026),
                                 config.get_localized_string(70104))
         config.set_setting("videolibrary_kodi", True)
         update()
     else:
         platformtools.dialog_ok(config.get_localized_string(80026),
                                 config.get_localized_string(80024))
         config.set_setting("videolibrary_kodi", False)
def chooseBranch():
    global branch
    apiLink = 'https://api.github.com/repos/' + user + '/' + repo + '/branches'
    try:
        branches = urllib.urlopen(apiLink).read()
    except Exception as e:
        platformtools.dialog_ok(config.get_localized_string(20000), config.get_localized_string(80031))
        logger.info(e)
        return False
    branches = json.loads(branches)
    chDesc = [config.get_localized_string(80034), config.get_localized_string(80035)]
    chDesc.extend([b['name'] for b in branches if b['name'] not in ['stable', 'master']])
    chName = ['stable', 'master']
    chName.extend([b['name'] for b in branches if b['name'] not in ['stable', 'master']])
    sel = platformtools.dialog_select(config.get_localized_string(80033), chDesc)
    branch = chName[sel]
    return True
def refreshLang():
    language = config.get_localized_string(20001)
    if language == 'eng':
        xbmc.executebuiltin("SetGUILanguage(resource.language.it_it)")
        xbmc.executebuiltin("SetGUILanguage(resource.language.en_en)")
    else:
        xbmc.executebuiltin("SetGUILanguage(resource.language.en_en)")
        xbmc.executebuiltin("SetGUILanguage(resource.language.it_it)")
Esempio n. 4
0
def dialog_yesno(heading,
                 message,
                 nolabel=config.get_localized_string(70170),
                 yeslabel=config.get_localized_string(30022),
                 autoclose=0,
                 customlabel=None):
    dialog = xbmcgui.Dialog()
    # customlabel only work on kodi 19
    if PY3 and customlabel:
        return dialog.yesnocustom(heading,
                                  message,
                                  customlabel=customlabel,
                                  nolabel=nolabel,
                                  yeslabel=yeslabel,
                                  autoclose=autoclose)
    else:
        return dialog.yesno(heading,
                            message,
                            nolabel=nolabel,
                            yeslabel=yeslabel,
                            autoclose=autoclose)
def updateFromZip(message=config.get_localized_string(80050)):
    dp = platformtools.dialog_progress_bg(config.get_localized_string(20000), message)
    dp.update(0)

    remotefilename = 'https://github.com/' + user + "/" + repo + "/archive/" + branch + ".zip"
    localfilename = filetools.join(xbmc.translatePath("special://home/addons/"), "plugin.video.kod.update.zip")
    destpathname = xbmc.translatePath("special://home/addons/")
    extractedDir = filetools.join(destpathname, "addon-" + branch)

    logger.info("remotefilename=%s" % remotefilename)
    logger.info("localfilename=%s" % localfilename)
    logger.info('extract dir: ' + extractedDir)

    # pulizia preliminare
    remove(localfilename)
    removeTree(extractedDir)

    try:
        urllib.urlretrieve(remotefilename, localfilename,
                           lambda nb, bs, fs, url=remotefilename: _pbhook(nb, bs, fs, url, dp))
    except Exception as e:
        platformtools.dialog_ok(config.get_localized_string(20000), config.get_localized_string(80031))
        logger.info('Non sono riuscito a scaricare il file zip')
        logger.info(e)
        dp.close()
        return False

    # Lo descomprime
    logger.info("decompressione...")
    logger.info("destpathname=%s" % destpathname)

    if os.path.isfile(localfilename):
        logger.info('il file esiste')

    dp.update(80, config.get_localized_string(20000), config.get_localized_string(80032))

    import zipfile
    try:
        hash = fixZipGetHash(localfilename)
        logger.info(hash)

        with zipfile.ZipFile(fOpen(localfilename, 'rb')) as zip:
            size = sum([zinfo.file_size for zinfo in zip.filelist])
            cur_size = 0
            for member in zip.infolist():
                zip.extract(member, destpathname)
                cur_size += member.file_size
                dp.update(int(80 + cur_size * 15 / size))

    except Exception as e:
        logger.info('Non sono riuscito ad estrarre il file zip')
        logger.error(e)
        import traceback
        logger.error(traceback.print_exc())
        dp.close()
        remove(localfilename)

        return False

    dp.update(95)

    # puliamo tutto
    global addonDir
    if extractedDir != addonDir:
        removeTree(addonDir)
    xbmc.sleep(1000)

    rename(extractedDir, 'plugin.video.kod')
    addonDir = filetools.join(destpathname, 'plugin.video.kod')

    logger.info("Cancellando il file zip...")
    remove(localfilename)

    dp.update(100)
    xbmc.sleep(1000)
    dp.close()
    xbmc.executebuiltin("UpdateLocalAddons")

    # in run()
    # refreshLang()

    return hash
def ask_set_content(silent=False):
    logger.debug()
    logger.debug("videolibrary_kodi %s" %
                 config.get_setting("videolibrary_kodi"))

    def do_config(custom=False):
        if set_content("movie", True, custom) and set_content(
                "tvshow", True, custom):
            platformtools.dialog_ok(config.get_localized_string(80026),
                                    config.get_localized_string(70104))
            config.set_setting("videolibrary_kodi", True)
            update()
        else:
            platformtools.dialog_ok(config.get_localized_string(80026),
                                    config.get_localized_string(80024))
            config.set_setting("videolibrary_kodi", False)

    # configuration during installation
    if not silent:
        # ask to configure Kodi video library
        if platformtools.dialog_yesno(config.get_localized_string(20000),
                                      config.get_localized_string(80015)):
            # ask for custom or default settings
            if not platformtools.dialog_yesno(
                    config.get_localized_string(80026),
                    config.get_localized_string(80016),
                    config.get_localized_string(80017),
                    config.get_localized_string(80018)):
                # input path and folders
                path = platformtools.dialog_browse(
                    3, config.get_localized_string(80019),
                    config.get_setting("videolibrarypath"))
                movies_folder = platformtools.dialog_input(
                    config.get_setting("folder_movies"),
                    config.get_localized_string(80020))
                tvshows_folder = platformtools.dialog_input(
                    config.get_setting("folder_tvshows"),
                    config.get_localized_string(80021))

                if path != "" and movies_folder != "" and tvshows_folder != "":
                    movies_path, tvshows_path = check_sources(
                        filetools.join(path, movies_folder),
                        filetools.join(path, tvshows_folder))
                    # configure later
                    if movies_path or tvshows_path:
                        platformtools.dialog_ok(
                            config.get_localized_string(80026),
                            config.get_localized_string(80029))
                    # set path and folders
                    else:
                        update_sources(path,
                                       config.get_setting("videolibrarypath"))
                        config.set_setting("videolibrarypath", path)
                        config.set_setting("folder_movies", movies_folder)
                        config.set_setting("folder_tvshows", tvshows_folder)
                        config.verify_directories_created()
                        do_config(False)
                # default path and folders
                else:
                    platformtools.dialog_ok(config.get_localized_string(80026),
                                            config.get_localized_string(80030))
                    do_config(False)
            # default settings
            else:
                platformtools.dialog_ok(config.get_localized_string(80026),
                                        config.get_localized_string(80027))
                do_config(False)
        # configure later
        else:
            platformtools.dialog_ok(config.get_localized_string(20000),
                                    config.get_localized_string(80022))
    # configuration from the settings menu
    else:
        platformtools.dialog_ok(config.get_localized_string(80026),
                                config.get_localized_string(80023))
        do_config(False)
def set_content(content_type, silent=False, custom=False):
    """
    Procedure to auto-configure the kodi video library with the default values
    @type content_type: str ('movie' o 'tvshow')
    @param content_type: content type to configure, series or movies
    """
    logger.debug()
    continuar = True
    msg_text = ""
    videolibrarypath = config.get_setting("videolibrarypath")

    if content_type == 'movie':
        scraper = [
            config.get_localized_string(70093),
            config.get_localized_string(70096)
        ]
        if not custom:
            seleccion = 0  # tmdb
        else:
            seleccion = platformtools.dialog_select(
                config.get_localized_string(70094), scraper)

        # Instalar The Movie Database
        if seleccion == -1 or seleccion == 0:
            if not xbmc.getCondVisibility(
                    'System.HasAddon(metadata.themoviedb.org)'):
                if not silent:
                    # Ask if we want to install metadata.themoviedb.org
                    install = platformtools.dialog_yesno(
                        config.get_localized_string(60046), '')
                else:
                    install = True

                if install:
                    try:
                        # Install metadata.themoviedb.org
                        xbmc.executebuiltin(
                            'InstallAddon(metadata.themoviedb.org)', True)
                        logger.debug(
                            "Instalado el Scraper de películas de TheMovieDB")
                    except:
                        pass

                continuar = (install and xbmc.getCondVisibility(
                    'System.HasAddon(metadata.themoviedb.org)'))
                if not continuar:
                    msg_text = config.get_localized_string(60047)
            if continuar:
                xbmc.executebuiltin(
                    'Addon.OpenSettings(metadata.themoviedb.org)', True)

        # Instalar Universal Movie Scraper
        elif seleccion == 1:
            if continuar and not xbmc.getCondVisibility(
                    'System.HasAddon(metadata.universal)'):
                continuar = False
                if not silent:
                    # Ask if we want to install metadata.universal
                    install = platformtools.dialog_yesno(
                        config.get_localized_string(70095), '')
                else:
                    install = True

                if install:
                    try:
                        xbmc.executebuiltin('InstallAddon(metadata.universal)',
                                            True)
                        if xbmc.getCondVisibility(
                                'System.HasAddon(metadata.universal)'):
                            continuar = True
                    except:
                        pass

                continuar = (install and continuar)
                if not continuar:
                    msg_text = config.get_localized_string(70097)
            if continuar:
                xbmc.executebuiltin('Addon.OpenSettings(metadata.universal)',
                                    True)

    else:  # SERIES
        scraper = [
            config.get_localized_string(70093),
            config.get_localized_string(70098)
        ]
        if not custom:
            seleccion = 0  # tmdb
        else:
            seleccion = platformtools.dialog_select(
                config.get_localized_string(70107), scraper)

        # Instalar The Movie Database
        if seleccion == -1 or seleccion == 0:
            if continuar and not xbmc.getCondVisibility(
                    'System.HasAddon(metadata.tvshows.themoviedb.org)'):
                continuar = False
                if not silent:
                    # Ask if we want to install metadata.tvshows.themoviedb.org
                    install = platformtools.dialog_yesno(
                        config.get_localized_string(60050), '')
                else:
                    install = True

                if install:
                    try:
                        # Install metadata.tvshows.themoviedb.org
                        xbmc.executebuiltin(
                            'InstallAddon(metadata.tvshows.themoviedb.org)',
                            True)
                        if xbmc.getCondVisibility(
                                'System.HasAddon(metadata.tvshows.themoviedb.org)'
                        ):
                            continuar = True
                    except:
                        pass

                continuar = (install and continuar)
                if not continuar:
                    msg_text = config.get_localized_string(60051)
            if continuar:
                xbmc.executebuiltin(
                    'Addon.OpenSettings(metadata.tvshows.themoviedb.org)',
                    True)

        # Instalar The TVDB
        elif seleccion == 1:
            if not xbmc.getCondVisibility(
                    'System.HasAddon(metadata.tvdb.com)'):
                if not silent:
                    #Ask if we want to install metadata.tvdb.com
                    install = platformtools.dialog_yesno(
                        config.get_localized_string(60048), '')
                else:
                    install = True

                if install:
                    try:
                        # Install metadata.tvdb.com
                        xbmc.executebuiltin('InstallAddon(metadata.tvdb.com)',
                                            True)
                        logger.debug("The TVDB series Scraper installed ")
                    except:
                        pass

                continuar = (install and xbmc.getCondVisibility(
                    'System.HasAddon(metadata.tvdb.com)'))
                if not continuar:
                    msg_text = config.get_localized_string(60049)
            if continuar:
                xbmc.executebuiltin('Addon.OpenSettings(metadata.tvdb.com)',
                                    True)

    idPath = 0
    idParentPath = 0
    if continuar:
        continuar = False

        # We look for the idPath
        sql = 'SELECT MAX(idPath) FROM path'
        nun_records, records = execute_sql_kodi(sql)
        if nun_records == 1:
            idPath = records[0][0] + 1

        sql_videolibrarypath = videolibrarypath
        if sql_videolibrarypath.startswith("special://"):
            sql_videolibrarypath = sql_videolibrarypath.replace(
                '/profile/', '/%/').replace('/home/userdata/', '/%/')
            sep = '/'
        elif scrapertools.find_single_match(sql_videolibrarypath,
                                            r'(^\w+:\/\/)'):
            sep = '/'
        else:
            sep = os.sep

        if not sql_videolibrarypath.endswith(sep):
            sql_videolibrarypath += sep

        # We are looking for the idParentPath
        sql = 'SELECT idPath, strPath FROM path where strPath LIKE "%s"' % sql_videolibrarypath
        nun_records, records = execute_sql_kodi(sql)
        if nun_records == 1:
            idParentPath = records[0][0]
            videolibrarypath = records[0][1][:-1]
            continuar = True
        else:
            # There is no videolibrarypath in the DB: we insert it
            sql_videolibrarypath = videolibrarypath
            if not sql_videolibrarypath.endswith(sep):
                sql_videolibrarypath += sep

            sql = 'INSERT INTO path (idPath, strPath,  scanRecursive, useFolderNames, noUpdate, exclude) VALUES ' \
                  '(%s, "%s", 0, 0, 0, 0)' % (idPath, sql_videolibrarypath)
            nun_records, records = execute_sql_kodi(sql)
            if nun_records == 1:
                continuar = True
                idParentPath = idPath
                idPath += 1
            else:
                msg_text = config.get_localized_string(70101)

    if continuar:
        continuar = False

        # We set strContent, strScraper, scanRecursive and strSettings
        if content_type == 'movie':
            strContent = 'movies'
            scanRecursive = 2147483647
            if seleccion == -1 or seleccion == 0:
                strScraper = 'metadata.themoviedb.org'
                path_settings = xbmc.translatePath(
                    "special://profile/addon_data/metadata.themoviedb.org/settings.xml"
                )
            elif seleccion == 1:
                strScraper = 'metadata.universal'
                path_settings = xbmc.translatePath(
                    "special://profile/addon_data/metadata.universal/settings.xml"
                )
            if not os.path.exists(path_settings):
                logger.debug("%s: %s" %
                             (content_type, path_settings + " doesn't exist"))
                return continuar
            settings_data = filetools.read(path_settings)
            strSettings = ' '.join(settings_data.split()).replace("> <", "><")
            strSettings = strSettings.replace("\"", "\'")
            strActualizar = "Do you want to set this Scraper in Spanish as the default option for movies?"
            if not videolibrarypath.endswith(sep):
                videolibrarypath += sep
            strPath = videolibrarypath + config.get_setting(
                "folder_movies") + sep
        else:
            strContent = 'tvshows'
            scanRecursive = 0
            if seleccion == -1 or seleccion == 0:
                strScraper = 'metadata.tvshows.themoviedb.org'
                path_settings = xbmc.translatePath(
                    "special://profile/addon_data/metadata.tvshows.themoviedb.org/settings.xml"
                )
            elif seleccion == 1:
                strScraper = 'metadata.tvdb.com'
                path_settings = xbmc.translatePath(
                    "special://profile/addon_data/metadata.tvdb.com/settings.xml"
                )
            if not os.path.exists(path_settings):
                logger.debug("%s: %s" %
                             (content_type, path_settings + " doesn't exist"))
                return continuar
            settings_data = filetools.read(path_settings)
            strSettings = ' '.join(settings_data.split()).replace("> <", "><")
            strSettings = strSettings.replace("\"", "\'")
            strActualizar = "Do you want to configure this Scraper in Spanish as a default option for series?"
            if not videolibrarypath.endswith(sep):
                videolibrarypath += sep
            strPath = videolibrarypath + config.get_setting(
                "folder_tvshows") + sep

        logger.debug("%s: %s" % (content_type, strPath))
        # We check if strPath already exists in the DB to avoid duplicates
        sql = 'SELECT idPath FROM path where strPath="%s"' % strPath
        nun_records, records = execute_sql_kodi(sql)
        sql = ""
        if nun_records == 0:
            # Insertamos el scraper
            sql = 'INSERT INTO path (idPath, strPath, strContent, strScraper, scanRecursive, useFolderNames, ' \
                  'strSettings, noUpdate, exclude, idParentPath) VALUES (%s, "%s", "%s", "%s", %s, 0, ' \
                  '"%s", 0, 0, %s)' % (
                      idPath, strPath, strContent, strScraper, scanRecursive, strSettings, idParentPath)
        else:
            if not silent:
                # Preguntar si queremos configurar themoviedb.org como opcion por defecto
                actualizar = platformtools.dialog_yesno(
                    config.get_localized_string(70098), strActualizar)
            else:
                actualizar = True

            if actualizar:
                # Actualizamos el scraper
                idPath = records[0][0]
                sql = 'UPDATE path SET strContent="%s", strScraper="%s", scanRecursive=%s, strSettings="%s" ' \
                      'WHERE idPath=%s' % (strContent, strScraper, scanRecursive, strSettings, idPath)

        if sql:
            nun_records, records = execute_sql_kodi(sql)
            if nun_records == 1:
                continuar = True

        if not continuar:
            msg_text = config.get_localized_string(60055)

    if not continuar:
        heading = config.get_localized_string(70102) % content_type
    elif content_type == 'tvshow' and not xbmc.getCondVisibility(
            'System.HasAddon(metadata.tvshows.themoviedb.org)'):
        heading = config.get_localized_string(70103) % content_type
        msg_text = config.get_localized_string(60058)
    else:
        heading = config.get_localized_string(70103) % content_type
        msg_text = config.get_localized_string(70104)

    logger.debug("%s: %s" % (heading, msg_text))
    return continuar