1 # -*- coding: utf-8 -*-
4 from optparse import make_option
7 from django.core.management.base import BaseCommand
8 from django.core.management.color import color_style
9 from django.db import transaction
10 from librarian.dcparser import BookInfo
11 from librarian import ParseError, ValidationError
13 from catalogue.models import Book
16 WL_API = 'http://www.wolnelektury.pl/api/books/'
19 class Command(BaseCommand):
20 option_list = BaseCommand.option_list + (
21 make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
24 help = 'Imports XML files from WL.'
26 def handle(self, *args, **options):
28 self.style = color_style()
30 verbose = options.get('verbose')
32 # Start transaction management.
33 transaction.commit_unless_managed()
34 transaction.enter_transaction_management()
35 transaction.managed(True)
38 print 'Reading currently managed files.'
40 for b in Book.objects.all():
43 text = b.materialize().encode('utf-8')
45 info = BookInfo.from_string(text)
46 except (ParseError, ValidationError):
53 "author_name": 'Platforma',
54 "description": 'Import from WL',
58 print 'Opening books list'
59 for book in json.load(urllib2.urlopen(WL_API)):
60 book_detail = json.load(urllib2.urlopen(book['href']))
61 xml_text = urllib2.urlopen(book_detail['xml']).read()
62 info = BookInfo.from_string(xml_text)
63 previous_book = slugs.get(info.slug, None)
65 comm = previous_book.slug
68 print book_count, info.slug , '-->', comm
69 Book.import_xml_text(xml_text, title=info.title,
70 slug=info.slug, previous_book=slugs.get(info.slug, None))
76 print "Imported %d books from WL:" % (
82 transaction.leave_transaction_management()