2 # -*- coding: utf-8 -*-
4 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from __future__ import print_function, unicode_literals
12 from lxml import etree
13 from librarian import html
14 from slughifi import slughifi
17 BOOK_URL = 'http://wolnelektury.pl/katalog/lektura/'
20 if __name__ == '__main__':
21 # Parse commandline arguments
22 usage = """Usage: %prog [options] SOURCE [SOURCE...]
23 Generate slugs for SOURCE."""
25 parser = optparse.OptionParser(usage=usage)
27 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
28 help='print status messages to stdout')
29 parser.add_option('-f', '--force', action='store_true', dest='force', default=False,
30 help='overwrite current identifiers')
32 options, input_filenames = parser.parse_args()
34 if len(input_filenames) < 1:
39 for input_filename in input_filenames:
43 doc = etree.parse(input_filename)
45 title = doc.find('//{http://purl.org/dc/elements/1.1/}title').text
46 except AttributeError:
47 print('%s:error:Book title not found. Skipping.' % input_filename)
52 parent_url = doc.find('//{http://purl.org/dc/elements/1.1/}relation.isPartOf').text
53 parent = parent_url.rsplit('/', 1)[1] + ' '
54 except AttributeError:
57 print('%s:error:Invalid parent URL "%s". Skipping.' % (input_filename, parent_url))
59 book_url = doc.find('//{http://purl.org/dc/elements/1.1/}identifier.url')
61 book_description = doc.find('//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description')
62 book_url = etree.SubElement(book_description, '{http://purl.org/dc/elements/1.1/}identifier.url')
63 if not options.force and book_url.text.startswith('http://'):
64 print('%s:Notice:Book already has identifier URL "%s". Skipping.' % (input_filename, book_url.text))
67 book_url.text = BOOK_URL + slughifi(parent + title)[:60]
69 doc.write(input_filename, xml_declaration=True, pretty_print=True, encoding='utf-8')