文章首发于个人站点 实现微信消息每日贴心提醒 | 隐舍
微信公众号:【DreamHub】
如何做一个贴心的程序员男朋友?从微信每日提醒开始吧!!!
微信消息内容
包括如下:
消息效果
功能模块及其Python实现
依赖包
- requests==2.22.0
- itchat==1.3.10
- sxtwl==1.0.7
日期(阳历+阴历)
def getDate():
"""
Get date: solar + lunar calendar
:return: String calendar
"""
ymc = [u"十一", u"十二", u"正", u"二", u"三", u"四",
u"五", u"六", u"七", u"八", u"九", u"十"]
rmc = [u"初一", u"初二", u"初三", u"初四", u"初五", u"初六", u"初七", u"初八", u"初九", u"初十",
u"十一", u"十二", u"十三", u"十四", u"十五", u"十六", u"十七", u"十八", u"十九",
u"二十", u"廿一", u"廿二", u"廿三", u"廿四", u"廿五", u"廿六", u"廿七", u"廿八", u"廿九", u"三十", u"卅一"]
numCn = ["天", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
# 获取阳历和阴历
year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day
lunar = sxtwl.Lunar()
date_lunar = lunar.getDayBySolar(year, month, day)
print_date = str(date_lunar.y) + "年" + str(date_lunar.m) + \
"月" + str(date_lunar.d) + "日"
if date_lunar.Lleap:
print_lunar = "润" + ymc[date_lunar.Lmc] + \
"月" + rmc[date_lunar.Ldi] + "日"
else:
print_lunar = ymc[date_lunar.Lmc] + "月" + rmc[date_lunar.Ldi] + "日"
print_week = "星期" + numCn[date_lunar.week]
calendar = '日期:' + print_date + ', ' + print_week + '\n' + \
'农历: ' + print_lunar + '\n'
return calendar
每日壹句(内容来自爱词霸)
def getDailySentence():
"""
Get AiCiBa Daily Sentence
return: String English + Chinese
"""
url = 'http://open.iciba.com/dsapi/'
r = requests.get(url)
all = json.loads(r.text)
Englis = all['content']
Chinese = all['note']
daily_sentence = '\n@每日壹句:'+'\n'+Englis+'\n'+Chinese+'\n'
return daily_sentence
天气预报(内容来自中国天气网)
- 天气情况;
- 温度情况;
- 穿衣指数;
- 减肥指数;
- 空气指数;
- 紫外线指数;
def getInfo(location):
"""
Get More Weather Information.
Arguments:
location {String} -- location html
Returns:
String -- Information
"""
response = requests.get(html)
content = response.content.decode("utf-8")
aim = re.findall(
r'<input type="hidden" id="hidden_title" value="(.*?)月(.*?)日(.*?)时(.*?) (.*?) (.*?) (.*?)"', content)
airdata = re.findall(
r'<li class="li6">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>', content)
ult_index = re.findall(
r'<li class="li1">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>\n</li>', content)
cloth_index = re.findall(
r'<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>\n</a>\n</li>\n<li class="li4">', content)
# wash_index = re.findall(r'<li class="li4">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>', content)
lose_index = re.findall(
r'</span>\n<em>(.*?)</em>\n<p>(.*?)</p>\n</a>\n</li>\n<li class="li5">', content)
# print(lose_index)
txt1 = '@天气预报:'+'\n'
txt2 = '天气情况: '+aim[0][5]+'\n'+'温度情况: '+aim[0][6]+'\n'
txt3 = '穿衣指数: '+cloth_index[0][0]+', '+cloth_index[0][2]+'\n'
txt4 = '减肥指数:' + lose_index[0][1]+'\n'
txt5 = '空气指数: '+airdata[0][0]+', '+airdata[0][2]+'\n'
txt6 = '紫外线指数: '+ult_index[0][0]+', '+ult_index[0][2]+'\n'
# txt7 = '洗车指数: '+wash_index[0][0]+', '+wash_index[0][2]+'\n'
more_information = '\n'+txt1+txt2+txt3+txt4+txt5+txt6
return more_information
微信登录及主函数
微信操作模块API参考:https://github.com/littlecodersh/ItChat
def autoWechat(text, nickname):
"""Wechat automatic login and send message.
Arguments:
name {wechat name} -- send by wechat name
text {String} -- message.
"""
itchat.auto_login()
result = itchat.search_friends(nickName=nickname)
user_name = result[0]['UserName']
itchat.send(text, user_name)
def main():
xxx = "\n来自梦宝宝的爱:\n"
city_code = "101240101"
nickname = "******" # 对方的微信昵称
location = "http://www.weather.com.cn/weather/"+city_code+".shtml"
autoWechat(getDate()+xxx+getDailySentence()+getInfo(location), nickname)
main()
PS:每个城市在中国天气网都有对应的代码在其https链接中,可自行查阅;小编为大家整理了城市对应的编码表格,可在微信公众号【DreamHub】回复【daily】领取;
待解决的问题
如果推送每日消息提醒,那么每天需要手动执行代码,这就很难受了!
下次有时间更新如何部署到服务器中定时执行!
关注作者
转载:https://blog.csdn.net/DreamHome_S/article/details/105638942
查看评论