Advertisement
metalx1000

Fetch bitcoin value

Nov 12th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.75 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2024  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation version 3 of the License.
  9.  
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #GNU General Public License for more details.
  14.  
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. ######################################################################
  18.  
  19. # this will get the current bitcoin value in USD
  20. # Note that there is a limit of requests that can be made to the API
  21. # so we will make sure we don't check more then once a minute
  22.  
  23. url="https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"
  24. bittime="/tmp/bittime.log"
  25. bitvalue="/tmp/bitvalue.log"
  26.  
  27. normal=$(echo -en "\e[0m")
  28. red=$(echo -en "\e[31m")
  29. green=$(echo -en "\e[32m")
  30.  
  31. # Make sure it's been at least 60 seconds
  32. currenttime="$(date +%s)"
  33. lasttime=0
  34. [[ -f $bittime ]] && lasttime="$(head -n 1 $bittime)"
  35. timediff=$(( $currenttime - $lasttime ))
  36. if [[ $timediff -lt 60 ]]
  37. then
  38.     #If it has been less then 60 seconds
  39.     # show last value in red
  40.     printf "${red}"
  41.     cat $bitvalue
  42.     exit
  43. fi
  44.  
  45. #log current time to file
  46. echo $currenttime > $bittime
  47.  
  48. # fetch and print current value in green
  49. value="$(wget -qO- "$url"|jq '.bitcoin.usd')"
  50. printf "$%'d\n" $value > $bitvalue
  51. printf "${green}$%'d\n" $value  
  52.  
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement