#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
本软件实现3个功能：
1，可爬取信息进指定邮箱
2，可以把爬取信息记录进excel
3，可以选择是否每天定时发送信息进邮箱
Author： lyb
version：1.0
'''


import smtplib
import requests
import bs4
from email.mime.text import MIMEText
from email.header import Header
import schedule
import time
import datetime
import openpyxl
import threading


def make_mail(content,reciver):
    mail_login='1617963255@qq.com'
    mail_password='skekavswkvqfedhh'
    qqhost='smtp.qq.com'
#    reciver='1617963255@qq.com'  #可以设置为input
    smtp_object=smtplib.SMTP_SSL(host=qqhost)
    smtp_object.connect(qqhost,port=465)
    smtp_object.login(user=mail_login,password=mail_password)

    subject='今明两天天气预报'
    message=MIMEText(content,_subtype='plain',_charset='utf-8')
    message['Subject']=Header(subject,charset='utf-8')
    try:
        smtp_object.sendmail(mail_login,reciver,message.as_string())
        print('投递成功')
    except:
        print('投递不成功')
    smtp_object.quit()

def send_mail(reciver):
    make_mail(crawler()[0],reciver)


def crawler():
    headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'}
    url='http://www.weather.com.cn/weather/101020100.shtml'
    res=requests.get(url,headers=headers)
    res.encoding='utf-8'
    soup=bs4.BeautifulSoup(res.text,features='html.parser')
    lis=soup.find('ul',class_='t clearfix').find_all('li')
    today=lis[0].find('h1').text
    today_weather=lis[0].find('p',class_='tem').find('i').text
    tomorrow=lis[1].find('h1').text
    tomorrow_weather=lis[1].find('p',class_='tem').find('i').text
    str1=today+'的天气是：'+today_weather
    str2=tomorrow+'的天气是：'+tomorrow_weather
    email_content=str1+'\n'+str2
    excel_content=[today, today_weather, tomorrow, tomorrow_weather]
    return email_content,excel_content #crawler()[0]是邮件内容，crawler()[1]是excel所需列表


def schedule_job(reciver):
    send_mail(reciver)
#这里需要添加写入excel的函数  write_excel(crawler_contents[1])
    print(datetime.datetime.now())

def run_progarm(reciver):
    global coin
    coin =True
    schedule.every(3).seconds.do(lambda:schedule_job(reciver))
    while coin:
        schedule.run_pending()
        time.sleep(1)
def change_coin():
    global coin
    coin = False
    print('coin is changed to ''False''')

def task_stop():
    schedule.clear()


def write_execl(content):
    path=r'C:\Users\Admin\Desktop\Project\Project_Email\Weather.xlsx'
    wb=openpyxl.load_workbook(path)
    sheet=wb['weather email']
    sheet.append(content)
    wb.save(path)
    # 覆盖写法
    # wb=openpyxl.Workbook()
    # sheet=wb.active
    # sheet.title='weather email'
    # sheet.append(content)
    # wb.save(r'C:\Users\Admin\Desktop\Project\Project_Email\Weather.xlsx')

if __name__ ==  '__main__':
    #write_execl(crawler()[1])
    t0=threading.Thread(target=run_progarm,args=('1617963255@qq.com',))
    t0.start()
    print('????????????????????????????????????')
    #run_progarm('1617963255@qq.com')
    time.sleep(10)
    change_coin()


    #schedule_job()
    #send_mail('1617963255@qq.com')
    #make_mail(crawler()[0],'1617963255@qq.com')
    #print(type(crawler()))
