Skip to content
Snippets Groups Projects
Commit 883f2f0e authored by zx2e23's avatar zx2e23
Browse files

Upload New File

parent 8d7e5e0e
No related branches found
No related tags found
No related merge requests found
s3.sh 0 → 100644
#!/bin/bash
# Configuration
API_URL="https://api.coinmarketcap.com/data-api/v3/cryptocurrency/detail/chart?id=1&range=7D"
DB_USER="root"
DB_PASS=""
DB_NAME="bitcoin_data"
TABLE_NAME="bitcoin_prices_24h"
# Fetch data from API
echo "Fetching data from $API_URL..."
RESPONSE=$(curl -s "$API_URL")
if [ -z "$RESPONSE" ]; then
echo "Error: No data received from API."
exit 1
fi
# Save to file
echo "$RESPONSE" > coinmarketcap_data.json
echo "Data saved to coinmarketcap_data.json."
# Debug: Full .data.points data
echo "Debug: Full .data.points data:"
cat coinmarketcap_data.json | jq '.data.points'
# Parse data using jq
current_price=$(cat coinmarketcap_data.json | jq -r '.data.points | to_entries | sort_by(.key) | .[-1].value.v[0] // empty')
high_24h=$(cat coinmarketcap_data.json | jq -r '.data.points | to_entries | map(.value.v[0]) | max // empty')
low_24h=$(cat coinmarketcap_data.json | jq -r '.data.points | to_entries | map(.value.v[0]) | min // empty')
# Debug: Extracted values
echo "Debug: Extracted Current Price: $current_price"
echo "Debug: Extracted High 24h: $high_24h"
echo "Debug: Extracted Low 24h: $low_24h"
# Validate extracted data
if [ -z "$current_price" ] || [ -z "$high_24h" ] || [ -z "$low_24h" ]; then
echo "Error: Extracted data contains empty fields."
exit 1
fi
# Print the parsed data
echo "Current Price: $current_price"
echo "24h High: $high_24h"
echo "24h Low: $low_24h"
# Insert into MySQL
echo "Inserting data into MySQL..."
SQL="CREATE TABLE IF NOT EXISTS \`$TABLE_NAME\` (
id INT AUTO_INCREMENT PRIMARY KEY,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
current_price DECIMAL(18, 8),
high_24h DECIMAL(18, 8),
low_24h DECIMAL(18, 8)
);
INSERT INTO \`$TABLE_NAME\` (current_price, high_24h, low_24h) VALUES ($current_price, $high_24h, $low_24h);"
mysql -u "$DB_USER" -h "$DB_PASS" -D "$DB_NAME" -e "$SQL"
if [ $? -eq 0 ]; then
echo "Data inserted successfully."
else
echo "Error: Failed to insert data."
exit 1
fi
echo "Script execution completed."
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment