-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.rb
199 lines (180 loc) · 5.92 KB
/
interface.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# coding: utf-8
require "iconv"
require "uri"
require 'net/http'
require 'json'
require_relative "stock_record"
class WebInterface
def self.fetch_data(url)
uri = URI.parse(url)
res = nil
http = Net::HTTP.new(uri.host)
http.read_timeout = 3
http.open_timeout = 5
begin
res = http.request_get(uri.request_uri)
# rescue Net::ReadTimeout, Net::OpenTimeout, Zlib::BufError, SocketError
rescue
sleep(1)
retry
end
remote_data = res.body if res.is_a?(Net::HTTPSuccess)
end
end
class StockHistoryBase < WebInterface
#http://api.finance.ifeng.com/akdaily/?code=sh000001&type=last
#http://table.finance.yahoo.com/table.csv?a=0&b=1&c=2012&d=3&e=19&f=2012&s=600000.ss
# @@decoder = Iconv.new("UTF-8//IGNORE", "GBK//IGNORE")
@@base_url = "http://table.finance.yahoo.com/table.csv"
# def initialize(base_url)
# @base_url = base_url
# # @fetchAgent = WWW::Mechanize.new { |agent|
# # agent.user_agent_alias = 'Linux Mozilla'
# # agent.max_history = 0
# # }
# end
def self.get_url(stock, beginDate, endDate)
market = stock.market
market = 'ss' if stock.market == 'sh'
code = stock.code
code = code[1..-1] if code.start_with?('0')
stockName = code + "." + market
infos = {}
infos['a'] = beginDate.mon - 1
infos['b'] = beginDate.mday
infos['c'] = beginDate.year
infos['d'] = endDate.mon - 1
infos['e'] = endDate.mday
infos['f'] = endDate.year
infos['s'] = stockName
url = @@base_url + "?" + URI.encode_www_form(infos)
end
def self.get_status(stock, begDate, endDate)
url = self.get_url(stock, begDate, endDate)
remote_data = self.fetch_data(url)
return nil if remote_data.nil?
self.parse_data(remote_data)
end
def self.parse_data(rdata)
records = []
first = true
rdata.split("\n").each do |dataLine|
if first # skip first line
first = false
next
end
infos = dataLine.split(",")
info_hash = {}
info_hash[:vol] = infos[5].to_i
if info_hash[:vol] == 0 #volume is 0
next
end
info_hash[:date] = infos[0]
info_hash[:open] = infos[1].to_f
info_hash[:high] = infos[2].to_f
info_hash[:low] = infos[3].to_f
info_hash[:close] = infos[4].to_f
info_hash[:adj_close] = infos[6].to_f
records << StockRecord.new(info_hash)
end
return records
end
end
class IFengHistory < StockHistoryBase
@@base_url = "http://api.finance.ifeng.com/akdaily/?code=%s&type=last"
def self.get_url(stock, beginDate, endDate)
stockName = stock.market+stock.code
url = sprintf(@@base_url, stockName)
end
end
class YahooHistory < StockHistoryBase
@@base_url = "http://table.finance.yahoo.com/table.csv"
end
# coding: utf-8
class TradingDayAbstract < WebInterface
@@decoder = Iconv.new("UTF-8//IGNORE", "GBK//IGNORE")
@@base_url = nil
@@inter_name = nil
@@inter_name_hk = nil
@@inter_keys_hk = nil
@@inter_keys = nil
@@hk_realtime_prefix = nil
@@info_separator = nil
def self.get_url(stock_list)
stock_infos = []
stock_list.each do |stock|
str = ""
str = @@hk_realtime_prefix if "hk" == stock.market
str += stock.market + stock.code
stock_infos << str
end
url = @@base_url + stock_infos.join(",")
end
def self.get_status(stock)
stock_list = [stock]
infos = self.get_status_batch(stock_list)
return infos.values[0]
end
def self.get_status_batch(stock_list)
start = 0
limit = 30
infos = {}
(0..stock_list.size + limit).step(limit) do |n|
url = get_url(stock_list[start..n])
remote_data = self.fetch_data(url)
remote_data = @@decoder.iconv(remote_data)
next if remote_data.nil?
infos.merge!(self.parse_data(remote_data))
start = n + 1
break if start >= stock_list.size
end
infos
end
def self.update_stocks_batch(stock_list)
infos = self.get_status_batch(stcok_list)
end
def self.parse_data(rdata)
info_hash = {}
rdata.each_line do |data_line|
ref, info = data_line.split('=')
market = ref[/sz|sh|hk/]
code = ref[/\d+/]
info_str = info.delete("\";")
infos = info_str.split(@@info_separator)
stock_info = {}
keys = @@inter_keys
keys = @@inter_keys_hk if market == "hk"
raise "data format not as expected" unless keys.size == infos.size
infos.each_index do |i|
v = infos[i]
stock_info[keys[i]] = v
end
info_hash[code] = stock_info
end
return info_hash
end
end
class SinaTradingDay < TradingDayAbstract
@@decoder = Iconv.new("UTF-8//IGNORE", "GBK//IGNORE")
@@base_url = "http://hq.sinajs.cn/list="
@@inter_name = ["股票名", "今开", "昨收", "报价", "最高价", "最低价", "竞买", "竞卖", "成交量",
"成交金额", "买一量", "买一", "买二量", "买二", "买三量", "买三", "买四量", "买四",
"买五量", "买五", "卖一量", "卖一", "卖二量", "卖二", "卖三量", "卖三",
"卖四量", "卖四", "卖五量", "卖五", "日期", "时间"]
@@inter_name_hk = %w(英文名 中文名 今开 昨收 最高价 最低价 报价 涨跌 振幅 竞买 竞卖 成交金额 成交量 市盈率 周息 年高点 年低点 日期 时间)
@@inter_keys_hk = %i(name_e name t_open y_close high low deal change change_ratio buy sell turnover vol pe wir year_high year_low date time)
@@inter_keys = %i( name t_open y_close deal high low buy sell vol turnover buy_vol1 buy1 buy_vol2 buy2 buy_vol3 buy3 buy_vol4 buy4 buy_vol5 buy5 sell_vol1 sell1 sell_vol2 sell2 sell_vol3 sell3 sell_vol4 sell4 sell_vol5 sell5 date time na)
# http://hq.sinajs.cn/list=sz002238,sz000033
@@hk_realtime_prefix = "rt_"
@@info_separator = ","
end
if $0 == __FILE__
require_relative "stock"
code = "00001"
market = "hk"
# code = "000001"
# market = "sz"
stock = Stock.new(code, market)
stock.update_trading!()
puts stock.deal
end