Added copyright notice about AGPL. Removed setuputils in favour of plain distutils.
[librarian.git] / scripts / genslugs
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 #    This file is part of Librarian.
5 #
6 #    Copyright © 2008,2009,2010 Fundacja Nowoczesna Polska <fundacja@nowoczesnapolska.org.pl>
7 #    
8 #    For full list of contributors see AUTHORS file. 
9 #
10 #    This program is free software: you can redistribute it and/or modify
11 #    it under the terms of the GNU Affero General Public License as published by
12 #    the Free Software Foundation, either version 3 of the License, or
13 #    (at your option) any later version.
14 #
15 #    This program is distributed in the hope that it will be useful,
16 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #    GNU Affero General Public License for more details.
19 #
20 #    You should have received a copy of the GNU Affero General Public License
21 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23 import os
24 import optparse
25
26 from lxml import etree
27 from librarian import html
28 from slughifi import slughifi
29
30
31 BOOK_URL = 'http://wolnelektury.pl/katalog/lektura/'
32
33
34 if __name__ == '__main__':
35     # Parse commandline arguments
36     usage = """Usage: %prog [options] SOURCE [SOURCE...]
37     Generate slugs for SOURCE."""
38
39     parser = optparse.OptionParser(usage=usage)
40
41     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
42         help='print status messages to stdout')
43     parser.add_option('-f', '--force', action='store_true', dest='force', default=False,
44         help='overwrite current identifiers')
45     
46     options, input_filenames = parser.parse_args()
47
48     if len(input_filenames) < 1:
49         parser.print_help()
50         exit(1)
51
52     # Do some real work
53     for input_filename in input_filenames:
54         if options.verbose:
55             print input_filename
56         
57         doc = etree.parse(input_filename)
58         try:
59             title = doc.find('//{http://purl.org/dc/elements/1.1/}title').text
60         except AttributeError:
61             print '%s:error:Book title not found. Skipping.' % input_filename
62             continue
63         
64         parent = ''
65         try:
66             parent_url = doc.find('//{http://purl.org/dc/elements/1.1/}relation.isPartOf').text
67             parent = parent_url.rsplit('/', 1)[1] + ' '
68         except AttributeError:
69             pass
70         except IndexError:
71             print '%s:error:Invalid parent URL "%s". Skipping.' % (input_filename, parent_url)
72             
73         book_url = doc.find('//{http://purl.org/dc/elements/1.1/}identifier.url')
74         if book_url is None:
75             book_description = doc.find('//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description')
76             book_url = etree.SubElement(book_description, '{http://purl.org/dc/elements/1.1/}identifier.url')
77         if not options.force and book_url.text.startswith('http://'):
78             print '%s:Notice:Book already has identifier URL "%s". Skipping.' % (input_filename, book_url.text)
79             continue
80         
81         book_url.text = BOOK_URL + slughifi(parent + title)[:60]
82
83         doc.write(input_filename, xml_declaration=True, pretty_print=True, encoding='utf-8')
84