-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
99 lines (79 loc) · 3.11 KB
/
main.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import schedule
import time
import requests
import json
import fire
COINMARKETCAP_API_URL = "https://api.coinmarketcap.com/v2/ticker/?limit=10"
COINMARKETCAP_API_URL_SINGLE_COIN = "https://api.coinmarketcap.com/v2/ticker/"
def notify(title="💰", subtitle="", message="", icon=None, url=None):
message = (
'terminal-notifier -title "%s" -subtitle "%s" -message "%s" -sound default'
% (title, subtitle, message)
)
if icon:
message += " -appIcon %s" % icon
if url:
message += " -open '%s'" % url
os.system(message)
def generate_coins_config(percent_difference=20):
"""
Writes the basic config for the top 10 currencies from CoinMarketCap to config.json
Sets a min and max at 20% difference from when the script was run
"""
url = COINMARKETCAP_API_URL
r = requests.get(url)
data = r.json()["data"]
coin_list = []
for coin_id in data:
coin_data = data[coin_id]
coin_list.append(
{
"coinmarketcap_id": coin_id,
"name": coin_data["name"],
"image": "https://s2.coinmarketcap.com/static/img/coins/32x32/%s.png"
% coin_id,
"website_url": "https://coinmarketcap.com/currencies/%s/"
% coin_data["website_slug"],
"symbol": coin_data["symbol"],
"active": True,
"max": coin_data["quotes"]["USD"]["price"]
* ((100 + percent_difference) / 100),
"min": coin_data["quotes"]["USD"]["price"]
* ((100 - percent_difference) / 100),
}
)
with open("coin-config.json", "w") as outfile:
json.dump(coin_list, outfile, sort_keys=True, indent=4, ensure_ascii=False)
message = "\n".join(["%s: %s" % (c["symbol"], c["name"]) for c in coin_list])
print("Generated coin-config.json for the following coins:\n\n" + message + "\n")
def get_coin_current_price(coin_id):
r = requests.get(COINMARKETCAP_API_URL_SINGLE_COIN + coin_id)
return r.json()["data"]["quotes"]["USD"]["price"]
def compare_prices():
config = []
with open("coin-config.json") as file:
config = json.load(file)
for coin in config:
if coin["active"]:
coin_current_price = get_coin_current_price(coin["coinmarketcap_id"])
if coin_current_price > coin["max"] or coin_current_price < coin["min"]:
notify(
title="%s Alert" % coin["name"],
subtitle="Value: %sUSD" % coin_current_price,
message="Click here to view",
icon=coin["image"],
url=coin["website_url"],
)
print("%s: %sUSD" % (coin["symbol"], coin_current_price))
def track_coins():
notify(subtitle="Watching coins")
schedule.every().minute.do(compare_prices)
print("Initiated Coin Price tracker")
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == "__main__":
fire.Fire(
{"track_coins": track_coins, "generate_coins_config": generate_coins_config,}
)