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