Esempio n. 1
0
def rename_and_move(directory):
    new_folder = rename_folder(directory)
    if type(new_folder) is None:
        settings.debug_message("Encountered an issue with {}! Skipping!"
                               .format(directory))
    else:
        move_folder(new_folder)
Esempio n. 2
0
def move_folder(new_directory, dir_type='movie'):
    if dir_type is 'movie':
        settings_dir = settings.movie_dir
    elif dir_type is 'audio':
        settings_dir = settings.audio_dir

    if new_directory is None:
        settings.debug_message("Something went wrong! Skipping!")
    else:

        if not os.path.isdir(os.path.join(settings_dir, new_directory)):

            try:
                shutil.move(os.path.join(settings.incoming_dir, new_directory),
                            os.path.join(settings_dir, new_directory))
                settings.debug_message(
                    "Move successful! Folder {} now located at {}".
                    format(new_directory, settings_dir + "\\" +
                           new_directory))
            except (OSError, shutil.Error):
                print("{} is already in the destination directory! Renaming!".
                      format(new_directory))
                rename_duplicate(new_directory)
        else:
            print("{} is already in the destination directory! Renaming!".
                  format(new_directory))
            rename_duplicate(new_directory)
Esempio n. 3
0
def delete_samples(directory, dir_files):
    # nuke any sample files and anything in the extensions to delete string
    # like txt files, nfo files, and jpg files
    for thing_to_delete in dir_files:
        if settings.get_extension(thing_to_delete) in settings.video_formats:
            if is_sample(directory, thing_to_delete):
                settings.debug_message("Found sample movie {}! ENGAGING LASERS"
                                       .format(thing_to_delete))
                os.remove(os.path.join(settings.incoming_dir, directory,
                          thing_to_delete))
        if settings.get_extension(thing_to_delete) in settings.exts_to_delete:
            settings.debug_message("NUKING {}".format(thing_to_delete))
            os.remove(os.path.join(settings.incoming_dir, directory,
                      thing_to_delete))
Esempio n. 4
0
def root_level_files(files):

    settings.debug_message("Found root level files: {}".format(files))
    if 'Thumbs.db' in files:
        # ignore the file; this does not delete it
        files.remove('Thumbs.db')
        settings.debug_message("Ignoring Thumbs.db")

    for prospect_file in files:
        if not check_for_skips(prospect_file):
            if get_extension(prospect_file) in settings.video_formats:
                if not settings.in_use(os.path.join(settings.incoming_dir,
                                                    prospect_file)):
                    process_root_level_movie(prospect_file)
Esempio n. 5
0
def rename_folder(directory):
    settings.debug_message("Attempting rename of parent folder!")

    translated_folder = folder_translator(directory)

    if translated_folder is None:
        print("{} has an issue I can't recover from. Skipping!".
              format(directory))
        try:
            rename_skipped(directory)
        except OSError:
            settings.debug_message("{} is locked by the OS. Skipping!".
                                   format(directory))
            pass
    else:
        new_directory = "{} ({})".format(translated_folder[0],
                                         translated_folder[1])
        try:
            os.rename(os.path.join(settings.incoming_dir, directory),
                      os.path.join(
                      settings.incoming_dir, new_directory))
            settings.debug_message("Rename successful!")
        except OSError:
            settings.debug_message("Access denied while trying to rename {}!".
                                   format(new_directory))
            return None

        return new_directory
