-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmarketcap.py
More file actions
70 lines (60 loc) · 2.59 KB
/
cmarketcap.py
File metadata and controls
70 lines (60 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import requests
import os
import sys
import time
import ctypes
import threading
import argparse
from threading import Thread
currencies = ["bitcoin", "litecoin", "gulden"]
watcher = {"bitcoin": [3000, -1, False], "litecoin": [35, -1, False], "gulden": [100, -1, False]}
def print_row(currency, market_cap, price, change, color = False):
if color == True and float(change) > 0:
print ("\x1b[1;32;40m %-30s %-15s %15s %35s \x1b[0m" % (currency, market_cap, price, change))
elif color == True:
print ("\x1b[1;31;40m %-30s %-15s %15s %35s \x1b[0m" % (currency, market_cap, price, change))
else:
print (" %-30s %-15s %15s %35s" % (currency, market_cap, price, change))
def alert(title, message):
ctypes.windll.user32.MessageBoxW(0, message, title, 0)
def handle(currency):
watcher[currency['id']][1] = float(currency['price_usd'])
print_row (currency['id'], currency['market_cap_usd'], currency['price_usd'], currency['percent_change_24h'], True)
def watch(arg, stop_event):
while not stop_event.is_set():
for currency in watcher:
if watcher[currency][1] >= watcher[currency][0] and watcher[currency][2] == False:
watcher[currency][2] = True
alert(currency, "Limit reached !")
time.sleep(2)
parser = argparse.ArgumentParser(description='Trigger alert when setuped alarm limits are reached.')
parser.add_argument('--http-proxy', dest='http_proxy', required=False,help='Configure the http proxy')
parser.add_argument('--https-proxy', dest='https_proxy', required=False,help='Configure the https proxy')
args = parser.parse_args()
proxies = None
if args.http_proxy is not None:
proxies = {
'http' : args.http_proxy,
'https' : args.https_proxy if args.https_proxy is not None else args.http_proxy
}
try:
thread_stop = threading.Event()
thread = Thread(target = watch, args=(1, thread_stop))
thread.start()
while 1:
if proxies is not None:
response = requests.get('https://api.coinmarketcap.com/v1/ticker/', proxies = proxies)
else:
response = requests.get('https://api.coinmarketcap.com/v1/ticker/')
assert response.status_code == 200
os.system('cls')
print_row ("Currency", "Market Cap", "Price", "% Changes (24h)")
print_row ("--------------", "--------------", "---------", "--------------")
for currency in response.json():
if currency['id'] in currencies:
handle(currency)
except KeyboardInterrupt:
print ("Exit...")
thread_stop.set()
sys.exit()