-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.py
More file actions
45 lines (31 loc) · 1.23 KB
/
reader.py
File metadata and controls
45 lines (31 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import logging as log
import ebooklib
from ebooklib import epub
class Reader:
def __init__(self, bpath):
self.book = epub.read_epub(bpath)
self.items = [self.book.get_item_with_id(item[0]) for item in self.book.spine]
log.debug("Total items: %s", len(self.items))
self.currentChapIndex = -1
def __getText(self, chap):
return chap.get_content().decode()
def nextChapter(self):
'''
Returns the next chapter of the book as string.
'''
self.currentChapIndex += 1
if self.currentChapIndex >= len(self.items):
self.currentChapIndex = len(self.items) - 1
return (False, None,)
log.debug("nextChapter - Going to chapter: %s", self.currentChapIndex)
return (True, self.__getText(self.items[self.currentChapIndex]),)
def prevChapter(self):
'''
Returns the previous chapter of the book as string.
'''
self.currentChapIndex -= 1
if self.currentChapIndex < 0:
self.currentChapIndex = -1
return (False, None)
log.debug("prevChapter - Going to chapter: %s", self.currentChapIndex)
return (True, self.__getText(self.items[self.currentChapIndex]))