2 # -*- coding: utf-8 -*-
7 from librarian import html
8 from slughifi import slughifi
11 BOOK_URL = 'http://wolnelektury.pl/katalog/lektura/'
14 if __name__ == '__main__':
15 # Parse commandline arguments
16 usage = """Usage: %prog [options] SOURCE [SOURCE...]
17 Generate slugs for SOURCE."""
19 parser = optparse.OptionParser(usage=usage)
21 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
22 help='print status messages to stdout')
23 parser.add_option('-f', '--force', action='store_true', dest='force', default=False,
24 help='overwrite current identifiers')
26 options, input_filenames = parser.parse_args()
28 if len(input_filenames) < 1:
33 for input_filename in input_filenames:
37 doc = etree.parse(input_filename)
39 title = doc.find('//{http://purl.org/dc/elements/1.1/}title').text
40 except AttributeError:
41 print '%s:error:Book title not found. Skipping.' % input_filename
46 parent_url = doc.find('//{http://purl.org/dc/elements/1.1/}relation.isPartOf').text
47 parent = parent_url.rsplit('/', 1)[1] + ' '
48 except AttributeError:
51 print '%s:error:Invalid parent URL "%s". Skipping.' % (input_filename, parent_url)
53 book_url = doc.find('//{http://purl.org/dc/elements/1.1/}identifier.url')
55 book_description = doc.find('//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description')
56 book_url = etree.SubElement(book_description, '{http://purl.org/dc/elements/1.1/}identifier.url')
57 if not options.force and book_url.text.startswith('http://'):
58 print '%s:Notice:Book already has identifier URL "%s". Skipping.' % (input_filename, book_url.text)
61 book_url.text = BOOK_URL + slughifi(parent + title)[:60]
63 doc.write(input_filename, xml_declaration=True, pretty_print=True, encoding='utf-8')