subject.curriculum.new in select list
[redakcja.git] / apps / catalogue / management / commands / import_wl.py
1 # -*- coding: utf-8 -*-
2
3 from collections import defaultdict
4 import json
5 from optparse import make_option
6 import urllib2
7
8 from django.core.management.base import BaseCommand
9 from django.core.management.color import color_style
10 from django.db import transaction
11 from librarian.dcparser import BookInfo
12 from librarian import ParseError, ValidationError
13
14 from catalogue.models import Book
15
16
17 WL_API = 'http://www.wolnelektury.pl/api/books/'
18
19
20 class Command(BaseCommand):
21     option_list = BaseCommand.option_list + (
22         make_option('-q', '--quiet', action='store_false', dest='verbose', default=True, help='Less output'),
23     )
24     help = 'Imports XML files from WL.'
25
26     def handle(self, *args, **options):
27
28         self.style = color_style()
29
30         verbose = options.get('verbose')
31
32         # Start transaction management.
33         transaction.commit_unless_managed()
34         transaction.enter_transaction_management()
35         transaction.managed(True)
36
37         if verbose:
38             print 'Reading currently managed files (skipping hidden ones).'
39         slugs = defaultdict(list)
40         for b in Book.objects.exclude(slug__startswith='.').all():
41             if verbose:
42                 print b.slug
43             text = b.materialize().encode('utf-8')
44             try:
45                 info = BookInfo.from_string(text)
46             except (ParseError, ValidationError):
47                 pass
48             else:
49                 slugs[info.slug].append(b)
50
51         book_count = 0
52         commit_args = {
53             "author_name": 'Platforma',
54             "description": 'Automatycznie zaimportowane z Wolnych Lektur',
55             "publishable": True,
56         }
57
58         if verbose:
59             print 'Opening books list'
60         for book in json.load(urllib2.urlopen(WL_API)):
61             book_detail = json.load(urllib2.urlopen(book['href']))
62             xml_text = urllib2.urlopen(book_detail['xml']).read()
63             info = BookInfo.from_string(xml_text)
64             previous_books = slugs.get(info.slug)
65             if previous_books:
66                 if len(previous_books) > 1:
67                     print self.style.ERROR("There is more than one book with slug %s:"),
68                 previous_book = previous_books[0]
69                 comm = previous_book.slug
70             else:
71                 previous_book = None
72                 comm = '*'
73             print book_count, info.slug, '-->', comm
74             Book.import_xml_text(
75                 xml_text, title=info.title[:255],
76                 slug=info.slug[:128], previous_book=previous_book,
77                 commit_args=commit_args)
78             book_count += 1
79
80         # Print results
81         print
82         print "Results:"
83         print "Imported %d books from WL:" % (
84                 book_count, )
85         print
86
87         transaction.commit()
88         transaction.leave_transaction_management()