示例#1
0
def isdir(path, silent=False, vfs=True):
    """
    Comprueba si la ruta es un directorio
    @param path: ruta
    @type path: str
    @rtype: bool
    @return: Retorna True si la ruta existe y es un directorio
    """
    path = encode(path)
    try:
        if xbmc_vfs and vfs:
            if not scrapertools.find_single_match(path, '(^\w+:\/\/)'):
                return os.path.isdir(path)
            if path.endswith('/') or path.endswith('\\'):
                path = path[:-1]
            dirs, files = xbmcvfs.listdir(dirname(path))
            base_name = basename(path)
            for dir in dirs:
                if base_name == dir:
                    return True
            return False
        elif path.lower().startswith("smb://"):
            return samba.isdir(path)
        else:
            return os.path.isdir(path)
    except:
        logger.error("ERROR when checking the directory: %s" % path)
        if not silent:
            logger.error(traceback.format_exc())
        return False
def update(folder_content=config.get_setting("folder_tvshows"), folder=""):
    """
    Actualiza la libreria dependiendo del tipo de contenido y la ruta que se le pase.

    @type folder_content: str
    @param folder_content: tipo de contenido para actualizar, series o peliculas
    @type folder: str
    @param folder: nombre de la carpeta a escanear.
    """
    logger.info(folder)

    payload = {"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": 1}

    if folder:
        folder = str(folder)
        videolibrarypath = config.get_videolibrary_config_path()

        if folder.endswith('/') or folder.endswith('\\'):
            folder = folder[:-1]

        update_path = None

        if videolibrarypath.startswith("special:"):
            if videolibrarypath.endswith('/'):
                videolibrarypath = videolibrarypath[:-1]
            update_path = videolibrarypath + "/" + folder_content + "/" + folder + "/"
        else:
            #update_path = filetools.join(videolibrarypath, folder_content, folder) + "/"   # Problemas de encode en "folder"
            update_path = filetools.join(videolibrarypath, folder_content,
                                         ' ').rstrip()

        if not scrapertools.find_single_match(update_path, '(^\w+:\/\/)'):
            payload["params"] = {"directory": update_path}
    """while xbmc.getCondVisibility('Library.IsScanningVideo()'):
示例#3
0
def remove_smb_credential(path):
    """
    devuelve el path sin contraseña/usuario para paths de SMB
    @param path: ruta
    @type path: str
    @return: cadena sin credenciales
    @rtype: str
    """
    logger.info()

    if not scrapertools.find_single_match(path, '(^\w+:\/\/)'):
        return path

    protocol = scrapertools.find_single_match(path, '(^\w+:\/\/)')
    path_without_credentials = scrapertools.find_single_match(
        path, '^\w+:\/\/(?:[^;\n]+;)?(?:[^:@\n]+[:|@])?(?:[^@\n]+@)?(.*?$)')

    if path_without_credentials:
        return (protocol + path_without_credentials)
    else:
        return path
示例#4
0
def remove_tags(title):
    """
    devuelve el titulo sin tags como color
    @type title: str
    @param title: title
    @rtype: str
    @return: cadena sin tags
    """
    logger.info()

    title_without_tags = scrapertools.find_single_match(
        title, '\[color .+?\](.+)\[\/color\]')

    if title_without_tags:
        return title_without_tags
    else:
        return title
示例#5
0
def encode(path, _samba=False):
    """
    Codifica una ruta según el sistema operativo que estemos utilizando.
    El argumento path tiene que estar codificado en utf-8
    @type path unicode o str con codificación utf-8
    @param path parámetro a codificar
    @type _samba bool
    @para _samba si la ruta es samba o no
    @rtype: str
    @return ruta codificada en juego de caracteres del sistema o utf-8 si samba
    """
    if not isinstance(path, unicode):
        path = unicode(path, "utf-8", "ignore")

    if scrapertools.find_single_match(path, '(^\w+:\/\/)') or _samba:
        path = path.encode("utf-8", "ignore")
    else:
        if fs_encoding and not PY3:
            path = path.encode(fs_encoding, "ignore")

    return path
示例#6
0
def verify_directories_created():
    from dependencies import logger, filetools, xbmc_videolibrary

    config_paths = [["videolibrarypath", "videolibrary"],
                    ["downloadpath", "downloads"],
                    ["downloadlistpath", "downloads/list"],
                    ["settings_path", "settings_channels"]]

    for path, default in config_paths:
        saved_path = get_setting(path)

        # videoteca
        if path == "videolibrarypath":
            if not saved_path:
                saved_path = xbmc_videolibrary.search_library_path()
                if saved_path:
                    set_setting(path, saved_path)

        if not saved_path:
            saved_path = "special://profile/addon_data/plugin.video." + PLUGIN_NAME + "/" + default
            set_setting(path, saved_path)

        saved_path = xbmc.translatePath(saved_path)
        if not filetools.exists(saved_path):
            logger.debug("Creating %s: %s" % (path, saved_path))
            filetools.mkdir(saved_path)

    config_paths = [["folder_movies", "CINE"], ["folder_tvshows", "SERIES"]]

    for path, default in config_paths:
        saved_path = get_setting(path)

        if not saved_path:
            saved_path = default
            set_setting(path, saved_path)

        content_path = filetools.join(get_videolibrary_path(), saved_path)
        if not filetools.exists(content_path):
            logger.debug("Creating %s: %s" % (path, content_path))

            # si se crea el directorio
            filetools.mkdir(content_path)

    from dependencies import xbmc_videolibrary
    xbmc_videolibrary.update_sources(get_setting("videolibrarypath"))
    xbmc_videolibrary.update_sources(get_setting("downloadpath"))

    try:
        from dependencies import scrapertools
        # Buscamos el archivo addon.xml del skin activo
        skindir = filetools.join(xbmc.translatePath("special://home"),
                                 'addons', xbmc.getSkinDir(), 'addon.xml')
        if not os.path.isdir(skindir):
            return  # No hace falta mostrar error en el log si no existe la carpeta
        # Extraemos el nombre de la carpeta de resolución por defecto
        folder = ""
        data = filetools.read(skindir)
        res = scrapertools.find_multiple_matches(data, '(<res .*?>)')
        for r in res:
            if 'default="true"' in r:
                folder = scrapertools.find_single_match(r, 'folder="([^"]+)"')
                break

        # Comprobamos si existe en el addon y sino es así, la creamos
        default = filetools.join(get_runtime_path(), 'resources', 'skins',
                                 'Default')
        if folder and not filetools.exists(filetools.join(default, folder)):
            filetools.mkdir(filetools.join(default, folder))

        # Copiamos el archivo a dicha carpeta desde la de 720p si éste no existe o si el tamaño es diferente
        if folder and folder != '720p':
            for root, folders, files in filetools.walk(
                    filetools.join(default, '720p')):
                for f in files:
                    if not filetools.exists(filetools.join(default, folder, f)) or \
                            (filetools.getsize(filetools.join(default, folder, f)) !=
                                filetools.getsize(filetools.join(default, '720p', f))):
                        filetools.copy(filetools.join(default, '720p', f),
                                       filetools.join(default, folder, f),
                                       True)
    except:
        import traceback
        logger.error("Al comprobar o crear la carpeta de resolución")
        logger.error(traceback.format_exc())
def update_sources(new='', old=''):
    logger.info()
    if new == old: return

    SOURCES_PATH = xbmc.translatePath("special://userdata/sources.xml")
    if filetools.isfile(SOURCES_PATH):
        xmldoc = minidom.parse(SOURCES_PATH)
    else:
        xmldoc = minidom.Document()
        source_nodes = xmldoc.createElement("sources")

        for type in ['programs', 'video', 'music', 'picture', 'files']:
            nodo_type = xmldoc.createElement(type)
            element_default = xmldoc.createElement("default")
            element_default.setAttribute("pathversion", "1")
            nodo_type.appendChild(element_default)
            source_nodes.appendChild(nodo_type)
        xmldoc.appendChild(source_nodes)

    # collect nodes
    # nodes = xmldoc.getElementsByTagName("video")
    video_node = xmldoc.childNodes[0].getElementsByTagName("video")[0]
    paths_node = video_node.getElementsByTagName("path")

    if old:
        # delete old path
        for node in paths_node:
            if node.firstChild.data == old:
                parent = node.parentNode
                remove = parent.parentNode
                remove.removeChild(parent)

        # write changes
        if sys.version_info[0] >= 3:  #PY3
            filetools.write(
                SOURCES_PATH, '\n'.join([
                    x for x in xmldoc.toprettyxml().splitlines() if x.strip()
                ]))
        else:
            filetools.write(SOURCES_PATH,
                            '\n'.join([
                                x for x in xmldoc.toprettyxml().splitlines()
                                if x.strip()
                            ]),
                            vfs=False)
        logger.info("The path %s has been removed from sources.xml" % old)

    if new:
        # create new path
        list_path = [p.firstChild.data for p in paths_node]
        if new in list_path:
            logger.info("The path %s already exists in sources.xml" % new)
            return
        logger.info("The path %s does not exist in sources.xml" % new)

        # if the path does not exist we create one
        source_node = xmldoc.createElement("source")

        # <name> Node
        name_node = xmldoc.createElement("name")
        sep = os.sep
        if new.startswith("special://") or scrapertools.find_single_match(
                new, r'(^\w+:\/\/)'):
            sep = "/"
        name = new
        if new.endswith(sep):
            name = new[:-1]
        name_node.appendChild(xmldoc.createTextNode(name.rsplit(sep)[-1]))
        source_node.appendChild(name_node)

        # <path> Node
        path_node = xmldoc.createElement("path")
        path_node.setAttribute("pathversion", "1")
        path_node.appendChild(xmldoc.createTextNode(new))
        source_node.appendChild(path_node)

        # <allowsharing> Node
        allowsharing_node = xmldoc.createElement("allowsharing")
        allowsharing_node.appendChild(xmldoc.createTextNode('true'))
        source_node.appendChild(allowsharing_node)

        # Añadimos <source>  a <video>
        video_node.appendChild(source_node)

        # write changes
        if sys.version_info[0] >= 3:  #PY3
            filetools.write(
                SOURCES_PATH, '\n'.join([
                    x for x in xmldoc.toprettyxml().splitlines() if x.strip()
                ]))
        else:
            filetools.write(SOURCES_PATH,
                            '\n'.join([
                                x for x in xmldoc.toprettyxml().splitlines()
                                if x.strip()
                            ]),
                            vfs=False)
        logger.info("The path %s has been added to sources.xml" % new)
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