2014-03-10 05:18:05 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2015-06-05 00:12:57 +00:00
|
|
|
from . import __version__
|
2014-03-10 05:18:05 +00:00
|
|
|
from xml.dom.minidom import parseString
|
|
|
|
|
2015-06-05 00:12:57 +00:00
|
|
|
try:
|
|
|
|
from http.client import HTTPSConnection
|
|
|
|
except ImportError:
|
|
|
|
from httplib import HTTPSConnection
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-05 00:12:57 +00:00
|
|
|
try:
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
except ImportError:
|
|
|
|
from urllib import urlencode
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PyNMA(object):
|
2015-06-05 00:12:57 +00:00
|
|
|
"""
|
|
|
|
http://www.notifymyandroid.com/api.jsp
|
|
|
|
PyNMA(apikey=None, developerkey=None)
|
2014-03-10 05:18:05 +00:00
|
|
|
takes 2 optional arguments:
|
2015-06-05 00:12:57 +00:00
|
|
|
- (opt) apykey: a string containing 1 key or an array of keys
|
2014-03-10 05:18:05 +00:00
|
|
|
- (opt) developerkey: where you can store your developer key
|
|
|
|
"""
|
|
|
|
|
2015-06-05 00:12:57 +00:00
|
|
|
def __init__(self, apikey=None, developerkey=None):
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
self._developerkey = None
|
|
|
|
self.developerkey(developerkey)
|
2015-06-05 00:12:57 +00:00
|
|
|
|
|
|
|
self.api_server = 'https://www.notifymyandroid.com'
|
|
|
|
self.add_path = '/publicapi/notify'
|
|
|
|
self.user_agent = 'PyNMA/v%s' % __version__
|
|
|
|
|
|
|
|
key = []
|
2014-03-10 05:18:05 +00:00
|
|
|
if apikey:
|
2015-06-05 00:12:57 +00:00
|
|
|
key = (apikey, [apikey])[str == type(apikey)]
|
|
|
|
|
|
|
|
self._apikey = self.uniq(key)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def uniq(seq):
|
|
|
|
# Not order preserving
|
|
|
|
return list({}.fromkeys(seq).keys())
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def addkey(self, key):
|
2015-06-05 00:12:57 +00:00
|
|
|
"""
|
|
|
|
Add a key (register ?)
|
|
|
|
"""
|
|
|
|
if str == type(key):
|
|
|
|
if key not in self._apikey:
|
2014-03-10 05:18:05 +00:00
|
|
|
self._apikey.append(key)
|
2015-06-05 00:12:57 +00:00
|
|
|
|
|
|
|
elif list == type(key):
|
2014-03-10 05:18:05 +00:00
|
|
|
for k in key:
|
2015-06-05 00:12:57 +00:00
|
|
|
if k not in self._apikey:
|
2014-03-10 05:18:05 +00:00
|
|
|
self._apikey.append(k)
|
|
|
|
|
|
|
|
def delkey(self, key):
|
2015-06-05 00:12:57 +00:00
|
|
|
"""
|
|
|
|
Removes a key (unregister ?)
|
|
|
|
"""
|
|
|
|
if str == type(key):
|
2014-03-10 05:18:05 +00:00
|
|
|
if key in self._apikey:
|
|
|
|
self._apikey.remove(key)
|
2015-06-05 00:12:57 +00:00
|
|
|
|
|
|
|
elif list == type(key):
|
2014-03-10 05:18:05 +00:00
|
|
|
for k in key:
|
|
|
|
if key in self._apikey:
|
|
|
|
self._apikey.remove(k)
|
|
|
|
|
|
|
|
def developerkey(self, developerkey):
|
2015-06-05 00:12:57 +00:00
|
|
|
"""
|
|
|
|
Sets the developer key (and check it has the good length)
|
|
|
|
"""
|
|
|
|
if str == type(developerkey) and 48 == len(developerkey):
|
2014-03-10 05:18:05 +00:00
|
|
|
self._developerkey = developerkey
|
|
|
|
|
2015-06-05 00:12:57 +00:00
|
|
|
def push(self, application='', event='', description='', url='', content_type=None, priority=0, batch_mode=False, html=False):
|
|
|
|
"""
|
|
|
|
Pushes a message on the registered API keys.
|
2014-03-10 05:18:05 +00:00
|
|
|
takes 5 arguments:
|
|
|
|
- (req) application: application name [256]
|
|
|
|
- (req) event: event name [1000]
|
|
|
|
- (req) description: description [10000]
|
|
|
|
- (opt) url: url [512]
|
2015-06-05 00:12:57 +00:00
|
|
|
- (opt) contenttype: Content Type (act: None (plain text) or text/html)
|
2014-03-10 05:18:05 +00:00
|
|
|
- (opt) priority: from -2 (lowest) to 2 (highest) (def:0)
|
|
|
|
- (opt) batch_mode: call API 5 by 5 (def:False)
|
2015-06-05 00:12:57 +00:00
|
|
|
- (opt) html: shortcut for content_type=text/html
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
Warning: using batch_mode will return error only if all API keys are bad
|
2015-06-05 00:12:57 +00:00
|
|
|
http://www.notifymyandroid.com/api.jsp
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
2015-06-05 00:12:57 +00:00
|
|
|
datas = {'application': application[:256].encode('utf8'),
|
|
|
|
'event': event[:1000].encode('utf8'),
|
|
|
|
'description': description[:10000].encode('utf8'),
|
|
|
|
'priority': priority}
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
if url:
|
2015-06-05 00:12:57 +00:00
|
|
|
datas['url'] = url[:2000]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
if self._developerkey:
|
|
|
|
datas['developerkey'] = self._developerkey
|
|
|
|
|
2015-06-05 00:12:57 +00:00
|
|
|
if 'text/html' == content_type or True == html: # Currently only accepted content type
|
|
|
|
datas['content-type'] = 'text/html'
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
results = {}
|
|
|
|
|
|
|
|
if not batch_mode:
|
|
|
|
for key in self._apikey:
|
|
|
|
datas['apikey'] = key
|
2015-06-05 00:12:57 +00:00
|
|
|
res = self.callapi('POST', self.add_path, datas)
|
2014-03-10 05:18:05 +00:00
|
|
|
results[key] = res
|
|
|
|
else:
|
2015-06-05 00:12:57 +00:00
|
|
|
datas['apikey'] = ','.join(self._apikey)
|
|
|
|
res = self.callapi('POST', self.add_path, datas)
|
|
|
|
results[datas['apikey']] = res
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
return results
|
2015-06-05 00:12:57 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def callapi(self, method, path, args):
|
2015-06-05 00:12:57 +00:00
|
|
|
headers = {'User-Agent': self.user_agent}
|
|
|
|
|
|
|
|
if 'POST' == method:
|
|
|
|
headers['Content-type'] = 'application/x-www-form-urlencoded'
|
|
|
|
|
|
|
|
http_handler = HTTPSConnection(self.api_server)
|
2014-03-10 05:18:05 +00:00
|
|
|
http_handler.request(method, path, urlencode(args), headers)
|
|
|
|
resp = http_handler.getresponse()
|
|
|
|
|
|
|
|
try:
|
2015-06-05 00:12:57 +00:00
|
|
|
res = self._parse_response(resp.read())
|
|
|
|
except Exception as e:
|
|
|
|
res = {'type': 'pynmaerror',
|
|
|
|
'code': 600,
|
|
|
|
'message': str(e)}
|
2014-03-10 05:18:05 +00:00
|
|
|
pass
|
2015-06-05 00:12:57 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
return res
|
|
|
|
|
2015-06-05 00:12:57 +00:00
|
|
|
@staticmethod
|
|
|
|
def _parse_response(response):
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
root = parseString(response).firstChild
|
2015-06-05 00:12:57 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
for elem in root.childNodes:
|
2015-06-05 00:12:57 +00:00
|
|
|
if elem.TEXT_NODE == elem.nodeType:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if 'success' == elem.tagName:
|
|
|
|
res = dict(list(elem.attributes.items()))
|
|
|
|
res['message'] = ''
|
|
|
|
res['type'] = elem.tagName
|
2014-03-10 05:18:05 +00:00
|
|
|
return res
|
2015-06-05 00:12:57 +00:00
|
|
|
|
|
|
|
if 'error' == elem.tagName:
|
|
|
|
res = dict(list(elem.attributes.items()))
|
2014-03-10 05:18:05 +00:00
|
|
|
res['message'] = elem.firstChild.nodeValue
|
2015-06-05 00:12:57 +00:00
|
|
|
res['type'] = elem.tagName
|
2014-03-10 05:18:05 +00:00
|
|
|
return res
|