mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 18:54:09 -04:00
Switch fetch-ebook-metadata to use the new metadata download framework
This commit is contained in:
parent
261eaad8d2
commit
cc0f8f4323
79
src/calibre/ebooks/metadata/sources/cli.py
Normal file
79
src/calibre/ebooks/metadata/sources/cli.py
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
|
from __future__ import (unicode_literals, division, absolute_import,
|
||||||
|
print_function)
|
||||||
|
|
||||||
|
__license__ = 'GPL v3'
|
||||||
|
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
|
import sys, textwrap
|
||||||
|
from io import BytesIO
|
||||||
|
from threading import Event
|
||||||
|
|
||||||
|
from calibre import prints
|
||||||
|
from calibre.utils.config import OptionParser
|
||||||
|
from calibre.ebooks.metadata import string_to_authors
|
||||||
|
from calibre.ebooks.metadata.opf2 import metadata_to_opf
|
||||||
|
from calibre.ebooks.metadata.sources.base import create_log
|
||||||
|
from calibre.ebooks.metadata.sources.identify import identify
|
||||||
|
|
||||||
|
def option_parser():
|
||||||
|
parser = OptionParser(textwrap.dedent(
|
||||||
|
'''\
|
||||||
|
%prog [options]
|
||||||
|
|
||||||
|
Fetch book metadata from online sources. You must specify at least one
|
||||||
|
of title, authors or ISBN.
|
||||||
|
'''
|
||||||
|
))
|
||||||
|
parser.add_option('-t', '--title', help='Book title')
|
||||||
|
parser.add_option('-a', '--authors', help='Book author(s)')
|
||||||
|
parser.add_option('-i', '--isbn', help='Book ISBN')
|
||||||
|
parser.add_option('-v', '--verbose', default=False, action='store_true',
|
||||||
|
help='Print the log to the console (stderr)')
|
||||||
|
parser.add_option('-o', '--opf', help='Output the metadata in OPF format')
|
||||||
|
parser.add_option('-d', '--timeout', default='30',
|
||||||
|
help='Timeout in seconds. Default is 30')
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
|
def main(args=sys.argv):
|
||||||
|
parser = option_parser()
|
||||||
|
opts, args = parser.parse_args(args)
|
||||||
|
|
||||||
|
buf = BytesIO()
|
||||||
|
log = create_log(buf)
|
||||||
|
abort = Event()
|
||||||
|
|
||||||
|
authors = []
|
||||||
|
if opts.authors:
|
||||||
|
authors = string_to_authors(opts.authors)
|
||||||
|
|
||||||
|
identifiers = {}
|
||||||
|
if opts.isbn:
|
||||||
|
identifiers['isbn'] = opts.isbn
|
||||||
|
|
||||||
|
results = identify(log, abort, title=opts.title, authors=authors,
|
||||||
|
identifiers=identifiers, timeout=int(opts.timeout))
|
||||||
|
|
||||||
|
log = buf.getvalue()
|
||||||
|
|
||||||
|
if not results:
|
||||||
|
print (log, file=sys.stderr)
|
||||||
|
prints('No results found', file=sys.stderr)
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
result = results[0]
|
||||||
|
result = (metadata_to_opf(result) if opts.opf else
|
||||||
|
unicode(result).encode('utf-8'))
|
||||||
|
|
||||||
|
if opts.verbose:
|
||||||
|
print (log, file=sys.stderr)
|
||||||
|
|
||||||
|
print (result)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
@ -271,6 +271,9 @@ class GoogleBooks(Source):
|
|||||||
identifiers={}, timeout=30):
|
identifiers={}, timeout=30):
|
||||||
query = self.create_query(log, title=title, authors=authors,
|
query = self.create_query(log, title=title, authors=authors,
|
||||||
identifiers=identifiers)
|
identifiers=identifiers)
|
||||||
|
if not query:
|
||||||
|
log.error('Insufficient metadata to construct query')
|
||||||
|
return
|
||||||
br = self.browser
|
br = self.browser
|
||||||
try:
|
try:
|
||||||
raw = br.open_novisit(query, timeout=timeout).read()
|
raw = br.open_novisit(query, timeout=timeout).read()
|
||||||
|
@ -30,7 +30,7 @@ entry_points = {
|
|||||||
'calibre-customize = calibre.customize.ui:main',
|
'calibre-customize = calibre.customize.ui:main',
|
||||||
'calibre-complete = calibre.utils.complete:main',
|
'calibre-complete = calibre.utils.complete:main',
|
||||||
'pdfmanipulate = calibre.ebooks.pdf.manipulate.cli:main',
|
'pdfmanipulate = calibre.ebooks.pdf.manipulate.cli:main',
|
||||||
'fetch-ebook-metadata = calibre.ebooks.metadata.fetch:main',
|
'fetch-ebook-metadata = calibre.ebooks.metadata.sources.cli:main',
|
||||||
'epub-fix = calibre.ebooks.epub.fix.main:main',
|
'epub-fix = calibre.ebooks.epub.fix.main:main',
|
||||||
'calibre-smtp = calibre.utils.smtp:main',
|
'calibre-smtp = calibre.utils.smtp:main',
|
||||||
],
|
],
|
||||||
@ -183,7 +183,7 @@ class PostInstall:
|
|||||||
from calibre.ebooks.lrf.lrfparser import option_parser as lrf2lrsop
|
from calibre.ebooks.lrf.lrfparser import option_parser as lrf2lrsop
|
||||||
from calibre.gui2.lrf_renderer.main import option_parser as lrfviewerop
|
from calibre.gui2.lrf_renderer.main import option_parser as lrfviewerop
|
||||||
from calibre.gui2.viewer.main import option_parser as viewer_op
|
from calibre.gui2.viewer.main import option_parser as viewer_op
|
||||||
from calibre.ebooks.metadata.fetch import option_parser as fem_op
|
from calibre.ebooks.metadata.sources.cli import option_parser as fem_op
|
||||||
from calibre.gui2.main import option_parser as guiop
|
from calibre.gui2.main import option_parser as guiop
|
||||||
from calibre.utils.smtp import option_parser as smtp_op
|
from calibre.utils.smtp import option_parser as smtp_op
|
||||||
from calibre.library.server.main import option_parser as serv_op
|
from calibre.library.server.main import option_parser as serv_op
|
||||||
|
Loading…
x
Reference in New Issue
Block a user