Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import httplib
- import urllib
- import ConfigParser
- import os.path
- import inspect
- import json
- from pprint import pprint
- # Variables
- poURL = 'api.pushover.net:443'
- poApiKey = ''
- poUserKey = ''
- # Read Config
- def readconfig():
- global poApiKey
- global poUserKey
- #Init
- config = ConfigParser.ConfigParser(allow_no_value=True)
- sourcePath = os.path.dirname(inspect.getsourcefile(readconfig))
- poConfigFile = os.path.join(sourcePath,'pushover.cfg')
- #Check if ConfigFile exists
- if not os.path.isfile(poConfigFile):
- configExists = False
- print 'Pushover ERROR: Missing PushOver configfile'
- else:
- configExists = True
- #Read Config
- if configExists:
- config.read(poConfigFile)
- try:
- poApiKey = config.get('PushOver','ApiKey')
- poUserKey = config.get('PushOver','UserKey')
- except ConfigParser.NoOptionError, err:
- print 'Pushover ERROR: Could not parse PushOver config file'
- if poApiKey and poUserKey:
- configRead = True
- else:
- configRead = False
- return configExists and configRead
- # Send Notification
- def notify(title, message, priority, sound, url, urltitle):
- # HTTPS Connection
- conn = httplib.HTTPSConnection(poURL)
- # Headers
- postHeaders = { 'Content-type': 'application/x-www-form-urlencoded' }
- # Build Body
- if not sound:
- sound = 'pushover'
- postBody = urllib.urlencode({
- 'token': poApiKey,
- 'user': poUserKey,
- 'title': title,
- 'message': message,
- 'priority': priority,
- 'sound': sound,
- 'url': url,
- 'url_title': urltitle,
- })
- # POST
- conn.request('POST', '/1/messages.json', postBody, postHeaders)
- # Response
- response = conn.getresponse()
- responseData = response.read()
- # Check Response
- poStatus = 0
- if response.status == 200:
- responseJson = json.loads(responseData)
- poStatus = responseJson['status']
- success = response.status == 200 and poStatus == 1
- if (not success):
- print 'PushOver Error :'
- print 'Response Status: ' + str(response.status)
- print 'Response Reason: ' + response.reason
- print 'Response Data : ' + responseData
- print 'PushOver Status: ' + str(poStatus)
- return success
- # Main
- if __name__ == "__main__":
- # Read Config
- configRead = readconfig()
- print 'URL : ' + poURL
- print 'ApiKey : ' + poApiKey
- print 'UserKey: ' + poUserKey
- # Notify
- if configRead:
- success = notify('Testing','Bla bla',0,'bike')
- print 'Notify Success: ' + str(success)
Advertisement
Add Comment
Please, Sign In to add comment