-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.py
356 lines (304 loc) · 13.9 KB
/
library.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint
import os
import logging
import zipfile
import io
import pickle
logger = logging.getLogger(__name__)
class AuthorsParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.bookListParsing=False
self.bookLine=False
self.currentAuthor=None
self.currentTitle=None
self.currentPath=None
self.currentLanguage=None
self.bookList = []
def handle_starttag(self, tag, attrs):
#print "Start tag:", tag
if tag=="h3" and self.bookListParsing == True:
self.currentAuthor = True
self.currentTitle = None
self.currentPath = None
self.currentLanguage = None
if tag=="li" and self.bookListParsing ==True:
self.bookLine=True
if tag == "a" and self.bookLine:
self.currentTitle = True
for attr in attrs:
#print " attr:", attr
if tag == "div":
if attr[0] == 'class' and attr[1] == 'list':
self.bookListParsing = True
logger.debug("Start authors parsing")
if tag == "a" and self.bookLine:
if attr[0]=='href':
self.currentPath=attr[1]
def handle_endtag(self, tag):
# print "End tag :", tag
if tag == "div" and self.bookListParsing == True:
self.bookListParsing = False
logger.debug("End authors parsing")
if tag=="li" and self.bookLine == True:
self.bookLine=False
book_tmp = Book(self.currentTitle, self.currentAuthor, self.currentLanguage, self.currentPath)
if book_tmp.language==True:
book_tmp.language="None"
self.bookList.append(book_tmp)
logger.debug("Book info \n%s",book_tmp)
self.currentTitle = None
self.currentPath = None
self.currentLanguage = None
if tag=="a" and self.currentTitle is not None and self.currentLanguage is None:
self.currentLanguage=True
def handle_data(self, data):
# print "Data :", data
if self.currentAuthor == True:
self.currentAuthor = data
if self.currentTitle == True:
self.currentTitle = data
if self.currentLanguage == True:
self.currentLanguage = data.strip('()')
class BookParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.bookListParsing=False
self.currentType=None
self.currentCharset=None
self.currentPath=None
self.subjectList=[]
self.subjectParsing=0
self.fileList=[]
def handle_starttag(self, tag, attrs):
if tag == 'td' and self.subjectParsing == 1:
self.subjectParsing = 2
if self.bookListParsing:
#print "Start tag:", tag
if tag=='td' and self.currentCharset is None:
#print "Charset tag detected"
self.currentCharset=True
if tag=='a':
# reset if this is a <td> for file path
#print "RESET charset"
if self.currentCharset == True:
self.currentCharset = None
for attr in attrs:
if tag=='a' and attr[0]=='href':
self.currentPath=attr[1]
self.fileList.append(BookFile(self.currentType, self.currentCharset, self.currentPath))
self.currentType=None
self.currentCharset=None
self.currentPath=None
#print " attr:", attr
def handle_endtag(self, tag):
if tag=='table' and self.bookListParsing ==True:
self.bookListParsing=False
if self.bookListParsing:
pass
#print "End tag :", tag
def handle_data(self, data):
if self.subjectParsing == 2:
self.subjectList.append(data)
self.subjectParsing = 0
if data == "Subject":
self.subjectParsing = 1
if data == 'EBook Files':
self.bookListParsing = True
if self.bookListParsing:
#print "Data :", data
if self.currentCharset == True:
#print "CHARSET Parsing"
tmp = data.split(';')
#print tmp
self.currentType = tmp[0]
if len(tmp) == 2:
self.currentCharset = tmp[1].split('=')[1].strip("\"")
else:
self.currentCharset = "None"
bannedEncodings=['MS Lit for PocketPC (lit)','Finale (mus)', 'macintosh', 'LilyPond (ly)', 'TeX (tex)', 'x-other']
class BookFile:
def __init__(self, type, charset,path):
self.type=type
self.charset=charset
self.path=path
def __str__(self):
return "Type: %s\nCharset: %s\nPath: %s" % (self.type, self.charset, self.path)
class Book:
def __init__(self, title=None, author=None, language=None, path=None, subject=[]):
self.author=author
self.title=title
self.language=language
self.path=path
self.subject=subject
self.zipList=None
self.contentValid=None
def description(self):
return "Title: %s\nAuthor: %s\nLanguage: %s\n Subject: %s\nPath: %s" % ( self.author, self.title, self.language, self.subject, self.path)
def __str__(self):
return self.getText()
def lower(self):
return self.getText().lower()
def getText(self):
fileToOpen=None
for file in self.zipList:
if file.type=='text/plain' and file.charset not in bannedEncodings:
fileToOpen=file
break
if fileToOpen is None:
logger.warn("Returning empty string on book %s - no suitable file format", self.path)
self.contentValid=False
return ""
try:
with zipfile.ZipFile(fileToOpen.path) as zfile:
if len(zfile.namelist()) != 1:
txtCnt=0
for name in zfile.namelist():
filename, file_extension = os.path.splitext(name)
if file_extension.lower()=='.txt':
txtCnt+=1
if txtCnt != 1:
logger.warn("Unexpected zip contents %s", fileToOpen.path)
logger.warn("Contents %s", str(zfile.namelist()))
raise zipfile.BadZipfile("Invalid file contents")
for name in zfile.namelist():
filename, file_extension = os.path.splitext(name)
if file_extension.lower()=='.txt':
with zfile.open(name) as readfile:
if fileToOpen.charset=='None':
logger.debug("Opening file %s with None encoding", fileToOpen.path)
self.contentValid = True
strTmp = readfile.read()
#go for unicode format
if isinstance(strTmp, str):
strTmp = unicode(strTmp, errors='ignore')
return strTmp
else:
logger.debug("Using encoding %s", fileToOpen.charset)
logger.debug("Opening file %s with %s encoding", fileToOpen.path, fileToOpen.charset)
try:
self.contentValid = True
return io.TextIOWrapper(readfile, encoding=fileToOpen.charset, errors='replace').read()
except UnicodeDecodeError as e:
logger.debug("Could not decode file %s in %s encoding", fileToOpen.path, fileToOpen.charset)
logger.debug("Exception: %s", str(e))
if fileToOpen.charset=='us-ascii':
logger.debug("Retrying with no encoding for us-ascii")
self.contentValid=True
return readfile.read()
else:
logger.warn("Giving up on file, cannot decode %s", fileToOpen.path)
except NotImplementedError as e:
logger.warn("Cannot open %s", fileToOpen.path)
logger.warn("%s", str(e))
logger.warn("%s", file_extension)
except zipfile.BadZipfile as e:
filename, file_extension = os.path.splitext(fileToOpen.path)
if file_extension.lower() != '.txt':
logger.warn("Cannot open %s", fileToOpen.path)
logger.warn("%s", str(e))
else:
logger.debug("Recovering - found text file %s instead of zip", fileToOpen.path)
if fileToOpen.charset == 'None':
with io.open(fileToOpen.path) as readfile:
logger.debug("Opening file %s with None encoding", fileToOpen.path)
self.contentValid = True
strTmp = readfile.read()
if isinstance(strTmp, str):
strTmp = unicode(strTmp, errors='ignore')
return strTmp
else:
logger.debug("Using encoding %s", fileToOpen.charset)
logger.debug("Opening file %s with %s encoding", fileToOpen.path, fileToOpen.charset)
with io.open(fileToOpen.path, "r",encoding=fileToOpen.charset, errors='replace') as readfile:
self.contentValid=True
return readfile.read()
self.contentValid=False
return ""
class Library:
def __init__(self, pathToBooks, forceReload=False):
self.libPath=pathToBooks
self.bookList=None
self.tmpListFile="libListing.pickle"
if forceReload==True or not os.path.exists(self.tmpListFile):
self._loadBooks()
self._validateLibrary()
with open(self.tmpListFile, 'wb') as f:
logger.info("Pickling library listing into %s", self.tmpListFile)
pickle.dump(self.bookList, f, pickle.HIGHEST_PROTOCOL)
else:
logger.info("Loading existing file %s", self.tmpListFile)
with open(self.tmpListFile, 'rb') as f:
self.bookList = pickle.load(f)
logger.info("Loaded %d books", len(self.bookList))
def _loadBooks(self):
indices = os.listdir(self.libPath)
indices_author = [a for a in indices if "AUTHORS" in a.upper()]
indices_lang = [a for a in indices if "LANGUAGE" in a.upper()]
indices_subj = [a for a in indices if "SUBJECTS" in a.upper()]
parser = AuthorsParser()
# Parse individual leter indices
for ind in indices_author:
logger.info("Parsing %s", ind)
path = os.path.join(self.libPath, ind)
with open(path, "r") as fp:
parser.feed(fp.read())
logger.info("Found %d books, preparing to parse contents...", len(parser.bookList))
tmpBookList = parser.bookList
self.bookList = []
bookCnt = 0
for b in tmpBookList:
# resolve book html path
b.path = os.path.abspath(os.path.join(self.libPath, b.path))
# resolve book references
parser = BookParser()
# Parse the html file
with open(b.path, "r") as fp:
parser.feed(fp.read())
b.zipList = parser.fileList
b.subject = parser.subjectList
for f in b.zipList:
f.path = os.path.abspath(os.path.join(self.libPath, f.path))
# filter out music files and files not containing text data
# filter takes text/plain files
hasTextFile=False
hasInvalidFile=False
for f in b.zipList:
if f.type=='text/plain' and f.charset not in bannedEncodings:
hasTextFile=True
elif 'audio' in f.type or 'application' in f.type:
hasInvalidFile=True
logger.debug("Incompatible type %s, path %s", f.type, b.path)
else:
logger.debug("Other type %s, path %s", f.type, b.path)
if hasTextFile and not hasInvalidFile:
bookCnt+=1
self.bookList.append(b)
logger.info("Found %d book files", bookCnt)
def _validateLibrary(self):
tmpBooklist=[]
for b in self.bookList:
b.getText()
if b.contentValid:
tmpBooklist.append(b)
logger.info("Dropped %d books due to invalid file content", len(self.bookList) - len(tmpBooklist))
self.bookList=tmpBooklist
@staticmethod
def checkBookFolder(path):
return os.path.isdir(path)
#Can accept a list of languages or asingle language
def filterBooks(self, language=None, onlyWithSubject=False):
filteredList=[]
if language is None:
return filteredList
elif isinstance(language, basestring):
language=[language]
for b in self.bookList:
for lang in language:
if lang in b.language:
if onlyWithSubject and len(b.subject) == 0:
continue
else:
filteredList.append(b)
return filteredList