Created
May 23, 2024 18:07
-
-
Save philipbankier/92f57e8bf9f2deda41d835d00031d7ca to your computer and use it in GitHub Desktop.
covalent get historic data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from covalent import CovalentClient | |
import csv | |
from datetime import datetime, timedelta | |
# Constants | |
API_KEY = 'ckey_1bc79adaaaf24ab59fd0d566f25' # Replace this with your actual API key | |
CHAIN_ID = 'eth-mainnet' | |
CONTRACT_ADDRESS = '0xd417144312dbf50465b1c641d016962017ef6240' # Covalent Token address | |
TODAY = datetime.now() | |
TWO_MONTHS_AGO = TODAY - timedelta(days=60) | |
def fetch_prices(): | |
client = CovalentClient(API_KEY) | |
formatted_start_date = TWO_MONTHS_AGO.strftime('%Y-%m-%d') | |
formatted_end_date = TODAY.strftime('%Y-%m-%d') | |
# Debugging: Print the formatted dates to ensure they are correct | |
print("Start Date:", formatted_start_date) | |
print("End Date:", formatted_end_date) | |
response = client.pricing_service.get_token_prices( | |
"eth-mainnet", | |
"USD", | |
CONTRACT_ADDRESS, | |
{"from": formatted_start_date, "to": formatted_end_date} | |
) | |
if not response.error: | |
return response.data['items'] | |
else: | |
raise Exception(response.error_message) | |
def save_to_csv(prices): | |
with open('covalent_token_prices.csv', mode='w', newline='') as file: | |
writer = csv.writer(file) | |
writer.writerow(['date', 'price', 'pretty_price']) | |
for item in prices: | |
writer.writerow([item['date'], item['price'], item['pretty_price']]) | |
def main(): | |
prices = fetch_prices() | |
save_to_csv(prices) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment