Commit 020b2d61 authored by linbo's avatar linbo

JPush Python Client Version 0.1.0

parents
*.pyc
build/
dist/
jpush.egg-info/
===================
JPush Python Client
===================
A Python client for the JPush REST APIs
------------
Installation
------------
To install jpush-api-python-client, simply:
.. code-block:: sh
$ sudo pip install jpush
or alternatively install via easy_install:
.. code-block:: sh
$ sudo easy_install jpush
or from source:
.. code-block:: sh
$ sudo python setup.py install
--------
Examples
--------
Detailes refer to `examples <https://github.com/jpush/jpush-api-python-client/blob/master/examples>`_
# -*- coding: utf-8 -*-
import time
from jpush import JPushClient
sendno = int(time.time())
username = 'username'
app_key = 'appkey'
master_secret = 'master_secret'
jpush_client = JPushClient(username, master_secret)
# Send message by tag
jpush_client.send_notification_by_tag('tagapi', app_key, sendno, 'des',
'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')
from .client import JPushClient
__version__ = '0.1.0'
VERSION = tuple(map(int, __version__.split('.')))
__all__ = ['JPushClient']
# -*- coding: utf-8 -*-
import json
import hashlib
import urllib
import urllib2
API_URL = "http://api.jpush.cn:8800/sendmsg/v2/sendmsg"
KEY_PARAMS = [
"sendno",
"app_key",
"receiver_type",
"receiver_value",
"verification_code",
"msg_type",
"msg_content",
"send_description",
"platform",
]
class JPushClient:
"""JPush Python Client Class"""
def __init__(self, username, master_secret, callback_url=""):
self.username = username
self.master_secret = master_secret
self.callback_url = callback_url
def _gen_params(self, kargs):
"""Generate Post params"""
params = {}
params["username"] = self.username
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 = {"extras": extras}
if msg_type == 1: # notification
content["n_title"] = msg_title
content["n_content"] = msg_content
else: # custom message
content["title"] = msg_title
content["message"] = msg_content
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):
'''Push API for all kinds of message and notification,
dict params restore all parameters'''
try:
api_post = urllib2.urlopen(data=urllib.urlencode(params),
url=API_URL, timeout=5)
print api_post.read()
except Exception, e:
print e, e.read()
#Deprecated"
def send_notification_by_imei(self, imei, app_key, sendno, senddes,
msg_title, msg_content, platform, extras={}):
"""Send notification by imei"""
pass
#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)
# -*- coding: utf-8 -*-
from setuptools import setup
from jpush import __version__
setup(
name='jpush',
version=__version__,
description='Python client library for JPush APIs',
keywords=('JPush', 'JPush API'),
license='MIT License',
long_description=open("README.rst", "r").read(),
url='https://github.com/jpush/jpush-api-python-client',
author='linbo',
author_email='llbgurs@gmail.com',
packages=['jpush'],
platforms='any',
classifiers=[
'Environment :: Console',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
],
)
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