summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/examples/books/templates/from_openlibrary.yaml32
-rwxr-xr-xscripts/openlibrary2lesana105
2 files changed, 137 insertions, 0 deletions
diff --git a/docs/examples/books/templates/from_openlibrary.yaml b/docs/examples/books/templates/from_openlibrary.yaml
new file mode 100644
index 0000000..2fab669
--- /dev/null
+++ b/docs/examples/books/templates/from_openlibrary.yaml
@@ -0,0 +1,32 @@
+title: '{{ edition.title }}'
+subtitle: ''
+authors: {% if authors %}{% for a in authors %}
+- {{ a.name }}{% endfor %}{% else %}[]{% endif %}
+editors: []
+binding: ''
+purchase_date: ''
+purchase_price: ''
+publisher: '{{ edition.publishers | join(", ") }}'
+edition: ''
+copyright_year: []
+publication_year: {{ pub_date.year }}
+isbn: '{{ edition.isbn_13.0 }}'
+lccn: ''
+openlibrary: 'https://openlibrary.org{{ edition.key }}'
+pages: {{ edition.number_of_pages }}
+translators: []
+languages: {% if langs %}{% for l in langs %}
+- {{ l }}{% endfor %}{% else %}[]{% endif %}
+genres: []
+keywords: []
+series: ''
+series_number: 0
+condition: ''
+signed:
+read:
+gift:
+loaned:
+rating: 0
+cover: ''
+plot: ''
+comments: ''
diff --git a/scripts/openlibrary2lesana b/scripts/openlibrary2lesana
new file mode 100755
index 0000000..4ea94ea
--- /dev/null
+++ b/scripts/openlibrary2lesana
@@ -0,0 +1,105 @@
+#!/usr/bin/env python3
+
+import argparse
+import logging
+import os
+import sys
+
+import requests
+import dateutil.parser
+
+import lesana
+import lesana.command
+
+
+OPENAPI_BASE = "https://openlibrary.org"
+OPENAPI_ISBN_URL = OPENAPI_BASE + '/isbn/{isbn}.json'
+
+
+logger = logging.getLogger(__name__)
+
+
+class OL2L(lesana.command.Command):
+ """
+ Manage collections
+ """
+
+ arguments = [
+ (
+ ['--collection', '-c'],
+ dict(
+ help='Name of the lesana collection, default is .',
+ default='.',
+ ),
+ ),
+ (
+ ['--template', '-t'],
+ {
+ "help": "Template, "
+ + "default is templates/from_openlibrary.yaml",
+ "default": 'templates/from_openlibrary.yaml',
+ },
+ ),
+ (
+ ['isbn'],
+ {
+ 'help': "ISBN of the book we want to search"
+ },
+ ),
+ ]
+
+ def _load_args(self):
+ self.parser = argparse.ArgumentParser()
+ for arg in self.arguments:
+ self.parser.add_argument(*arg[0], **arg[1])
+ self.args = self.parser.parse_args()
+
+ def main(self):
+ self._load_args()
+ self.collection = self.collection_class(
+ directory=self.args.collection
+ )
+
+ logging.info("Looking for %s on OpenLibrary", self.args.isbn)
+ res = requests.get(OPENAPI_ISBN_URL.format(isbn=self.args.isbn))
+
+ if res.status_code != 200:
+ logger.error("No such isbn found on openlibrary")
+ sys.exit(1)
+
+ edition = res.json()
+
+ langs = []
+ for l in edition["languages"]:
+ langs.append(l["key"].split("/")[-1])
+
+ pub_date = dateutil.parser.parse(edition["publish_date"])
+
+ authors = []
+ for aid in edition["authors"]:
+ logging.info("Retrieving %s from OpenLibrary", aid)
+ res = requests.get(OPENAPI_BASE + aid["key"] + ".json")
+ authors.append(res.json())
+
+ data = {
+ "edition": edition,
+ "langs": langs,
+ "pub_date": pub_date,
+ "authors": authors,
+ }
+
+ entry = self.collection.entry_from_rendered_template(
+ self.args.template,
+ data
+ )
+ filepath = os.path.join(self.collection.itemdir, entry.fname)
+ if self.edit_file_in_external_editor(filepath):
+ self.collection.update_cache([entry.fname])
+ if self.collection.settings["git"]:
+ self.collection.git_add_files([filepath])
+ saved_entry = self.collection.entry_from_eid(entry.eid)
+ print(saved_entry)
+
+
+if __name__ == '__main__':
+ OL2L().main()