forked from PCFiL/monero_girl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.rb
143 lines (112 loc) · 4.26 KB
/
bot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require "rubygems"
require "bundler/setup"
require "json"
require "rest-client"
require "cinch"
require "thread"
require "yaml"
CONFIG = YAML.load_file(File.expand_path("config.yml"))
POOLS_FILE = File.expand_path("pools.yml")
LOCK = Mutex.new
def refresh_pools
LOCK.synchronize do
ctime = File.ctime(POOLS_FILE)
if (@pools_ctime.nil? || (@pools_ctime != ctime))
@pools = YAML.load_file(POOLS_FILE)
@pools_ctime = ctime
end
end
end
def refresh_price
LOCK.synchronize do
@price ||= {}
if (@last_price_update.nil? || (Time.now - @last_price_update > 180))
resp = RestClient.get("https://poloniex.com/public?command=returnTicker")
@price["Poloniex"] = {}
@price["Poloniex"]["price"] = JSON.parse(resp)["BTC_XMR"]["last"].to_f.round(8)
@price["Poloniex"]["vol"] = JSON.parse(resp)["BTC_XMR"]["baseVolume"].to_f.round(2)
resp = RestClient.get("http://api.hitbtc.com/api/1/public/XMRBTC/ticker")
@price["HitBTC"] = {}
@price["HitBTC"]["price"] = JSON.parse(resp)["last"].to_f.round(8)
@price["HitBTC"]["vol"] = JSON.parse(resp)["volume"].to_f.round(2)
resp = RestClient.get("https://api.mintpal.com/v1/market/stats/XMR/BTC")
@price["Mintpal"] = {}
@price["Mintpal"]["price"] = JSON.parse(resp)[0]["last_price"].to_f.round(8)
@price["Mintpal"]["vol"] = JSON.parse(resp)[0]["24hvol"].to_f.round(2)
resp = RestClient.get("http://data.bter.com/api/1/ticker/xmr_btc")
@price["Bter"] = {}
@price["Bter"]["price"] = JSON.parse(resp)["last"].to_f.round(8)
@price["Bter"]["vol"] = JSON.parse(resp)["vol_xmr"].to_f.round(2)
@last_price_update = Time.now
end
end
end
def refresh_stats
LOCK.synchronize do
if (@last_stats_update.nil? || (Time.now - @last_stats_update > 60))
url = "http://#{CONFIG["daemon"]["rpc_host"]}:#{CONFIG["daemon"]["rpc_port"]}/json_rpc"
body = { "jsonrpc" => "2.0", "id" => "test", "method" => "get_info" }
resp = RestClient.post(url, body.to_json)
@stats = JSON.parse(resp)["result"]
@last_stats_update = Time.now
end
end
end
def silence?(channel)
return false if channel.nil? # private message
CONFIG["silencers"].each do |nick|
return true if channel.has_user?(nick)
end
false
end
bot = Cinch::Bot.new do
configure do |c|
c.nick = CONFIG["nick"]
c.password = CONFIG["password"]
c.server = CONFIG["server"]
c.channels = CONFIG["channels"]
end
on :message, "!help" do |m|
next if silence?(m.channel)
m.user.msg "Commands: !pools, !worth <amount>, !price, !diff, !calc <hashrate>"
end
on :message, "!pools" do |m|
next if silence?(m.channel)
refresh_pools
reply = "List of available Monero pools (randomized):\n\n"
@pools.shuffle.each do |pool|
name = pool.keys[0]
reply << "#{name} #{pool[name][1]}, #{pool[name][0]}% fee\n"
end
m.user.msg reply
m.user.msg "Send pull-request with your pool to https://github.com/sammy007/monero_girl"
end
on :message, "!diff" do |m|
next if silence?(m.channel)
refresh_stats
diff = @stats["difficulty"]
m.user.msg "Difficulty: #{diff}"
end
on :message, "!price" do |m|
next if silence?(m.channel)
refresh_price
m.user.msg "Last: #{@price["Poloniex"]["price"]} BTC | Volume: #{@price["Poloniex"]["vol"]} BTC | Poloniex | https://poloniex.com/exchange/btc_xmr"
m.user.msg "Last: #{@price["HitBTC"]["price"]} BTC | Volume: #{@price["HitBTC"]["vol"]} XMR | HitBTC.com | https://hitbtc.com/terminal#XMRBTC"
m.user.msg "Last: #{@price["Mintpal"]["price"]} BTC | Volume: #{@price["Mintpal"]["vol"]} BTC | Mintpal | https://www.mintpal.com/market/XMR/BTC"
m.user.msg "Last: #{@price["Bter"]["price"]} BTC | Volume: #{@price["Bter"]["vol"]} XMR | Bter | https://bter.com/trade/XMR_BTC"
end
on :message, /^!worth (\d+)/ do |m, amount|
next if silence?(m.channel)
refresh_price
total = amount.to_f * @price["Poloniex"]["price"].to_f
m.user.msg "#{amount} XMR = #{total} BTC"
end
on :message, /^!calc (\d+)/ do |m, hashrate|
next if silence?(m.channel)
refresh_stats
diff = @stats["difficulty"]
total = 15 / (diff / hashrate.to_f / 86400)
m.user.msg "With #{hashrate} H/s you will mine ~#{total.round(8)} XMR per day"
end
end
bot.start