Created
November 14, 2013 08:29
-
-
Save carlzulauf/7463352 to your computer and use it in GitHub Desktop.
Watch the price of Bitcoin on Coinbase, and alert me.
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
require 'logger' | |
require 'net/http' | |
require 'json' | |
require 'mail' # gem 'mail' | |
dir = File.dirname(__FILE__) | |
logger = Logger.new(STDOUT) | |
logger.level = Logger::INFO | |
# credentials.yml example: | |
# --- | |
# :user_name: [email protected] | |
# :password: muchdoge | |
# | |
credentials = YAML.load_file(File.join(dir, "credentials.yml")) | |
Mail.defaults do | |
delivery_method :smtp, credentials.merge( | |
address: "smtp.gmail.com", | |
port: "587", | |
authentication: :plain, | |
enable_starttls_auto: true | |
) | |
end | |
def send_notice(to, price) | |
Mail.deliver do | |
from "BTC Communicator <#{to}>" | |
to to | |
subject "BTC is TOO DAMN HIGH! ($#{price})" | |
body "Get out now, while you still can!" | |
end | |
end | |
target = 990.0 | |
seconds = 30 # seconds between API calls | |
loop do | |
json = Net::HTTP.get URI("https://coinbase.com/api/v1/prices/spot_rate") | |
logger.debug "JSON Response: #{json}" | |
data = JSON.parse json | |
price = data["amount"].to_f | |
logger.debug "Price: $ #{price}" | |
if price > target | |
logger.info "Price ($#{price}) above target ($#{target}). Sending alert." | |
send_notice credentials[:user_name], price | |
break | |
else | |
logger.info "Price ($#{price}) below target ($#{target}). Doing nothing." | |
end | |
sleep seconds | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!