-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_fetcher.py
25 lines (21 loc) · 1.11 KB
/
data_fetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import pandas as pd
import requests
from config import API_KEY, BASE_URL, timeframes_config
def fetch_crypto_data(crypto, timeframe, limit):
# Extract endpoint and aggregate information from the consolidated configuration using the timeframe parameter
endpoint, aggregate = timeframes_config[timeframe]['endpoint']
# Construct the API URL with the specified parameters
url = f"{BASE_URL}{endpoint}?fsym={crypto}&tsym=USD&limit={limit}&aggregate={aggregate}&api_key={API_KEY}"
# Make the API request
response = requests.get(url)
# Check if the response is successful (HTTP status code 200)
if response.status_code == 200:
# Parse the JSON response and convert it to a Pandas DataFrame
data = response.json()['Data']['Data']
df = pd.DataFrame(data)
# Convert the 'time' column to a datetime format for easier manipulation
df['time'] = pd.to_datetime(df['time'], unit='s')
return df
else:
# If the response is not successful, raise an error with the HTTP status code
response.raise_for_status()