-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.py
94 lines (89 loc) · 3.01 KB
/
handle.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
'''
@Description: In User Settings Edit
@Author: your name
@Date: 2019-09-09 12:39:49
@LastEditTime: 2019-09-20 14:22:30
@LastEditors: Please set LastEditors
'''
import requests
import json
import time
class Handel():
def __init__(self,gaodeApi,city_key):
self.access_token = self.get_access_token()
self.url = gaodeApi
self.cityKey = city_key
def get_access_token(self):
url = 'https://api.weixin.qq.com/cgi-bin/token'
data = {
'grant_type': 'client_credential',
'appid': 'wx7541c0c21aa8be11',
'secret': '75b65bcfbe9866703a326105e6c0bea0'
}
ret = requests.get(url, data)
token_data = (json.loads(ret.text))
self.access_token = token_data.get('access_token')
print('获取token成功')
def get_user(self):
self.get_access_token()
url = 'https://api.weixin.qq.com/cgi-bin/user/get'
param = {
'access_token':self.access_token,
'next_openid':'' # 第一个拉取的OPENID,不填默认从头开始拉取
}
ret= requests.get(url, param)
users = json.loads(ret.text)['data'].get('openid') ##list
print('获取用户列表完成')
return users
def getWeather(self):
ret = requests.get(self.url,self.cityKey)
if ret.status_code ==200:
weathers = json.loads(ret.text)
print('获取天气成功')
else:
print('获取天气失败')
return weathers['lives'][0]
def send(self,openid,weather):
url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='+self.access_token
date = time.strftime("%Y-%m-%d", time.localtime())
#天气模板
data = {
"touser": openid,
"template_id": "u6j9_lD98uyjzbuWDQs1DHzWcrXaFGpHIHgNxhTKRDU",
"url": "https://lb0629.gitee.io",
"data": {
"date": {
"value": date,
"color": "#173177"
},
"temperature": {
"value": weather['temperature'],
"color": "#173177"
},
"location": {
"value": weather['province']+' '+weather['city'],
"color": "#173177"
},
"weather": {
"value": weather['weather'],
"color": "#173177"
},
"reporttime": {
"value": weather['reporttime'],
"color": "#173177"
},
"windpower": {
"value": weather['windpower'],
"color": "#173177"
},
}
}
ret = requests.post(url,json=data)
if ret.status_code==200:
data = json.loads(ret.text)
if data['errcode'] ==0:
return True
else:
return False
else:
return False