Skip to content

Instantly share code, notes, and snippets.

@McKlayne
Created June 18, 2021 04:22
Show Gist options
  • Save McKlayne/95b896c30145085b31e193a5c4c01a4f to your computer and use it in GitHub Desktop.
Save McKlayne/95b896c30145085b31e193a5c4c01a4f to your computer and use it in GitHub Desktop.
def get_coingecko_data(crypto_ids, crypto_abbreviations):
# define the base url and another piece that will be added later
base = 'https://api.coingecko.com/api/v3/simple/price?ids='
vs_currencies_string= '&vs_currencies='
# determine the number of ids in the list. This will help us know how many commas to add to the url
id_remaining = len(crypto_ids)
# loop through each coin and append its id to the url. Decrease id_remaining each iteration, add a comma to the url if it is needed
for coin in crypto_ids:
base += coin
id_remaining -= 1
if id_remaining > 0:
base += ','
# add the second predetermine piece to the url
base += vs_currencies_string
# determine the number of abbreviations in the id file. This will help us know how many commas to add to the url
vs_remaining = len(crypto_abbreviations)
# loop through each coin and append its abbreviation to the url. Decrease vs_remaining each iteration, add a comma to the url if it is needed
for vs in crypto_abbreviations:
base += vs
vs_remaining -= 1
if vs_remaining > 0:
base += ','
# print url to verify it looks correct
print(base)
# request the constructed url to get the data
request = requests.get(base)
results_dict = json.loads(request.text)
# print(results_dict['bitcoin-cash'])
return(results_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment