Skip to content

Commit 003990f

Browse files
authoredAug 29, 2022
Add files via upload
0 parents  commit 003990f

File tree

4 files changed

+309
-0
lines changed

4 files changed

+309
-0
lines changed
 

‎README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# daily_reminder
2+
给女朋友做的微信天气推送
3+
4+
教程链接
5+
6+
https://www.coolapk.com/feed/38579891?shareKey=NGI3ZGZlZTM4MDBjNjMwMzdlM2M~&shareUid=3198334&shareFrom=com.coolapk.app_4.10
7+
8+
9+
10+
11+
网址1 http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
12+
网址2 https://id.qweather.com/
13+
14+
15+
模板内容如下:
16+
17+
{{date.DATA}}
18+
19+
地区:{{region.DATA}}
20+
21+
天气:{{weather.DATA}}
22+
23+
气温:{{temp.DATA}}
24+
25+
风向:{{wind_dir.DATA}}
26+
27+
今天是我们恋爱的第{{love_day.DATA}}天
28+
29+
{{birthday1.DATA}}
30+
{{birthday2.DATA}}
31+
32+
33+
{{note_en.DATA}}
34+
{{note_ch.DATA}}
35+
36+
37+
天气key生成教程
38+
![image](https://raw.githubusercontent.com/limoest/daily_reminder/main/%E5%92%8C%E9%A3%8E%E5%A4%A9%E6%B0%94key%E7%94%9F%E6%88%90.png)
39+
40+
41+
可以去天行数据申请各种各样的接口用来推送
42+
![image](https://raw.githubusercontent.com/limoest/daily_reminder/main/others/Snipaste_2022-08-24_12-13-19.png)
43+
![image](https://raw.githubusercontent.com/limoest/daily_reminder/main/others/Snipaste.png)
44+
45+
46+
47+
有别的建议欢迎留言

‎config.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
# 公众号配置
3+
# 公众号appId
4+
"app_id": "wxf079351aff089958",
5+
# 公众号appSecret
6+
"app_secret": "07d2568530237fe81a19103347cc3bcf",
7+
# 模板消息id
8+
"template_id": "iKDwLodpECkfdkvyga98kYxjp5JFtdw3yxqcDNsp5rg",
9+
# 接收公众号消息的微信号,如果有多个,需要在[]里用英文逗号间隔,例如["wx1", "wx2"]
10+
"user": ["oRo5v51nxycKND0SvL_eXQMROpzM"],
11+
12+
# 信息配置
13+
# 和风天气apikey
14+
"weather_key": "1cec73554aea4b4ebc15ca395f5fb90d",
15+
# 所在地区,可为省,城市,区,县,同时支持国外城市,例如伦敦
16+
"region": "崇州市",
17+
# 生日1,修改名字为对应需要显示的名字,如果生日为农历,在最前面加上r即可
18+
"birthday1": {"name": "小静同学", "birthday": "2005-05-14"},
19+
# 生日2
20+
"birthday2": {"name": "##", "birthday": "2001-01-01"},
21+
# 在一起的日子,格式同上,暂不不支持农历
22+
"love_date": "2022-01-01",
23+
# 金句中文,如果设置了,则会显示这里的,如果为空,默认会读取金山的每日金句
24+
"note_ch": "",
25+
# 金句英文
26+
"note_en": ""
27+
}

‎main.py

+233
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
import random
2+
from time import localtime
3+
from requests import get, post
4+
from datetime import datetime, date
5+
from zhdate import ZhDate
6+
import sys
7+
import os
8+
9+
10+
def get_color():
11+
# 获取随机颜色
12+
get_colors = lambda n: list(map(lambda i: "#" + "%06x" % random.randint(0, 0xFFFFFF), range(n)))
13+
color_list = get_colors(100)
14+
return random.choice(color_list)
15+
16+
17+
def get_access_token():
18+
# appId
19+
app_id = config["app_id"]
20+
# appSecret
21+
app_secret = config["app_secret"]
22+
post_url = ("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}"
23+
.format(app_id, app_secret))
24+
try:
25+
access_token = get(post_url).json()['access_token']
26+
except KeyError:
27+
print("获取access_token失败,请检查app_id和app_secret是否正确")
28+
os.system("pause")
29+
sys.exit(1)
30+
# print(access_token)
31+
return access_token
32+
33+
34+
def get_weather(region):
35+
headers = {
36+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
37+
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
38+
}
39+
key = config["weather_key"]
40+
region_url = "https://geoapi.qweather.com/v2/city/lookup?location={}&key={}".format(region, key)
41+
response = get(region_url, headers=headers).json()
42+
if response["code"] == "404":
43+
print("推送消息失败,请检查地区名是否有误!")
44+
os.system("pause")
45+
sys.exit(1)
46+
elif response["code"] == "401":
47+
print("推送消息失败,请检查和风天气key是否正确!")
48+
os.system("pause")
49+
sys.exit(1)
50+
else:
51+
# 获取地区的location--id
52+
location_id = response["location"][0]["id"]
53+
weather_url = "https://devapi.qweather.com/v7/weather/now?location={}&key={}".format(location_id, key)
54+
response = get(weather_url, headers=headers).json()
55+
# 天气
56+
weather = response["now"]["text"]
57+
# 当前温度
58+
temp = response["now"]["temp"] + u"\N{DEGREE SIGN}" + "C"
59+
# 风向
60+
wind_dir = response["now"]["windDir"]
61+
return weather, temp, wind_dir
62+
63+
64+
def get_birthday(birthday, year, today):
65+
birthday_year = birthday.split("-")[0]
66+
# 判断是否为农历生日
67+
if birthday_year[0] == "r":
68+
r_mouth = int(birthday.split("-")[1])
69+
r_day = int(birthday.split("-")[2])
70+
# 获取农历生日的今年对应的月和日
71+
try:
72+
birthday = ZhDate(year, r_mouth, r_day).to_datetime().date()
73+
except TypeError:
74+
print("请检查生日的日子是否在今年存在")
75+
os.system("pause")
76+
sys.exit(1)
77+
birthday_month = birthday.month
78+
birthday_day = birthday.day
79+
# 今年生日
80+
year_date = date(year, birthday_month, birthday_day)
81+
82+
else:
83+
# 获取国历生日的今年对应月和日
84+
birthday_month = int(birthday.split("-")[1])
85+
birthday_day = int(birthday.split("-")[2])
86+
# 今年生日
87+
year_date = date(year, birthday_month, birthday_day)
88+
# 计算生日年份,如果还没过,按当年减,如果过了需要+1
89+
if today > year_date:
90+
if birthday_year[0] == "r":
91+
# 获取农历明年生日的月和日
92+
r_last_birthday = ZhDate((year + 1), r_mouth, r_day).to_datetime().date()
93+
birth_date = date((year + 1), r_last_birthday.month, r_last_birthday.day)
94+
else:
95+
birth_date = date((year + 1), birthday_month, birthday_day)
96+
birth_day = str(birth_date.__sub__(today)).split(" ")[0]
97+
elif today == year_date:
98+
birth_day = 0
99+
else:
100+
birth_date = year_date
101+
birth_day = str(birth_date.__sub__(today)).split(" ")[0]
102+
return birth_day
103+
104+
105+
def get_ciba():
106+
url = "http://open.iciba.com/dsapi/"
107+
headers = {
108+
'Content-Type': 'application/json',
109+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
110+
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
111+
}
112+
r = get(url, headers=headers)
113+
note_en = r.json()["content"]
114+
note_ch = r.json()["note"]
115+
return note_ch, note_en
116+
117+
118+
def send_message(to_user, access_token, region_name, weather, temp, wind_dir, note_ch, note_en):
119+
url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}".format(access_token)
120+
week_list = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
121+
year = localtime().tm_year
122+
month = localtime().tm_mon
123+
day = localtime().tm_mday
124+
today = datetime.date(datetime(year=year, month=month, day=day))
125+
week = week_list[today.isoweekday() % 7]
126+
# 获取在一起的日子的日期格式
127+
love_year = int(config["love_date"].split("-")[0])
128+
love_month = int(config["love_date"].split("-")[1])
129+
love_day = int(config["love_date"].split("-")[2])
130+
love_date = date(love_year, love_month, love_day)
131+
# 获取在一起的日期差
132+
love_days = str(today.__sub__(love_date)).split(" ")[0]
133+
# 获取所有生日数据
134+
birthdays = {}
135+
for k, v in config.items():
136+
if k[0:5] == "birth":
137+
birthdays[k] = v
138+
data = {
139+
"touser": to_user,
140+
"template_id": config["template_id"],
141+
"url": "http://weixin.qq.com/download",
142+
"topcolor": "#FF0000",
143+
"data": {
144+
"date": {
145+
"value": "{} {}".format(today, week),
146+
"color": get_color()
147+
},
148+
"region": {
149+
"value": region_name,
150+
"color": get_color()
151+
},
152+
"weather": {
153+
"value": weather,
154+
"color": get_color()
155+
},
156+
"temp": {
157+
"value": temp,
158+
"color": get_color()
159+
},
160+
"wind_dir": {
161+
"value": wind_dir,
162+
"color": get_color()
163+
},
164+
"love_day": {
165+
"value": love_days,
166+
"color": get_color()
167+
},
168+
"note_en": {
169+
"value": note_en,
170+
"color": get_color()
171+
},
172+
"note_ch": {
173+
"value": note_ch,
174+
"color": get_color()
175+
}
176+
}
177+
}
178+
for key, value in birthdays.items():
179+
# 获取距离下次生日的时间
180+
birth_day = get_birthday(value["birthday"], year, today)
181+
if birth_day == 0:
182+
birthday_data = "今天{}生日哦,祝{}生日快乐!".format(value["name"], value["name"])
183+
else:
184+
birthday_data = "距离{}的生日还有{}天".format(value["name"], birth_day)
185+
# 将生日数据插入data
186+
data["data"][key] = {"value": birthday_data, "color": get_color()}
187+
headers = {
188+
'Content-Type': 'application/json',
189+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
190+
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
191+
}
192+
response = post(url, headers=headers, json=data).json()
193+
if response["errcode"] == 40037:
194+
print("推送消息失败,请检查模板id是否正确")
195+
elif response["errcode"] == 40036:
196+
print("推送消息失败,请检查模板id是否为空")
197+
elif response["errcode"] == 40003:
198+
print("推送消息失败,请检查微信号是否正确")
199+
elif response["errcode"] == 0:
200+
print("推送消息成功")
201+
else:
202+
print(response)
203+
204+
205+
if __name__ == "__main__":
206+
try:
207+
with open("config.txt", encoding="utf-8") as f:
208+
config = eval(f.read())
209+
except FileNotFoundError:
210+
print("推送消息失败,请检查config.txt文件是否与程序位于同一路径")
211+
os.system("pause")
212+
sys.exit(1)
213+
except SyntaxError:
214+
print("推送消息失败,请检查配置文件格式是否正确")
215+
os.system("pause")
216+
sys.exit(1)
217+
218+
# 获取accessToken
219+
accessToken = get_access_token()
220+
# 接收的用户
221+
users = config["user"]
222+
# 传入地区获取天气信息
223+
region = config["region"]
224+
weather, temp, wind_dir = get_weather(region)
225+
note_ch = config["note_ch"]
226+
note_en = config["note_en"]
227+
if note_ch == "" and note_en == "":
228+
# 获取词霸每日金句
229+
note_ch, note_en = get_ciba()
230+
# 公众号推送消息
231+
for user in users:
232+
send_message(user, accessToken, region, weather, temp, wind_dir, note_ch, note_en)
233+
os.system("pause")

‎requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests==2.28.1
2+
zhdate==0.1

0 commit comments

Comments
 (0)
Please sign in to comment.