-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessCharpter.py
executable file
·356 lines (327 loc) · 13.8 KB
/
processCharpter.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
#!/usr/bin/python2.7
# coding: utf-8
import urllib2,re,smtplib,sys,datetime,pypinyin
from pypinyin import pinyin, lazy_pinyin
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
from lxml import etree,html
from lxml.html.clean import clean_html,Cleaner
from charpters import charpters
from book import book
from websiteProfile import websiteProfile
from mails import mail
from multiprocessing.dummy import Pool as ThreadPool
import sched,time
#TODO:
# Settings can't updated
# Update Charpters - after each read
# MultiThreads for eachbook or multi Charpters
# Update Load WebsiteProfile - add format check in loadReqs & len(bk)
# Update plain txt secrets with netrc text file
# Use DB instead of setting file
# Update Reading Format
#DONE
# Finish load Reqs - could read requests form file
# Sending Email - so that we could send Emails to Kindle
# Figure out the getNextChar by page link garbash words
class Ser(object):
def main(self):
print("Server Started")
print(datetime.datetime.now())
# self.loadReqs('aName')
books = self.loadReqs('settings','book')
#PROCESS BOOK
for abook in books:
print(abook.bkname)
self.processBook(abook)
self.processResult(books)
#Load Setting files then call processBook to get Book Infos - then processResult
def loadReqs(self,fileName,settingType):
#Load Requests from a file.
#Set requests to Book object
reqs = []
try:
f = open(fileName,"r+")
#Readable
for i in re.findall(r'\$.+\n!.+\n!.+\n!.+\n\$.+',f.read(),re.M|re.I):
bk = []
i = ''.join([i,'\n'])
ops = re.findall(r'.*\n',i,re.M|re.I)
for r in ops:
ress = re.search(r'\[.*\]',r)
# bk = [res.group().replace("[","").replace("]","") if res.group()!= '[]' else '' for res in [re.search(r'\[.*\]',r) for r in ops]]
if(ress.group() != '[]'):
bk += [ress.group().replace("[","").replace("]","")]
if len(bk)>0:
if settingType is 'book':
reqs += [book(bk[0],bk[1],bk[2],int(bk[3]),int(bk[4]))]
elif settingType is 'email':
reqs += [mail(bk[0],bk[1],bk[2],bk[3],bk[4])]
else:
print("")
f.close()
except:
print("ERROR: Setting file can not open")
return reqs
def searchDB(self,bkName,charNumStart,charNumEnd):
#use multi thread to search book for multiple website - choose one of them
#return book obj - pass it to process Index that contains website name,index Link, book name
# if charNumEnd is '' - set the last char as End
pass
def loadWebsiteProfile(self,websitename):
# first = websiteProfile('http:www.2shu.cc',"//div[@id='content']",0,"/html/body//a[@href]",13,'','','ISO-8859-1','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',['br','table','tr','td'])
website = 'http://www.uukanshu.com'
textxPathKeyW = "/html/body//div[@id='contentbox']/child::text()"
nextCharxPathKeyW = '/html/body//div[@class="fanye_cen"]/child::a/attribute::href'
indexxPathKeyW = "/html/body//ul[@id='chapterList']/child::li/a/attribute::href"
titlexPathKeyW = "/html/body//h1[@id='timu']/child::text()"
IndexGBK = 1
second = websiteProfile(website,textxPathKeyW,1,nextCharxPathKeyW,titlexPathKeyW,IndexGBK,indexxPathKeyW,'','','ISO-8859-1','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',['br','table','tr','td','p'])
return second
def processBook(self,abook):
print(" START: Process BOOK")
#get a book named abook
#return no thing
#Key part for this program
#searchDB
DBres = self.searchDB('name',1,2)
#crate Charpters
profile = self.loadWebsiteProfile(abook.website)
chars = charpters(profile,abook.website)
#update abook
abook.chars += [chars]
#processIndex
if(abook.charNumEnd == ''):
print("Starting process book by Set starting charpter <==> %d"%abook.charNumStart)
#means to the end of whole file
#Recurrisive go over all pages
index = 0
startCharLink = self.processIndex(abook,0)
abook.chars[0].htmlContent = self.processPage(startCharLink,profile)
#Process Content
abook.chars[0].text = self.processContent(abook.chars[0])
print('<<<<<<<<<<<<<<<')
abook.chars[0].nextCharlink = self.processnextCharLink(abook.chars[0])
while(abook.chars[index].nextCharlink != None):
#Generate a base charpters
cha = charpters(profile,abook.website)
#Process html
nextLink = ''.join([abook.website,abook.chars[index].nextCharlink])
cha.htmlContent = self.processPage(nextLink,abook.chars[index].websiteProfile)
# print(abook.chars[0].htmlContent)
# print(cha.htmlContent)
#Process html to get text
cha.text = self.processContent(cha)
#Process html to get nextCharlink
cha.nextCharlink = self.processnextCharLink(cha)
#char # - based on index
cha.charNum = index
abook.chars += [cha]
index+=1
print("Book process by start char DONE")
else:
print("Starting Process book by Indexes")
#to target chars
indexArr = self.processIndex(abook,1)
profile.IndexGBK = 0
#Process Page Range
if(abook.charNumStart ==-1):
#start Char # == -1 ---> from beginning
#All to the end
start = 0
else:
# to a specific posn
start = abook.charNumStart
if(abook.charNumEnd == -1):
# end = -1 ----> to the end
end = len(indexArr)-1
else:
end = abook.charNumEnd
print("Start %d -> setting %d ,end %d -> setting %d"%(start,abook.charNumStart,end,abook.charNumEnd))
for x in range(start,end):
print("Char INDEX : %d"%x)
if(profile.charOrder == 0):
#inorder
link = ''.join([profile.website,indexArr[x]])
else:
link = ''.join([profile.website,indexArr[len(indexArr)-1-x]])
cha = charpters(profile,abook.website)
cha.CurrentCharLink = link
cha.htmlContent = self.processPage(link,cha.websiteProfile)
cha.charName = self.processCharTitle(cha)
cha.charNum = str(x)
cha.text = self.processContent(cha)
abook.chars += [cha]
print("Book Process by Index DONE")
print(" FINISH: Process BOOK")
return abook
def processIndex(self,abook,model):
print(" START: Process Index")
#process Indexs
index = abook.indexlink
profile = abook.chars[0].websiteProfile
#process index page
html = self.processPage(index,profile)
#process&pick links from html
element = etree.HTML(html)
s = element.xpath(profile.indexxPathKeyW,smart_strings=True)
if(model == 0):
#all to the end
#only need to know the target index of the first char
# if website is reverse ordered - send back the last page
if(abook.chars[0].websiteProfile.charOrder == 0):
res = ''.join([profile.website,s[abook.charNumStart]])
else:
res = ''.join([profile.website,s[-1]])
print(" FINISH: Process INDEX")
return res
else:
#from charNumStart to charNumEnd
print("Index arr:%d"%len(s))
abook.indexNum = len(s)
print(" FINISH: Process INDEX")
return s
def processPage(self,url,profile):
page = None
res = None
print(" START: Process Page")
#grep webpage and process to text
#Can not be charpter as input - used for both index process and concent grep
req = urllib2.Request(url=url,headers={'User-Agent':profile})
page = urllib2.urlopen(req)
info = page.info()
charset = info.getparam('charset')
response = page.read()
response = response.decode(charset,'ignore')
# if(profile.IndexGBK == 1):
# response = unicode(response,'GBK').encode('UTF-8')
cleaner = Cleaner(page_structure=False,links=False,remove_tags=profile.remove_tags)
page = cleaner.clean_html(response)
print(" FINISH: Process Page")
# print(page)
res = page
del page
return res
def processContent(self,charpter):
#get website content - text and process out the text
print(" START: Process CONTENT")
element = etree.HTML(charpter.htmlContent)
# print(charpter.htmlContent)
s = element.xpath(charpter.websiteProfile.textxPathKeyW)
text =''
for t in s:
# print(t)
try:
# text = ''.join([text,t.encode('ISO-8859-1')])
text = ''.join([text,t,'\n'])
except:
pass
print(" FINISH: Process CONTENT")
# print("Test is %s\n"%text)
return text
def processnextCharLink(self,charpter):
print(" START: Process NextCharLINK")
#get html content of a char page, process it and figure out the next char link
element = etree.HTML(charpter.htmlContent)
x = element.xpath(charpter.websiteProfile.nextCharxPathKeyW)
print(" FINISH: Process NextCharLINK")
return x[0]
def processCharTitle(self,charpter):
print(" START: Process CharTitle")
#get html content of a char page, process it and figure out the next char link
element = etree.HTML(charpter.htmlContent)
x = element.xpath(charpter.websiteProfile.titlexPathKeyW)
# print(x[0].encode('ISO-8859-1'))
print(" FINISH: Process CharTitle")
return x[0]
def processResult(self,books):
print(" START: Process Result")
i = 2;
for abook in books:
content = ''
if((abook.charNumEnd == -1 and abook.charNumStart < abook.indexNum) or abook.charNumStart < abook.charNumEnd ):
for x in abook.chars:
content = ''.join([content,x.toString()])
bkname = (abook.bkname).decode("utf-8")#.encode('gbk')
py = lazy_pinyin(bkname)
name = reduce((lambda x,y : '-'.join([x,y])),py)
fileName = ''.join([name,str(abook.charNumStart),'-',str((abook.charNumEnd,abook.indexNum)[abook.charNumEnd == -1]),'.txt'])
print(fileName)
#Save To File
self.saveToFile(fileName,content)
#Send TO Mail
self.sendByMail(fileName)
#Update DB/File
self.updateSettings('settings',abook,i)
i += 1
else:
print("ERROR:Starting Char Num equals to Ending Char Num")
print(" FINISH: Process CharTitle")
def sendByMail(self,fileName):
print(" START: Sending File By EMAIL")
amail = self.loadReqs('mail','email')[0]
sender = amail.sender
receiver = amail.receiver
msg = MIMEMultipart()
msg['From']=sender
msg['To']=receiver
msg['Subject']= amail.subject
body = amail.body
msg.attach(MIMEText(body,'plain'))
attachment = open(fileName,"rb")
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % fileName)
msg.attach(part)
try:
smtpObj = smtplib.SMTP('smtp-mail.outlook.com',587)
print("1 - Pass : open SMTP Connection")
smtpObj.starttls()
print("2 - Pass : Open SSL Connection")
smtpObj.login(sender,amail.passWD.decode('base64'))
print("3 - Pass : Login")
smtpObj.sendmail(sender,receiver,msg.as_string())
print("4 - Pass : Sent")
smtpObj.quit()
print("5 - Pass : Quit Email sending Program ")
except:
print(sys.exc_info()[0])
print("EMAIL SENDING ERROR")
pass
def saveToFile(self,fileName,content):
print(" START: Save File")
fw = open(fileName,"w")
#print(content)
fw.write(content)
fw.close()
print(" FINISH: Save File")
#Update settings file - such as update char #
def updateSettings(self,fileName,reqs,lineNum):
print(" START: UPDATE SETTING")
time.sleep(1)
f = open(fileName,'r')
#with open(fileName,'r') as file:
# data = file.readlines()
data = f.readlines()
f.close()
if(reqs.charNumEnd == -1):
endNum = reqs.indexNum
else:
endNum = reqs.charNumEnd
data[lineNum*6-3] = data[lineNum*6-3].replace(str(reqs.charNumStart),str(endNum))
print(data[lineNum*6-3])
f = open(fileName,'w')
f.writelines(data)
f.close()
#with open(fileName,'w') as file:
# file.writelines(data)
time.sleep(1)
print(" FINISH: UPDATE SETTING ")
def main():
ser = Ser()
ser.main()
if (__name__ == '__main__'):
main()