-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
108 lines (90 loc) · 2.51 KB
/
util.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
#encoding:utf-8
import urllib2
import json
import requests
from datetime import datetime, timedelta
from django.conf import settings
APPID = settings.WX_APPID
APPSECRET = settings.WX_APPSECRET
class _AccessToken(str):
def __init__(self):
super(_AccessToken, self).__init__()
self.last_refresh = None
self._token = ''
def __str__(self):
return self.token
__repr__ = __str__
@property
def token(self):
if not self._token:
self._refresh()
elif datetime.now() - self.last_refresh > timedelta(seconds=7130):
self._refresh()
return self._token
def _refresh(self):
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' \
% (APPID, APPSECRET)
self.last_refresh = datetime.now()
response_data = json.loads(urllib2.urlopen(url).read())
try:
self._token = response_data['access_token']
except KeyError:
self._token = ''
ACCESSON_TOKEN = _AccessToken()
menu_data = {
"button": [
{
"name": "",
"sub_button": [
{
"type": "click",
"name": "",
"key": ""
}
]
},
{
"name": "",
"sub_button": [
{
"type": "click",
"name": "",
"key": "DEL",
},
{
"type": "click",
"name": "",
"key": "NORMAL",
},
{
"type": "click",
"name": "",
"key": "",
}
]
},
{
"name": "功能",
"sub_button": [
{
"type": "click",
"name": "",
"key": "",
},
{
"type": "click",
"name": "",
"key": "",
},
{
"type": "click",
"name": "",
"key": "",
}
]
}]
}
def update_menu():
menu_data = data
requests.post('https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s' % ACCESSON_TOKEN,
data=json.dumps(menu_data, ensure_ascii=False, encoding='utf-8'))