-
-
Save janklimo/a1a5ca8d07adf96ae70438b422c619fc to your computer and use it in GitHub Desktop.
bx.in.th API
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 | |
# frozen_string_literal: true | |
require 'net/http' | |
require 'json' | |
require 'yaml' | |
unless ARGV.count == 0 || ARGV.count == 1 | |
puts "Usage #{$0} [--mappings]" | |
exit | |
end | |
MAPPINGS = { | |
'1': 'BTC', | |
'21': 'ETH', | |
'22': 'DASH', | |
'25': 'XRP', | |
'26': 'OMG' | |
} | |
CURRENCY_SYMBOLS = { 'THB' => '฿', 'BTC' => '₿' } | |
bx_response = Net::HTTP.get_response(URI('https://bx.in.th/api/')) | |
data = JSON.parse(bx_response.body).sort_by { |k, _| k.to_i }.to_h | |
portfolio = YAML.load_file('portfolio.yml') | |
if ARGV[0] == '--mappings' | |
data.each do |key, crypto| | |
puts "#{key.rjust(2)}: #{crypto['primary_currency']}/#{crypto['secondary_currency']}" | |
end | |
exit | |
end | |
balance = 0 | |
cryptos = MAPPINGS.empty? ? data.keys : MAPPINGS.keys | |
puts "Coin\tLast Price\tAmount\tNet Value" | |
cryptos.each do |key| | |
next unless portfolio[MAPPINGS[key]] | |
crypto = data[key.to_s] | |
symbol = CURRENCY_SYMBOLS.fetch(crypto['primary_currency']) | |
bid = crypto['orderbook']['bids']['highbid'].to_f | |
amount = portfolio[MAPPINGS[key]].to_f | |
crypto_value = amount * bid | |
puts "#{crypto['secondary_currency']}\t#{symbol}#{crypto['last_price'].to_i}" \ | |
"\t\t#{amount.round(3)}\t฿#{crypto_value.to_i}" | |
balance += crypto_value | |
end | |
puts "Balance: ฿#{balance.to_i}" |
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
# How many coins do you own? | |
OMG: 1 | |
ETH: 2 | |
BTC: 3 | |
DASH: 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output:
![screen shot 2017-09-02 at 3 29 16 pm](https://user-images.githubusercontent.com/7811733/29994168-95ebeab8-8ff3-11e7-9cc1-b6c2677f0601.png)