摘要♨️:通过访问API将收集到的天气、有道/扇贝每日一词、情话、每日微博热搜信息通过邮件服务定时发送到指定邮箱。
Python库 | 作用 | Document En | Document zh_CN |
---|---|---|---|
requests | 调用目标API获取所需信息 | Requests: HTTP for Humans™ | Requests: 让 HTTP 服务人类 |
schedule | 定时触发某一功能或某一函数 | schedule | X |
yagmail | 发送邮件给目标邮箱 | yagmail | X |
✅ 获取天气信息⛅
def get_weather_info():
'''
:city_code: code of city to search
'''
url = "https://devapi.qweather.com/v7/weather/3d?location=%s&key=503f78630621427086cf1f77f612f2f9" % city_code
resp = requests.get(url)
threedays_weather_info_resp = resp.content.decode('utf-8')
threedays_weather_info = json.loads(threedays_weather_info_resp)['daily']
weather_info = 'XXX地区天气预报:\n'
for i in threedays_weather_info:
weather_info += i['fxDate'] + ", 最高温: " + i['tempMax'] + "℃, 最低温: " + i['tempMin'] + "℃, 白天天气: " + i['textDay'] + ", 晚间天气: " + i['textNight'] + ", 白天风力: " + i['windScaleDay'] + "级, 晚上风力: " + i['windScaleNight'] + "级\n"
return weather_info
:pushpin: 天气信息的获取通过调用和风天气API来实现,具体信息可查询API官方文档。此次获取3天预报,参数调用如下:
3 天预报
商业版 https://api.qweather.com/v7/weather/3d?[请求参数]
开发版 https://devapi.qweather.com/v7/weather/3d?[请求参数]
-
PS:请求参数包括必选和可选参数,其中必选参数为地区参数location及用户认证参数key。
location:需要查询地区的LocationID或以英文逗号分隔的经度,纬度坐标(十进制),LocationID可通过城市搜索服务获取。例如 location=101010100 或 location=116.41,39.92;
key:用户认证key,请参考如何获取你的KEY。支持数字签名方式进行认证。例如 key=123456789ABC
✅ 获取每日一词 ⏲️
def get_youdao_oneday_words():
yd_url = "https://dict.youdao.com/infoline?mode=publish&date=" + today + "&update=auto&apiversion=5.0"
for record in requests.get(yd_url).json()[today]:
if record['type'] == '壹句':
oneday_words = "有道时间: " + today + ", 每日一句: " + record['title'] + ", 翻译: " + record['summary']
break
return oneday_words
def get_shanbei_oneday_words():
sb_url = "https://apiv3.shanbay.com/weapps/dailyquote/quote/?date=" + today
record = requests.get(sb_url).json()
oneday_words = "扇贝时间: " + today + ", 每日一句: " + record['content'] + ", 翻译: " + record['translation']
return oneday_words
- 每日一词内容来自有道或扇贝,调用方式为利用datetime模块获取当日日期
today = datetime.date.today().isoformat()
✅ 获取每日微博热搜:loudspeaker:
def get_weibo_hot():
url = "https://res.abeim.cn/api-weibo_hot"
resp = requests.get(url)
weibo_hots_resp = resp.content.decode('utf-8')
weibo_hots = json.loads(weibo_hots_resp)['data']
weibo_hots_contents = ''
for i in weibo_hots:
# 此处利用html语法将热搜获得超链接功能
weibo_hots_contents += "<a href=" + i['url'] + ">" + i['title'] + "</a>, 热搜指数: " + str(i['hot']) + "\n"
return weibo_hots_contents
- PS:每日微博热搜API已失效,方法仅供参考
✅ 定时发送邮件
def get_mailposter_info(filename):
'''
:filename: filename including mail poster information
'''
with open(filename, 'rb') as f:
mailinfo = f.readlines()
return [info.strip().decode() for info in mailinfo]
def send_oneday_contents(mail_receiver, contents, mailinfo):
yag = yagmail.SMTP(mailinfo[0], mailinfo[1], host = 'smtp.163.com')
# "xxxx每日提醒"为邮件主题, contents为邮件正文内容
yag.send(mail_receiver, "XXXX每日提醒", contents=contents)
if __name__ == "__main__":
mailinfo = get_mailposter_info("xxx/xxx/xxx/filename")
weibo_hots = get_weibo_hot()
oneday_words = get_youdao_oneday_words() + "\n" + get_shanbei_oneday_words()
weather_info = get_weather_info()
mail_receiver = ["邮箱1", "邮箱2",...,"邮箱n"]
schedule.every().day.at("09:37").do(send_oneday_contents, mail_receiver, weather_info, mailinfo)
# 或者
# schedule.every().day.at("09:37").do(lambda:send_oneday_contents(mail_receiver, weather_info, mailinfo))
schedule.every().day.at("10:38").do(send_oneday_contents, mail_receiver, weibo_hots, mailinfo)
schedule.every().day.at("12:35").do(send_oneday_contents, mail_receiver, oneday_words, mailinfo)
schedule.every().day.at("20:00").do(send_oneday_contents, mail_receiver, weather_info, mailinfo)
-
PS:
> 目标邮箱mail_receiver可以是单一邮箱地址,也可以是邮箱群,其中群组邮箱mail_receiver=[‘邮箱1’, ‘邮箱2’, ‘邮箱3’, … ,’邮箱n’];
> 发送邮箱信息mailinfo存放于[filename]文件内,包含发送邮箱地址及授权码;