Ejemplo n.º 1
0
def GetFile(subtitle_file_io, path, file_name):
    """ 
        Get subtitle file from BytesIO object. subtitle_file_io is file like
        object that contains either a single subtitle file or a zip file. 
        path specify the directory in whice the subtitle will be saved. The 
        file_name specify the name for the subtitle. If the subtitle_file_io is 
        a zip file, will extract all the subtitles under it, otherwise, will 
        save the file in the given location.

        Note: will use the original name if: 
            a. file_name is empty 
            b. there is more then one file in the archive

        In case of success, returns true, else false
    """
    from Logs import INFO as INFO_LOGS
    from Logs import WARN as WARN_LOGS
    from Logs import DIRECTION as DIRC_LOGS

    import Interaction

    writeLog = Interaction.getInteractor().writeLog

    is_success = False
    subtitle_directory = os.path.abspath(path)
    subtitle_full_path = os.path.join(subtitle_directory, file_name)

    writeLog(INFO_LOGS.STARTING_SUBTITLE_DOWNLOAD_PROCEDURE)
    writeLog(INFO_LOGS.DESTINATION_DIRECTORY_FOR_SUBTITLE % subtitle_directory)

    file_is_zip = zipfile.is_zipfile(subtitle_file_io)

    if file_is_zip:
        try:
            with zipfile.ZipFile(subtitle_file_io, "r") as zfile:
                # Get the file names of the subtitles in the archive.
                # Filtering out any other file types
                filter_func = lambda x: x.lower().endswith(tuple(GetSubtitlesExtensions()))
                sub_filenames = myfilter(filter_func, zfile.namelist())
                if not sub_filenames:
                    writeLog(WARN_LOGS.NO_SUBTITLE_FILES_IN_THE_ARCHIVE)
                else:
                    # If we have more than one subtitle file in the archive, we
                    # keep the original filenames that comes with the archive)
                    if len(sub_filenames) > 1:
                        writeLog(INFO_LOGS.GOT_SEVERAL_FILES_IN_THE_ARCHIVE)
                        for file_name in sub_filenames:
                            # Write the file with the original filename
                            file_path = os.path.join(subtitle_directory, file_name)
                            open(file_path, "wb").write(zfile.open(file_name).read())
                            writeLog(INFO_LOGS.EXTRACTED_SUBTITLE_FILE % file_name)
                    else:
                        # If the file_name was empty, we keep the original also
                        if not file_name:
                            subtitle_full_path = os.path.join(subtitle_directory, sub_filenames[0])
                        sub_content = zfile.open(sub_filenames[0]).read()
                        open(subtitle_full_path, "wb").write(sub_content)
                        writeLog(INFO_LOGS.EXTRACTED_SUBTITLE_FILE % subtitle_full_path)
            # Notify successful extraction
            writeLog(INFO_LOGS.SUCCESSFULL_EXTRACTION_FROM_ARCHIVE)
            is_success = True
        except Exception as eX:
            WriteDebug("Failed constructing ZipFile object: %s" % eX)
            writeLog(WARN_LOGS.FAILED_EXTRACTING_SUBTITLE_FILE_FROM_ARCHIVE)
    # If the file is simple subtitle text file
    else:
        try:
            with open(subtitle_full_path, "wb") as sub_file:
                subtitle_file_io.seek(0)
                content = subtitle_file_io.getvalue()
                sub_file.write(content)
        except Exception as eX:
            WriteDebug("Failed saving subtitle as simple text file: %s" % eX)
            writeLog(WARN_LOGS.FAILED_SAVING_SUBTITLE_SIMPLE_FILE)
        is_success = True
    return is_success
Ejemplo n.º 2
0
    return_value = setNextSubProviderInModule()
    writeLog(INFO_LOGS.SETTING_PROVIDER % getSubProvider().PROVIDER_NAME)
    return return_value

from SubStages.QuerySubStage import QuerySubStage

