Skip to content
Snippets Groups Projects
Commit 60cd14f5 authored by iehl1g23's avatar iehl1g23
Browse files

collect script edited for collected of high and lows for every other currency...

collect script edited for collected of high and lows for every other currency and insertion code fixed for that as well
parent 33b83cad
No related branches found
No related tags found
No related merge requests found
#!/bin/bash
# Download the HTML content
curl -s https://www.coindesk.com/price/bitcoin > bitcoin.html
curl -s https://www.coindesk.com/price/bitcoin > btc.html
curl -s https://www.coindesk.com/price/ethereum > eth.html
curl -s https://www.coindesk.com/price/solana > sol.html
curl -s https://www.coindesk.com/price/binance-coin > bnb.html
curl -s https://www.coindesk.com/price/dogecoin > doge.html
# Extract the current price
price=$(grep -oP '\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' bitcoin.html | head -n 1)
btcprice=$(grep -oP '\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' btc.html | head -n 1)
ethprice=$(grep -oP '\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' eth.html | head -n 1)
solprice=$(grep -oP '\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' sol.html | head -n 1)
bnbprice=$(grep -oP '\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' bnb.html | head -n 1)
dogeprice=$(grep -oP '\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' doge.html | head -n 1)
# Extract the 24-hour high price
high=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' bitcoin.html | head -n 1)
# Extract all prices from the file and separate high and low explicitly
btchigh=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' btc.html | sed -n '1p')
btclow=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' btc.html | sed -n '2p')
# Extract the 24-hour low price
low=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' bitcoin.html | tail -n 1)
ethhigh=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' eth.html | sed -n '1p')
ethlow=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' eth.html | sed -n '2p')
# Remove the "USD" text and commas for proper insertion into the database
price=$(echo $price | sed 's/[^0-9.]//g')
high=$(echo $high | sed 's/[^0-9.]//g')
low=$(echo $low | sed 's/[^0-9.]//g')
sollow=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' sol.html | sed -n '1p')
solhigh=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' sol.html | sed -n '2p')
# Extract other currency prices
eth=$(grep -oP '<div class="text-black-colorcoindeskblack truncate font-normal">\K\d{1,3}(?:,\d{3})*(?:\.\d+)?' bitcoin.html | sed -n '2p')
sol=$(grep -oP '<div class="text-black-colorcoindeskblack truncate font-normal">\K\d{1,3}(?:,\d{3})*(?:\.\d+)?' bitcoin.html | sed -n '5p')
doge=$(grep -oP '<div class="text-black-colorcoindeskblack truncate font-normal">\K\d{1,3}(?:,\d{3})*(?:\.\d+)?' bitcoin.html | sed -n '7p')
bnblow=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' bnb.html | sed -n '1p')
bnbhigh=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' bnb.html | sed -n '2p')
echo "Price: $price, High: $high, Low: $low, ETH: $eth, SOL: $sol, DOGE: $doge"
dogehigh=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' doge.html | sed -n '1p')
dogelow=$(grep -oP '<span class="Noto_Sans_xs_Sans-600-xs text-color-black ">\K\d{1,3}(?:,\d{3})*(?:\.\d+)? USD' doge.html | sed -n '2p')
eth=$(echo $eth | sed 's/[^0-9.]//g')
sol=$(echo $sol | sed 's/[^0-9.]//g')
doge=$(echo $doge | awk '{printf "%f\n", $1}')
# Remove the "USD" text, commas and making doge have 4 dp. for proper insertion into the database
btcprice=$(echo $btcprice | sed 's/[^0-9.]//g')
btchigh=$(echo $btchigh | sed 's/[^0-9.]//g')
btclow=$(echo $btclow | sed 's/[^0-9.]//g')
ethprice=$(echo $ethprice | sed 's/[^0-9.]//g')
ethhigh=$(echo $ethhigh | sed 's/[^0-9.]//g')
ethlow=$(echo $ethlow | sed 's/[^0-9.]//g')
solprice=$(echo $solprice | sed 's/[^0-9.]//g')
solhigh=$(echo $solhigh | sed 's/[^0-9.]//g')
sollow=$(echo $sollow | sed 's/[^0-9.]//g')
bnbprice=$(echo $bnbprice | sed 's/[^0-9.]//g')
bnbhigh=$(echo $bnbhigh | sed 's/[^0-9.]//g')
bnblow=$(echo $bnblow | sed 's/[^0-9.]//g')
dogeprice=$(echo $dogeprice | sed 's/[^0-9.]//g')
dogehigh=$(echo $dogehigh | sed 's/[^0-9.]//g')
dogelow=$(echo $dogelow | sed 's/[^0-9.]//g')
# Echo the extracted values
echo "Bitcoin: Price=$btcprice, High=$btchigh, Low=$btclow"
echo "Ethereum: Price=$ethprice, High=$ethhigh, Low=$ethlow"
echo "Solana: Price=$solprice, High=$solhigh, Low=$sollow"
echo "Binance Coin: Price=$bnbprice, High=$bnbhigh, Low=$bnblow"
echo "Dogecoin: Price=$dogeprice, High=$dogehigh, Low=$dogelow"
# Database credentials
DB_USER="root"
DB_PASS="password"
DB_NAME="bitcoin_tracker"
# Insert data into the MySQL table
mysql -u $DB_USER -p$DB_PASS $DB_NAME -e "
INSERT INTO btc_price (price, high, low)
VALUES ('$price', '$high', '$low');
INSERT INTO cryptocurrency_prices (cryptocurrency_name, price)
VALUES ('eth', '$eth');
INSERT INTO cryptocurrency_prices (cryptocurrency_name, price)
VALUES ('sol', '$sol');
INSERT INTO cryptocurrency_prices (cryptocurrency_name, price)
VALUES ('doge', '$doge');
"
DB_NAME="crypto_tracker"
# Insert the extracted data into the respective cryptocurrency tables
mysql -u $DB_USER -p$DB_PASS $DB_NAME <<EOF
INSERT INTO btc_prices (price, high, low) VALUES ($btcprice, $btchigh, $btclow);
INSERT INTO eth_prices (price, high, low) VALUES ($ethprice, $ethhigh, $ethlow);
INSERT INTO sol_prices (price, high, low) VALUES ($solprice, $solhigh, $sollow);
INSERT INTO bnb_prices (price, high, low) VALUES ($bnbprice, $bnbhigh, $bnblow);
INSERT INTO doge_prices (price, high, low) VALUES ($dogeprice, $dogehigh, $dogelow);
EOF
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment