-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
372 lines (321 loc) · 12.2 KB
/
main.py
File metadata and controls
372 lines (321 loc) · 12.2 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import time
from datetime import date, datetime
import math
from wechatpy import WeChatClient
from wechatpy.client.api import WeChatMessage, WeChatTemplate
import requests
import os
import random
############### 参数设定区域 #################
# 微信测试号信息
appID = ''
appsecret = ''
# 模板消息接口id
template1 = '' # 自定义模板一的模板消息接口id号
template2 = '' # 自定义模板二的模板消息接口id号,不启用不用填写
template3 = '' # 自定义模板三的模板消息接口id号,不启用不用填写
template4 = '' # 自定义模板四的模板消息接口id号,不启用不用填写
# 关注的成员ID,测试号页面的微信号,实则为账户的openID
user_id = ''
# 天行数据
key = ''
# 心知天气
zxkey = ''
# 高德key
gdkey = ''
# 生日日期参数填写
birthday = '' # 格式 %month-%day 【用月份-日期格式填写,不要出现年份】
# 相处开始日期
start_date = '' # 使用年份-月份-日期的形式填写
location = '' # 心知天气api 空气质量监测地区,英文拼写
cityacode = '' # 高德地图天气情况获取,城市acode 参照readme文档填写
############### 参数设定区域结束 ##################
############## ↓ ↓ ↓ ↓ 下方程序根据需求自定义更改 ↓ ↓ ↓ ↓ ################
today = datetime.now()
# 当前日期获取
currentTime = time.strftime("%Y-%m-%d", time.localtime(time.time()))
nowDate = time.strftime("%H:%M:%S", time.localtime(time.time()))
# 相处日期
#### 逻辑:从现在时间-相识时间得出时间长!
def get_count():
delta = today - datetime.strptime(start_date, "%Y-%m-%d")
return delta.days
### 逻辑: 判断生日是否已经过了,过了的话下一年的时间长计算,没过则用现在的月份日期-生日的月份日期
# 生日日期计算
def get_birthday():
next = datetime.strptime(str(date.today().year) + "-" + birthday, "%Y-%m-%d")
if next < datetime.now():
next = next.replace(year=next.year + 1)
return (next - today).days
# 当前日期获取处理
def getweek():
weekEng = time.strftime("%A", time.localtime(time.time()))
week_list = {
"Monday": "星期一",
"Tuesday": "星期二",
"Wednesday": "星期三",
"Thursday": "星期四",
"Friday": "星期五",
"Saturday": "星期六",
"Sunday": "星期日"
}
week = week_list[weekEng]
return week
##### API调用部分开始 ###
# 高德
def citybase():
url = 'https://restapi.amap.com/v3/weather/weatherInfo'
params = {
"key": gdkey,
"city": cityacode, # 城市acode码填写
"extensions": "base",
}
resp = requests.get(url, params)
data = resp.json()
if resp.status_code == 200:
return data
else:
print('请求失败,请查看错误')
def cityall():
url = 'https://restapi.amap.com/v3/weather/weatherInfo'
params = {
"key": gdkey,
"city": cityacode, # 城市acode码填写
"extensions": "all",
}
resp = requests.get(url, params)
data = resp.json()
if resp.status_code == 200:
return data
else:
print('请求失败')
citybase = citybase()
cityall = cityall()
address = citybase['lives'][0]['city'] # 地点
weather = citybase['lives'][0]['weather'] # 天气
temperature = citybase['lives'][0]['temperature'] # 温度
winddirection = citybase['lives'][0]['winddirection'] # 风向
windpower = citybase['lives'][0]['windpower'] # 风力
dayweather = cityall['forecasts'][0]['casts'][0]['dayweather']
nightweather = cityall['forecasts'][0]['casts'][0]['nightweather']
daytemp = cityall['forecasts'][0]['casts'][0]['daytemp']
nighttemp = cityall['forecasts'][0]['casts'][0]['nighttemp']
print('--------', '\n', '城市:', address, '\n', '天气:', weather, '\n', '当下温度:', temperature + '℃', '\n', '风向:',
winddirection, '\n', '风速:', windpower)
print('--------')
print(cityall['forecasts'][0]['casts'][0]['dayweather'])
print(cityall['forecasts'][0]['casts'][0]['nightweather'])
print(cityall['forecasts'][0]['casts'][0]['daytemp'])
print(cityall['forecasts'][0]['casts'][0]['nighttemp'])
print('--------')
print(cityall['forecasts'][0]['casts'][1]['dayweather'])
print(cityall['forecasts'][0]['casts'][1]['nightweather'])
print(cityall['forecasts'][0]['casts'][1]['daytemp'])
print(cityall['forecasts'][0]['casts'][1]['nighttemp'])
# 随机颜色渲染
def get_random_color():
return "#%06x" % random.randint(0, 0xFFFFFF)
# 获取空气质量
def getkqzl():
params = {
"key": zxkey,
"location": location, # 查询地点设置为访问IP所在地
"language": "zh-Hans",
"unit": "c",
"days": "1"
}
url = 'https://api.seniverse.com/v3/life/suggestion.json'
resp = requests.get(url, params)
if resp.status_code == 200:
data = resp.json()["results"]
return data[0]['suggestion'][0]['air_pollution']['brief']
else:
print('请求失败')
# 处理获取到的数据
getkqzl = getkqzl()
suggestion = getkqzl # 空气质量
# 根据空气质量设置颜色
def suggestioncolor():
if suggestion == '优':
return '#33FF33'
elif suggestion == '良' or suggestion == '中':
return '#77FF00'
else:
return '#FFAA33'
# 土味情话
def getSayLove():
url = 'http://api.tianapi.com/saylove/index?key='
resp = requests.get(url + key)
if resp.status_code == 200:
data = resp.json()
return data['newslist'][0]['content']
else:
print('请求失败')
# 情诗
def getqingshi():
url = 'http://api.tianapi.com/qingshi/index?key='
resp = requests.get(url + key)
if resp.status_code == 200:
data = resp.json()
return data['newslist'][0]['content']
else:
print('请求失败')
# 早安心语
def getzaoan():
url = 'http://api.tianapi.com/zaoan/index?key='
resp = requests.get(url + key)
if resp.status_code == 200:
data = resp.json()
return data['newslist'][0]['content']
else:
print('请求失败')
# 晚安心语
def getwanan():
url = 'http://api.tianapi.com/wanan/index?key='
resp = requests.get(url + key)
if resp.status_code == 200:
data = resp.json()
return data['newslist'][0]['content']
else:
print('请求失败')
# 励志古言
def getlzmy():
url = 'http://api.tianapi.com/lzmy/index?key='
resp = requests.get(url + key)
if resp.status_code == 200:
data = resp.json()
return data['newslist'][0]
else:
print('请求失败')
# 彩虹屁
def getcaihongpi():
url = 'http://api.tianapi.com/caihongpi/index?key='
resp = requests.get(url + key)
if resp.status_code == 200:
data = resp.json()
return data['newslist'][0]['content']
else:
print('请求失败')
# 节假日
def getjiejiari():
url = 'http://api.tianapi.com/jiejiari/index?key='
resp = requests.get(url + key + '&date=' + currentTime + '&type=2')
if resp.status_code == 200:
data = resp.json()
return data['newslist'][0]
else:
print('请求失败')
# one一个
def getone():
url = 'http://api.tianapi.com/one/index?key='
resp = requests.get(url + key + '&rand=1')
if resp.status_code == 200:
data = resp.json()
return data['newslist'][0]['word']
else:
print('请求失败')
# 天气诗句
def gettianqishiju():
url = 'http://api.tianapi.com/tianqishiju/index?key='
weatherlist = {
"风": "1",
"云": "2",
"雨": "3",
"雪": "4",
"霜": "5",
"露": "6",
"雾": "7",
"雷": "8",
"晴": "9",
"阴": "10",
}
resp = requests.get(url + key + '&tqtype=' + weatherlist[weather])
if resp.status_code == 200:
data = resp.json()
return data['newslist'][0]
else:
print('请求失败')
getSayLove = getSayLove()
getqingshi = getqingshi()
getzaoan = getzaoan()
getwanan = getwanan()
getlzmy = getlzmy()
getcaihongpi = getcaihongpi()
getjiejiari = getjiejiari()
getone = getone()
# gettianqishiju = gettianqishiju()
##### API调用部分结束 ###
# 推送消息
client = WeChatClient(appID, appsecret)
wm = WeChatMessage(client)
if "06:00:00" < nowDate < "22:00:00":
# 微信消息模板 ID
template_id = template1
# 自定义的内容
data = {
"getSayLove": {"value": getSayLove, "color": get_random_color()},
"currentTime": {"value": currentTime, "color": get_random_color()},
"nongli": {"value": getjiejiari['lunarmonth'] + '-' + getjiejiari['lunarday'], "color": get_random_color()},
"week": {"value": getweek(), "color": get_random_color()},
"nowDate": {"value": nowDate, "color": get_random_color()},
"address": {"value": address, "color": get_random_color()},
"dayweather": {"value": dayweather, "color": get_random_color()},
"nightweather": {"value": nightweather, "color": get_random_color()},
"winddirection": {"value": winddirection, "color": get_random_color()},
"windpower": {"value": windpower, "color": get_random_color()},
"weather": {"value": weather, "color": get_random_color()},
"daytemp": {"value": daytemp, "color": get_random_color()},
"nighttemp": {"value": nighttemp, "color": get_random_color()},
"temperature": {"value": temperature + '℃', "color": get_random_color()},
"suggestion": {"value": suggestion, "color": suggestioncolor()},
"saying": {"value": getlzmy['saying'], "color": get_random_color()},
"source": {"value": getlzmy['source'], "color": get_random_color()},
"transl": {"value": getlzmy['transl'], "color": get_random_color()},
"get_birthday": {"value": get_birthday(),"color": get_random_color()},
"meeting": {"value": get_count(),"color": get_random_color()},\
}
# if "11:00:00" < nowDate < "14:00:00":
# template_id = template2
# data = {
# "nowDate": {"value": nowDate, "color": get_random_color()},
# "week": {"value": getweek(), "color": get_random_color()},
# "city": {"value": address, "color": get_random_color()},
# "weather": {"value": weather, "color": get_random_color()},
# "kqtype": {"value": suggestion, "color": suggestioncolor()},
# "tem": {"value": temperature + '℃', "color": get_random_color()},
# }
# if "14:00:00" < nowDate < "18:00:00":
# template_id = template3
# data = {
# "nowDate": {"value": nowDate, "color": get_random_color()},
# "week": {"value": getweek(), "color": get_random_color()},
# "city": {"value": address, "color": get_random_color()},
# "weather": {"value": weather, "color": get_random_color()},
# "kqtype": {"value": suggestion, "color": suggestioncolor()},
# "tem": {"value": temperature + '℃', "color": get_random_color()},
# }
# if "18:00:00" < nowDate < "24:00:00":
# template_id = template4
# data = {
# "nowDate": {"value": nowDate, "color": get_random_color()},
# "week": {"value": getweek(), "color": get_random_color()},
# "city": {"value": address, "color": get_random_color()},
# "weather": {"value": weather, "color": get_random_color()},
# "kqtype": {"value": suggestion, "color": suggestioncolor()},
# "tem": {"value": temperature + '℃', "color": get_random_color()},
# }
# 发送自定义参数集,请求微信api进行处理,改成模板形式进行请求
resp = wm.send_template(user_id,template_id,data)
print(resp)
######## 下方是可获取api信息的显示,如有需要可以打开进行查看 #######
# print("当前时间:", currentTime, '-', nowDate, '-', getweek())
# print('位置天气:', address, '-', temperature + '℃', '-', weather, '-', suggestion)
# print('土味情话', getSayLove)
# print('情诗', getqingshi)
# print('早安', getzaoan)
# print('晚安', getwanan)
# print('励志名言', getlzmy['saying'], '-', getlzmy['source'], '-', getlzmy['transl'])
# print('彩虹屁', getcaihongpi)
# print('节假日', getjiejiari['lunarmonth'], '-', getjiejiari['lunarday'], getjiejiari['info'])
# print('one', getone)
# print(weather, '-', gettianqishiju['source'], '-', gettianqishiju['author'], '-', gettianqishiju['content'])