Last active
January 5, 2018 03:17
-
-
Save djvs/284865f2db8455828a95c07e8c53e684 to your computer and use it in GitHub Desktop.
Cryptocurrency holdings snapshot report generator via coinmarketcap.com API (requires pry and curb gems)
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
#!/usr/bin/env ruby | |
# | |
require 'date' | |
require 'json' | |
require 'curb' | |
require 'pry' | |
puts "Report generated #{DateTime.now.strftime("%Y-%m-%d at %I:%M %p %z")}" | |
puts | |
@req = Curl.get("https://api.coinmarketcap.com/v1/ticker/") | |
@ticker = JSON.parse(@req.body_str) | |
@tickerh = Hash[@ticker.map{|x| [x['id'], x] }] | |
def ticker_for(k) | |
req = Curl.get("https://api.coinmarketcap.com/v1/ticker/#{k}/") | |
ticker = JSON.parse(req.body_str) | |
return ticker[0] | |
rescue => e | |
puts e.message | |
puts e.backtrace | |
return false | |
end | |
puts | |
@balances = { | |
ethereum: 0.0, | |
cardano: 0.0, | |
:"bitcoin-cash" => 0.0, | |
lisk: 0.0, | |
monero: 0.0, | |
ark: 0.0, | |
iota: 0.0, # miota | |
district0x: 0.0, | |
bitcoin: 0.0, | |
nxt: 0.0, | |
ripple: 0.0, | |
litecoin: 0.0 | |
} | |
puts "Currencies:" | |
puts | |
def pct(i, str) | |
i["percent_change_#{str}"] + "%" | |
end | |
def to_b(num) | |
(num.to_i / 1000000000.0).round(2).to_s + "bil" | |
end | |
@totals = {usd: 0.0, btc: 0.0} | |
cols = ['name', 'symbol', 'qty', 'USD', 'BTC', 'USD total', 'BTC total', '1h', '24h', '7d', 'm. cap'] | |
def tableize(*vals) | |
printf vals.map{'%-13s'}.join(''), *vals | |
printf "\n" | |
STDOUT.flush | |
end | |
tableize *cols | |
tableize ('-' * 12 + ' ') * cols.count | |
#tableize '-' * (14 * 9) | |
@balances.each do |k,v| | |
begin | |
ks = k.to_s | |
@i = @tickerh[ks] || ticker_for(ks) | |
@v_usd = @i['price_usd'].to_f * v | |
@v_btc = @i['price_btc'].to_f * v | |
@totals[:usd] += @v_usd | |
@totals[:btc] += @v_btc | |
tableize @i['name'], @i['symbol'], v.round(4), @i['price_usd'].to_f.round(3), @i['price_btc'].to_f.round(3), @v_usd.round(3), @v_btc.round(5), pct(@i,'1h'), pct(@i,'24h'), pct(@i,'7d'), to_b(@i['market_cap_usd']) | |
rescue => e | |
puts e.message | |
puts e.backtrace | |
binding.pry | |
end | |
end | |
puts | |
puts "Totals:" | |
puts | |
puts "#{@totals[:usd].round(2)} USD" | |
puts "#{@totals[:btc].round(6)} BTC" | |
puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment