forked from hustuhao/FunnyCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_love_msg.py
executable file
·271 lines (250 loc) · 8.37 KB
/
send_love_msg.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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from calendar import month
from cgi import print_form
from copyreg import pickle
from datetime import datetime
from errno import E2BIG
from time import time
import pytz
import requests
import json
from urllib3.exceptions import *
ali_weather_appcode = "xxxxxxxxxx" # 阿里天气 https://market.aliyun.com/products/57126001/cmapi014302.html#sku=yuncode830200000
def getMsgHeader():
tz = pytz.timezone('Asia/Shanghai')
dt = datetime.now(tz)
h = "今天是 <font color=\"info\">{}</font>".format(dt.strftime('%Y-%m-%d %A'))
return h
def getMsgHeaderToWechat():
tz = pytz.timezone('Asia/Shanghai')
dt = datetime.now(tz)
h = "今天是 <font color=\"#87CEEB\">{}</font>".format(dt.strftime('%Y-%m-%d %A'))
return h
class Aqiinfo:
level = ''
color = ''
affect = ''
measure = ''
# def __init__(self, level, color, affect, measure):
# self.level = level
# self.color = color
# self.affect = affect
# self.measure = measure
class Aqi:
quality = ''
aqiinfo = Aqiinfo()
class Weather:
city = ''
date = ''
weather = ''
temphigh = ''
templow = ''
windspeed = ''
winddirect = ''
windpower = ''
aqi = ''
def isValide(self) -> bool:
if self.city != '':
return True
return False
def jsonDecode(self, jsonTex):
self.city = jsonTex['city']
self.date = jsonTex['date']
self.weather = jsonTex['weather']
self.temphigh = jsonTex['temphigh']
self.templow = jsonTex['templow']
self.windspeed = jsonTex['windspeed']
self.winddirect = jsonTex['winddirect']
self.windpower = jsonTex['windpower']
aqi = Aqi()
aqi.quality = jsonTex['aqi']['quality']
self.aqi = aqi
aqiinfo = Aqiinfo()
aqiinfo.level = jsonTex['aqi']['quality']
aqiinfo.color = jsonTex['aqi']['aqiinfo']['color']
aqiinfo.affect = jsonTex['aqi']['aqiinfo']['affect']
aqiinfo.measure = jsonTex['aqi']['aqiinfo']['measure']
aqi.aqiinfo = aqiinfo
def getWeatherTextToWechatWork(self):
tex = "武汉天气\n > <font color=\"info\">{}</font>, 温度: <font color=\"info\">{}</font> ~ <font color=\"info\">{}</font>\n{}-{}-风速{},空气质量:<font color=\"info\">{}</font>,{}。".format(
self.weather,
self.templow,
self.temphigh,
self.windpower,
self.winddirect,
self.windspeed,
self.aqi.quality,
self.aqi.aqiinfo.affect,
self.aqi.aqiinfo.measure
)
return tex
def getWeatherTextToWechat(self):
tex = "<hr>武汉天气 <br> <font color=\"green\">{}</font>, 温度: <font color=\"green\">{}</font> ~ <font color=\"green\">{}</font>, 空气质量:<font color=\"green\">{}</font>,{}。".format(
self.weather,
self.templow,
self.temphigh,
self.aqi.quality,
self.aqi.aqiinfo.affect,
self.aqi.aqiinfo.measure
)
return tex
def getWeather() -> Weather:
url = 'https://jisutqybmf.market.alicloudapi.com/weather/query?city=武汉'
headers={'Authorization': 'APPCODE'+ ' '+ ali_weather_appcode}
r = requests.get(url, headers=headers)
r.encoding = 'utf-8'
msg = r.json()['msg']
w = Weather()
if msg != 'ok':
return w
result = r.json()['result']
w.jsonDecode(result)
return w
def getMeetingDay():
# 2019-10-23 00:00:00 1571760000
unixTimeStamp = 1571760000
tz = pytz.timezone('Asia/Shanghai')
now = datetime.now(tz)
day = int((now.timestamp() - unixTimeStamp) / (24 * 60 * 60))
print(day)
print('相遇的:',day)
return day
def getBirthDayOfMa():
tz = pytz.timezone('Asia/Shanghai')
now = datetime.now(tz)
dt = datetime(now.year, now.month, now.day)
birthday = datetime(now.year, 9, 13)
day = int((birthday.timestamp() -dt.timestamp()) / (24*60*60))
print('生日:',day)
return day
def getExpressLoveDay():
# 2020-09-04 00:00:00 1599148800
unixTimeStamp = 1599148800
tz = pytz.timezone('Asia/Shanghai')
now = datetime.now(tz)
day = int((now.timestamp() - unixTimeStamp) / (24 * 60 * 60))
print(day)
print('相爱的天:',day)
return day
class DailyWord:
sid = ''
note = ''
content = ''
pic = ''
def __init__(self) -> None:
pass
def isValide(self) -> bool:
if self.sid != "":
return True
return False
def getDailyWordHtml(self) -> str:
return "<br>每日一句<br>{}<br>{}<br><img src=\"{}\" align=\"center\">".format(self.content, self.note, self.pic)
def getDailyWord() -> DailyWord:
url = "http://open.iciba.com/dsapi"
r = requests.get(url)
r.encoding = 'utf-8'
result = r.json()
dw = DailyWord()
if result.get('sid'):
sid = result['sid']
n = result['note']
c = result['content']
pic = result['fenxiang_img']
dw.sid = sid
dw.note = n
dw.content = c
dw.pic = pic
return dw
def sendDailyWordToWechatWork(dw: DailyWord):
if dw.isValide:
webhook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxx"
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
message = {
"msgtype": "news",
"news": {
"articles":[{
"title": "每日一句",
"description": dw.content,
"url": dw.pic,
"picurl": dw.pic
}]
}
}
message_json = json.dumps(message)
info = requests.post(url=webhook,data=message_json,headers=header)
return
# @mdTex 企业微信支持的 markdown 格式文字
def sendAlarmMsg(mdTex):
wechatwork(mdTex)
# 企业微信推送
def wechatwork(tex):
webhook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxx"
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
message = {
"msgtype": "markdown",
"markdown": {
"content": tex
}
}
print(message)
message_json = json.dumps(message)
try:
info = requests.post(url=webhook,data=message_json,headers=header)
except (NewConnectionError, MaxRetryError, ConnectTimeoutError) as e:
print("unable to connect to wechat server, err:", e)
except Exception as e2:
m = print("send message to wechat server, err:", e2)
sendAlarmMsg(m)
# 微信推送
def wxPusher(tex):
url = "http://wxpusher.zjiecode.com/api/send/message"
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
message = {
"appToken":"xxxxxxxxxxxxxxxxx",
"content": tex,
"summary":"相爱一生",
"contentType":2,
"topicIds":[6666],
"url":"http://wxpusher.zjiecode.com"
}
message_json = json.dumps(message)
try:
info = requests.post(url=url,data=message_json, headers=header)
print(info.text)
except (NewConnectionError, MaxRetryError, ConnectTimeoutError) as e:
m = print("unable to connect to wx, err:", e)
sendAlarmMsg(m)
except Exception as e:
m = print("send message to wx , err:", e)
sendAlarmMsg(m)
if __name__=="__main__":
# 今天是我们相爱的{}天
# 我们已经相遇{}
# 距离你的生日还{}天
h = getMsgHeader()
w = getWeather()
bd = getBirthDayOfMa()
md = getMeetingDay()
ed = getExpressLoveDay()
dw = getDailyWord()
# 企业微信
w1 = w.getWeatherTextToWechatWork()
tex1 = "{}\n> 今天是我们相爱的<font color=\"warning\"> {} </font>天\n我们已经相遇<font color=\"warning\"> {} </font>天\n距离你的生日还有<font color=\"warning\"> {} </font>天\n\n{}".format(h,ed, md, bd, w1)
wechatwork(tex1)
sendDailyWordToWechatWork(dw)
# wxpusher
h2 = getMsgHeaderToWechat()
w2 = w.getWeatherTextToWechat()
dw2 = dw.getDailyWordHtml()
tex2 = "{}<br> 今天是我们相爱的<font color=\"#F5BCA9\"> {} </font>天<br>我们已经相遇<font color=\"#F5BCA9\"> {} </font>天<br>距离你的生日还有<font color=\"#F5BCA9\"> {} </font>天<br><br>{}<hr>{}".format(h2,ed, md, bd, w2, dw2)
wxPusher(tex2)