Esempio n. 1
0
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

__author__ = 'Dan Tracy'
__email__ = 'djt5019 at gmail dot com'
__version__ = '0.1.7'

import atexit
import os

from eplist import utils
from eplist import logger
from eplist import constants

if not os.path.exists(constants.resource_path):
    utils.init_resource_folder()

logger.init_logging()

atexit.register(utils.save_last_access_times)
atexit.register(logger.shutdown_logging)

utils.load_renamed_file()
Esempio n. 2
0
def main():
    cmd = argparse.ArgumentParser(description="Renames your TV shows",
                        prog='eplist', usage='%(prog)s --help [options] title')

    cmd.add_argument('title', default="", nargs='?',
        help="The title of the show")

    cmd.add_argument('-d', '--display-header', action="store_true",
        help="Display the header at the top of the output")

    cmd.add_argument('-v', '--verbose', action="store_true",
        help="Be verbose, enable additional output")

    cmd.add_argument('-s', '--season', default="", type=str, metavar='N',
        help="The specific season range to search for. Ex: 1-3")

    cmd.add_argument('-e', '--episode', default="", type=str, metavar='N',
        help="The specific episode range to search for Ex: 15-30")

    cmd.add_argument('-f', '--format', dest="format", metavar='F',
        help="Rename the files in a directory with a custom format")

    cmd.add_argument('-g', '--gui-enabled', action="store_true",
        help="Use the gui rather than the command line")

    group = cmd.add_mutually_exclusive_group()

    group.add_argument('-r', '--rename', dest='pathname', metavar="PATH",
        help="Rename the files in the path provided")

    group.add_argument('-u', '--undo-rename', action='store_true',
        help="Undo the last rename operation")

    cmd.add_argument('--delete-cache', action="store_true",
        help="Delete the cache file and create a new one")

    cmd.add_argument('--update-db', action="store_true",
        help="Update the AniDB titles file, limit to once a day due to size")

    cmd.add_argument('--verify', action="store_true",
        help="Verify the checksums in the filename if they are present")

    cmd.add_argument('--filter', choices=['episodes', 'specials', 'both'],
        help="Filters episodes based on type (default=both)")

    args = cmd.parse_args()

    if not os.path.exists(constants.resource_path):
        utils.init_resource_folder()

    logger.init_logging()

    atexit.register(utils.save_last_access_times)
    atexit.register(logger.shutdown_logging)

    if 'title' in args:
        Settings.title = args.title
    else:
        Settings.title = ""

    # Set the correct working path
    if args.pathname:
        Settings.path = os.path.realpath(args.pathname)
    else:
        Settings.path = os.path.realpath(os.getcwd())

    # If we are acting on files then load the old names into memory
    if args.pathname or args.undo_rename:
        utils.load_renamed_file()

    if args.filter:
        Settings.filter = args.filter

    cache = Cache(Settings.db_name)
    atexit.register(cache.close)

    if args.delete_cache:
        cache.recreate_cache()

    if Settings.title in ('-', '.', 'pwd'):
        # If a dash is entered use the current basename of the path
        Settings.title = os.path.split(os.getcwd())[1]
        print("Searching for {}".format(Settings.title))

    if args.verbose:
        Settings.verbose = True
        l = logging.getLogger()
        for handle in l.handlers:
            handle.setLevel(logging.NOTSET)
        l.setLevel(logging.NOTSET)

    if args.gui_enabled:
        from .gui.gui import main
        exit(main())

    if args.update_db:
        utils.update_db()

    if args.undo_rename:
        do_rename(utils.find_old_filenames(Settings.path, Settings.title))
        sys.exit(0)

    rename = args.pathname is not None

    if rename and not os.path.exists(args.pathname):
        sys.exit("ERROR - Path provided does not exist")

    if not Settings.title:
        cmd.print_usage()
        sys.exit(1)

    episodeParser = ShowFinder(Settings.title, cache)

    show = episodeParser.getShow()
    formatter = episode.EpisodeFormatter(show, args.format)
    show.formatter = formatter

    if not show.episodes:
        sys.exit(1)

    # If the user specified a specific season we will filter our results
    # this also checks to make sure its a reasonable season number
    eps = filter_episodes(show, args.season, args.episode)

    show.add_episodes(eps)

    ## Renaming functionality
    if rename:
        do_rename(utils.prepare_filenames(Settings.path, show))
        sys.exit(0)

    if args.verify:
        files = utils.clean_filenames(Settings.path)

        verify_files(files)
        sys.exit(1)

    if Settings.filter in ('both', 'episodes'):
        display_episodes(show, show.episodes, args.display_header)

    if Settings.filter in ('specials', 'both'):
        display_specials(show, args.display_header)