Python定时发送信息到微信

1.想要一个定时提醒天气情况的场景,就想到了用python,这个是基于wxpy 来实现的

注意点:

1
2
3
需要安装对应插件包
python2 和 python3 有引入包不一样注意环境
想在服务器一直运行 可以参考 tmux

代码如下:

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
# -*- coding: utf-8 -*
import time
import random
import requests
from wxpy import *
import socket
import http
import json
import urlparse
#from urllib.parse import urlencode
import datetime
import urllib
from threading import Timer
from datetime import datetime

import sys
reload(sys)
sys.setdefaultencoding('utf8')

def get_everydayWords():
url = 'http://open.iciba.com/dsapi/'
r = requests.get(url)
content = json.loads(r.text)
return '每日一句:\n'+content['content'] +'\n'+content['note'] + "\n"

def get_huangli():
data = {}
data["appkey"] = '' //填写自己申请的key 地址:jisuapi.com
data["year"] = datetime.now().year
data["month"] = datetime.now().month
data["day"] = datetime.now().day
url_values =urllib.urlencode(data)
url = "http://api.jisuapi.com/huangli/date" + "?" + url_values
r = requests.get(url)
jsonarr = json.loads(r.text)
if jsonarr["status"] != u"0":
print(jsonarr["msg"])
return "今日无黄历信息"
result = jsonarr["result"]
xingqi = '星期' + result['week']
yangli = '阳历日期:' + result['yangli']
nongli = '农历日期:' + result['nongli']
wuxing = '今日五行:' + result['wuxing']
chong ='今日注意的生肖:' + result["chong"]
suici = '岁次:' + ','.join(result['suici'])
star = '本月星座:' + result['star']
shengxiao = '今年生肖:' + result['shengxiao']
yi = '宜:' + ','.join(result['yi'])
ji = '忌:' + ','.join(result['ji'])
return xingqi + '黄历:' + '\n' + '\n' + yangli + '\n' + nongli + '\n' + wuxing + '\n' + chong + "\n" + suici + "\n" + star + "\n" + shengxiao + "\n" + yi + "\n" + ji + "\n"

def get_html(url, data=None):
# 模拟浏览器来获取网页的html代码
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}
# 设定超时时间,防止被网站认为是爬虫
timeout = random.choice(range(80, 180))
while True:
try:
rep = requests.get(url, headers=header, timeout=timeout)
rep.encoding = "utf-8"
if rep.text[2] != 's': # 该api调用时有失败的可能,一个简单的判断调用是否成功
break
except timeout as e:
print("3:", e)
time.sleep(random.choice(range(8, 15)))
except socket.error as e:
print("4:", e)
time.sleep(random.choice(range(20, 60)))
except http.client.BadStatusLine as e:
print("5:", e)
time.sleep(random.choice(range(30, 80)))

except http.client.IncompleteRead as e:
print("6:", e)
time.sleep(random.choice(range(5, 15)))

except:
print ('获取内容失败')
time.sleep(10)

result = ''
temp = rep.json()
result = rep.json()
nowTime=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
result = result['data']
ondeday = result['yesterday']

result2 = '今日'+ ondeday['date'] + '\n' \
+ '温度:'+ ondeday['low'] + ' --- ' + ondeday['high'] + '\n' \
+ '日出:' + ondeday['sunrise'] + ' --- ' + '日落:' + ondeday['sunset'] + '\n' \
+ '风向:' + ondeday['fx'] + ' --- ' + ondeday['fl'] + '\n' \
+ '天气:' + ondeday['type'] + '\n' \
+ '提示:' + ondeday['notice'] + '\n' \
+ '湿度:' + result['shidu'] + '\n' \
+ '空气质量:' + result['quality'] + '\n' \
+ 'pm25:' + str(result['pm25']) + '\n' \
+ '------------------------------' + '\n'
for item in result['forecast']:
result2 = result2 + '日期:'+item['date'] + '\n' \
+ '风力:' + item['fl'] + '\n' \
+ '风向:' + item['fx'] + '\n' \
+ '最高温:' + item['high'] + '\n' \
+ '最低温:' + item['low'] + '\n' \
+ '温馨提醒:' + item['notice'] + '\n' \
+ '日出时间:' + item['sunrise'] + '\n' \
+ '日落时间:' + item['sunset'] + '\n' \
+ '天气:' + item['type'] + '\n' \
+ '------------------------------' + '\n' \

