Skip to content

Instantly share code, notes, and snippets.

@rxbit
Created October 22, 2015 06:36
Show Gist options
  • Save rxbit/d161c349f1fb58be65c7 to your computer and use it in GitHub Desktop.
Save rxbit/d161c349f1fb58be65c7 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'optparse'
print "\033[2J\033[;H"
$options = {}
$parser = OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options] 000001"
opts.on("-c", "--[no-]color", "Print color") do |v|
$options[:color] = v
end
end
def parserStockCode(codes)
codes.map! do |c|
c = 'sh' + c if c.start_with?('60')
c = 'sz' + c if c.start_with?('00')
c
end
formal = codes.detect do |c|
c =~ /^(sh|sz)\d{6}$/
end
if formal then
return codes
else
puts $parser.help()
exit
end
end
$parser.parse!
codes = parserStockCode ARGV
codestrings = codes.join(",")
$uri = URI('http://hq.sinajs.cn/list=' + codestrings)
def request
res = Net::HTTP.get_response($uri)
res.decode_content = true
body = res.body.force_encoding('gb2312').encode('utf-8') if res.is_a?(Net::HTTPSuccess)
dataArray = body.lines.map do |x|
x =~ /\"(.*)\"/
arr = $1.split(',')
data = {}
data[:name] = arr[0]
data[:openingPrice] = arr[1]
data[:closingPrice] = arr[2]
data[:currentPrice] = arr[3]
data[:hPrice] = arr[4]
data[:lPrice] = arr[5]
data[:buyPrice] = arr[6]
data[:sellPrice] = arr[7]
data[:totalNumber] = arr[8]
data[:turnover] = arr[9]
data[:buy1] = arr[10]
data[:buy1Pirce] = arr[11]
data[:buy2] = arr[12]
data[:buy2Pirce] = arr[13]
data[:buy3] = arr[14]
data[:buy3Pirce] = arr[15]
data[:buy4] = arr[16]
data[:buy4Pirce] = arr[17]
data[:buy5] = arr[18]
data[:buy5Pirce] = arr[19]
data[:sell1] = arr[20]
data[:sell1Pirce] = arr[21]
data[:sell2] = arr[22]
data[:sell2Pirce] = arr[23]
data[:sell3] = arr[24]
data[:sell3Pirce] = arr[25]
data[:sell4] = arr[26]
data[:sell4Pirce] = arr[27]
data[:sell5] = arr[28]
data[:sell5Pirce] = arr[29]
data[:date] = arr[30]
data[:time] = arr[31]
data[:up] = data[:currentPrice].to_f - data[:closingPrice].to_f
data[:isUp] = data[:up] >= 0
data[:rate] = data[:up] / data[:closingPrice].to_f * 100
data
end
(1..dataArray.count).each do
print "\033[1A\033[2K"
print "\033[1A\033[2K"
end
dataArray.each do |x|
if $options[:color] then
if x[:isUp] then
print "\033[0;31;40m"
else
print "\033[0;32;40m"
end
end
puts "%s\t%s\t%-5.2f\t%-5.2f\%" % [x[:name], x[:currentPrice], x[:up], x[:rate]]
puts "%0.2f\%" % x[:rate]
end
end
while true
request
sleep 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment