1 # -*- coding: utf-8 -*-
3 from collections import defaultdict
5 from optparse import make_option
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
14 from catalogue.models import Book
17 WL_API = 'http://www.wolnelektury.pl/api/books/'
20 class Command(BaseCommand):
21 option_list = BaseCommand.option_list + (
22 make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
25 help = 'Imports XML files from WL.'
27 def handle(self, *args, **options):
29 self.style = color_style()
31 verbose = options.get('verbose')
33 # Start transaction management.
34 transaction.enter_transaction_management()
37 print 'Reading currently managed files (skipping hidden ones).'
38 slugs = defaultdict(list)
39 for b in Book.objects.exclude(slug__startswith='.').all():
42 text = b.materialize().encode('utf-8')
44 info = BookInfo.from_bytes(text)
45 except (ParseError, ValidationError):
48 slugs[info.slug].append(b)
52 "author_name": 'Platforma',
53 "description": 'Automatycznie zaimportowane z Wolnych Lektur',
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_bytes(xml_text)
63 previous_books = slugs.get(info.slug)
65 if len(previous_books) > 1:
66 print self.style.ERROR("There is more than one book "
68 previous_book = previous_books[0]
69 comm = previous_book.slug
73 print book_count, info.slug , '-->', comm
74 Book.import_xml_text(xml_text, title=info.title[:255],
75 slug=info.slug[:128], previous_book=previous_book,
76 commit_args=commit_args)
82 print "Imported %d books from WL:" % (
88 transaction.leave_transaction_management()