Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
coursework 2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
zx2e23
coursework 2
Commits
883f2f0e
Commit
883f2f0e
authored
6 months ago
by
zx2e23
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
8d7e5e0e
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
s3.sh
+69
-0
69 additions, 0 deletions
s3.sh
with
69 additions
and
0 deletions
s3.sh
0 → 100644
+
69
−
0
View file @
883f2f0e
#!/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."
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment