-
Notifications
You must be signed in to change notification settings - Fork 2
/
growl_bitcoin.rb
executable file
·146 lines (122 loc) · 4.99 KB
/
growl_bitcoin.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
144
145
146
# encoding: utf-8 #
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'net/http'
require 'money'
require 'growl'
require 'colorize'
include Growl
bitmarket_eur_sell = bitmarket_eur_buy = bitmarket_eur_last = Money.new(0, "EUR")
mtgox_eur_sell = mtgox_eur_buy =mtgox_eur_last = Money.new(0, "USD")
max_wait_time_to_display = 90
def pdiff(pnew, pold)
up_mark = 1.007
down_mark = 0.993
mark_down_exceeded = pnew < (pold * down_mark)
mark_up_exceeded = pnew > (pold * up_mark)
col1 = mark_down_exceeded ? "down".on_black.red : "down".green
col2 = mark_up_exceeded ? "up".on_black.red : "up".green
poldt = pold.to_s.yellow
if pold == pnew
pnewt = pnew.to_s.yellow
elsif pold < pnew
pnewt = pnew.to_s.on_black.green
elsif pold > pnew
pnewt = pnew.to_s.on_black.red
end
puts "[#{poldt}; #{pnewt}]\t #{col1} (#{(pold * down_mark)}): #{mark_down_exceeded},\t #{col2} (#{(pold * up_mark)}): #{mark_up_exceeded}"
return mark_down_exceeded || mark_up_exceeded
end
def bitmarket_getcurr
uri = URI.parse("https://bitmarket.eu/api/ticker/")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.read_timeout = 90
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
result = JSON.parse(response.body)
# currencies"=>{"EUR"=>{"sell"=>"12.02", "buy"=>"12.11", "last"=>"12.05"}
end
def mtgox_getcurr
uri = URI.parse("https://mtgox.com/code/data/ticker.php")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.read_timeout = 90
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
result = JSON.parse(response.body)
# currencies"=>{"EUR"=>{"sell"=>"12.02", "buy"=>"12.11", "last"=>"12.05"}
end
interrupted = false
trap("INT") { interrupted = true }
i = 0
while true do
begin
result = bitmarket_getcurr
rescue
text = "====== Bitmarket.eu Timeout ERROR ======"
puts text.on_black.red
end
begin
result2 = mtgox_getcurr
rescue
text = "====== Mt.Gox Timeout ERROR ======"
puts text.on_black.red
end
begin
puts "#{Time.new} - New query, i = #{i}\n"
new_bitmarket_eur_sell = Money.new((result["currencies"]["EUR"]["sell"].to_f * 100.0).to_i, "EUR")
new_bitmarket_eur_buy = Money.new((result["currencies"]["EUR"]["buy"].to_f * 100.0).to_i, "EUR")
new_bitmarket_eur_last = Money.new((result["currencies"]["EUR"]["last"].to_f * 100.0).to_i, "EUR")
new_mtgox_eur_sell = Money.new((result2["ticker"]["sell"].to_f * 100.0).to_i, "USD")
new_mtgox_eur_buy = Money.new((result2["ticker"]["buy"].to_f * 100.0).to_i, "USD")
new_mtgox_eur_last = Money.new((result2["ticker"]["last"].to_f * 100.0).to_i, "USD")
bitmarket_changed = (pdiff(new_bitmarket_eur_sell, bitmarket_eur_sell) || pdiff(new_bitmarket_eur_buy, bitmarket_eur_buy) || pdiff(new_bitmarket_eur_last, bitmarket_eur_last))
mtgox_changed = (pdiff(new_mtgox_eur_sell, mtgox_eur_sell) || pdiff(new_mtgox_eur_buy, mtgox_eur_buy) || pdiff(new_mtgox_eur_last, mtgox_eur_last))
changed = bitmarket_changed || mtgox_changed
if i > max_wait_time_to_display || changed || i == 0
text = ""
if i > max_wait_time_to_display || bitmarket_changed || i == 0
text += "Bitmarket.eu"
text += ": NIX PASSIERT (#{new_bitmarket_eur_last})" if !bitmarket_changed
text += " #{new_bitmarket_eur_last > bitmarket_eur_last ? '++++++++' : '--------'}"
text += "\n[Last Trade] #{bitmarket_eur_last} € -> #{new_bitmarket_eur_last} €" if pdiff(new_bitmarket_eur_last, bitmarket_eur_last)
text += "\n[Last Sell] #{bitmarket_eur_sell} € -> #{new_bitmarket_eur_sell} €" if pdiff(new_bitmarket_eur_sell, bitmarket_eur_sell)
text += "\n[Last Buy] #{bitmarket_eur_buy} € -> #{new_bitmarket_eur_buy} €" if pdiff(new_bitmarket_eur_buy, bitmarket_eur_buy)
end
if i > max_wait_time_to_display || mtgox_changed || i == 0
text += "\n\nMt.Gox"
text += ": NIX PASSIERT (#{new_mtgox_eur_last})" if !mtgox_changed
text += " #{new_mtgox_eur_last > mtgox_eur_last ? '++++++++' : '--------'}"
text += "\n[Last Trade] #{mtgox_eur_last} $ -> #{new_mtgox_eur_last} $" if pdiff(new_mtgox_eur_last, mtgox_eur_last)
text += "\n[Last Sell] #{mtgox_eur_sell} $ -> #{new_mtgox_eur_sell} $" if pdiff(new_mtgox_eur_sell, mtgox_eur_sell)
text += "\n[Last Buy] #{ mtgox_eur_buy} $ -> #{ new_mtgox_eur_buy} $" if pdiff(new_mtgox_eur_buy, mtgox_eur_buy)
end
Growl.notify {
self.message = text
self.image = "/Users/peterlampesberger/Bitcoin/bitcoin.png"
sticky! if changed
}
if changed
bitmarket_eur_sell = new_bitmarket_eur_sell
bitmarket_eur_buy = new_bitmarket_eur_buy
bitmarket_eur_last = new_bitmarket_eur_last
mtgox_eur_sell = new_mtgox_eur_sell
mtgox_eur_buy = new_mtgox_eur_buy
mtgox_eur_last = new_mtgox_eur_last
end
i = 1
end
rescue StandardError => e
puts e.inspect
end
# 20.times do
if interrupted
puts "safe exit"
exit
end
# sleep 0.5
#end
i += 1
end