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
| import requests import json import time import smtplib from email.mime.text import MIMEText from email.header import Header key='xxxxxxxxxxxxxxxxxxxxxxxxx'
list = [["[email protected]",'100000','Triority'] ]
def send(msg,rec_user): mail_host="smtp.qq.com" send_user="[email protected]" send_pass="pass" message=MIMEText(msg,'plain','utf-8') message['from']=Header(send_user,'utf-8') message['to']=Header(rec_user,'utf-8') subject='今日份天气预报来咯' message['Subject']=Header(subject,'utf-8') try: smtpObj=smtplib.SMTP_SSL(mail_host,465) smtpObj.login(send_user,send_pass) smtpObj.sendmail(send_user,rec_user,message.as_string()) print('succeed') except smtplib.SMTPException: print('error')
def get_time(): tim=time.localtime(time.time()) tm_year=tim[0] tm_mon=tim[1] tm_mday=tim[2] tm_hour=tim[3] tm_min=tim[4] tm_yday = tim[7] return tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_yday
def now_weather(key,location): url='https://devapi.qweather.com/v7/weather/now?key='+key+'&location='+location header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'} now_res=requests.get(url,headers=header) now_res_json=now_res.json() now_temp = now_res_json['now']['temp'] now_feelsLike = now_res_json['now']['feelsLike'] now_windScale = now_res_json['now']['windScale'] now_windSpeed = now_res_json['now']['windSpeed'] now_windDir = now_res_json['now']['windDir'] now_text = now_res_json['now']['text'] now_obsTime = now_res_json['now']['obsTime'] return now_temp,now_feelsLike,now_windScale,now_windSpeed,now_windDir,now_text,now_obsTime
def today_weather(key,location): url='https://devapi.qweather.com/v7/weather/3d?key='+key+'&location='+location header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'} now_res=requests.get(url,headers=header) now_res_json=now_res.json() sunrise = now_res_json['daily'][0]['sunrise'] sunset = now_res_json['daily'][0]['sunset'] moonrise = now_res_json['daily'][0]['moonrise'] moonset = now_res_json['daily'][0]['moonset'] tempMax = now_res_json['daily'][0]['tempMax'] tempMin = now_res_json['daily'][0]['tempMin'] textDay = now_res_json['daily'][0]['textDay'] textNight = now_res_json['daily'][0]['textNight'] windDirDay = now_res_json['daily'][0]['windDirDay'] windScaleDay = now_res_json['daily'][0]['windScaleDay'] windSpeedDay = now_res_json['daily'][0]['windSpeedDay'] precip = now_res_json['daily'][0]['precip'] uvIndex = now_res_json['daily'][0]['uvIndex'] humidity = now_res_json['daily'][0]['humidity'] return sunrise, sunset, moonrise, moonset, tempMax, tempMin, textDay, textNight, windDirDay, windScaleDay, windSpeedDay, precip, uvIndex, humidity
def now_air(key,location): url='https://devapi.qweather.com/v7/air/now?key='+key+'&location='+location header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'} now_res=requests.get(url,headers=header) now_res_json=now_res.json() category = now_res_json['now']['category'] aqi = now_res_json['now']['aqi'] return category, aqi
def txt(key,location,name): tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_yday = get_time() now_temp, now_feelsLike, now_windScale, now_windSpeed, now_windDir, now_text, now_obsTime = now_weather(key,location) sunrise, sunset, moonrise, moonset, tempMax, tempMin, textDay, textNight, windDirDay, windScaleDay, windSpeedDay, precip, uvIndex, humidity = today_weather(key,location) category, aqi = now_air(key,location) msg =''' <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>HTML Editor - LDDGO.NET</title> </head> <body> <p><span style="font-size: 14pt;">Dear '''+name+''':</span></p> <p><span style="font-size: 12pt;">早安!</span></p> <p><span style="font-size: 12pt;">今天是'''+str(tm_year)+'''年'''+str(tm_mon)+'''月'''+str(tm_mday)+'''日,是今年的第'''+str(tm_yday)+'''天了哦</span></p> <p> </p> <p><span style="font-size: 12pt;">下面是现在实时的天气状况:</span></p> <p><span style="font-size: 12pt;">现在室外的空气状况为'''+category+''',质量指数为'''+aqi+'''</span></p> <p><span style="font-size: 12pt;">今天早上天气'''+now_text+''',气温'''+now_temp+'''度,体感温度'''+now_feelsLike+'''度,</span></p> <p><span style="font-size: 12pt;">刮'''+now_windDir+''',风力'''+now_windScale+'''级,速度约'''+now_windSpeed+'''m/s</span></p> <p><span style="font-size: 12pt;">以上信息的测量时间为'''+now_obsTime+'''</span></p> <p> </p> <p><span style="font-size: 12pt;">下面是今天全天的天气情况:</span></p> <p><span style="font-size: 12pt;">今天白天天气'''+textDay+''',最低气温是'''+tempMin+'''度,最高'''+tempMax+'''度,</span></p> <p><span style="font-size: 12pt;">刮'''+windDirDay+''',风力'''+windScaleDay+'''级,速度约'''+windSpeedDay+'''m/s</span></p> <p><span style="font-size: 12pt;">预计今天总降水量为'''+precip+'''毫米,紫外线指数'''+uvIndex+''',相对湿度为'''+humidity+'''.</span></p> <p><span style="font-size: 12pt;">今天的日出时间是:'''+sunrise+''',日落时间:'''+sunset+''',月亮在'''+moonrise+'''升起,在'''+moonset+'''落下,</span></p> <p><span style="font-size: 12pt;">今天夜晚天气'''+textNight+'''</span></p> <p> </p> <p><span style="font-size: 12pt;">Sincerely wish you all the best of luck today !!!</span></p> <p> </p> <p style="text-align: right;"><span style="font-size: 12pt;">Triority</span></p> <p style="text-align: right;"><span style="font-size: 12pt;">'''+str(tm_year)+'''.'''+str(tm_mon)+'''.'''+str(tm_mday)+'''</span></p> </body> </html> ''' return msg
for i in list: send(txt(key, i[1], i[2]), i[0])
|