#-*- coding=utf-8 -*-
import os,sys,time
import urllib2
import re
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from novelconfig import *
#kindle上对应的帐号,用于推送到kindle
mailto_list = "@kindle.cn"
#用来推送的邮箱地址,这里使用的是163网易邮箱
mail_host = "smtp.126.com"
mail_user = ""
mail_pass = ""
mail_postfix = "126.com"
class novelParser():
def __init__(self,titleIn,urlIn,idIn):
self.booktitle = titleIn
self.cpurl = urlIn
self.id =idIn
self.urls = []
self.text = []
def getnovel(self):
self.geturls()
self.getcontents()
issend = True
if len(self.text)==0:
issend = False
return self.savedata(),self.id,issend
def geturls(self):
#更改目录页编码gb2312
cp= urllib2.urlopen(self.cpurl).read().decode("gb2312")
print cp
#更改目录页正则表达式
cpMatch = re.search(r'
', cp, re.S)
if cpMatch:
#更改目录列表正则表达式
bpMatch = re.findall('( self.id:
self.id = int(bp[1])
self.urls.append(self.cpurl+bp[1]+'.html')
else:
print( '无法获得内容页链接!')
else:
print( '无法获得总目录页!')
def getcontents(self):
#更改标题正则表达式
tpMatch = re.compile(r'(.*?)
')
#更改内容正则表达式
dpMatch = re.compile(r'.*?
')
EndCharToNoneRex = re.compile("<.*?>")
for url in self.urls:
#更改内容页编码gbk
dp= urllib2.urlopen(url).read().decode("gbk")
title = tpMatch.search(dp,re.S)
content = dpMatch.search(dp,re.S)
if content:
self.text.append(title.group(1))
self.text.append(EndCharToNoneRex.sub("",content.group(0).replace("
","\r\n").replace(" "," ")))
self.text.append("--------------------")
else:
print( '当前页无法获取内容')
def savedata(self):
# 打开本地文件
filename = self.booktitle+'最新章节'+time.strftime("%Y%m%d",time.localtime())+'.txt'
f = open(filename,'w+',encoding='utf-8')
for text in self.text:
f.writelines(text+'\r\n')
f.close()
return filename
def sendmail(to, From, Filename):
'''
sendmail函数,用来发送附件
'''
msg = MIMEMultipart()
att = MIMEText(open(Filename, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment',filename=Filename)
encoders.encode_base64(part)
msg.attach(att)
msg['to'] = to
msg['from'] = From
msg['Subject'] = Filename
server = smtplib.SMTP(mail_host)
server.login(mail_user, mail_pass)
server.sendmail(msg['from'], msg['to'], msg.as_string())
server.quit()
def main():
for novel_key in NOVEL_SETTING:
print(novel_key,NOVEL_SETTING[novel_key],NOVEL_NEW[novel_key])
FileName,new_id,issend = novelParser(novel_key,NOVEL_SETTING[novel_key],NOVEL_NEW[novel_key]).getnovel()
if issend:
sendmail(mailto_list, mail_user + "@" + mail_postfix, FileName)
os.remove(FileName)
NOVEL_NEW[novel_key] = new_id
refresh_new(NOVEL_NEW)
input()
def refresh_new(new_id):
a = ""
f = open("novelconfig.py", 'r',encoding="utf-8")
b_new = False
b_need = True
for line in f:
if line.split(' ')[0] == "NOVEL_NEW":
b_new = True
if b_new:
for keys in new_id:
if line.split(":")[0] == " \"" + keys + "\"":
a += " \"" + keys + "\":" + str(new_id[keys]) + ",\n"
b_need = False
break
if b_need:
a += line
else:
b_need = True
f.close()
f = open("novelconfig.py", 'w',encoding="utf-8")
f.write(a)
f.close()
main()