result2 = result2 + '发送时间:' + nowTime + '\n'
return result2

def get_now(city, data=None):
# 模拟浏览器来获取网页的html代码
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}

url = 'https://free-api.heweather.com/s6/weather/now?location='+city+'&key=614d3a7941034ba48088f689788e789f'

# 设定超时时间,防止被网站认为是爬虫
timeout = random.choice(range(80, 180))
while True:
try:
rep = requests.get(url, headers=header, timeout=timeout)
rep.encoding = "utf-8"
if rep.text[2] != 's': # 该api调用时有失败的可能,一个简单的判断调用是否成功
break
except timeout as e:
print("3:", e)
time.sleep(random.choice(range(8, 15)))
except socket.error as e:
print("4:", e)
time.sleep(random.choice(range(20, 60)))
except http.client.BadStatusLine as e:
print("5:", e)
time.sleep(random.choice(range(30, 80)))

except http.client.IncompleteRead as e:
print("6:", e)
time.sleep(random.choice(range(5, 15)))

except:
print ('获取内容失败')
time.sleep(10)

result = ''
temp = rep.json()
temp = temp['HeWeather6'][0]
update = temp['update']
now = temp['now']
nowTime=datetime.now().strftime('%Y-%m-%d %H:%M:%S')

result = '实时天气预报---' + '更新时间:'+ update['loc'] + '\n'+'\n'\
+ '当前天气:'+ now['cond_txt'] + '\n'\
+ '当前温度:'+ now['tmp'] + '°C' + '\n'\
+ '体感温度:'+ now['fl'] + '°C' + '\n'\
+ '风向:'+ now['wind_dir'] + ' ' + now['wind_sc'] + '级 '+ now['wind_spd'] + '公里/小时'+ '\n'\
+ '相对湿度:'+ now['hum'] + '%' + '\n'\
+ '降水量:'+ now['pcpn'] + 'ml' + '\n'\
+ '能见度:'+ now['vis'] + '公里' + '\n'\
+ '云量:'+ now['cloud'] + '\n'

result = result + '发送时间:' + nowTime
return result

def auto_send(): # unix时间戳设定为每天早上7:30分自动发送消息
while True:
time_now = int(time.time())
now = datetime.now()
now_str = now.strftime('%Y/%m/%d %H:%M:%S')[11:]

t = Timer(1,auto_send)
# t.start()
print(now_str)
#发送全部 7点24 发送 每天早上
if now_str in ['07:24:00']:
for i in ['苏州']:
url = 'https://www.sojson.com/open/api/weather/json.shtml?city=%s' %i
weather = get_html(url)
word = get_everydayWords()
huangli = get_huangli()
# 选择对象发送消息
bot.file_helper.send(weather)
print(bot.groups().search(u''))
my_friend = bot.friends().search(u'Lie')[0]
my_friend2 = bot.friends().search(u'cc')[0]
# my_group = bot.groups().search('Test')[0]
my_friend.send(word + weather + huangli)
my_friend2.send(word + weather + huangli)
my_group.send(word + weather + huangli)

if now_str in ['09:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','16:08:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00',]:
city = '苏州'

weather = get_now(url)

word = get_everydayWords()
huangli = get_huangli()

# 选择对象发送消息
bot.file_helper.send(weather) // 这是发送给登陆微信的微信助手
print(bot.groups().search(u''))
my_friend = bot.friends().search(u'Lie')[0] // 好友
my_friend2 = bot.friends().search(u'cc')[0]
my_group = bot.groups().search('Test')[0] // 群组
my_friend.send(weather)
my_friend2.send(weather)
my_group.send(weather)


if __name__ == "__main__":
tuling = Tuling(api_key='') //填写自己的图灵机器人key
bot = Bot(cache_path=True,console_qr = True) // 添加缓存,不用每次都扫码登陆,如果是linux 则显示二维码,需安装对应插件包
myself = bot.self
bot.enable_puid('wxpy_puid.pkl')

@bot.register(msg_types=TEXT) // 图灵机器人调用
def auto_reply_all(msg):
tuling.do_reply(msg)

auto_send()

bot.join()