-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWX.py
120 lines (106 loc) · 4.57 KB
/
WX.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
# Copyright (C) 2014 SZDIY Hackers' Community
#
# This file is part of szdiyCam.
#
# szdiyCam is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# szdiyCam is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with szdiyCam. If not, see <http://www.gnu.org/licenses/>.
import requests
import datetime
from credentials import wxAppID, wxAppSecret
urlEndPoint_AccessToken = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+wxAppID+'&secret='+wxAppSecret
class WX:
def __init__(self):
self.currentAccessToken = self.__getNewAccessToken() #at initialization, get a new access token
self.lastUploadTime = datetime.datetime.now() #keeps track of last upload time
self.lastMedia_id = -1 #keeps track of last successful upload media_id
self.lastCreated_at = -1 #keeps track of last successful upload created_at
def __getNewAccessToken(self):
try:
resp = requests.get(urlEndPoint_AccessToken)
respJSON = resp.json()
if 'access_token' in respJSON:
print 'access_token: {}'.format(respJSON['access_token'])
print 'expires in: {} sec'.format(respJSON['expires_in'])
return respJSON['access_token']
else:
print respJSON
except:
print "get new access token network error"
#refresh access token upon expire
def refreshAccessToken(self):
self.currentAccessToken = self.__getNewAccessToken()
"""
upload a file to weixin, private function
return (media_id, created_at)
return (-1,-1) upon exception
"""
def __uploadToWx(self, image, directory):
files={'files': open(directory+'/'+image, 'rb')}
try:
r= requests.post(self.__getUrlEndpointForUpload(),files=files)
# print r.json()
if 'media_id' in r.json() and 'created_at' in r.json():
print 'media_id: {} | created_at: {}'.format(r.json()['media_id'], r.json()['created_at'])
return (r.json()['media_id'], r.json()['created_at'])
elif 'errcode' in r.json():
if r.json()['errcode'] in (40001, 40014, 41001, 42001, 42002, 40030, 41003): #those error token indicate expire of access token
print ('old access token no longer work, get new access token')
self.refreshAccessToken()
print ('retry uploading')
return self.__uploadToWx(image,directory) #retry if access token is expired
else: #handling unknown error code
print 'error code: {} | {}'.format(r.json()['errcode'],r.json()['errmsg'])
raise ValueError
else:
print "unknown error"
raise ValueError
except ValueError:
return (-1,-1)
except:
print "upload network error"
return (-1,-1)
"""
upload a file to weixin with api rate limiting
return (media_id, created_at)
return last successful media_id and created_at on error
:type numbersPerDay: int
:param numbersPerDay: limit api call to x times per day
:type image: str
:param image: image name such as new.jpg
:type directory: str
:param directory: directory location such as /tmp
"""
def uploadToWxWithAPICallLimit(self, numbersPerDay, image, directory):
try:
oncePerHowManySeconds = 24*3600/numbersPerDay #calcuate the execution frequency 'once per x seconds'
except:
print "numbersPerDay cannot be 0, set to default upload"
media_id,created_at = self.__uploadToWx(image,directory)
self.lastMedia_id = media_id
self.lastCreated_at = created_at
return (media_id, created_at)
currentTime = datetime.datetime.now()
pastTime = self.lastUploadTime
timeDiff = (currentTime - pastTime).total_seconds()
if timeDiff > oncePerHowManySeconds:
media_id,created_at = self.__uploadToWx(image,directory)
self.lastMedia_id = media_id
self.lastCreated_at = created_at
self.lastUploadTime = currentTime #update upload time
else:
print 'last upload to WX happened {} seconds ago, less than {} seconds required to meet the API limit for {} times per day'.format(timeDiff, oncePerHowManySeconds, numbersPerDay)
print 'media_id: {} | created_at: {}'.format(self.lastMedia_id, self.lastCreated_at)
return (self.lastMedia_id, self.lastCreated_at)
#weixin need to inject current access token into its upload url address
def __getUrlEndpointForUpload(self):
return 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token='+self.currentAccessToken+'&type=image'