| 1 |
|
| 2 |
|
| 3 |
import argparse |
| 4 |
import logging |
| 5 |
import os |
| 6 |
import sys |
| 7 |
|
| 8 |
import dateutil.parser |
| 9 |
import requests |
| 10 |
|
| 11 |
import lesana |
| 12 |
import lesana.command |
| 13 |
|
| 14 |
OPENAPI_BASE = "https://openlibrary.org" |
| 15 |
OPENAPI_ISBN_URL = OPENAPI_BASE + '/isbn/{isbn}.json' |
| 16 |
|
| 17 |
|
| 18 |
logger = logging.getLogger(__name__) |
| 19 |
|
| 20 |
|
| 21 |
class OL2L(lesana.command.Command): |
| 22 |
""" |
| 23 |
Manage collections |
| 24 |
""" |
| 25 |
|
| 26 |
arguments = [ |
| 27 |
( |
| 28 |
['--collection', '-c'], |
| 29 |
dict( |
| 30 |
help='Name of the lesana collection, default is .', |
| 31 |
default='.', |
| 32 |
), |
| 33 |
), |
| 34 |
( |
| 35 |
['--template', '-t'], |
| 36 |
{ |
| 37 |
"help": "Template, " |
| 38 |
+ "default is templates/from_openlibrary.yaml", |
| 39 |
"default": 'templates/from_openlibrary.yaml', |
| 40 |
}, |
| 41 |
), |
| 42 |
( |
| 43 |
['isbn'], |
| 44 |
{ |
| 45 |
'help': "ISBN of the book we want to search" |
| 46 |
}, |
| 47 |
), |
| 48 |
] |
| 49 |
|
| 50 |
def _load_args(self): |
| 51 |
self.parser = argparse.ArgumentParser() |
| 52 |
for arg in self.arguments: |
| 53 |
self.parser.add_argument(*arg[0], **arg[1]) |
| 54 |
self.args = self.parser.parse_args() |
| 55 |
|
| 56 |
def main(self): |
| 57 |
self._load_args() |
| 58 |
self.collection = self.collection_class( |
| 59 |
directory=self.args.collection |
| 60 |
) |
| 61 |
|
| 62 |
logging.info("Looking for %s on OpenLibrary", self.args.isbn) |
| 63 |
res = requests.get(OPENAPI_ISBN_URL.format(isbn=self.args.isbn)) |
| 64 |
|
| 65 |
if res.status_code != 200: |
| 66 |
logger.error("No such isbn found on openlibrary") |
| 67 |
sys.exit(1) |
| 68 |
|
| 69 |
edition = res.json() |
| 70 |
|
| 71 |
langs = [] |
| 72 |
for l in edition.get("languages", []): |
| 73 |
langs.append(l["key"].split("/")[-1]) |
| 74 |
|
| 75 |
pub_date = dateutil.parser.parse(edition.get("publish_date")) |
| 76 |
|
| 77 |
authors = [] |
| 78 |
for aid in edition.get("authors", []): |
| 79 |
logging.info("Retrieving %s from OpenLibrary", aid) |
| 80 |
res = requests.get(OPENAPI_BASE + aid["key"] + ".json") |
| 81 |
authors.append(res.json()) |
| 82 |
|
| 83 |
data = { |
| 84 |
"edition": edition, |
| 85 |
"langs": langs, |
| 86 |
"pub_date": pub_date, |
| 87 |
"authors": authors, |
| 88 |
} |
| 89 |
|
| 90 |
entry = self.collection.entry_from_rendered_template( |
| 91 |
self.args.template, |
| 92 |
data |
| 93 |
) |
| 94 |
filepath = os.path.join(self.collection.itemdir, entry.fname) |
| 95 |
filepath = os.path.normpath(filepath) |
| 96 |
if self.edit_file_in_external_editor(filepath): |
| 97 |
self.collection.update_cache([entry.fname]) |
| 98 |
if self.collection.settings["git"]: |
| 99 |
self.collection.git_add_files([filepath]) |
| 100 |
saved_entry = self.collection.entry_from_eid(entry.eid) |
| 101 |
print(saved_entry) |
| 102 |
|
| 103 |
|
| 104 |
def main(): |
| 105 |
OL2L().main() |
| 106 |
|
| 107 |
|
| 108 |
if __name__ == '__main__': |
| 109 |
main() |