File size: 1,250 Bytes
a45d429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
33
34
35
36
# Import necessary libraries
import requests
import pandas as pd
import logging
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()
url_cmc = os.getenv("URL_CMC")
api_key_cmc = os.getenv("API_KEY_CMC")
log_folder = os.getenv("LOG_FOLDER")

# Ensure the log folder exists
os.makedirs(log_folder, exist_ok=True)
log_file = os.path.join(log_folder, "cmc_global_metrics.log")
logging.basicConfig(filename=log_file, level=logging.INFO, format="%(asctime)s [%(levelname)s] - %(message)s")

# Fetch global metrics
def fetch_global_metrics():
    endpoint = "v1/global-metrics/quotes/latest"
    headers = {'Accepts': 'application/json', 'X-CMC_PRO_API_KEY': api_key_cmc}
    try:
        response = requests.get(f"{url_cmc}/{endpoint}", headers=headers)
        response.raise_for_status()  # Raise an exception for HTTP errors
        data = response.json()
        df = pd.json_normalize(data['data'])
        df.to_csv('output/global_metrics.csv', index=False)
        logging.info("Global metrics data fetched successfully.")
    except requests.exceptions.RequestException as e:
        logging.error(f"Error fetching global metrics data: {e}")

# Run the function
if __name__ == "__main__":
    fetch_global_metrics()