-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain_request_historical.py
More file actions
90 lines (61 loc) · 2.58 KB
/
Main_request_historical.py
File metadata and controls
90 lines (61 loc) · 2.58 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Daniele Bianchi, d.bianchi@qmul.ac.uk, whitesphd.com
"""
import requests
import json
from datetime import datetime, timedelta
from Coinmarketcap_functions import *
import pandas as pd
import time
import gzip
api_key = 'put your API key here'
# Fetch the full list of cryptocurrencies you want to obtain.
# If you have your list you can skip this part
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/map'
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': api_key,
}
response = requests.get(url, headers=headers)
if response.status_code == 200: #Check you get a correct response
# Convert the response to JSON format
data = response.json()
# Extract the list of cryptocurrencies.
cryptocurrencies = data['data']
# Optionally, you can save this list to a json file
with open('cryptocurrencies.json', 'w') as f:
json.dump(cryptocurrencies, f, indent=4)
else:
print(f'Failed to retrieve data: Error code {response.status_code}')
# Define the time series you need (boundaries), the fiat currency you want the
# prices to be expressed and the sample frequency. For a full description of the
# available fiat currencies and frequencies you should check the CoinMarketCap API
# documentation
start_date = datetime(2013, 4, 28)
end_date = datetime.now()
fiat = 'USD'
interval = 'daily'
# Fetch the historical prices and volumes for all cryptocurrencies in the list
df_results = pd.DataFrame()
for w in range(len(cryptocurrencies)):
crypto_id = cryptocurrencies[w].get('id')
# To fetch the data you use the fetch_historical_data function
df = fetch_historical_data(crypto_id, api_key, start_date, end_date, fiat, interval)
# Check if the dataframe is not None before appending
if df is not None:
df_results = df_results.append(df, ignore_index=True)
print(f"Retrieved cryptocurrency {w}.")
else:
print(f"Failed to retrieve data for cryptocurrency {w}.")
time.sleep(1) # Respect the API's rate limit (check API documentation)
# Convert the DataFrame to a JSON string without line delimiters
json_str = df_results.to_json(orient='records')
file_name = f'name_you_prefer..gz'
# Compress and save the JSON string
with gzip.open(file_name, 'wt', encoding='utf-8') as f:
f.write(json_str)
# save the file as a compressed json to save space
file_name = f'market_data.json.gz'
df_results.to_json(file_name, orient='records', lines=True, compression='gzip')