Esempio n. 6
0
def process_root_level_movie(movie):

    translated_folder = folder_translator(movie)
    if translated_folder is not None:
        renamed_movie = "{} ({})".format(translated_folder[0],
                                         translated_folder[1])
    else:
        rename_skipped(movie)
        return

    settings.debug_message("Moving root level file {} into new folder!".format(
                           movie))
    if not os.path.isdir(os.path.join(settings.movie_dir, renamed_movie)):

        try:

            for banned_ch in settings.banned_characters:
                if banned_ch in renamed_movie:
                    renamed_movie = renamed_movie.replace(banned_ch, "")

            settings.debug_message("Creating folder for root level file {}"
                                   .format(movie))
            os.mkdir(os.path.join(settings.incoming_dir, renamed_movie))
            shutil.move(os.path.join(settings.incoming_dir, movie),
                        os.path.join(settings.incoming_dir, renamed_movie,
                                     movie))

            settings.debug_message("Moving folder to movies folder!")
            shutil.move(os.path.join(settings.incoming_dir, renamed_movie),
                        os.path.join(settings.movie_dir, renamed_movie))
            settings.debug_message("Move successful!")
        except (OSError, shutil.Error):

            print("{} is already in the destination directory! Renaming!".
                  format(renamed_movie))
            rename_duplicate(renamed_movie)
    else:

        if not os.path.isfile(os.path.join(settings.movie_dir, renamed_movie,
                                           movie)):
            try:
                shutil.move(os.path.join(settings.incoming_dir, movie),
                            os.path.join(settings.movie_dir, renamed_movie,
                                         movie))
            except shutil.Error:
                print("Something went wrong with moving {}! Skipping!".format(
                      movie))
        else:
            print("{} is already in the destination directory! Renaming!".
                  format(renamed_movie))
            if not os.path.isdir(os.path.join(settings.incoming_dir,
                                              renamed_movie)):
                os.mkdir(os.path.join(settings.incoming_dir, renamed_movie))
                shutil.move(os.path.join(settings.incoming_dir, movie),
                            os.path.join(settings.incoming_dir, renamed_movie,
                                         movie))
                rename_duplicate(renamed_movie)
Esempio n. 7
0
def main_loop():
    # search directories and start figuring out what's what
    # returns first level folder names under dir

    dirs = [x for x in os.listdir(settings.incoming_dir) if os.path.isdir(
            os.path.join(settings.incoming_dir, x))]
    settings.debug_message("Found directories: {}".format(dirs))

    process_folders(dirs)

    # check for files that aren't under their own folders for some
    # godforsaken reason
    files = [f for f in os.listdir(settings.incoming_dir) if os.path.
             isfile(os.path.join(settings.incoming_dir, f))]

    if files is not []:
        root_level_files(files)

    settings.debug_message("Sleeping for {} seconds!".
                           format(settings.delay_time))

    status_update("Sleeping for {} seconds!\r".format(settings.delay_time))
    time.sleep(int(settings.delay_time))
    status_update("Working...                  \r")
Esempio n. 8
0
def is_video_folder(directory, dir_files):
    for directory_file in dir_files:
        settings.debug_message("Testing file {} for video format!".
                               format(directory_file))
        if settings.get_extension(directory_file) in settings.video_formats:
            settings.debug_message("Found video folder: {}".format(directory))
            return True

    settings.debug_message("Folder does not contain first level video files."
                           " Skipping.")
    return False
Esempio n. 9
0
def is_audio_folder(directory, dir_files):
    for directory_file in dir_files:
        settings.debug_message("Testing file {} for audio format!".
                               format(directory_file))
        if settings.get_extension(directory_file) in settings.audio_formats:
            settings.debug_message("Found audio folder: {}".format(directory))
            return True
        else:
            settings.debug_message("Folder does not contain first level audio "
                                   "files. Skipping.")
            return False
Esempio n. 10
0
def in_use(test_file):

    # yes, possible race condition, but due to the system we're putting this
    # into race conditions will not be an issue.

    try:
        os.rename(test_file, test_file+"_")
        settings.debug_message("Testing to see if {} is in use".
                               format(test_file))
        os.rename(test_file+"_", test_file)
        settings.debug_message("Not in use! Proceed!")
        return False
    except OSError:
        settings.debug_message("File in use!")
        return True
Esempio n. 11
0
def check_for_skips(item):

    if "[TV]" in item:
        settings.debug_message("Found previously scanned TV folder. Skipping!")
        return True
    elif "[DUPLICATE]" in item:
        settings.debug_message("Found previously scanned duplicate directory. "
                               "Skipping!")
        return True
    elif "[SKIP]" in item:
        settings.debug_message("Found previously scanned skipped directory."
                               " Skipping again!")
        return True
    else:
        return False
Esempio n. 12
0
def folder_translator(foldername, title_parser=title_parser):
    '''Uses regex to yoink the name and year out of movie titles.'''
    settings.debug_message("Running folder/name translation on {}".format(
                           foldername))

    # check to see if we're operating on a file or a folder
    if settings.get_extension(foldername) in settings.video_formats:
        extension = settings.get_extension(foldername)
        foldername = foldername[:-len(extension)].replace(".", " ")
        foldername_less_extension = foldername.replace("_", " ")
        foldername = foldername_less_extension + extension
    else:
        foldername = foldername.replace(".", " ")
        foldername = foldername.replace("_", " ")

    foldername = foldername.title()

    settings.debug_message("Now {} after replacement!".format(foldername))
    title_data = title_parser.match(foldername)

    try:
        # try and force an attribute error before it happens
        x = title_data.group("title")
        x = title_data.group("year")
    except AttributeError:
        settings.debug_message("folder_translator - AttributeError!")

        # if it's a folder, foldername_less_extension will not have been
        # created; we catch the unbound error and just use the normal folder
        # name here for that reason
        try:
            title = foldername_less_extension
        except UnboundLocalError:
            title = foldername

        if settings.imdb:
            settings.debug_message("Attempting lookup through IMDbPy!")
            settings.debug_message("IMDbPy - Searching for year of {}".format(
                                   title))
            try:
                maybe_title = imdb_access.search_movie(
                    "{}".format(title))[0]['title']
                settings.debug_message("IMDbPy - I think I found it! It's {}!".
                                       format(maybe_title))
                maybe_year = imdb_access.search_movie(
                    "{}".format(title))[0]['year']
                settings.debug_message("IMDbPy - I think I found it! It's {}!".
                                       format(maybe_year))
                settings.debug_message("IMDbPy - returning ({}, {})".format(
                    maybe_title, maybe_year))
                return(maybe_title, maybe_year)
            except (AttributeError, IndexError):
                # it can't find a title or year! Oh no! Give up for now.
                return None
        else:
            # guess it's not installed. Crap.
            return None

    return (
        title_data.group("title"),
        title_data.group("year")
        )
Esempio n. 13
0
def process_folders(dirs):
    for directory in dirs:
        settings.debug_message("Switching to directory {}".format(directory))

        if not check_for_skips(directory):

            dir_files = [f for f in os.listdir(os.path.join(settings.
                         incoming_dir, directory)) if
                         os.path.isfile(os.path.join(settings.incoming_dir,
                                        directory, f))]
            try:
                if not in_use(os.path.join(settings.incoming_dir, directory,
                              dir_files[0])):
                    settings.debug_message("Folder is good to go - time to see"
                                           " if it's a video folder!")
                    if is_video_folder(directory, dir_files):
                        process_movie(directory, dir_files, rename_and_move)
                    else:
                        settings.debug_message("Folder does not appear to be a"
                                               "movie. Skipping.")

                    if is_audio_folder(directory, dir_files):
                        #  There will eventually be a process_audio() function
                        #  here, but for now we just need to move stuff.
                        move_folder(directory, 'audio')
                    else:
                        settings.debug_message("Folder does not appear to be "
                                               "an album. Skipping.")

            except IndexError:

                try:

                    dir_folders = [f for f in os.listdir(os.path.join(settings.
                                   incoming_dir, directory)) if os.path.isdir(
                                   os.path.join(settings.incoming_dir,
                                                directory, f))]

                    if dir_folders[0].lower() == "video_ts":
                        rename_and_move(directory)

                except IndexError:
                    settings.debug_message("Folder appears to be empty. Will "
                                           "mark as skip and move on.")
                    rename_skipped(directory)

            else:
                settings.debug_message("Can't use folder!"
                                       " Moving to next folder.")