@@ -0,0 +1,2728 @@
+#!/usr/bin/env python3%0A# -*- coding: utf-8 -*-%0A%22%22%22%0AGet push bullet notification.%0A%22%22%22%0Aimport os%0Aimport sys%0Aimport json%0Aimport argparse%0Afrom subprocess import Popen, PIPE%0A%0A# Doesn't work on python2%0Aif sys.version_info.major != 3:%0A    sys.stderr.write(%22Doesn't work on python2%5Cn%22)%0A    sys.exit(1)%0A%0Atry:%0A    import requests%0A    use_alternate_method = False%0Aexcept ImportError:%0A    import urllib%0A    import urllib.request%0A    import urllib.parse%0A    use_alternate_method = True%0A%0AHOST = os.environ.get('HOSTNAME')%0APUSH_API = %22https://api.pushbullet.com/v2/pushes%22%0APUSH_KEY = os.environ.get('PUSH_BULLET_KEY')%0A%0A%0Adef send_get(data):%0A    %22%22%22Use urllib to send instead.%22%22%22%0A    # Build basic stuff%0A    handler = urllib.request.HTTPHandler()%0A    opener = urllib.request.build_opener(handler)%0A    # Prepare the request%0A    data = urllib.parse.urlencode(data)%0A    long_url = PUSH_API + '?' + data%0A    request = urllib.request.Request(long_url)%0A    request.add_header('Access-Token', PUSH_KEY)%0A    request.add_header('Conent-Type', 'application/json')%0A    request.get_method = lambda: %22GET%22%0A    # Send it%0A    try:%0A        connection = opener.open(request)%0A    except urllib.request.HTTPError as err:%0A        connection = err%0A    return connection%0A%0A%0Adef get_push_bullet():%0A    %22%22%22Get a Push Bullet Notification.%22%22%22%0A    headers = %7B'Content-Type': 'application/json', 'Access-Token': PUSH_KEY%7D%0A    req = %7B'active': 'true', 'limit': '1'%7D%0A    if use_alternate_method:%0A        connection = send_get(req)%0A        code = connection.code%0A    else:%0A        r = requests.get(PUSH_API, params=req, headers=headers)%0A        code = r.status_code%0A    if not code == 200:%0A        sys.stderr.write(%0A            'Push Bullet request failed with code %7B0%7D%5Cn'.format(code)%0A        )%0A        return None%0A    if use_alternate_method:%0A        pushes = json.loads(connection.read().decode())%0A    else:%0A        pushes = json.loads(r.content.decode())%0A    push = pushes%5B'pushes'%5D%5B0%5D%0A    message = push%5B'body'%5D%0A    return message%0A%0A%0Adef main(argv=None):%0A    %22%22%22Notify email and push bullet.%22%22%22%0A    if not argv:%0A        argv = sys.argv%5B1:%5D%0A%0A    parser = argparse.ArgumentParser(%0A        description=__doc__,%0A        formatter_class=argparse.RawDescriptionHelpFormatter)%0A%0A    conf = parser.add_argument_group('config')%0A    conf.add_argument('--push-key', help=%22Override PUSH_BULLET_KEY variable%22)%0A%0A    args = parser.parse_args(argv)%0A%0A    if args.push_key:%0A        global PUSH_KEY%0A        PUSH_KEY = args.push_key%0A%0A    if not PUSH_KEY:%0A        sys.stderr.write('No Push Bullet API (PUSH_BULLET_KEY)%5Cn')%0A        return 1%0A%0A    message = get_push_bullet()%0A%0A    sys.stdout.write(message)%0A    return 0%0A%0A%0Aif __name__ == '__main__' and '__file__' in globals():%0A    sys.exit(main())%0A

Add support for getting most recent push bullet push