Created
September 18, 2014 03:29
-
-
Save zxytim/e45c29bfb0277fa4860d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
# $File: push-phone | |
# $Date: Thu Sep 18 11:29:13 2014 +0800 | |
# $Author: Xinyu Zhou <zxytim[at]gmail[dot]com> | |
import sys | |
import getpass | |
import socket | |
import argparse | |
import requests | |
from os.path import expanduser | |
import os | |
DEFAULT_CONTACTS = expanduser('~/.pushbullet/contacts') | |
def read_contacts(path): | |
if not os.path.exists(path): | |
raise RuntimeError('contact list {} does not exist'.format( | |
DEFAULT_CONTACTS)) | |
with open(path) as f: | |
return dict([line.rstrip().split() for line in f]) | |
def get_args(): | |
default_sender = '{}@{}'.format(getpass.getuser(), socket.gethostname()) | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-r', '--receiver', | |
help=('whom to send the message to. ' | |
'The first person in you contact list will be default.')) | |
parser.add_argument('-t', '--title', | |
default='A message from {}'.format(default_sender)) | |
parser.add_argument('-m', '--message', | |
help='message to send', | |
default='Hi from {}'.format(default_sender)) | |
parser.add_argument('-c', '--contacts', | |
help=('contact list file. each line comprises a name and ID'), | |
default=DEFAULT_CONTACTS) | |
args = parser.parse_args() | |
return args | |
def main(): | |
args = get_args() | |
contacts = read_contacts(args.contacts) | |
if args.receiver is None: | |
receiver = contacts.values()[0] | |
else: | |
receiver = contacts[args.receiver] | |
res = requests.post( | |
'https://api.pushbullet.com/v2/pushes', | |
auth=(receiver, ''), | |
data=dict( | |
type='note', | |
title=args.title, | |
body=args.message)) | |
print res.text | |
if __name__ == '__main__': | |
main() | |
# vim: foldmethod=marker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment