btc.rb 6.44 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/usr/bin/env ruby

# Plugin to get bitcoin value
class Btc

	require 'json'
	
	# This method is called when the plugin is first loaded
	def initialize( status, config, output, irc, timer )
10 11 12 13 14
		@status   = status
		@config   = config
		@output   = output
		@irc      = irc
		@timer    = timer
15
		
16 17 18
		@api_host = 'data.mtgox.com'
		@api_path = '/api/2/BTCUSD/money/ticker'
		@last     = 0.0
19 20 21 22

		@bits_host = 'www.bitstamp.net'
		@bits_path = '/api/ticker/'
		@bits_last = 0.0
23 24
	end

Cool Fire's avatar
Cool Fire committed
25
	def main( _nick, _user, _host, from, _msg, _arguments, con )
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
		uri = URI.parse( "https://#{@bits_host}#{@bits_path}" )
		
		http = Net::HTTP.new(uri.host, uri.port)
		http.use_ssl = true
		http.verify_mode = OpenSSL::SSL::VERIFY_NONE
		
		request = Net::HTTP::Get.new(uri.request_uri)
		response = http.request(request)
		
		result = JSON.parse( response.body )

		# Calculate diff between last and ask
		diff = result[ 'ask' ].to_f - result[ 'last' ].to_f
		diff = ( diff * 1000 ).round / 1000.0
		if( diff > 0 )
Cool Fire's avatar
Cool Fire committed
41
			diff = "+#{diff}"
42
		else
Cool Fire's avatar
Cool Fire committed
43
			diff = "#{diff}"
44 45 46 47 48 49
		end

		# Calculate delta from last !btc
		ldiff = result[ 'ask' ].to_f - @bits_last
		ldiff = ( ldiff * 1000 ).round / 1000.0
		if( ldiff > 0 )
Cool Fire's avatar
Cool Fire committed
50
			ldiff = "+#{ldiff}"
51
		else
Cool Fire's avatar
Cool Fire committed
52
			ldiff = "#{ldiff}"
53 54 55
		end
		@bits_last = result[ 'ask' ].to_f
		
Cool Fire's avatar
Cool Fire committed
56
		line = "Bitstamp rate: $#{result[ 'ask' ]} #{diff} (#{ldiff} since last !btc)"
57 58 59 60 61 62 63 64 65

		if( con )
			@output.c( line + "\n" )
		else
			@irc.message( from, line )
		end
	end

	# Get current value
Cool Fire's avatar
Cool Fire committed
66
	def mtgox( _nick, _user, _host, from, _msg, _arguments, con )
Cool Fire's avatar
 
Cool Fire committed
67 68 69 70 71 72 73 74 75 76
		uri = URI.parse( "https://#{@api_host}#{@api_path}" )
		
		http = Net::HTTP.new(uri.host, uri.port)
		http.use_ssl = true
		http.verify_mode = OpenSSL::SSL::VERIFY_NONE
		
		request = Net::HTTP::Get.new(uri.request_uri)
		response = http.request(request)
		
		result = JSON.parse( response.body )
Cool Fire's avatar
Cool Fire committed
77 78 79 80
		
		if( result[ "result" ] != "success" )
			line = "Mtgox API error."
		else
Cool Fire's avatar
Cool Fire committed
81
			# Calculate delta from API 'last'
Cool Fire's avatar
Cool Fire committed
82
			diff = result[ 'data' ][ 'sell' ][ 'value' ].to_f - result[ 'data' ][ 'last' ][ 'value' ].to_f
Cool Fire's avatar
Cool Fire committed
83
			diff = ( diff * 1000 ).round / 1000.0
Cool Fire's avatar
Cool Fire committed
84
			if( diff > 0 )
Cool Fire's avatar
Cool Fire committed
85
				diff = "+#{diff}"
Cool Fire's avatar
Cool Fire committed
86
			else
Cool Fire's avatar
Cool Fire committed
87
				diff = "#{diff}"
Cool Fire's avatar
Cool Fire committed
88
			end
Cool Fire's avatar
Cool Fire committed
89 90 91 92 93

			# Calculate delta from last !btc
			ldiff = result[ 'data' ][ 'sell' ][ 'value' ].to_f - @last
			ldiff = ( ldiff * 1000 ).round / 1000.0
			if( ldiff > 0 )
Cool Fire's avatar
Cool Fire committed
94
				ldiff = "+#{ldiff}"
Cool Fire's avatar
Cool Fire committed
95
			else
Cool Fire's avatar
Cool Fire committed
96
				ldiff = "#{ldiff}"
Cool Fire's avatar
Cool Fire committed
97 98 99
			end
			@last = result[ 'data' ][ 'sell' ][ 'value' ].to_f
			
Cool Fire's avatar
Cool Fire committed
100
			line = "Mtgox rate: #{result[ 'data' ][ 'sell' ][ 'display' ]} (#{result[ 'data' ][ 'sell' ][ 'value' ]}) #{diff} (#{ldiff} since last !btc mtgox)"
Cool Fire's avatar
Cool Fire committed
101 102 103 104 105 106 107
		end
		
		if( con )
			@output.c( line + "\n" )
		else
			@irc.message( from, line )
		end
Cool Fire's avatar
Cool Fire committed
108 109 110
	end
	
	# Function to chain BTC and LTC lookup
Cool Fire's avatar
Cool Fire committed
111
	def ltc( nick, user, host, from, msg, arguments, con )
Cool Fire's avatar
Cool Fire committed
112 113 114
		
		# Run main value lookup
		main( nick, user, host, from, msg, arguments, con )
115 116
		
		# Check if ltc plugin is loaded
Cool Fire's avatar
Cool Fire committed
117
		ratio = ""
