Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
boxcar-growl.py - Send Boxcar Notification with CLI (Python)
#!/usr/bin/env python
# https://gist.github.com/joech4n/1b3d394ceb8b776f06d7
import os
import sys
import subprocess
import argparse
import ConfigParser
import shlex
# Default config
DEFAULT_SOUND = 'beep-crisp'
DEFAULT_IMAGE = 'http://i.imgur.com/Fea7vsF.png'
DEFAULT_CONFIG_PATH = '~/.' + os.path.basename(__file__)
parser = argparse.ArgumentParser(description='''Send a boxcar notification
API token is sourced from''' + DEFAULT_CONFIG_PATH + ''' by default, but can be overridden with --token. Format of config file:
[config]
token = MYTOKENHERE
''', formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-s', '--sound', help='''name of found to play. List of sounds: http://bit.ly/1uOAJG0
* beep-crisp
* beep-soft
* bell-modern
* bell-one-tone
* bell-simple
* bell-triple
* bird-1
* bird-2
* boing
* cash
* clanging
* detonator-charge
* digital-alarm
* done
* echo
* flourish
* harp
* light
* magic-chime
* magic-coin
* notifier-1
* notifier-2
* notifier-3
* orchestral-long
* orchestral-short
* score
* success
* up
''')
parser.add_argument('title')
parser.add_argument('description', nargs='?', default='')
parser.add_argument('--token', help='If not specified, token will be sourced from ' + DEFAULT_CONFIG_PATH)
parser.add_argument('-q', '--quiet', action='store_true', help='No output unless error.')
args = parser.parse_args()
if args.token is None:
try: # try loading token from config file
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser('~') + '/.' + os.path.basename(__file__))
token = config.get('config', 'token')
except:
sys.exit('ERROR: No token provided. An API token must be provided via ' + DEFAULT_CONFIG_PATH + ' or --token. See --help for format of credentials file and more details.')
else:
token = args.token
# Example: curl -d "user_credentials=ACCESS_TOKEN" -d "notification[title]=message title" -d "notification[long_message]=<b>Some text or HTML for the full layout page notification</b>" -d "notification[sound]=bird-1" -d "notification[source_name]=My own alert" -d "notification[icon_url]=http://new.boxcar.io/images/rss_icons/boxcar-64.png" https://new.boxcar.io/api/notifications
curlCmd = []
curlCmd.append('curl')
if args.quiet:
curlCmd.append('-s -o /dev/null')
curlCmd.append('--connect-timeout 2')
curlCmd.append('-d "user_credentials=' + token + '"')
curlCmd.append('-d "user_credentials=' + token + '"')
curlCmd.append('-d "notification[title]=' + args.title + '"')
if args.description:
curlCmd.append('-d "notification[long_message]=' + args.description + '"')
if args.sound:
curlCmd.append('-d "notification[sound]=' + args.sound + '"')
else:
curlCmd.append('-d "notification[sound]=' + DEFAULT_SOUND + '"')
curlCmd.append('-d "notification[source_name]=' + os.path.basename(__file__) + '"')
curlCmd.append('-d "notification[icon_url]=' + DEFAULT_IMAGE + '"')
curlCmd.append('https://new.boxcar.io/api/notifications')
subprocess.check_call(shlex.split(' '.join(curlCmd)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.