Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
# Your API Key (you need to replace 'YOUR_API_KEY' with your actual CryptoCompare API key)
|
5 |
+
api_key = '1606c591d78cde143733502669d97c3707d94637e2c32c3184bef54a2a1653d2'
|
6 |
+
|
7 |
+
# The base URL for the CryptoCompare API
|
8 |
+
base_url = 'https://min-api.cryptocompare.com/data/v2/histohour'
|
9 |
+
|
10 |
+
# Parameters for the API call
|
11 |
+
# In this example, we are getting hourly data for Bitcoin (BTC) priced in USD
|
12 |
+
parameters = {
|
13 |
+
'fsym': 'BTC',
|
14 |
+
'tsym': 'USD',
|
15 |
+
'limit': 2000, # The maximum number of data points you can get in one call is 2000
|
16 |
+
'api_key': api_key
|
17 |
+
}
|
18 |
+
|
19 |
+
# Make the API call
|
20 |
+
response = requests.get(base_url, params=parameters)
|
21 |
+
|
22 |
+
# Check if the request was successful
|
23 |
+
if response.status_code == 200:
|
24 |
+
# Parse the received JSON data
|
25 |
+
data = response.json()
|
26 |
+
|
27 |
+
# Extract the data points
|
28 |
+
data_points = data['Data']['Data']
|
29 |
+
|
30 |
+
# Create a DataFrame from the data points
|
31 |
+
df = pd.DataFrame(data_points)
|
32 |
+
|
33 |
+
# Convert timestamps to human-readable dates
|
34 |
+
df['time'] = pd.to_datetime(df['time'], unit='s')
|
35 |
+
|
36 |
+
# Save the DataFrame to a CSV file
|
37 |
+
df.to_csv('crypto_hourly_data.csv', index=False)
|
38 |
+
print(df)
|
39 |
+
#print("Data extraction successful, saved to crypto_hourly_data.csv")
|
40 |
+
else:
|
41 |
+
print(f"Failed to fetch data: {response.status_code}")
|
42 |
+
|
43 |
+
# Make sure to install pandas and requests modules if you haven't already
|
44 |
+
# pip install pandas requests
|