mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add export command to calibredb
This commit is contained in:
parent
aab8744748
commit
233662ebd7
@ -363,9 +363,41 @@ show_metadata command.
|
|||||||
do_set_metadata(get_db(dbpath, opts), id, opf)
|
do_set_metadata(get_db(dbpath, opts), id, opf)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def do_export(db, ids, dir, single_dir, by_author):
|
||||||
|
if ids is None:
|
||||||
|
ids = db.all_ids()
|
||||||
|
db.export_to_dir(dir, ids, byauthor=by_author, single_dir=single_dir, index_is_id=True)
|
||||||
|
|
||||||
|
def command_export(args, dbpath):
|
||||||
|
parser = get_parser(_('''\
|
||||||
|
%prog export [options] ids
|
||||||
|
|
||||||
|
Export the books specified by ids (a comma separated list) to the filesystem.
|
||||||
|
The export operation saves all formats of the book, its cover and metadata (in
|
||||||
|
an opf file). You can get id numbers from the list command.
|
||||||
|
'''))
|
||||||
|
parser.add_option('--all', default=False, action='store_true',
|
||||||
|
help=_('Export all books in database, ignoring the list of ids.'))
|
||||||
|
parser.add_option('--to-dir', default='.',
|
||||||
|
help=(_('Export books to the specified directory. Default is')+' %default'))
|
||||||
|
parser.add_option('--single-dir', default=False, action='store_true',
|
||||||
|
help=_('Export all books into a single directory'))
|
||||||
|
parser.add_option('--by-author', default=False, action='store_true',
|
||||||
|
help=_('Create file names as author - title instead of title - author'))
|
||||||
|
opts, args = parser.parse_args(sys.argv[1:]+args)
|
||||||
|
if (len(args) < 2 and not opts.all):
|
||||||
|
parser.print_help()
|
||||||
|
print
|
||||||
|
print >>sys.stderr, _('You must specify some ids or the %s option')%'--all'
|
||||||
|
return 1
|
||||||
|
ids = None if opts.all else map(int, args[1].split(','))
|
||||||
|
dir = os.path.abspath(os.path.expanduser(opts.to_dir))
|
||||||
|
do_export(get_db(dbpath, opts), ids, dir, opts.single_dir, opts.by_author)
|
||||||
|
return 0
|
||||||
|
|
||||||
def main(args=sys.argv):
|
def main(args=sys.argv):
|
||||||
commands = ('list', 'add', 'remove', 'add_format', 'remove_format',
|
commands = ('list', 'add', 'remove', 'add_format', 'remove_format',
|
||||||
'show_metadata', 'set_metadata')
|
'show_metadata', 'set_metadata', 'export')
|
||||||
parser = OptionParser(_(
|
parser = OptionParser(_(
|
||||||
'''\
|
'''\
|
||||||
%%prog command [options] [arguments]
|
%%prog command [options] [arguments]
|
||||||
|
@ -1384,6 +1384,9 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
self.conn.execute('VACUUM;')
|
self.conn.execute('VACUUM;')
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
|
def all_ids(self):
|
||||||
|
return [i[0] for i in self.conn.execute('SELECT id FROM books').fetchall()]
|
||||||
|
|
||||||
def export_to_dir(self, dir, indices, byauthor=False, single_dir=False,
|
def export_to_dir(self, dir, indices, byauthor=False, single_dir=False,
|
||||||
index_is_id=False):
|
index_is_id=False):
|
||||||
if not os.path.exists(dir):
|
if not os.path.exists(dir):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user