118 119 120
		if( @status.checkplugin( "ltc" ) )
			plugin = @status.getplugin( "ltc" )
			ltcv = plugin.main( nick, user, host, from, nil, nil, false )
121
			btcv = @bits_last
122 123 124 125 126 127 128 129
			
			ltctobtc = ltcv / btcv
			ltctobtc = ( ltctobtc * 1000 ).round / 1000.0
			btctoltc = btcv / ltcv
			btctoltc = ( btctoltc * 1000 ).round / 1000.0
			
			ratio = "Ratios: BTC to LTC #{btctoltc} | LTC to BTC #{ltctobtc}"
			
Cool Fire's avatar
Cool Fire committed
130 131 132 133 134 135 136 137 138
			
		else
			ratio = "Litecoin plugin not loaded."
		end
		
		if( con )
			@output.c( ratio + "\n" )
		else
			@irc.message( from, ratio )
139
		end
140 141
	end
	# Get last value
Cool Fire's avatar
Cool Fire committed
142
	def last( _nick, _user, _host, from, _msg, _arguments, con )
143 144 145 146 147 148 149 150 151 152 153 154
		result = JSON.parse( Net::HTTP.get( @bits_host, @bits_path ) )
		
		line = "#{result[ 'last' ]}"
		
		if( con )
			@output.c( line + "\n" )
		else
			@irc.message( from, line )
		end
	end
	
	# Get buy price
Cool Fire's avatar
Cool Fire committed
155
	def buy( _nick, _user, _host, from, _msg, _arguments, con )
156 157 158 159 160 161 162 163 164 165 166 167
		result = JSON.parse( Net::HTTP.get( @bits_host, @bits_path ) )
		
		line = "Bitstamp will buy your bitcoins for #{result[ 'bid' ]}"
		
		if( con )
			@output.c( line + "\n" )
		else
			@irc.message( from, line )
		end
	end
	
	# Get sell price
Cool Fire's avatar
Cool Fire committed
168
	def sell( _nick, _user, _host, from, _msg, _arguments, con )
169 170 171 172 173 174 175 176 177 178 179 180
		result = JSON.parse( Net::HTTP.get( @bits_host, @bits_path ) )
		
		line = "Bitstamp will sell you bitcoins for #{result[ 'ask' ]}"
		
		if( con )
			@output.c( line + "\n" )
		else
			@irc.message( from, line )
		end
	end

	# Get last value
Cool Fire's avatar
Cool Fire committed
181
	def mtgoxlast( _nick, _user, _host, from, _msg, _arguments, con )
182 183 184 185 186
		result = JSON.parse( Net::HTTP.get( @api_host, @api_path ) )
		
		if( result[ "result" ] != "success" )
			line = "Mtgox API error."
		else
Cool Fire's avatar
Cool Fire committed
187
			line = "#{result[ 'data' ][ 'last' ][ 'display' ]} (#{result[ 'data' ][ 'last' ][ 'value' ]})"
188 189 190 191 192 193 194 195 196 197 198
		end
		
		
		if( con )
			@output.c( line + "\n" )
		else
			@irc.message( from, line )
		end
	end
	
	# Get buy price
Cool Fire's avatar
Cool Fire committed
199
	def mtgoxbuy( _nick, _user, _host, from, _msg, _arguments, con )
200 201 202 203 204
		result = JSON.parse( Net::HTTP.get( @api_host, @api_path ) )
		
		if( result[ "result" ] != "success" )
			line = "Mtgox API error."
		else
Cool Fire's avatar
Cool Fire committed
205
			line = "Mtgox will buy your bitcoins for #{result[ 'data' ][ 'buy' ][ 'display' ]} (#{result[ 'data' ][ 'buy' ][ 'value' ]})"
206 207 208 209 210 211 212 213 214 215 216
		end
		
		
		if( con )
			@output.c( line + "\n" )
		else
			@irc.message( from, line )
		end
	end
	
	# Get sell price
Cool Fire's avatar
Cool Fire committed
217
	def mtgoxsell( _nick, _user, _host, from, _msg, _arguments, con )
218 219 220 221 222
		result = JSON.parse( Net::HTTP.get( @api_host, @api_path ) )
		
		if( result[ "result" ] != "success" )
			line = "Mtgox API error."
		else
Cool Fire's avatar
Cool Fire committed
223
			line = "Mtgox will sell you bitcoins for #{result[ 'data' ][ 'sell' ][ 'display' ]} (#{result[ 'data' ][ 'sell' ][ 'value' ]})"
224 225 226 227 228 229 230 231 232 233 234
		end
		
		
		if( con )
			@output.c( line + "\n" )
		else
			@irc.message( from, line )
		end
	end
	
	# Function to send help about this plugin (Can also be called by the help plugin.)
Cool Fire's avatar
Cool Fire committed
235
	def help( nick, _user, _host, _from, _msg, _arguments, con )
236 237
		help = [
			"Gets current values from mtgox",
238 239 240 241 242 243 244 245
			"  btc            - Get bitstamp exchange rate (buying rate)",
			"  btc ltc        - Get bitcoin & litecoin exchange rates",
			"  btc last       - Last reported value for bitcoins on  bitstamp",
			"  btc buy        - What price bitstamp will buy your bitcoins for",
			"  btc sell       - What price bitstamp will sell you bitcoins for",
			"  btc mtgoxlast  - Last reported value for bitcoins on mtgox",
			"  btc mtgoxbuy   - What price mtgox will buy your bitcoins for",
			"  btc mtgoxsell  - What price mtgox will sell you bitcoins for"
246 247 248 249 250 251 252 253 254 255 256 257
		]
		
		# Print out help
		help.each do |line|
			if( con )
				@output.c( line + "\n" )
			else
				@irc.notice( nick, line )
			end
		end
	end
end