gtermors

pushover.py

Mar 17th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. import sys
  2. import httplib
  3. import urllib
  4. import ConfigParser
  5. import os.path
  6. import inspect
  7. import json
  8. from pprint import pprint
  9.  
  10. # Variables
  11. poURL        = 'api.pushover.net:443'
  12. poApiKey     = ''
  13. poUserKey    = ''
  14.  
  15. # Read Config
  16. def readconfig():
  17.     global poApiKey
  18.     global poUserKey
  19.  
  20.     #Init
  21.     config = ConfigParser.ConfigParser(allow_no_value=True)
  22.  
  23.     sourcePath = os.path.dirname(inspect.getsourcefile(readconfig))
  24.     poConfigFile = os.path.join(sourcePath,'pushover.cfg')
  25.  
  26.     #Check if ConfigFile exists
  27.     if not os.path.isfile(poConfigFile):
  28.         configExists = False
  29.         print 'Pushover ERROR: Missing PushOver configfile'
  30.     else:
  31.         configExists = True
  32.  
  33.     #Read Config
  34.     if configExists:
  35.         config.read(poConfigFile)
  36.  
  37.         try:
  38.             poApiKey  = config.get('PushOver','ApiKey')
  39.             poUserKey = config.get('PushOver','UserKey')
  40.         except ConfigParser.NoOptionError, err:
  41.             print 'Pushover ERROR: Could not parse PushOver config file'
  42.  
  43.     if poApiKey and poUserKey:    
  44.         configRead = True
  45.     else:
  46.         configRead = False
  47.  
  48.     return configExists and configRead
  49.    
  50. # Send Notification
  51. def notify(title, message, priority, sound, url, urltitle):    
  52.     # HTTPS Connection
  53.     conn = httplib.HTTPSConnection(poURL)
  54.  
  55.     # Headers
  56.     postHeaders = { 'Content-type': 'application/x-www-form-urlencoded' }
  57.  
  58.     # Build Body
  59.     if not sound:
  60.         sound = 'pushover'
  61.    
  62.     postBody = urllib.urlencode({
  63.                     'token': poApiKey,
  64.                     'user': poUserKey,
  65.                     'title': title,
  66.                     'message': message,
  67.                     'priority': priority,
  68.                     'sound': sound,
  69.                     'url': url,
  70.                     'url_title': urltitle,
  71.                     })
  72.  
  73.     # POST
  74.     conn.request('POST', '/1/messages.json', postBody, postHeaders)
  75.  
  76.     # Response
  77.     response     = conn.getresponse()
  78.     responseData = response.read()
  79.  
  80.     # Check Response
  81.     poStatus = 0
  82.    
  83.     if response.status == 200:
  84.         responseJson = json.loads(responseData)
  85.         poStatus = responseJson['status']
  86.  
  87.     success = response.status == 200 and poStatus == 1
  88.  
  89.     if (not success):
  90.         print 'PushOver Error :'
  91.         print 'Response Status: ' + str(response.status)
  92.         print 'Response Reason: ' + response.reason
  93.         print 'Response Data  : ' + responseData
  94.         print 'PushOver Status: ' + str(poStatus)
  95.  
  96.     return success
  97.    
  98. # Main
  99. if __name__ == "__main__":
  100.     # Read Config
  101.     configRead = readconfig()
  102.  
  103.     print 'URL    : ' + poURL
  104.     print 'ApiKey : ' + poApiKey
  105.     print 'UserKey: ' + poUserKey
  106.  
  107.     # Notify
  108.     if configRead:
  109.         success = notify('Testing','Bla bla',0,'bike')
  110.         print 'Notify Success: ' + str(success)
Advertisement
Add Comment
Please, Sign In to add comment