def preview_from_xml(request): from hashlib import sha1 from PIL import Image from os import makedirs from lxml import etree from librarian.cover import DefaultEbookCover from librarian.dcparser import BookInfo xml = request.POST['xml'] try: info = BookInfo.from_string(xml.encode('utf-8')) except: return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png")) coverid = sha1(etree.tostring(info.to_etree())).hexdigest() cover = DefaultEbookCover(info) cover_dir = 'cover/preview' try: makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir)) except OSError: pass fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext())) image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS) image.save(os.path.join(settings.MEDIA_ROOT, fname)) return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
def preview(request, book, chunk=None, rev=None): """Creates a cover image. If chunk and rev number are given, use version from given revision. If rev is not given, use publishable version. """ from PIL import Image from librarian.cover import DefaultEbookCover from librarian.dcparser import BookInfo chunk = Chunk.get(book, chunk) if rev is not None: try: revision = chunk.at_revision(rev) except Chunk.change_model.DoesNotExist: raise Http404 else: revision = chunk.publishable() if revision is None: raise Http404 xml = revision.materialize().encode('utf-8') try: info = BookInfo.from_string(xml) except: return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png")) cover = DefaultEbookCover(info) response = HttpResponse(mimetype=cover.mime_type()) image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS) image.save(response, cover.format) return response
def book_info(self, publishable=True): try: book_xml = self.materialize(publishable=publishable) except self.NoTextError: pass else: from librarian.dcparser import BookInfo from librarian import NoDublinCore, ParseError, ValidationError try: return BookInfo.from_string(book_xml.encode('utf-8')) except (self.NoTextError, ParseError, NoDublinCore, ValidationError): return None
def handle(self, *args, **options): self.style = color_style() verbose = options.get('verbose') # Start transaction management. transaction.enter_transaction_management() if verbose: print 'Reading currently managed files (skipping hidden ones).' slugs = defaultdict(list) for b in Book.objects.exclude(slug__startswith='.').all(): if verbose: print b.slug text = b.materialize().encode('utf-8') try: info = BookInfo.from_string(text) except (ParseError, ValidationError): pass else: slugs[info.slug].append(b) book_count = 0 commit_args = { "author_name": 'Platforma', "description": 'Automatycznie zaimportowane z Wolnych Lektur', "publishable": True, } if verbose: print 'Opening books list' for book in json.load(urllib2.urlopen(WL_API)): book_detail = json.load(urllib2.urlopen(book['href'])) xml_text = urllib2.urlopen(book_detail['xml']).read() info = BookInfo.from_string(xml_text) previous_books = slugs.get(info.slug) if previous_books: if len(previous_books) > 1: print self.style.ERROR("There is more than one book " "with slug %s:"), previous_book = previous_books[0] comm = previous_book.slug else: previous_book = None comm = '*' print book_count, info.slug , '-->', comm Book.import_xml_text(xml_text, title=info.title[:255], slug=info.slug[:128], previous_book=previous_book, commit_args=commit_args) book_count += 1 # Print results print print "Results:" print "Imported %d books from WL:" % ( book_count, ) print transaction.commit() transaction.leave_transaction_management()
""" from StringIO import StringIO from urllib2 import urlopen from zipfile import ZipFile from librarian.dcparser import BookInfo from librarian import text from lesmianator import Lesmianator XML_FILES = "http://www.wolnelektury.pl/media/packs/xml-all.zip" if __name__ == '__main__': poet = Lesmianator() xml_zip = ZipFile(StringIO(urlopen(XML_FILES).read())) for filename in xml_zip.namelist(): print filename info = BookInfo.from_file(xml_zip.open(filename)) if u'Wiersz' in info.genres: output = StringIO() text.transform(xml_zip.open(filename), output, False, ('raw-text',)) poet.add_text(output.getvalue()) poet.save()