]> jfr.im git - erebus.git/blob - modules/coins.py
Modified coins.py, committing what i have atm
[erebus.git] / modules / coins.py
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
6 modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [1], # compatible module API versions
10 'depends': [], # other modules required to work properly?
11 }
12
13 # preamble
14 import modlib
15 lib = modlib.modlib(__name__)
16 modstart = lib.modstart
17 modstop = lib.modstop
18
19 # module code
20 import re
21 import json
22 import requests
23
24 coin_regex = (
25 re.compile(r'([0-9., ]+[0-9]+) (BTC|bitcoin|doge|dogecoin|ltc|litecoin)'), # Fix regex
26 )
27
28 url = 'http://www.cryptocoincharts.info/v2/api/tradingPairs'
29
30 def get_coin_price(pairs):
31 response = requests.post(url, data = {'pairs': pairs})
32 return json.loads(response.text)
33
34 @lib.hook('btc')
35 def cmd_gtest(bot, user, chan, realtarget, *args):
36 if len(args) > 0:
37 try:
38 response = get_coin_price('btc_eur')
39 price = str(float(response[0]['price']) * float(args[0]))
40 bot.msg(chan, "%s BTC = %s EUR" % (args[0], price))
41 except:
42 bot.msg(chan, "Invalid amount.")
43 else:
44 response = get_coin_price('btc_eur')
45 price = str(float(response[0]['price']))
46 bot.msg(chan, "1 BTC = %s EUR" % price)
47
48 @lib.hook('doge')
49 def cmd_gtest(bot, user, chan, realtarget, *args):
50 if len(args) > 0:
51 try:
52 doge_btc = get_coin_price('doge_btc')
53 btc_eur = get_coin_price('btc_eur')
54 price = str(float(doge_btc[0]['price']) * float(btc_eur[0]['price']) * float(args[0]))
55 bot.msg(chan, "%s DOGE = %s EUR" % (args[0], price))
56 except:
57 bot.msg(chan, "Invalid amount.")
58 else:
59 doge_btc = get_coin_price('doge_btc')
60 btc_eur = get_coin_price('btc_eur')
61 price = str(float(doge_btc[0]['price']) * float(btc_eur[0]['price']))
62 bot.msg(chan, "1 DOGE = %s EUR" % price)
63
64 @lib.hook('ltc')
65 def cmd_gtest(bot, user, chan, realtarget, *args):
66 if len(args) > 0:
67 try:
68 ltc_btc = get_coin_price('ltc_btc')
69 btc_eur = get_coin_price('btc_eur')
70 price = str(float(ltc_btc[0]['price']) * float(btc_eur[0]['price']) * float(args[0]))
71 bot.msg(chan, "%s LTC = %s EUR" % (args[0], price))
72 except:
73 bot.msg(chan, "Invalid amount.")
74 else:
75 ltc_btc = get_coin_price('ltc_btc')
76 btc_eur = get_coin_price('btc_eur')
77 price = str(float(ltc_btc[0]['price']) * float(btc_eur[0]['price']))
78 bot.msg(chan, "1 LTC = %s EUR" % price)
79
80 @lib.hooknum("PRIVMSG")
81 def privmsg_hook(bot, line):
82
83 try:
84 linetx = line.split(None, 3)[3][1:]
85 except IndexError:
86 linetx = ''
87
88 chan = line.split()[2]
89
90 if 'btc' in line or 'bitcoin' in line or 'doge' in line or 'dogecoin' in line:
91 for r in coin_regex:
92 for amount, coin in r.findall(linetx):
93 amount = amount.replace(" ", "")
94 if 'btc' in coin or 'bitcoin' in coin:
95 try:
96 response = get_coin_price('btc_eur')
97 price = str(float(response[0]['price']) * float(amount))
98 bot.msg(chan, "%s BTC = %s EUR" % (amount, price))
99 except:
100 pass
101
102 if 'ltc' in coin or 'litecoin' in coin:
103 pass # Add LTC
104
105 if 'doge' in coin or 'dogecoin' in coin:
106 try:
107 doge_btc = get_coin_price('doge_btc')
108 btc_eur = get_coin_price('btc_eur')
109 price = str(float(doge_btc[0]['price']) * float(btc_eur[0]['price']) * float(amount))
110 bot.msg(chan, "%s DOGE = %s EUR" % (amount, price))
111 except:
112 bot.msg(chan, "Invalid amount.")
113
114
115 print amount
116 print coin