This repository was archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcur.py
80 lines (71 loc) · 2.99 KB
/
cur.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
import requests
import sys
import json
import sopel
with open('/home/bot/.sopel/cur_py_cache') as read_file:
last_prices = json.load(read_file)
main_coins = ["btc", "bch", "xrp", "eth", "iot", "ltc"]
single_url = "https://api.bitfinex.com/v1/pubticker/{0}usd"
@sopel.module.rule('^\.({0})$'.format("|".join(main_coins)))
def crypto_spot(bot, trigger):
from_cur = trigger.group(1)
global last_prices
from_cur = from_cur.lower()
if from_cur not in main_coins:
bot.say("Invalid currency!")
api_result = requests.get(single_url.format(from_cur)).json()
if from_cur not in last_prices:
last_prices[from_cur] = 0
digits = False if from_cur.lower()=='xrp' else True
diffStr = getDiffString(float(api_result["last_price"]), last_prices[from_cur], digits)
last_prices[from_cur] = float(api_result["last_price"])
with open('/home/bot/.sopel/cur_py_cache', 'w') as outfile:
json.dump(last_prices, outfile)
bot.say("{0}: ${1:.{2}f}{3}".format(from_cur, float(api_result["last_price"]), 2 if digits else 4, diffStr))
@sopel.module.commands('ticker','tick')
def tick(bot, trigger):
global last_prices
results = []
for coin in main_coins:
api_result = requests.get(single_url.format(coin)).json()
if coin not in last_prices:
last_prices[coin] = 0
digits = False if coin.lower() == 'xrp' else True
diffStr = getDiffString(float(api_result["last_price"]), last_prices[coin], digits)
last_prices[coin] = float(api_result["last_price"])
results.append("{0}: ${1:.{2}f}{3}".format(coin, float(api_result["last_price"]), 2 if digits else 4, diffStr))
with open('/home/bot/.sopel/cur_py_cache', 'w') as outfile:
json.dump(last_prices, outfile)
bot.say(" | ".join(results))
def getDiffString(current_price, last_price, crates=False):
global last_prices
diff = current_price - last_price
diffStr = ""
if diff != 0:
sign = "+" if diff > 0 else ''
diffStr = " ({0}{1:.{2}f})".format(sign, diff, 2 if crates else 4)
with open('/home/bot/.sopel/cur_py_cache', 'w') as outfile:
json.dump(last_prices, outfile)
return diffStr
def getSteamMarketPrice(item):
global last_prices
steam_price = requests.get("https://steamcommunity.com/market/priceoverview/?appid=578080¤cy=1&market_hash_name={}".format(item)).json()
name = item.replace(" CRATE","")
name = name.replace(" INVITATIONAL","")
lowest_price = steam_price['lowest_price']
if name not in last_prices:
last_prices[name] = '$0'
last_price = '$0'
last_price = last_prices[name]
diffStr = getDiffString(float(lowest_price[1:]),float(last_price[1:]), True)
last_prices[name] = lowest_price
return "{0}: {1}{2}".format(name, lowest_price, diffStr)
@sopel.module.commands('crates')
def crates(bot, trigger):
crates = ['SURVIVOR CRATE','WANDERER CRATE','GAMESCOM INVITATIONAL CRATE']
results = []
for crate in crates:
results.append(getSteamMarketPrice(crate))
with open('/home/bot/.sopel/cur_py_cache', 'w') as outfile:
json.dump(last_prices, outfile)
bot.say(" | ".join(results))