# -*- coding: UTF-8 -*-
import requests
from bs4 import BeautifulSoup
import csv
from tkinter import * 
import time
import random
import codecs
import deliver_email
import sys
import threading

def exit():
	sys.exit(0)

def messagebox():
	top=Toplevel()
	top.title('完成')
	top.geometry()
	lab=Label(top,text='爬取完成')
	lab.grid(row=0,column=0)
	button=Button(top,text='确定',command=exit)
	button.grid(row=1,column=0)

def flag1():
	global flag
	flag=0
	
def flag2():
	global flag
	flag=1
	
def flag3():
	global flag
	flag=2
	
def flag4():
	global flag_num
	flag_num=1
	
def email():
	global entry
	global entry1
	global entry2
	start_page=entry1.get()
	end_page=entry2.get()
	taker=entry.get()
	filename='{}-{}.csv'.format(start_page,end_page)
	send=deliver_email.Deliver(taker,filename)
	send.deliver()
	
def gethtml(page):
	global flag
	list=[]
	start_url_1='https://www.qidian.com/finish?action=hidden&orderId=&style=1&pageSize=20&siteid=1&pubflag=0&hiddenField=2&page='
	start_url_2='https://www.qidian.com/finish?size=4&action=hidden&orderId=&page=1&style=1&pageSize=20&siteid=1&pubflag=0&hiddenField=2&page='#100万-200万
	start_url_3='https://www.qidian.com/finish?size=5&action=hidden&orderId=&page=1&style=1&pageSize=20&siteid=1&pubflag=0&hiddenField=2&page='#200万以上
	list.append(start_url_1)
	list.append(start_url_2)
	list.append(start_url_3)
	url=list[flag]+str(page)
	headers = {'User-Agent': 'Mozilla/5.0'}
	html=requests.get(url,headers=headers)
	return html
	
def parser_url(html):
	list=[]
	soup=BeautifulSoup(html.content,'html.parser')
	links=soup.find_all('h4')
	for link_url in links:
		link=link_url.find('a')
		if link==None:
			pass
		else:
			link=link.get('href')
			link='https:'+link
			list.append(link)
	return list
	
def gettext(list):
	lenth=len(list)
	headers = {'User-Agent': 'Mozilla/5.0'}
	for width in range(lenth):
		try:
			response=requests.get(list[width],headers=headers)
			soup=BeautifulSoup(response.content,'html.parser')
			link_text=soup.find('div',class_='book-intro').find('p')
			text=link_text.get_text()
			text=text.strip()
			list_text=text.split()
			lenth=len(list_text)
			text=''
			for i in range(lenth):
				text+=list_text[i]           #text:简介
			num=soup.find('ul',class_='work-state cf')
			num=num.find_all('em')      #num[1].get_text()累计字数
			sum=str(num[1].get_text())
			link_text=soup.find_all('h1')
			text_=link_text[0].get_text()
			list1=text_.split()
			writecsv(list1[1],list1[0],sum,text)    #list[1]:作者 list[0]:书名
#			t=random.random()
#			time.sleep(t)
		except:
			pass
		
def writecsv(author,name,intro,text):
	global entry1
	global entry2
	start_page=entry1.get()
	end_page=entry2.get()
	with open('{}-{}.csv'.format(start_page,end_page),'a+') as csvfile:
		writer=csv.writer(csvfile)
		writer.writerow([name,author,intro,text])
		
def main():
	global entry1
	global entry2
	global flag_num
	start_page=entry1.get()
	end_page=entry2.get()
	with open('{}-{}.csv'.format(start_page,end_page),'a+') as fp:
		writer=csv.writer(fp)
		writer.writerow(['书名','作者','累计字数','简介'])
	start_page=int(start_page)
	end_page=int(end_page)
	for page in range(start_page,(end_page)+1):
		html=gethtml(page)
		list=parser_url(html)
		gettext(list)
	
	messagebox()
	
	if flag_num==1:
		email()
	
def startSpider():
	t = threading.Thread(target=main,args=())
	t.start()
	
def face():
	global entry1
	global entry2
	global entry
	global flag_num
	root=Tk()
	root.title('起点完本小说信息爬取 by windy_ll 52pojie.cn')
	root.geometry()
	c1=Checkbutton(root,text='不计字数',command=flag1)
	c1.grid(row=0,column=0)
	c2=Checkbutton(root,text='100-200万字',command=flag2)
	c2.grid(row=0,column=1)
	c3=Checkbutton(root,text='200万字以上',command=flag3)
	c3.grid(row=0,column=2)
	lab=Label(root,text='邮箱:')
	lab.grid(row=1,column=0,sticky='w')
	entry=Entry(root)
	entry.grid(row=1,column=1,columnspan=2,sticky='w')
	c4=Checkbutton(root,text='发送',command=flag4)
	c4.grid(row=1,column=3)
	lab1=Label(root,text='起始页')
	lab1.grid(row=2,column=0,sticky='w')
	lab2=Label(root,text='结束页')
	lab2.grid(row=2,column=2,sticky='w')
	entry1=Entry(root)
	entry1.grid(row=3,column=0,sticky='w')
	lab3=Label(root,text='----')
	lab3.grid(row=3,column=1)
	entry2=Entry(root)
	entry2.grid(row=3,column=2,sticky='w')
	button=Button(root,text='确认',command=startSpider)
	button.grid(row=3,column=3)
	root.mainloop()
	
face()	