# from ebooklib import epub, ITEM_DOCUMENT
from epubfile import Epub

from .utils import BaseParser


class Parser(BaseParser):
    """Extract text from epub using python epub library
    """

    def extract(self, filename, **kwargs):
        book = Epub(filename, read_only=True)
        result = ''
        for text_name in book.get_texts():
            soup = book.read_file(text_name, soup=True)
            # Don't fail with some AttributeError exception when the item is of NoneType
            # (i.e. at the last position).
            if soup is None:
                continue
            html_content_tags = ['title', 'p', 'h1', 'h2', 'h3', 'h4']
            for child in soup.find_all(html_content_tags):
                inner_text = child.text.strip() if child.text else ""
                if inner_text:
                    result += inner_text + '\n'
        return result

@@ -1,72 +1,48 @@
-# from ebooklib import epub, ITEM_DOCUMENT%0Afrom epubfile import Epub
+import zipfile%0Afrom bs4 import BeautifulSoup
 %0A%0Afr
@@ -130,39 +130,8 @@
 epub
- using python epub library%0A    
 %22%22%22%0A
@@ -193,36 +193,31 @@
 k = 
-Epub(filename, read_only=Tru
+zipfile.ZipFile(filenam
 e)%0A 
@@ -264,59 +264,150 @@
  in 
-book.get_texts():%0A            soup = book.read_file
+self.__epub_sections(book):%0A            if not text_name.endswith(%22html%22):%0A                continue%0A            soup = BeautifulSoup(book.open
 (tex
@@ -416,486 +416,1538 @@
 name
+)
 , 
-soup=True)%0A            # Don't fail with some AttributeError exception when the item is of NoneType%0A            # (i.e. at the last position).%0A            if soup is None:%0A                continue%0A            html_content_tags = %5B'title', 'p', 'h1', 'h2', 'h3', 'h4'%5D%0A            for child in soup.find_all(html_content_tags):%0A                inner_text = child.text.strip() if child.text else %22%22%0A                if inner_text:%0A                    result += inner_text + '%5Cn'%0A
+features='lxml')%0A            html_content_tags = %5B'title', 'p', 'h1', 'h2', 'h3', 'h4'%5D%0A            for child in soup.find_all(html_content_tags):%0A                inner_text = child.text.strip() if child.text else %22%22%0A                if inner_text:%0A                    result += inner_text + '%5Cn'%0A        return result%0A%0A    def __epub_sections(self, book):%0A        opf_paths = self.__get_opf_paths(book)%0A        item_paths = self.__get_item_paths(book, opf_paths)%0A        return item_paths%0A%0A    def __get_opf_paths(self, book):%0A        meta_inf = book.open(%22META-INF/container.xml%22)%0A        meta_soup = BeautifulSoup(meta_inf, features='lxml')%0A        return %5Bf%5B%22full-path%22%5D for f in meta_soup.rootfiles.find_all(%22rootfile%22)%5D%0A    %0A    def __get_item_paths(self, book, opf_paths):%0A        item_paths = %5B%5D%0A        for opf_path in opf_paths:%0A            opf_soup = BeautifulSoup(book.open(opf_path), %22lxml%22)%0A            epub_items = opf_soup.spine.find_all(%22itemref%22)%0A            for epub_item in epub_items:%0A                item = self.__get_item(opf_soup, epub_item%5B%22idref%22%5D)%0A                item_paths.append(self.__get_full_item_path(book, item%5B%22href%22%5D))%0A        return item_paths%0A%0A    def __get_item(self, opf_soup, item_id):%0A        for item in opf_soup.manifest.find_all(%22item%22):%0A            if item%5B%22id%22%5D == item_id:%0A                return item%0A        return None%0A    %0A    def __get_full_item_path(self, book, partial_path):%0A        for filename in book.namelist():%0A            if filename.endswith(partial_path):%0A        
     
@@ -1945,27 +1945,29 @@
              return 
-result
+filename
 %0A

Remove epubfile dependency for python2 support