def generate_epub(request, id): try: issue = Issue.objects.get(id=id) except Issue.DoesNotExist: raise Http404 name = u"Журнал %s №%d за %d год" % (issue.magazine.name, issue.number, issue.year) folder = 'magazines' link_name = settings.MEDIA_URL + "/%s/%s/" % (settings.MEDIA_DOWNLOAD_FOLDER, folder) + quote(name.encode('utf-8')) + ".epub" book_folder = os.path.join(settings.MEDIA_ROOT,settings.MEDIA_DOWNLOAD_FOLDER, folder) if not os.path.exists(book_folder): os.makedirs(book_folder) book_name = os.path.join(book_folder, name.encode("utf-8") + ".epub") if not os.path.exists(book_name): temp_name = tempfile.mkstemp() properties = BookProperties(temp_name[1], name) properties.language = "ru" properties.genre = "magazine" magazine_parser = MagazineParser(issue, properties) magazine_parser.parse() magazine_parser.create_book() shutil.copy(temp_name[1], book_name) os.chmod(book_name, 0666) return HttpResponseRedirect("/" + link_name)
def add_doc_form(request): """ Displays form for converting and adding book in doc """ if request.method == "POST": form = DocCreateForm(request.POST, request.FILES) if form.is_valid(): temp_name = tempfile.mkstemp()[1] cd = form.cleaned_data properties = BookProperties(temp_name, cd['title'].encode("utf-8")) properties.language = cd['language'] properties.author = request.user.first_name + ' ' + request.user.last_name properties.subject = cd['subject'].encode("utf-8") properties.description = cd['description'].encode("utf-8") properties.genre = cd['type'].encode("utf-8") properties.date = cd['date'].encode("utf-8") properties.rights = cd['rights'].encode("utf-8") properties.author = request.user.first_name + " " + request.user.last_name file = request.FILES['file'] doc_file_name = tempfile.mkstemp()[1] doc_file = open(doc_file_name, 'wb') doc_file.write(file.read()) doc_file.close() doc_parser = DocParser(doc_file_name, properties) out_file_name = tempfile.mkdtemp() + "tmp.html" doc_parser.parse() try: language = Language.objects.get(short=cd['language']) except: return render_response(request, "epub/add_doc_form.html", {'errors': [_('No such language.')], 'user': request.user, 'menu_on': True, 'form': form}) lang_code = LANG_CODE[0] for lang_code in LANG_CODE: if lang_code[0] == cd['language']: break book_model = Book(language=language, title = cd['title'].encode("utf-8"), pagelink="") try: author = Author.objects.get(name=request.user.first_name + ' ' + request.user.last_name) except: author = Author.objects.create(name=request.user.first_name + ' ' + request.user.last_name) book_model.save() book_model.author = [author] ebook = EpubBook.objects.create(book=book_model) print "creating" ebook.name.save(cd['title'].encode("utf-8") + ".epub", _ExistingFile(temp_name)) try: book_file = BookFile(link="/" + MEDIA_URL + "/uploads/" + quote(cd['title'].encode("utf-8")) + '.epub') book_file.type = 'epub' book_file.save() except: return render_response(request, "epub/add_doc_form.html", {'errors': [_("Book name should be unique")], 'menu_on': True, 'form': form}) book_model.book_file = [book_file] book_model.save() ebook.save() hrr = HttpResponseRedirect("/book/id%d" % book_model.id) return hrr else: form = DocCreateForm() return render_response(request, "epub/add_doc_form.html", {'user': request.user, 'menu_on': True, 'form': form})
def get_book_from_libru(request): """ Gets book from lib.ru in epub """ if request.method == "GET" and 'link' in request.GET: errors = [] if not request.GET['link']: errors.append("Empty link") if errors: return render_response(request, "epub/get_book_from_libru.html", {'user': request.user, 'errors': errors, 'menu_on': True}) else: link = request.GET['link'] regex = r"^http://lib\.ru/(koi/|win/|lat/)?(.+\.txt)(_.*$)?" if not re.match(regex, link, flags=re.IGNORECASE): return render_response(request, "epub/get_book_from_libru.html", {'errors': ['Not valid lib.ru path'], 'menu_on': True}) match = re.search(regex, link, flags=re.IGNORECASE) search_link = match.group(2) books = Book.objects.filter(book_file__link__contains=search_link) if books: http = "http://lib.ru/" + search_link + "_with-big-pictures.html" book = books[0] name = book.title language = book.language authors = {} author_name = "" genres = [] for i, author in enumerate(book.author.get_query_set()): if i == 0: author_name = author.name else: authors.append(author.name) for tag in book.tag.get_query_set(): genres.append(tag.name) temp_name = tempfile.mkstemp() properties = BookProperties(temp_name[1], name) print temp_name[1] + " creation" properties.language = language properties.author = author_name properties.authors = authors properties.genre = ", ".join(genres) lrp = LibRuParser(http, properties) lrp.parse() if not lrp.create_book(): print "Error in creating book." folder = 'libru' link_name = settings.EPUB_URL + "/%s/%s/" % (settings.MEDIA_DOWNLOAD_FOLDER, folder) + quote(name.encode('utf-8')) + ".epub" book_folder = os.path.join(settings.MEDIA_ROOT,settings.MEDIA_DOWNLOAD_FOLDER, folder) if not os.path.exists(book_folder): os.makedirs(book_folder) book_name = os.path.join(book_folder, name.encode("utf-8") + ".epub") shutil.copy(temp_name[1], book_name) os.chmod(book_name, 0666) return HttpResponseRedirect("/" + link_name) else: return render_response(request, "epub/get_book_from_libru.html", {'messages': ["Sorry, but this book cannot be imported"], 'menu_on': True}) else: return render_response(request, "epub/get_book_from_libru.html", {'user': request.user, 'menu_on': True})
def epub_form(request): """ Displays form for converting from txt files and adding into db """ messages = [] if request.method == "POST": form = EpubAddForm(request.POST, request.FILES) if form.is_valid(): cd = form.cleaned_data book_name = cd['caption'] lang = cd['language'] tags = cd['tags'] subject = cd['subject'] description = cd['description'] type = cd['type'] date = cd['date'] rights = cd['rights'] temp_name = tempfile.mkstemp()[1] properties = BookProperties(temp_name, book_name) properties.language = lang properties.subject = subject properties.description = description properties.genre = type properties.date = date properties.rights = rights properties.author = request.user.first_name + " " + request.user.last_name book = BookCreator(properties) file = request.FILES['file'] toc = cd['toc'] if toc: book.split_by_toc(toc, file.read()) else: book.txt_to_html(file.read(), temp_name + "text.html") book.add_file(temp_name + "text.html", "c1", "") book.pack() try: language = Language.objects.get(short=lang) except: return render_response(request, "epub/epub_form.html", {'errors': [_('No such language.')], 'user': request.user, 'menu_on': True, 'form': form}) lang_code = LANG_CODE[0] for lang_code in LANG_CODE: if lang_code[0] == lang: break book_model = Book.objects.create(language=language, pagelink="") book_model.title = book_name try: author = Author.objects.get(name=request.user.first_name + ' ' + request.user.last_name) except: author = Author.objects.create(name=request.user.first_name + ' ' + request.user.last_name) book_model.author = [author] book_model.save() ebook = EpubBook.objects.create(book = book_model) exfile = _ExistingFile(temp_name) ebook.name.save(book_name + ".epub", exfile) print ebook.name try: book_file = BookFile(link="/" + MEDIA_URL + "/" + quote(str(ebook.name))) book_file.type = 'epub' book_file.save() except Exception, e: print e return render_response(request, "epub/epub_form.html", {'errors': [_("Book name should be unique")], 'menu_on': True, 'form': form}) book_model.book_file = [book_file] book_model.save() ebook.save() messages += [_("Book successfully created.")] hrr = HttpResponseRedirect("/book/id%d" % book_model.id) return hrr