Example #1
0
    def run(self, args, config):
        mdb = self.get_metadata_db(args.tree)
        mds = MovieDatasource(config.subsections('datasource'), args.tree, self.profile.object_class)

        if args.input is None: # Refresh all movies
            if printer.ask('Would you like to refresh all movies?', default=True):
                with printer.progress(mdb.count(), task=True) as update:
                    for movie_hash, movie in list(mdb.itermovies()):
                        movie = mds.refresh(movie)
                        mdb.save(movie_hash, movie)
                        printer.verbose('Saved {hash}', hash=movie_hash)
                        update(1)
        else:
            movie_hash = get_hash(args.input)

            try:
                movie = mdb.get(movie_hash)
            except KeyError:
                printer.p('Unknown movie hash.')
                return
            else:
                movie = mds.refresh(movie)
                show(movie)
                if printer.ask('Would you like to save the movie?', default=True):
                    mdb.save(movie_hash, movie)
                    printer.p('Saved.')
Example #2
0
    def run(self, args, config):
        # Check the args:
        if args.symlink and args.delete:
            raise KolektoRuntimeError('--delete can\'t be used with --symlink')
        elif args.symlink and args.hardlink:
            raise KolektoRuntimeError('--symlink and --hardlink are mutually exclusive')

        # Load the metadata database:
        mdb = self.get_metadata_db(args.tree)

        # Load informations from db:
        mds = MovieDatasource(config.subsections('datasource'), args.tree, self.profile.object_class)

        attachment_store = AttachmentStore(os.path.join(args.tree, '.kolekto', 'attachments'))

        for filename in args.file:
            filename = filename.decode('utf8')
            movie = self._import(mdb, mds, args, config, filename)

            # Refresh the full data for the choosen movie:
            movie = mds.refresh(movie)

            # Append the import date
            movie['import_date'] = datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')

            if args.show:
                show(movie)
                printer.p('')

            # Edit available data:
            if not args.auto and printer.ask('Do you want to edit the movie metadata', default=False):
                movie = self.profile.object_class(json.loads(printer.edit(json.dumps(movie, indent=True))))

            # Hardlink or copy the movie in the tree
            if args.hardlink or args.symlink:
                printer.p('\nComputing movie sha1sum...')
                movie_hash = link(args.tree, filename, args.symlink)
            else:
                printer.p('\nCopying movie in kolekto tree...')
                movie_hash = copy(args.tree, filename)
            printer.p('')

            mdb.save(movie_hash, movie)
            printer.debug('Movie {hash} saved to the database', hash=movie_hash)

            if args.delete:
                os.unlink(filename)
                printer.debug('Deleted original file {filename}', filename=filename)

            # Import the attachments
            if movie_hash is not None and args.import_attachments:
                attachments = list_attachments(filename)
                if attachments:
                    printer.p('Found {nb} attachment(s) for this movie:', nb=len(attachments))
                    for attach in attachments:
                        printer.p(' - {filename}', filename=attach)
                    if not args.auto and printer.ask('Import them?', default=True):
                        for attach in attachments:
                            _, ext = os.path.splitext(attach)
                            attachment_store.store(movie_hash, ext.lstrip('.'), open(attach))
Example #3
0
    def _import(self, mdb, mds, args, config, filename):
        printer.debug('Importing file {filename}', filename=filename)
        short_filename = os.path.basename(filename)
        title, ext = os.path.splitext(short_filename)

        year, title = clean_title(title)

        # Disable the year filter if auto mode is disabled:
        if not args.auto:
            year = None

        while True:
            # Disable the title input if auto mode is enabled:
            if not args.auto:
                title = printer.input(u'Title to search', default=title)
            datasource, movie = self._search(mds, title, short_filename, year, auto=args.auto)
            if datasource == 'manual':
                movie = self.profile.object_class
            elif datasource == 'abort':
                printer.p('Aborted import of {filename}', filename=filename)
                return
            break

        if datasource is None:
            return

        # Refresh the full data for the choosen movie:
        movie = mds.refresh(movie)

        if args.show:
            show(movie)
            printer.p('')

        # Edit available data:
        if not args.auto and printer.ask('Do you want to edit the movie metadata', default=False):
            movie = self.profile.object_class(json.loads(printer.edit(json.dumps(movie, indent=True))))

        # Hardlink or copy the movie in the tree
        if args.hardlink or args.symlink:
            printer.p('\nComputing movie sha1sum...')
            movie_hash = link(args.tree, filename, args.symlink)
        else:
            printer.p('\nCopying movie in kolekto tree...')
            movie_hash = copy(args.tree, filename)
        printer.p('')

        mdb.save(movie_hash, movie)
        printer.debug('Movie {hash} saved to the database', hash=movie_hash)

        if args.delete:
            os.unlink(filename)
            printer.debug('Deleted original file {filename}', filename=filename)