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