Permalink
Browse files

Add growl notification support and make all the variables configurabl…

…e on the command line.
  • Loading branch information...
1 parent 5b53bfd commit 18fb187e4766816ce83649390ed44ec8f87ddd18 @ldm5180 ldm5180 committed Aug 12, 2011
Showing with 33 additions and 9 deletions.
  1. +33 −9 card.py
View
42 card.py
@@ -1,24 +1,48 @@
+import optparse
import json
import urllib2
import commands
import time
+import os
-interval = 60 #interval in seconds
-duration = 3 #duration of check in hours
-threshold = 50.0 #alert threshold
n = 0
-while n < (duration*60*60/interval):
+parser = optparse.OptionParser()
+parser.add_option("-g", "--growl",
+ dest="growl", default=False,
+ action="store_true",
+ help="Enable Growl notifications. Must have 'growlnotify' installed")
+parser.add_option("-i", "--interval",
+ dest="interval", default="60",
+ help="Update interval",
+ metavar="SECONDS")
+parser.add_option("-d", "--duration",
+ dest="duration", default="3",
+ help="Duration to continue checking",
+ metavar="HOURS")
+parser.add_option("-t", "--threshold",
+ dest="threshold", default="50",
+ help="Threshold to notify",
+ metavar="DOLLARS")
+(options, args) = parser.parse_args()
+
+while n < (int(options.duration)*60*60/int(options.interval)):
try:
result = json.loads(urllib2.urlopen('http://jonathanstark.com/card/api/latest').read())
except:
print "Error loading resource."
- commands.getstatusoutput('open ./play.app') #Play your iTunes library
+ if options.growl:
+ os.system("growlnotify --title \"Jonathan's Card\" --message 'Unable to get balance." % balance)
+ else:
+ commands.getstatusoutput('open ./play.app') #Play your iTunes library
balance = float(result['balance']['amount'])
n += 1
- if balance > threshold:
- commands.getstatusoutput('open ./play.app') #Play your iTunes library
+ if balance > float(options.threshold):
+ if options.growl:
+ os.system("growlnotify --title \"Jonathan's Card\" --message 'Balance: $%.02f'" % balance)
+ else:
+ commands.getstatusoutput('open ./play.app') #Play your iTunes library
print "BALANCE IS %s" % balance
else:
- print "%s is not over %s" % (balance, threshold)
- time.sleep(interval)
+ print "%s is not over %s" % (balance, options.threshold)
+ time.sleep(int(options.interval))

0 comments on commit 18fb187

Please sign in to comment.