Skip to content

Instantly share code, notes, and snippets.

@ckhung
Last active December 5, 2021 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckhung/6f10f7146f8074aef15b457c6a20c731 to your computer and use it in GitHub Desktop.
Save ckhung/6f10f7146f8074aef15b457c6a20c731 to your computer and use it in GitHub Desktop.
generate a csv listing of your favorite cryptocurrencies, suitable for importing into a google sheet
<?php
// usage:
// http://.../gecko.pgp??q=bitcoin,ethereum,solana,polkadot
// or
// http://.../gecko.pgp??q=mycrypto.csv
// where mycrypto.csv contains the id of a coin on each line.
// Find the exact id of your coin in this list:
// https://api.coingecko.com/api/v3/coins/list
// See this doc for more info:
// https://www.coingecko.com/en/api/documentation
header("Content-Type: text/plain");
$coins = array('bitcoin', 'ethereum', 'harmony');
if (! array_key_exists('q', $_GET)) {
$_GET['q'] = 'ethereum';
}
if (preg_match('/\.csv$/', $_GET['q'])) {
$lines = file($_GET['q']);
$coins = '';
foreach ($lines as $L) {
if (preg_match('/^#/', $L) || preg_match('/^\s*$/', $L)) continue;
if (preg_match('/^([\w-]+)/', $L, $matches)) {
$coins .= ",$matches[1]";
}
}
$coins = substr($coins, 1);
} else {
$coins = $_GET['q'];
}
$gecko = "https://api.coingecko.com/api/v3/simple/price?ids=$coins&vs_currencies=usd&include_market_cap=true";
$data = json_decode(file_get_contents($gecko), true);
$all_coins = array_keys($data);
function cmp_market_cap($a, $b) {
global $data;
if ($data[$a]['usd_market_cap'] < $data[$b]['usd_market_cap']) {
return -1;
} elseif ($data[$a]['usd_market_cap'] > $data[$b]['usd_market_cap']) {
return 1;
} else {
return 0;
}
}
usort($all_coins, "cmp_market_cap");
print("coin_name,price,market_cap\n");
foreach (array_reverse($all_coins) as $coin) {
$info = $data[$coin];
printf("$coin,$info[usd],%0.3f\n", $info[usd_market_cap]/1e9);
}
?>
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
bitcoin
harmony
ethereum
cardano
solana
polkadot
bitcoin-cash
nexo
maxcoin
matic-network
yield-guild-games
gatechain-token
binancecoin
dfyn-network
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment