kerenon

Pushbullet

Aug 18th, 2015
256
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys
  5. import requests
  6.  
  7. API_KEY='INSERT API KEY HERE'
  8. DEVICE_ID='INSERT DEVICE ID HERE'
  9.  
  10. def push(title, message):
  11.     payload={'device_iden':DEVICE_ID, 'type': 'note', 'title': title, 'body': message, 'API_KEY': API_KEY}
  12.     auth=[API_KEY,'']
  13.     return(requests.post('https://api.pushbullet.com/api/pushes',auth=(API_KEY, ''),params=payload))
  14.  
  15. def main():
  16.     if len(sys.argv)==2 and sys.argv[1]=='getdevices':
  17.         print ('Generating device dump... please wait!\n')
  18.         r=requests.get('https://api.pushbullet.com/api/devices',auth=(API_KEY, ''))    
  19.         for device in r.json()['devices']:
  20.             print device['extras']['model'] + ' -> \'' + device['iden'] + '\''
  21.         sys.exit(0)
  22.     elif len(sys.argv)<3:
  23.         print 'Sending a message : ' + sys.argv[0] + ' <title> <message>'
  24.         print 'Dump device list  : ' + sys.argv[0] + ' getdevices'
  25.         sys.exit(1)
  26.  
  27.     r=push(sys.argv[1], sys.argv[2])
  28.  
  29.     if r.status_code == 200:
  30.         print('Notification sent!')
  31.     else:
  32.         print('Error while sending notification: ' + r.reason + ' (' + str(r.status_code) + ')')
  33.  
  34. if __name__ == '__main__':
  35.     main()
RAW Paste Data