]>
Commit | Line | Data |
---|---|---|
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 | |
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 | |
88eb6c82 | 20 | import re |
87b9ff2a CS |
21 | import json |
22 | import requests | |
23 | ||
88eb6c82 | 24 | coin_regex = ( |
99366200 CS |
25 | re.compile(r'([0-9.,\s]+)\s(btc|bitcoin|doge|dogecoin|ltc|litecoin)'), |
26 | ) | |
27 | ||
28 | cur_regex = ( | |
29 | re.compile(r'([0-9.,\s]+)\s([a-zA-Z]{3})\sin\s([a-zA-Z]{3})'), | |
88eb6c82 CS |
30 | ) |
31 | ||
87b9ff2a CS |
32 | url = 'http://www.cryptocoincharts.info/v2/api/tradingPairs' |
33 | ||
34 | def get_coin_price(pairs): | |
35 | response = requests.post(url, data = {'pairs': pairs}) | |
36 | return json.loads(response.text) | |
37 | ||
38 | @lib.hook('btc') | |
39 | def cmd_gtest(bot, user, chan, realtarget, *args): | |
40 | if len(args) > 0: | |
41 | try: | |
42 | response = get_coin_price('btc_eur') | |
43 | price = str(float(response[0]['price']) * float(args[0])) | |
44 | bot.msg(chan, "%s BTC = %s EUR" % (args[0], price)) | |
45 | except: | |
46 | bot.msg(chan, "Invalid amount.") | |
47 | else: | |
48 | response = get_coin_price('btc_eur') | |
49 | price = str(float(response[0]['price'])) | |
50 | bot.msg(chan, "1 BTC = %s EUR" % price) | |
51 | ||
52 | @lib.hook('doge') | |
53 | def cmd_gtest(bot, user, chan, realtarget, *args): | |
54 | if len(args) > 0: | |
55 | try: | |
56 | doge_btc = get_coin_price('doge_btc') | |
57 | btc_eur = get_coin_price('btc_eur') | |
58 | price = str(float(doge_btc[0]['price']) * float(btc_eur[0]['price']) * float(args[0])) | |
59 | bot.msg(chan, "%s DOGE = %s EUR" % (args[0], price)) | |
60 | except: | |
61 | bot.msg(chan, "Invalid amount.") | |
62 | else: | |
63 | doge_btc = get_coin_price('doge_btc') | |
64 | btc_eur = get_coin_price('btc_eur') | |
65 | price = str(float(doge_btc[0]['price']) * float(btc_eur[0]['price'])) | |
66 | bot.msg(chan, "1 DOGE = %s EUR" % price) | |
67 | ||
68 | @lib.hook('ltc') | |
69 | def cmd_gtest(bot, user, chan, realtarget, *args): | |
70 | if len(args) > 0: | |
71 | try: | |
72 | ltc_btc = get_coin_price('ltc_btc') | |
73 | btc_eur = get_coin_price('btc_eur') | |
74 | price = str(float(ltc_btc[0]['price']) * float(btc_eur[0]['price']) * float(args[0])) | |
75 | bot.msg(chan, "%s LTC = %s EUR" % (args[0], price)) | |
76 | except: | |
77 | bot.msg(chan, "Invalid amount.") | |
78 | else: | |
79 | ltc_btc = get_coin_price('ltc_btc') | |
80 | btc_eur = get_coin_price('btc_eur') | |
81 | price = str(float(ltc_btc[0]['price']) * float(btc_eur[0]['price'])) | |
82 | bot.msg(chan, "1 LTC = %s EUR" % price) | |
88eb6c82 CS |
83 | |
84 | @lib.hooknum("PRIVMSG") | |
85 | def privmsg_hook(bot, line): | |
86 | ||
87 | try: | |
88 | linetx = line.split(None, 3)[3][1:] | |
89 | except IndexError: | |
90 | linetx = '' | |
91 | ||
92 | chan = line.split()[2] | |
93 | ||
99366200 CS |
94 | if 'in' in line: |
95 | for r in cur_regex: | |
96 | for a, f, t in r.findall(linetx): | |
97 | ||
98 | # https://www.google.com/finance/converter?a=1.2&from=USD&to=EUR | |
99 | ||
100 | a = a.replace(",", ".") | |
101 | a = a.replace(" ", "") | |
99366200 | 102 | |
88eb6c82 CS |
103 | if 'btc' in line or 'bitcoin' in line or 'doge' in line or 'dogecoin' in line: |
104 | for r in coin_regex: | |
105 | for amount, coin in r.findall(linetx): | |
99366200 | 106 | amount = amount.replace(",", ".") |
88eb6c82 CS |
107 | amount = amount.replace(" ", "") |
108 | if 'btc' in coin or 'bitcoin' in coin: | |
109 | try: | |
110 | response = get_coin_price('btc_eur') | |
111 | price = str(float(response[0]['price']) * float(amount)) | |
112 | bot.msg(chan, "%s BTC = %s EUR" % (amount, price)) | |
113 | except: | |
114 | pass | |
115 | ||
116 | if 'ltc' in coin or 'litecoin' in coin: | |
117 | pass # Add LTC | |
118 | ||
119 | if 'doge' in coin or 'dogecoin' in coin: | |
120 | try: | |
121 | doge_btc = get_coin_price('doge_btc') | |
122 | btc_eur = get_coin_price('btc_eur') | |
123 | price = str(float(doge_btc[0]['price']) * float(btc_eur[0]['price']) * float(amount)) | |
124 | bot.msg(chan, "%s DOGE = %s EUR" % (amount, price)) | |
125 | except: | |
126 | bot.msg(chan, "Invalid amount.") |