Skip to content

Instantly share code, notes, and snippets.

@itherunder
Created December 15, 2021 03:08
Show Gist options
  • Save itherunder/f57d89dabc3daf32fdf91cfe3eae1a0a to your computer and use it in GitHub Desktop.
Save itherunder/f57d89dabc3daf32fdf91cfe3eae1a0a to your computer and use it in GitHub Desktop.
使用`coingecko`的`api`获取`token`的价格,需要先把`token_list_url`上面的所有`token`列表保存到`tokens.txt`
import requests, json
token_list_url = 'https://api.coingecko.com/api/v3/coins/list'
url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=%s'
tokens = json.loads(open('tokens.txt', 'r', encoding='utf-8').read())
symbol_to_id = {}
for token in tokens:
id, symbol = token['id'], token['symbol'].lower()
if symbol in symbol_to_id:
continue
symbol_to_id[symbol] = id
def get_price(symbol):
id = symbol_to_id[symbol.lower()]
res = requests.get(url % id)
if res.status_code != 200:
print(res.status_code)
return (0, 0)
obj = json.loads(res.text)[0]
return (obj['current_price'], obj['price_change_percentage_24h'])
def big_token_test():
with open('big_tokens.txt', 'r', encoding='utf-8') as r:
for l in r.readlines()[::-1]:
symbol, _ = l.strip().split('#')
print(symbol, symbol_to_id[symbol.lower()])
print(get_price(symbol))
# if __name__ == '__main__':
# big_token_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment