encoding fix
[lesmianator.git] / init-xml.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Copyright © 2011 Fundacja Nowoczesna Polska
4 #
5 # This file is part of Leśmianator.
6 #
7 # Leśmianator is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Leśmianator is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16 #
17 # You should have received a copy of the GNU Affero General Public License
18 # along with Leśmianator.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 """
22 Inicjalizuje bazę danych Leśmianatora z pobranych plików XML.
23
24 Skrypt pobiera paczkę plików XML, na podstawie metadanych wybiera te,
25 które należą do rodzaju "Liryka", za pomocą librariana przeprowadza
26 konwersję do postaci tekstowej, i tak uzyskany tekst przekazuje
27 Leśmianatorowi do analizy.
28
29 """
30
31 from StringIO import StringIO
32 from urllib2 import urlopen
33 from zipfile import ZipFile
34
35 from librarian.dcparser import BookInfo
36 from librarian import text
37
38 from lesmianator import Lesmianator
39
40
41 XML_FILES = "http://www.wolnelektury.pl/media/packs/xml-all.zip"
42
43
44 if __name__ == '__main__':
45     poet = Lesmianator()
46
47     xml_zip = ZipFile(StringIO(urlopen(XML_FILES).read()))
48     for filename in xml_zip.namelist():
49         print filename
50         info = BookInfo.from_file(xml_zip.open(filename))
51
52         if u'Wiersz' in info.genres:
53             output = StringIO()
54             text.transform(xml_zip.open(filename), output, False, ('raw-text',))
55             poet.add_text(output.getvalue())
56
57     poet.save()
58