from Settings.Config import SubiTConfig

from Logs import INFO as INFO_LOGS
from Logs import WARN as WARN_LOGS
from Logs import DIRECTION as DIRC_LOGS
from Logs import FINISH as FINISH_LOGS
from Logs import BuildLog

import Interaction
Interactor  = Interaction.getInteractor()
writeLog    = Interactor.writeLog

from Utils import WriteDebug
from Utils import SplitToFileAndDirectory


class SubFlow(object):
    """ SubFlow is the core of SubiT. That's where all the modules are joined 
        together in order to get the subtitles.
        
        The scope of a SubFlow instance is a single SingleInput instance. i.e.
        SubFlow is not responsible for retrieving movie files from directories
        or figure out whether the given file has a subtitle already downloaded
        or not (That's the job of whoever calling the instance).
Ejemplo n.º 3
0
def GetSubtitleDownloadDirectory(dir_by_flow = None, interactive = False):
    """ Get the full path for the subtitle download directory. The argument
        dir_by_flow specify the directory in which the movie file exists.
        
        If dir_by_flow is missing, and interactive is false, the function will
        back-off to the path stored in default_directory param in the config, 
        even if the flag of always_use_default_directory is turned off.
    """
    from Settings import DEFAULT_DIRECTORY_DEFAULT_VAL
    from Settings.Config import SubiTConfig

    from Logs import WARN as WARN_LOGS
    from Logs import DIRECTION as DIRC_LOGS

    download_directory = None
    conf_default_directory = SubiTConfig.Singleton().getStr\
        ('Global', 'default_directory', DEFAULT_DIRECTORY_DEFAULT_VAL)
    conf_always_use_default_dir = SubiTConfig.Singleton().getBoolean\
        ('Global', 'always_use_default_directory', False)

    if conf_default_directory == DEFAULT_DIRECTORY_DEFAULT_VAL:
        WriteDebug('conf_default_directory is: [%s], giving os.getcwd() [%s]' % (conf_default_directory, os.getcwd()))
        conf_default_directory = os.getcwd()
    elif not os.path.exists(conf_default_directory):
        WriteDebug('conf_default_directory [%s] is missing, giving os.getcwd() [%s]' % (conf_default_directory, os.getcwd()))
        conf_default_directory = os.getcwd()

    # In order to avoid failure of the os.path.exists function (by calling them
    # with None object). we replace the None value with empty string.
    if dir_by_flow is None:
        dir_by_flow = ''


    # The result of these 4 lines is simple. If dir_by_flow exists, and the conf
    # of always_use_default_dir is False, we return the dir_by_flow, if it's True
    # we return the conf_default_directory. In any other case, we return None
    if os.path.exists(dir_by_flow):
        WriteDebug('Setting download_directory to be dir_by_flow [%s]' % dir_by_flow)
        download_directory = dir_by_flow
    if conf_always_use_default_dir:
        WriteDebug('Setting download_directory to be conf_default_directory [%s]' % conf_default_directory)
        download_directory = conf_default_directory

    if not download_directory and interactive:
        import Interaction
        Interactor  = Interaction.getInteractor()
        writeLog = Interactor.writeLog

        while not download_directory:
            user_dir_choice = Interactor.getDestinationDirectoryInput\
                (conf_default_directory, DIRC_LOGS.INSERT_LOCATION_FOR_SUBTITLE_DOWNLOAD)
            if os.path.exists(user_dir_choice):
                WriteDebug('User enter legit path, using it: %s' % user_dir_choice)
                download_directory = user_dir_choice
            else:
                WriteDebug('User enter non-legit path [%s], asking again!' % user_dir_choice)
                writeLog(WARN_LOGS.ERROR_DIRECTORY_DOESNT_EXISTS % user_dir_choice)
    elif not download_directory:
        WriteDebug('To avoid problems, setting download_directory to conf_default_directory: %s' % conf_default_directory)
        download_directory = conf_default_directory

    return download_directory