]> jfr.im git - erebus.git/blame - modules/coins.py
urls - massive rework
[erebus.git] / modules / coins.py
CommitLineData
87b9ff2a 1# Erebus IRC bot - Author: Erebus Team
4477123d 2# vim: fileencoding=utf-8
87b9ff2a
CS
3# simple coin module
4# This file is released into the public domain; see http://unlicense.org/
5
6# module info
7modinfo = {
8 'author': 'Erebus Team',
9 'license': 'public domain',
fa93b933 10 'compatible': [0],
a62d0d18 11 'depends': [],
12 'softdeps': [],
87b9ff2a
CS
13}
14
15# preamble
16import modlib
17lib = modlib.modlib(__name__)
18modstart = lib.modstart
19modstop = lib.modstop
20
21# module code
88eb6c82 22import re
87b9ff2a
CS
23import json
24import requests
25
88eb6c82 26coin_regex = (
99366200
CS
27 re.compile(r'([0-9.,\s]+)\s(btc|bitcoin|doge|dogecoin|ltc|litecoin)'),
28)
29
30cur_regex = (
31 re.compile(r'([0-9.,\s]+)\s([a-zA-Z]{3})\sin\s([a-zA-Z]{3})'),
88eb6c82
CS
32)
33
87b9ff2a
CS
34url = 'http://www.cryptocoincharts.info/v2/api/tradingPairs'
35
36def get_coin_price(pairs):
37 response = requests.post(url, data = {'pairs': pairs})
38 return json.loads(response.text)
39
fb20be7c 40@lib.hook()
41def btc(bot, user, chan, realtarget, *args):
87b9ff2a
CS
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
fb20be7c 54@lib.hook()
55def doge(bot, user, chan, realtarget, *args):
87b9ff2a
CS
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
fb20be7c 70@lib.hook()
71def ltc(bot, user, chan, realtarget, *args):
87b9ff2a
CS
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)
88eb6c82
CS
85
86@lib.hooknum("PRIVMSG")
87def 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
99366200
CS
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(" ", "")
99366200 104
88eb6c82
CS
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):
99366200 108 amount = amount.replace(",", ".")
88eb6c82
CS
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.")