Skip to content

Instantly share code, notes, and snippets.

@mindjuice
Created October 14, 2019 02:33
Show Gist options
  • Save mindjuice/b371be31a1b9a28d6b75ca65ae7b1862 to your computer and use it in GitHub Desktop.
Save mindjuice/b371be31a1b9a28d6b75ca65ae7b1862 to your computer and use it in GitHub Desktop.
Siacoin Price from Bittrex
set initialInvestmentUsd to 142000
set scOwned to 1000000
set jsonBtcSc to do shell script "curl https://bittrex.com/api/v1.1/public/getticker?market=btc-sc"
set valueBtcSc to do shell script "sed s/[a-zA-Z:{}]//g <<< " & jsonBtcSc
set valueBtcSc to do shell script "sed s/.*,//g <<< " & valueBtcSc
set jsonUsdtBtc to do shell script "curl https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc"
set valueUsdtBtc to do shell script "sed s/[a-zA-Z:{}]//g <<< " & jsonUsdtBtc
set valueUsdtBtc to do shell script "sed s/.*,//g <<< " & valueUsdtBtc
# set numberBtcSc to replace_chars(valueBtcSc, ".", ",") as number
# set numberUsdtBtc to replace_chars(valueUsdtBtc, ".", ",") as number
set numberBtcSc to valueBtcSc as number
set numberUsdtBtc to valueUsdtBtc as number
set balanceBtc to numberBtcSc * scOwned
set balanceUsdt to balanceBtc * numberUsdtBtc
set percent to (balanceUsdt / initialInvestmentUsd) * 100 - 100
set percent to round (percent)
set btcFormatted to replace_chars(roundThis(balanceBtc, 3) as string, ",", ".")
set result to " BTC = $" & replace_chars(number_to_string(roundThis(balanceUsdt, 2)) as string, ",", ".")
set result to (btcFormatted as string) & result & " (" & percent & "%)"
return result
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
on roundThis(n, numDecimals)
set x to 10 ^ numDecimals
(((n * x) + 0.5) div 1) / x
end roundThis
on number_to_string(this_number)
set this_number to this_number as string
if this_number contains "E+" then
set x to the offset of "." in this_number
set y to the offset of "+" in this_number
set z to the offset of "E" in this_number
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
-1 of this_number as string as number
if x is not 0 then
set the first_part to characters 1 thru (x - 1) of this_number as string
else
set the first_part to ""
end if
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
set the converted_number to the first_part
repeat with i from 1 to the decimal_adjust
try
set the converted_number to ¬
the converted_number & character i of the second_part
on error
set the converted_number to the converted_number & "0"
end try
end repeat
return the converted_number
else
return this_number
end if
end number_to_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment