Created
December 4, 2021 03:31
-
-
Save ranuzz/2c45296b0d0c51f25a288a54a1855e95 to your computer and use it in GitHub Desktop.
crypto tracker that rings a bell when desired change has happened in price
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
""" | |
crypto tracker that rings a bell when desired change has happened in price | |
using coincap.io | |
spported assets: bitcoin, ethereum and more | |
""" | |
import http.client | |
import json | |
import sys | |
import time | |
COINCAP_API_HOST = 'api.coincap.io' | |
COINCAP_API_GET_ASSETS = '/v2/assets?ids={0}' | |
COINCAP_API_GET_ASSET_BY_ID = '/v2/assets/{0}' | |
SUPPORTED_IDS = ['bitcoin', 'ethereum'] | |
ASSETS = {} | |
for aid in SUPPORTED_IDS: | |
ASSETS[aid] = {} | |
def get_all_assets(): | |
conn = http.client.HTTPSConnection(COINCAP_API_HOST) | |
conn.request('GET', COINCAP_API_GET_ASSETS.format(','.join(SUPPORTED_IDS))) | |
response = conn.getresponse() | |
if response.status == 200: | |
data = json.loads(response.read()) | |
for a in data.get('data'): | |
if a.get('id') in SUPPORTED_IDS: | |
ASSETS[a.get('id')] = a | |
conn.close() | |
def ring_bell(): | |
sys.stdout.write('\a') | |
sys.stdout.flush() | |
def get_asset(asset): | |
conn = http.client.HTTPSConnection(COINCAP_API_HOST) | |
conn.request('GET', COINCAP_API_GET_ASSET_BY_ID.format(asset)) | |
response = conn.getresponse() | |
data = None | |
if response.status == 200: | |
data = json.loads(response.read()).get('data') | |
conn.close() | |
return data | |
def track(asset, change): | |
data = None | |
while True: | |
try: | |
cur_data = get_asset(asset) | |
if not data: | |
data = cur_data | |
if not cur_data == None: | |
cur_price = float(cur_data.get('priceUsd')) | |
old_price = float(data.get('priceUsd')) | |
diff = ((cur_price - old_price) / old_price) * 100 | |
if change > 0 and change <= diff: | |
ring_bell() | |
elif change < 0 and change >= diff: | |
ring_bell() | |
print("{0} : changed {1} % : prev_price {2} : cur_price {3}".format(asset, diff, old_price, cur_price)) | |
except: | |
pass | |
time.sleep(600) | |
def usage(): | |
print('[program] [asset] [change]') | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
usage() | |
sys.exit() | |
try: | |
asset = str(sys.argv[1]) | |
change = float(sys.argv[2]) | |
except: | |
usage() | |
sys.exit() | |
if not asset in SUPPORTED_IDS: | |
print('Supported Assets : {0}'.format(SUPPORTED_IDS)) | |
sys.exit() | |
track(asset, change) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment