-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjpush.py
171 lines (144 loc) · 6.62 KB
/
jpush.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
#-*- coding: utf8 -*-
# A lightweight wrapper for remote Jpush API
# Details refer to http://docs.jpush.cn/pages/viewpage.action?pageId=557084
import hashlib
import urllib
import urllib2
import json
class JpushClient:
def __init__(self, username, password, callback_url=""):
self.username = username
self.password = password
self.callback_url = callback_url
self.url = "http://api.jpush.cn:8800/sendmsg/sendmsg"
def build_params(self, kargs):
params = {}
params["username"] = self.username
params["callback_url"] = self.callback_url
params["sendno"] = kargs["sendno"]
params["appkeys"] = kargs["appkeys"]
params["receiver_type"] = kargs["receiver_type"]
params["receiver_value"] = kargs["receiver_value"]
params["verification_code"] = kargs["verification_code"]
params["msg_type"] = kargs["msg_type"]
params["msg_content"] = json.dumps(kargs["msg_content"])
params["send_description"] = kargs["senddes"]
params["platform"] = kargs["device"]
return params
def build_content(self, title, content, type):
if type == 1: # notification
return {"n_title": title, "n_content": content}
else: # custom message
return {"title": title, "message": content}
def generate_verification_code(self, sendno, receiver_type, receiver_value):
m = hashlib.md5()
m2 = hashlib.md5()
m.update(self.password)
input = "%s%d%d%s%s" % (self.username, sendno, receiver_type, receiver_value, m.hexdigest().upper())
m2.update(input)
return m2.hexdigest().upper()
def send_notification_with_imei(self, imei, appkeys, sendno,
senddes, msgtitle,
msg_content, device):
'''Send notification with imei id'''
receiver_type = 1
msg_type = 1
msg_content = self.build_content(msgtitle, msg_content, msg_type)
receiver_value = imei
verification_code = self.generate_verification_code(sendno, receiver_type, receiver_value)
params = self.build_params(locals())
self.send_msg(params)
def send_custom_msg_with_imei(self, imei, appkeys, sendno,
senddes, msgtitle,
msg_content, device):
'''Send custom message with imei id'''
receiver_type = 1
msg_type = 2
msg_content = self.build_content(msgtitle, msg_content, msg_type)
receiver_value = imei
verification_code = self.generate_verification_code(sendno, receiver_type, receiver_value)
params = self.build_params(locals())
self.send_msg(params)
def send_notification_with_tag(self, tag, appkeys, sendno,
senddes, msgtitle,
msg_content, device):
'''Send notification with tag'''
receiver_type = 2
msg_type = 1
msg_content = self.build_content(msgtitle, msg_content, msg_type)
receiver_value = tag
verification_code = self.generate_verification_code(sendno, receiver_type, receiver_value)
params = self.build_params(locals())
self.send_msg(params)
def send_custom_msg_with_tag(self, tag, appkeys, sendno,
senddes, msgtitle,
msg_content, device):
'''Send custom message with tag'''
receiver_type = 2
msg_type = 2
msg_content = self.build_content(msgtitle, msg_content, msg_type)
receiver_value = tag
verification_code = self.generate_verification_code(sendno, receiver_type, receiver_value)
params = self.build_params(locals())
self.send_msg(params)
def send_notification_with_alias(self, alias, appkeys, sendno,
senddes, msgtitle,
msg_content, device):
'''Send notification with alias'''
receiver_type = 3
msg_type = 1
msg_content = self.build_content(msgtitle, msg_content, msg_type)
receiver_value = alias
verification_code = self.generate_verification_code(sendno, receiver_type, receiver_value)
params = self.build_params(locals())
self.send_msg(params)
def send_custom_msg_with_alias(self, alias, appkeys, sendno,
senddes, msgtitle,
msg_content, device):
'''Send custom message with alias'''
receiver_type = 3
msg_type = 2
msg_content = self.build_content(msgtitle, msg_content, msg_type)
receiver_value = alias
verification_code = self.generate_verification_code(sendno, receiver_type, receiver_value)
params = self.build_params(locals())
self.send_msg(params)
def send_notification_with_appkey(self, appkeys, sendno,
senddes, msgtitle,
msg_content, device):
'''Send notification with appkey'''
receiver_type = 4
msg_type = 1
msg_content = self.build_content(msgtitle, msg_content, msg_type)
receiver_value = ""
verification_code = self.generate_verification_code(sendno, receiver_type, receiver_value)
params = self.build_params(locals())
self.send_msg(params)
def send_custom_msg_with_appkey(self, appkeys, sendno,
senddes, msgtitle,
msg_content, device):
'''Send custom message with appkey'''
receiver_type = 4
msg_type = 2
msg_content = self.build_content(msgtitle, msg_content, msg_type)
receiver_value = ""
verification_code = self.generate_verification_code(sendno, receiver_type, receiver_value)
params = self.build_params(locals())
self.send_msg(params)
def send_msg(self, params):
'''Push API for all kinds of message and notification, dict params restore all parameters'''
try:
f = urllib2.urlopen(
data = urllib.urlencode(params),
url = self.url
)
except:
print "网络无法访问"
else:
result = json.loads(f.read())
if result.get("errcode") == 0:
print u"发送成功, sendNo=%s" % result.get("sendno")
else:
print u"发送失败,错误代码=%s, 错误消息=%s" % (result.get("errcode"), result.get("errmsg"))
finally:
f.close()