Skip to content

Instantly share code, notes, and snippets.

@tpandit
Last active September 14, 2023 17:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tpandit/b2bc4f434ee7f5fd890e095e79283aec to your computer and use it in GitHub Desktop.
Save tpandit/b2bc4f434ee7f5fd890e095e79283aec to your computer and use it in GitHub Desktop.
Crypto currency live monitor that auto updates
import curses
import time
import sys
import requests
# Constants
CURRENCY='USD'
REQUESTS_PER_MINUTE=10
REQUEST_URI='https://api.coinmarketcap.com/v1/ticker/'
## Display related methods/classes
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
TITLE_ROW = 'Rank\tSymbol\tName\t\tPrice\t\t24h Volume\tMarket Cap\t% Change (1h)\t% Change (24h)\t% Change (7d)\tAvailable Supply\tLast Updated'
def report_progress(data):
stdscr.scrollok(1)
stdscr.idlok(1)
stdscr.scroll(100)
stdscr.addstr(0, 0, TITLE_ROW, curses.A_BOLD)
for idx, bit_coin in enumerate(data):
if idx > 51:
break
stdscr.addstr(idx+2, 0, "{}\t".format(bit_coin['rank']))
stdscr.addstr("{}\t".format(bit_coin['symbol']), curses.color_pair(4))
stdscr.addstr("{}\t\t".format(bit_coin['name'][:7]))
stdscr.addstr("{}\t\t".format(bit_coin['price_usd'][:7]), curses.color_pair(3))
stdscr.addstr("{:10}\t".format(bit_coin['24h_volume_usd']))
stdscr.addstr("{:13}\t".format(bit_coin['market_cap_usd']))
if float(bit_coin.get('percent_change_1h',0.0)) > 0:
stdscr.addstr("{}\t\t".format(bit_coin['percent_change_1h'][:7]), curses.color_pair(1))
elif float(bit_coin.get('percent_change_1h',0.0)) < 0:
stdscr.addstr("{}\t\t".format(bit_coin['percent_change_1h'][:7]), curses.color_pair(2))
else:
stdscr.addstr("None\t\t")
if float(bit_coin.get('percent_change_24h',0.0)) > 0:
stdscr.addstr("{}\t\t".format(bit_coin['percent_change_24h'][:7]), curses.color_pair(1))
elif float(bit_coin.get('percent_change_24h',0.0)) < 0:
stdscr.addstr("{}\t\t".format(bit_coin['percent_change_24h'][:7]), curses.color_pair(2))
else:
stdscr.addstr("None\t\t")
if bit_coin.get('percent_change_7d'):
if float(bit_coin.get('percent_change_7d',0.0)) > 0:
stdscr.addstr("{}\t\t".format(bit_coin['percent_change_7d'][:7]), curses.color_pair(1))
elif float(bit_coin.get('percent_change_7d',0.0)) < 0:
stdscr.addstr("{}\t\t".format(bit_coin['percent_change_7d'][:7]), curses.color_pair(2))
else:
stdscr.addstr("None\t\t")
stdscr.addstr("{}\t\t".format(bit_coin['available_supply'][:7]))
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(bit_coin['last_updated'])))
stdscr.addstr("{}".format(str(local_time)))
stdscr.refresh()
# Service request
def get_data():
r = requests.get(REQUEST_URI)
if r.status_code != 200:
print "Cannot get data from {}".format(REQUEST_URI)
print "Return code: {}, Message: {}".format(r.status_code, r.data)
return None
else:
return r.json()
if __name__ == "__main__":
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_CYAN, curses.COLOR_BLACK)
try:
while True:
resp = get_data()
report_progress(get_data())
time.sleep(60/REQUESTS_PER_MINUTE)
finally:
curses.echo()
curses.nocbreak()
curses.endwin()
@tpandit
Copy link
Author

tpandit commented Nov 8, 2017

MAC OSX setup

  • Open terminal on your mac and run the following commands:
# Install python
brew install python

# Install pip (python module installer)
sudo easy_install pip

# Install requests module 
pip install requests
  • Copy the file and run it
python snoopy.py
  • Known issues

If the terminal window is too small, code will crash. Just full screen
the window before running.

If you get this error, that means its a resolution/window size issue

_curses.error: addstr() returned ERR

You can try to lower the idx value on line 31

Windows setup

Buy a mac and then follow MAC OSX setup. :P

@tpandit
Copy link
Author

tpandit commented Nov 8, 2017

screen shot 2017-11-07 at 11 36 06 pm

@vladertel
Copy link

Fix:
resp = get_data()
report_progress(resp)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment