Created
November 18, 2013 13:41
-
-
Save voneiden/7527933 to your computer and use it in GitHub Desktop.
Checks Bitstamp API for latest price and produces a Pushover notification if price exceeds defined limits
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Oct 1 10:20:23 2013 | |
---------------------------------------------------------------------------- | |
"THE BEER-WARE LICENSE" (Revision 42): | |
voneiden <gmail:snaipperi> wrote this file. As long as you retain this notice you | |
can do whatever you want with this stuff. If we meet some day, and you think | |
this stuff is worth it, you can buy me a beer in return | |
---------------------------------------------------------------------------- | |
""" | |
import decimal | |
# Change these to edit alert tresholds | |
# After an alert has been issued, a new number must be chosen | |
# for a new alert | |
low = decimal.Decimal("500.0") | |
high = decimal.Decimal("550.0") | |
import json | |
import os | |
import pickle | |
import sys | |
import urllib | |
def alert(title, msg, priority=0): | |
import httplib | |
conn = httplib.HTTPSConnection("api.pushover.net:443") | |
conn.request("POST", "/1/messages.json", | |
urllib.urlencode({ | |
"token": "CHANGE TO PUSHOVER TOKEN", #<--- change this | |
"user": "CHANGE TO PUSHOVER USER", #<--- change this | |
u"message": msg, | |
"title":title, | |
"priority":priority, | |
}), { "Content-type": "application/x-www-form-urlencoded" }) | |
r = conn.getresponse() | |
r.read() | |
# Bitstamp's API has blocked python urllib user-agent. | |
# Changing the user-agent seemed like too much hassle for me | |
# So I use wget instead. Change the destination folder | |
destination_folder = "/home/voneiden/src" | |
destination_file = "%s/index.html"%(destination_folder) | |
pickle_file = "%s/btcstamp.db"%(destination_folder) | |
os.system("wget -qP %s https://www.bitstamp.net/api/ticker/"%(destination_folder)) | |
try: | |
f = open(destination_file, "r") | |
except IOError: | |
# Sometimes wget doesn't get the file. Bitstamp being a jerk. | |
# You might wanna retry, but I don't bother. Just quit. | |
print "IO ERROR" | |
sys.exit(1) | |
source = f.read() | |
f.close() | |
os.remove(destination_file) | |
data = json.loads(source) | |
price = decimal.Decimal(data["last"]) | |
try: | |
f = open(pickle_file,'r') | |
db = pickle.load(f) | |
f.close() | |
except: | |
print "Database not found, creating" | |
db = {'low':decimal.Decimal("0"), | |
"high":decimal.Decimal("0")} | |
def dump(db, pickle_file): | |
f = open(pickle_file,"w") | |
pickle.dump(db,f) | |
f.close() | |
if price >= high and high != db["high"]: | |
db["high"] = high | |
alert("BITSTAMP", "HIGH! Over $%s"%high) | |
dump(db, pickle_file) | |
if price <= low and low != db["low"]: | |
db["low"] = low | |
alert("BITSTAMP", "LOW! Below $%s"%low,1) # Priority alert | |
dump(db, pickle_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment