Commit 77f980fc authored by hupantingxue's avatar hupantingxue

JPush API 3.0 python client initial code;

parent 773d737c
APP_KEY = 'xxxxx'
MASTER_SECRET = 'xxxxxx'
...@@ -3,42 +3,13 @@ ...@@ -3,42 +3,13 @@
import time import time
from jpush import JPushClient from jpush import JPushClient
import conf
sendno = int(time.time()) sendno = int(time.time())
app_key = 'appkey' app_key = conf.APP_KEY
master_secret = 'master_secret' master_secret = conf.MASTER_SECRET
jpush_client = JPushClient(master_secret) jpush_client = JPushClient(master_secret)
# Send message by tag # Send message
jpush_client.send_notification_by_tag('tagapi', app_key, sendno, 'des', jpush_client.send_msg(msg_json)
'tag notify title',
'tag notify content', 'android')
jpush_client.send_custom_msg_by_tag('tagapi', app_key, sendno, 'des',
'tag msg title',
'tag msg content', 'android')
# Send message by alias
jpush_client.send_notification_by_alias('aliasapi', app_key, sendno, 'des',
'alias notify title',
'alias notify content', 'android')
jpush_client.send_custom_msg_by_alias('aliasapi', app_key, sendno, 'des',
'alais msg title',
'alias msg content', 'android')
# Send message by app_key
jpush_client.send_notification_by_appkey(app_key, sendno, 'des',
'appkey notify title',
'appkey notify content', 'android')
jpush_client.send_custom_msg_by_appkey(app_key, sendno, 'des',
'appkey msg title',
'appkey msg content', 'android')
# Send message by registration_id
jpush_client.send_notification_by_registrationid('regidapi', app_key, sendno, 'des',
'registration id notify title',
'registration id notify content', 'android')
jpush_client.send_custom_msg_by_registrationid('regidapi', app_key, sendno, 'des',
'registration_id msg title',
'registration_id msg content', 'android')
from .push import JPushClient from .push import JPushClient
from .recv import RecvClient from .recv import RecvClient
__version__ = '2.0.2' __version__ = '3.0.0'
VERSION = tuple(map(int, __version__.split('.'))) VERSION = tuple(map(int, __version__.split('.')))
__all__ = ['JPushClient', 'RecvClient'] __all__ = ['JPushClient', 'RecvClient']
...@@ -3,189 +3,39 @@ ...@@ -3,189 +3,39 @@
# Push API # Push API
# #
import base64
import json import json
import hashlib import hashlib
import urllib import urllib
import urllib2 import urllib2
API_URL = "http://api.jpush.cn:8800/v2/push" API_URL = "https://api.jpush.cn/v3/push"
KEY_PARAMS = [
"sendno",
"app_key",
"receiver_type",
"receiver_value",
"verification_code",
"msg_type",
"msg_content",
"send_description",
"platform",
]
class JPushClient: class JPushClient:
"""JPush Python Client Class""" """JPush Python Client Class"""
def __init__(self, master_secret, callback_url=""): def __init__(self, app_key, master_secret):
self.app_key = app_key
self.master_secret = master_secret self.master_secret = master_secret
self.callback_url = callback_url
def _gen_params(self, kargs):
"""Generate Post params"""
params = {}
params["callback_url"] = self.callback_url
for k in KEY_PARAMS:
params[k] = kargs.get(k)
return params
def _gen_content(self, msg_title, msg_content, msg_type, extras):
"""Generate message content"""
content = {}
if msg_type == 1: # notification
content["n_title"] = msg_title
content["n_content"] = msg_content
content["n_extras"] = extras
else: # custom message
content["title"] = msg_title
content["message"] = msg_content
content["extras"] = extras
return json.dumps(content, separators=(',', ':'))
def _gen_verification_code(self, sendno, receiver_type, receiver_value):
"""Generage verification code"""
mobj = hashlib.md5()
verification_str = "%d%s%s%s" % (sendno, receiver_type,
receiver_value, self.master_secret)
mobj.update(verification_str)
return mobj.hexdigest().upper()
def _send_msg(self, params): def _send_msg(self, params):
'''Push API for all kinds of message and notification, '''Push API for all kinds of message and notification,
dict params restore all parameters''' dict params restore all parameters'''
try: try:
api_post = urllib2.urlopen(data=urllib.urlencode(params), send_param=json.loads(params)
url=API_URL, timeout=5) base64string = base64.encodestring('%s:%s' % (self.app_key, self.master_secret))[:-1]
req = urllib2.Request(API_URL)
req.add_header("Authorization", "Basic %s" % base64string)
api_post = urllib2.urlopen(req, urllib.urlencode(send_param), timeout=5)
print api_post.read() print api_post.read()
except Exception, e: except ValueError as e:
print e, e.read() print 'params should be json format ', e
except Exception as e:
#Deprecated" print 'send message fail ', e
def send_notification_by_imei(self, imei, app_key, sendno, senddes,
msg_title, msg_content, platform, extras={}): def send_msg(self, params):
"""Send notification by imei""" '''Push API for message send.
pass params must be json-format string;'''
try:
#Deprecated"
def send_custom_msg_by_imei(self, imei, app_key, sendno, senddes,
msg_title, msg_content, platform, extras={}):
"""Send notification by imei"""
pass
def send_notification_by_tag(self, tag, app_key, sendno, senddes, msgtitle,
msg_content, platform, extras={}):
'''Send notification by tag'''
receiver_type = 2
msg_type = 1
msg_content = self._gen_content(msgtitle, msg_content,
msg_type, extras)
receiver_value = tag
verification_code = self._gen_verification_code(sendno,
receiver_type,
receiver_value)
params = self._gen_params(locals())
self._send_msg(params)
def send_custom_msg_by_tag(self, tag, app_key, sendno, senddes,
msgtitle, msg_content,
platform, extras={}):
'''Send custom message by tag'''
receiver_type = 2
msg_type = 2
msg_content = self._gen_content(msgtitle, msg_content,
msg_type, extras)
receiver_value = tag
verification_code = self._gen_verification_code(sendno,
receiver_type,
receiver_value)
params = self._gen_params(locals())
self._send_msg(params)
def send_notification_by_alias(self, alias, app_key, sendno, senddes,
msgtitle, msg_content, platform, extras={}):
'''Send notification by alias'''
receiver_type = 3
msg_type = 1
msg_content = self._gen_content(msgtitle, msg_content,
msg_type, extras)
receiver_value = alias
verification_code = self._gen_verification_code(sendno,
receiver_type,
receiver_value)
params = self._gen_params(locals())
self._send_msg(params)
def send_custom_msg_by_alias(self, alias, app_key, sendno, senddes,
msgtitle, msg_content, platform, extras={}):
'''Send custom message by alias'''
receiver_type = 3
msg_type = 2
msg_content = self._gen_content(msgtitle, msg_content,
msg_type, extras)
receiver_value = alias
verification_code = self._gen_verification_code(sendno, receiver_type,
receiver_value)
params = self._gen_params(locals())
self._send_msg(params)
def send_notification_by_appkey(self, app_key, sendno, senddes, msgtitle,
msg_content, platform, extras={}):
'''Send notification by appkey'''
receiver_type = 4
msg_type = 1
msg_content = self._gen_content(msgtitle, msg_content,
msg_type, extras)
receiver_value = ""
verification_code = self._gen_verification_code(sendno, receiver_type,
receiver_value)
params = self._gen_params(locals())
self._send_msg(params)
def send_custom_msg_by_appkey(self, app_key, sendno, senddes, msgtitle,
msg_content, platform, extras={}):
'''Send custom message by appkey'''
receiver_type = 4
msg_type = 2
msg_content = self._gen_content(msgtitle, msg_content,
msg_type, extras)
receiver_value = ""
verification_code = self._gen_verification_code(sendno, receiver_type,
receiver_value)
params = self._gen_params(locals())
self._send_msg(params)
def send_notification_by_registrationid(self, registrationid, app_key, sendno, senddes, msgtitle,
msg_content, platform, extras={}):
'''Send notification by registraion id'''
receiver_type = 5
msg_type = 1
msg_content = self._gen_content(msgtitle, msg_content,
msg_type, extras)
receiver_value = registrationid
verification_code = self._gen_verification_code(sendno,
receiver_type,
receiver_value)
params = self._gen_params(locals())
self._send_msg(params)
def send_custom_msg_by_registrationid(self, registrationid, app_key, sendno, senddes,
msgtitle, msg_content,
platform, extras={}):
'''Send custom message by registration id'''
receiver_type = 5
msg_type = 2
msg_content = self._gen_content(msgtitle, msg_content,
msg_type, extras)
receiver_value = registrationid
verification_code = self._gen_verification_code(sendno,
receiver_type,
receiver_value)
params = self._gen_params(locals())
self._send_msg(params) self._send_msg(params)
except Exception as e:
print e
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
'''Test sending msg by registration id''' '''Test sending msg'''
import time import time
import sys
sys.path.append("..")
from jpush import JPushClient from jpush import JPushClient
sendno = int(time.time()) sendno = int(time.time())
app_key = 'dd1066407b044738b6479275' app_key = 'dd1066407b044738b6479275'
master_secret = '2b38ce69b1de2a7fa95706ea' master_secret = '2b38ce69b1de2a7fa95706ea'
msg_json = '''{"platform":"all","audience":"all", "notification":{"android":{"alert":"Pall Nandroid alert"}},"options":{"time_to_live":60, "sendno":%d}}''' %(sendno)
jpush_client = JPushClient(master_secret) jpush_client = JPushClient(app_key, master_secret)
reg_id = '0900e8d85ef' jpush_client.send_msg(msg_json)
jpush_client.send_notification_by_registrationid(reg_id, app_key, sendno, 'des',
'registration id notify title',
'registration id notify content', 'android')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment