forked from tech-shrimp/FreeWechatPush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
169 lines (142 loc) · 5.71 KB
/
main.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 安装依赖 pip3 install requests html5lib bs4 schedule
import time
import requests
import json
import schedule
from bs4 import BeautifulSoup
# 从测试号信息获取
appID = "wx59b04fd11476bf4f"
appSecret = "690713999f9735bc7120a8b9ba8e12bc"
#收信人ID即 用户列表中的微信号,见上文
openId = "oRMjt6XTFydUBAj2CFH6Qs_uqMPE"
# 天气预报模板ID
weather_template_id = "YnydwM9Nc_j62z-jCZpNPB7bjd4rQE8XS8we5CQJDkc"
# 时间表模板ID
timetable_template_id = ""
def get_weather(my_city):
urls = ["http://www.weather.com.cn/textFC/hb.shtml",
"http://www.weather.com.cn/textFC/db.shtml",
"http://www.weather.com.cn/textFC/hd.shtml",
"http://www.weather.com.cn/textFC/hz.shtml",
"http://www.weather.com.cn/textFC/hn.shtml",
"http://www.weather.com.cn/textFC/xb.shtml",
"http://www.weather.com.cn/textFC/xn.shtml"
]
for url in urls:
resp = requests.get(url)
text = resp.content.decode("utf-8")
soup = BeautifulSoup(text, 'html5lib')
div_conMidtab = soup.find("div", class_="conMidtab")
tables = div_conMidtab.find_all("table")
for table in tables:
trs = table.find_all("tr")[2:]
for index, tr in enumerate(trs):
tds = tr.find_all("td")
# 这里倒着数,因为每个省会的td结构跟其他不一样
city_td = tds[-8]
this_city = list(city_td.stripped_strings)[0]
if this_city == my_city:
high_temp_td = tds[-5]
low_temp_td = tds[-2]
weather_type_day_td = tds[-7]
weather_type_night_td = tds[-4]
wind_td_day = tds[-6]
wind_td_day_night = tds[-3]
high_temp = list(high_temp_td.stripped_strings)[0]
low_temp = list(low_temp_td.stripped_strings)[0]
weather_typ_day = list(weather_type_day_td.stripped_strings)[0]
weather_type_night = list(weather_type_night_td.stripped_strings)[0]
wind_day = list(wind_td_day.stripped_strings)[0] + list(wind_td_day.stripped_strings)[1]
wind_night = list(wind_td_day_night.stripped_strings)[0] + list(wind_td_day_night.stripped_strings)[1]
# 如果没有白天的数据就使用夜间的
temp = f"{low_temp}——{high_temp}摄氏度" if high_temp != "-" else f"{low_temp}摄氏度"
weather_typ = weather_typ_day if weather_typ_day != "-" else weather_type_night
wind = f"{wind_day}" if wind_day != "--" else f"{wind_night}"
return this_city, temp, weather_typ, wind
def get_access_token():
# 获取access token的url
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}' \
.format(appID.strip(), appSecret.strip())
response = requests.get(url).json()
print(response)
access_token = response.get('access_token')
return access_token
def get_daily_love():
# 每日一句情话
url = "https://api.lovelive.tools/api/SweetNothings/Serialization/Json"
r = requests.get(url)
all_dict = json.loads(r.text)
sentence = all_dict['returnObj'][0]
daily_love = sentence
return daily_love
def send_weather(access_token, weather):
# touser 就是 openID
# template_id 就是模板ID
# url 就是点击模板跳转的url
# data就按这种格式写,time和text就是之前{{time.DATA}}中的那个time,value就是你要替换DATA的值
import datetime
today = datetime.date.today()
today_str = today.strftime("%Y年%m月%d日")
body = {
"touser": openId.strip(),
"template_id": weather_template_id.strip(),
"url": "https://weixin.qq.com",
"data": {
"date": {
"value": today_str
},
"region": {
"value": weather[0]
},
"weather": {
"value": weather[2]
},
"temp": {
"value": weather[1]
},
"wind_dir": {
"value": weather[3]
},
"today_note": {
"value": get_daily_love()
}
}
}
url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(access_token)
print(requests.post(url, json.dumps(body)).text)
def send_timetable(access_token, message):
body = {
"touser": openId,
"template_id": timetable_template_id.strip(),
"url": "https://weixin.qq.com",
"data": {
"message": {
"value": message
},
}
}
url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(access_token)
print(requests.post(url, json.dumps(body)).text)
def weather_report(city):
# 1.获取access_token
access_token = get_access_token()
# 2. 获取天气
weather = get_weather(city)
print(f"天气信息: {weather}")
# 3. 发送消息
send_weather(access_token, weather)
def timetable(message):
# 1.获取access_token
access_token = get_access_token()
# 3. 发送消息
send_timetable(access_token, message)
if __name__ == '__main__':
weather_report("宁波")
# timetable("第二教学楼十分钟后开始英语课")
# schedule.every().day.at("8:30").do(weather_report, "宁波")
# schedule.every().monday.at("13:50").do(timetable, "第二教学楼十分钟后开始英语课")
#while True:
# schedule.run_pending()
# time.sleep(1)
pip3 install -r requirements.txt
python main.py