]> jfr.im git - erebus.git/blame - modules/coins.py
weather - dont fail when wunderground screws up time
[erebus.git] / modules / coins.py
CommitLineData
87b9ff2a
CS
1# Erebus IRC bot - Author: Erebus Team
2# simple coin module
3# This file is released into the public domain; see http://unlicense.org/
4
5# module info
6modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
fa93b933 9 'compatible': [0],
a62d0d18 10 'depends': [],
11 'softdeps': [],
87b9ff2a
CS
12}
13
14# preamble
15import modlib
16lib = modlib.modlib(__name__)
17modstart = lib.modstart
18modstop = lib.modstop
19
20# module code
88eb6c82 21import re
87b9ff2a
CS
22import json
23import requests
24
88eb6c82 25coin_regex = (
99366200
CS
26 re.compile(r'([0-9.,\s]+)\s(btc|bitcoin|doge|dogecoin|ltc|litecoin)'),
27)
28
29cur_regex = (
30 re.compile(r'([0-9.,\s]+)\s([a-zA-Z]{3})\sin\s([a-zA-Z]{3})'),
88eb6c82
CS
31)
32
87b9ff2a
CS
33url = 'http://www.cryptocoincharts.info/v2/api/tradingPairs'
34
35def get_coin_price(pairs):
36 response = requests.post(url, data = {'pairs': pairs})
37 return json.loads(response.text)
38
fb20be7c 39@lib.hook()
40def btc(bot, user, chan, realtarget, *args):
87b9ff2a
CS
41 if len(args) > 0:
42 try:
43 response = get_coin_price('btc_eur')
44 price = str(float(response[0]['price']) * float(args[0]))
45 bot.msg(chan, "%s BTC = %s EUR" % (args[0], price))
46 except:
47 bot.msg(chan, "Invalid amount.")
48 else:
49 response = get_coin_price('btc_eur')
50 price = str(float(response[0]['price']))
51 bot.msg(chan, "1 BTC = %s EUR" % price)
52
fb20be7c 53@lib.hook()
54def doge(bot, user, chan, realtarget, *args):
87b9ff2a
CS
55 if len(args) > 0:
56 try:
57 doge_btc = get_coin_price('doge_btc')
58 btc_eur = get_coin_price('btc_eur')
59 price = str(float(doge_btc[0]['price']) * float(btc_eur[0]['price']) * float(args[0]))
60 bot.msg(chan, "%s DOGE = %s EUR" % (args[0], price))
61 except:
62 bot.msg(chan, "Invalid amount.")
63 else:
64 doge_btc = get_coin_price('doge_btc')
65 btc_eur = get_coin_price('btc_eur')
66 price = str(float(doge_btc[0]['price']) * float(btc_eur[0]['price']))
67 bot.msg(chan, "1 DOGE = %s EUR" % price)
68
fb20be7c 69@lib.hook()
70def ltc(bot, user, chan, realtarget, *args):
87b9ff2a
CS
71 if len(args) > 0:
72 try:
73 ltc_btc = get_coin_price('ltc_btc')
74 btc_eur = get_coin_price('btc_eur')
75 price = str(float(ltc_btc[0]['price']) * float(btc_eur[0]['price']) * float(args[0]))
76 bot.msg(chan, "%s LTC = %s EUR" % (args[0], price))
77 except:
78 bot.msg(chan, "Invalid amount.")
79 else:
80 ltc_btc = get_coin_price('ltc_btc')
81 btc_eur = get_coin_price('btc_eur')
82 price = str(float(ltc_btc[0]['price']) * float(btc_eur[0]['price']))
83 bot.msg(chan, "1 LTC = %s EUR" % price)
88eb6c82
CS
84
85@lib.hooknum("PRIVMSG")
86def privmsg_hook(bot, line):
87
88 try:
89 linetx = line.split(None, 3)[3][1:]
90 except IndexError:
91 linetx = ''
92
93 chan = line.split()[2]
94
99366200
CS
95 if 'in' in line:
96 for r in cur_regex:
97 for a, f, t in r.findall(linetx):
98
99 # https://www.google.com/finance/converter?a=1.2&from=USD&to=EUR
100
101 a = a.replace(",", ".")
102 a = a.replace(" ", "")
99366200 103
88eb6c82
CS
104 if 'btc' in line or 'bitcoin' in line or 'doge' in line or 'dogecoin' in line:
105 for r in coin_regex:
106 for amount, coin in r.findall(linetx):
99366200 107 amount = amount.replace(",", ".")
88eb6c82
CS
108 amount = amount.replace(" ", "")
109 if 'btc' in coin or 'bitcoin' in coin:
110 try:
111 response = get_coin_price('btc_eur')
112 price = str(float(response[0]['price']) * float(amount))
113 bot.msg(chan, "%s BTC = %s EUR" % (amount, price))
114 except:
115 pass
116
117 if 'ltc' in coin or 'litecoin' in coin:
118 pass # Add LTC
119
120 if 'doge' in coin or 'dogecoin' in coin:
121 try:
122 doge_btc = get_coin_price('doge_btc')
123 btc_eur = get_coin_price('btc_eur')
124 price = str(float(doge_btc[0]['price']) * float(btc_eur[0]['price']) * float(amount))
125 bot.msg(chan, "%s DOGE = %s EUR" % (amount, price))
126 except:
127 bot.msg(chan, "Invalid amount.")