Skip to content

Instantly share code, notes, and snippets.

@kriswebdev
Last active January 4, 2024 03:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriswebdev/3aae7a318d60c5f3062be252352b338a to your computer and use it in GitHub Desktop.
Save kriswebdev/3aae7a318d60c5f3062be252352b338a to your computer and use it in GitHub Desktop.
CoinGecko Crypto Coin Exchange Rates To CSV
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# CoinGecko Crypto Coin Exchange Rates To CSV
# Version: 1.00
# Description:
# Get all crypto coins exchange rates in CSV file
# Uses CoinGecko free API, no need to register
# Requirements:
# python3
# pip3 install pycoingecko
# Usage:
# Run without arguments
# Output file is rates.csv in current folder.
# Author: KrisWebDev
# Licenses:
# MIT: https://opensource.org/licenses/MIT
# CoinGecko API Terms of Service: https://www.coingecko.com/api_terms
import time
from pycoingecko import CoinGeckoAPI
filename_out = r'rates.csv'
cg = CoinGeckoAPI()
page = 0
mks = []
print('CoinGecko Coin Rate Crawler')
while True:
start = time.time()
mk = cg.get_coins_markets(vs_currency='usd',per_page=250,page=page)
mkl = len(mk)
if mkl > 0:
mks += mk
print('Page ' + str(page) + ' done: +' + str(mkl) + ' coins')
if mkl != 250:
break
page += 1
end = time.time()
wait = max(0,start+1-end)
time.sleep(wait) # API query rate limiter: Wait 1 second between API requests, including loop execution time
print('Crawl done: Total', len(mks), 'coins')
with open(filename_out,'w') as file_out:
for item in mks:
file_out.write(item['symbol'] + ';' + str(item['current_price']) + '\n')
print('Rates written to '+filename_out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment