sorry_yahoo_finance 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da52115a085f797fc41e72968b09705d3b77d58e
4
- data.tar.gz: 9ecd2ce04d9e710eb68338af0d8f953da9f29707
3
+ metadata.gz: f562300329ee5171fe666273431253eeaea2395a
4
+ data.tar.gz: 191b160a42899c79f829525311ba94c77d820f30
5
5
  SHA512:
6
- metadata.gz: 2d91efca0b5aa34706f8417cb48feaf4b223ee01e780841c14d7266b67ef5147c750de41fed376e90543d2250c5fb1700113dc9a804f48a99a052df756a358b2
7
- data.tar.gz: 9bc16e66932e277782a5191e7e8bfef9e74761b1ee587b318892700d8c9763a3bafd1ef68d485210615a94536602b61bb77fee9b6752ef5bca0d15646b17b35c
6
+ metadata.gz: 404d0de6efbee299a440c9be6f707141d2d244fbc6edf096c22394b79e036684fd3709af2af60a7a16b99d66f6f75a88cac91dbfeb999dcec44d8f0db9e37482
7
+ data.tar.gz: 64cedaf2f3de82c4deec09dc67533c6797e26d465f6d85cf1802df2c96de2a70b04c0e74155baf238c0ddcc4d8234305bb498e301c1888fe164274802f4274ee
data/README.md CHANGED
@@ -1,84 +1,125 @@
1
1
  SorryYahooFinance README
2
2
  =============
3
3
 
4
- I'll be pulling the stock of information from Yahoo! Finance. I am sorry. Yahoo!
4
+ 株式の情報取得するGem作ったよ!
5
5
 
6
- Update Info
6
+ # 2014/2/21 アップデートしたよ!
7
+ --------
8
+
9
+ https://github.com/gogotanaka/sorry_yahoo_finance
10
+
11
+ https://rubygems.org/gems/sorry_yahoo_finance
12
+
13
+ 株式の各種データをひっぱってきたいと思い、Gemを探したが以外となかったので作ってみた。
14
+
15
+ Yahoo!Japanファイナンス(http://finance.yahoo.co.jp/)
16
+
17
+ から株の情報をひっぱてきます。ごめんなさい。Yahoo!
18
+
19
+ 今の所
20
+
21
+ * 証券コード
22
+ * 市場
23
+ * 業種
24
+ * 株価
25
+ * 前日終値
26
+ * 始値
27
+ * 高値
28
+ * 安値
29
+ * 出来高
30
+ * 売買代金
31
+ * 値幅制限
32
+ * 信用買残
33
+ * 信用買残前週比
34
+ * 信用売残
35
+ * 信用売残前週比
36
+ * 貸借倍率
37
+
38
+ を取ってきます。余裕があったらもっと増やします。
39
+
40
+ 更新履歴
7
41
  --------
8
42
  * 0.1.0 (2014-02-15)
9
- * I was in shape once.
43
+ * 一応形にした
10
44
 
11
- Example
45
+ * 0.2.0 (2014-02-20)
46
+ * いい感じにした
47
+
12
48
  --------
13
49
 
14
- I will pull the information of the stock by securities code.
50
+ https://github.com/gogotanaka/sorry_yahoo_finance
51
+
52
+ ### 証券コードと日付を引数に該当する株式の情報をひっぱってくる。
53
+
54
+ 日付は省略可能
15
55
 
16
56
  ```ruby:ex1.rb
17
- SorryYahooFinance.get_from_code(3333)
18
- => {:code=>3333,
19
- :name=>"(株)あさひ",
20
- :market=>"東証1部",
21
- :industry=>"小売業",
22
- :price=>"1,308",
23
- :previousprice=>"1,321",
24
- :opening=>"1,326",
25
- :high=>"1,331",
26
- :low=>"1,302",
27
- :turnover=>"95,700",
28
- :trading_volume=>"125,686",
29
- :price_limit=>"1,021~1,621",
30
- :margin_buying=>"174,700",
31
- :margin_selling=>"135,400",
32
- :d_margin_buying=>"-7,800",
33
- :d_margin_selling=>"-39,300",
34
- :margin_rate=>"1.29",
35
- :chart_image=>"http://gchart.yahoo.co.jp/f?s=3333.T"}
57
+ SorryYahooFinance::GET(8058, Date.new(2008, 9, 15))
58
+ => #<SorryYahooFinance::GET:0x007fcb36260030>
59
+
60
+ SorryYahooFinance::GET(8606)
61
+ => #<SorryYahooFinance::GET:0x007fcb2b6d53c8>
36
62
  ```
37
63
 
38
- Multiple possible.
64
+ ちなみに SorryYahooFinance が長過ぎて無理な人のために Stock というエイリアスを張ってある。
65
+
66
+ ```ruby:ex1.rb
67
+ Stock::GET(8058, Date.new(2008, 9, 15))
68
+ => #<SorryYahooFinance::GET:0x007fcb36260030>
69
+
70
+ Stock::GET(8606)
71
+ => #<SorryYahooFinance::GET:0x007fcb2b6d53c8>
72
+ ```
73
+
74
+ SorryYahooFinance::GET#values で株情報をhash形式で
75
+
76
+ ```rb
77
+ SorryYahooFinance::GET(8411).values
78
+ => {:code=>"8411",
79
+ :name=>"(株)みずほフィナンシャルグループ",
80
+ :market=>"東証1部",
81
+ :industry=>"銀行業",
82
+ :price=>"212",
83
+ :previousprice=>"218",
84
+ :opening=>"218",
85
+ :high=>"218",
86
+ :low=>"211",
87
+ :turnover=>"197,084,500",
88
+ :trading_volume=>"42,334,350",
89
+ :price_limit=>"138~298",
90
+ :margin_buying=>"320,352,600",
91
+ :margin_selling=>"13,355,900",
92
+ :d_margin_buying=>"+15,929,300",
93
+ :d_margin_selling=>"-4,316,500",
94
+ :margin_rate=>"23.99",
95
+ :chart_image=>"http://gchart.yahoo.co.jp/f?s=8411.T"}
96
+ ```
97
+
98
+ SorryYahooFinance::GET#market などでそれぞれの情報
99
+
100
+ ```rb
101
+ SorryYahooFinance::GET(3333).market
102
+ => "東証1部"
103
+ ```
104
+
105
+ 一応、複数も
39
106
 
40
107
  ```ruby:ex2.rb
41
- SorryYahooFinance.get_from_codes([3333,4355])
42
- => [{:code=>3333,
43
- :name=>"(株)あさひ",
44
- :market=>"東証1部",
45
- :industry=>"小売業",
46
- :price=>"1,308",
47
- :previousprice=>"1,321",
48
- :opening=>"1,326",
49
- :high=>"1,331",
50
- :low=>"1,302",
51
- :turnover=>"95,700",
52
- :trading_volume=>"125,686",
53
- :price_limit=>"1,021~1,621",
54
- :margin_buying=>"174,700",
55
- :margin_selling=>"135,400",
56
- :d_margin_buying=>"-7,800",
57
- :d_margin_selling=>"-39,300",
58
- :margin_rate=>"1.29",
59
- :chart_image=>"http://gchart.yahoo.co.jp/f?s=3333.T"},
60
- {:code=>4355,
61
- :name=>"ロングライフホールディング(株)",
62
- :market=>"東証JQS",
63
- :industry=>"サービス業",
64
- :price=>"307",
65
- :previousprice=>"313",
66
- :opening=>"312",
67
- :high=>"312",
68
- :low=>"303",
69
- :turnover=>"21,600",
70
- :trading_volume=>"6,674",
71
- ........(略)
108
+ SorryYahooFinance::GET.get_by_codes([8606,8058])
109
+ => [#<SorryYahooFinance::GET:0x007fcb2e4816a0>, #<SorryYahooFinance::GET:0x007fcb30a17e78>
110
+
72
111
  ```
73
112
 
74
- All of the shares.
113
+ 全株式もとって来れる(http://www.tse.or.jp/market/data/listed_companies/)
114
+ 全株式の指す所はこのあたり参照
115
+
116
+ を取ってくる
75
117
 
76
118
  ```ruby:ex3.rb
77
- SorryYahooFinance.get_all
119
+ SorryYahooFinance::GET.get_all
78
120
  => .....(略)
79
121
  ```
80
122
 
81
-
82
123
  LICENSE
83
124
  -------
84
125
  (The MIT License)
@@ -0,0 +1,128 @@
1
+ require 'utils/all_stock_codes'
2
+ require 'utils/converter'
3
+ require 'utils/hash_accessor'
4
+ require 'utils/extended_hash'
5
+
6
+ module SorryYahooFinance
7
+ class Info
8
+ extend HashAccessor
9
+ hash_accessor :values, :code, :name, :market, :industry, :price, :previousprice, :opening, :high, :low, :turnover, :trading_volume, :price_limit, :margin_buying, :margin_selling, :d_margin_buying, :d_margin_selling, :margin_rate, :chart_image
10
+
11
+ # TODO: 休場の時はアラート出したい
12
+ def initialize(code, date=nil)
13
+ if code.class == Fixnum && code.to_s.size == 4
14
+ begin
15
+ @values = if date
16
+ infos_with_date(code, date)
17
+ else
18
+ infos(code)
19
+ end
20
+ rescue => ex
21
+ raise "code #{code} stock dont exist. #{ex}"
22
+ end
23
+ else
24
+ raise "code #{code} must be a four-digit number."
25
+ end
26
+ end
27
+
28
+ def values
29
+ @values
30
+ end
31
+
32
+ def formalize_values
33
+ return_values = @values
34
+
35
+ # to_integer
36
+ int_keys = [
37
+ :code,
38
+ :price,
39
+ :previousprice,
40
+ :opening,
41
+ :high,
42
+ :low,
43
+ :turnover,
44
+ :trading_volume,
45
+ :margin_buying,
46
+ :margin_selling,
47
+ :d_margin_buying,
48
+ :d_margin_selling,
49
+ :finish
50
+ ]
51
+ return_values = return_values.map_to_hash do |k,v|
52
+ if int_keys.include?(k) && v.class == String
53
+ v.delete(",").to_i
54
+ else
55
+ v
56
+ end
57
+ end
58
+
59
+ # to_range(str is like 183〜201)
60
+ price_limit = return_values[:price_limit]
61
+ price_limit.delete!(",")
62
+ price_limit =~ /(\d+)~(\d+)/
63
+ return_values[:price_limit] = Range.new($1.to_i,$2.to_i)
64
+
65
+ # to_f
66
+ return_values[:margin_rate] = return_values[:margin_rate].to_f
67
+
68
+ return_values
69
+ end
70
+
71
+ private
72
+
73
+ def infos(code)
74
+ url = yahoo_url(code)
75
+ html = Converter.do(url)
76
+ previousprice, opening, high, low, turnover, trading_volume, price_limit = html.css('div.innerDate dd').map{|x| x.css('strong').inner_text }
77
+ margin_deal = html.css("div.ymuiDotLine div.yjMS dd.ymuiEditLink strong").map(&:text)
78
+ {
79
+ code: html.css("div#divAddPortfolio + dl dt").text,
80
+ name: html.css('table.stocksTable th.symbol h1').inner_text,
81
+ market: html.css('div.stocksDtlWp dd')[0].content,
82
+ industry: html.css("div.stocksDtl dd.category a").text,
83
+ price: html.css('table.stocksTable td.stoksPrice')[1].content,
84
+ previousprice: previousprice,
85
+ opening: opening,
86
+ high: high,
87
+ low: low,
88
+ turnover: turnover,
89
+ trading_volume: trading_volume,
90
+ price_limit: price_limit,
91
+ margin_buying: margin_deal[0],
92
+ margin_selling: margin_deal[3],
93
+ d_margin_buying: margin_deal[1],
94
+ d_margin_selling: margin_deal[4],
95
+ margin_rate: margin_deal[2],
96
+ chart_image: html.css("div.styleChart img")[0][:src],
97
+ }
98
+ end
99
+
100
+ def infos_with_date(code, date)
101
+ url = yahoo_url_with_date(code, date)
102
+ html = Converter.do(url)
103
+ tds = html.xpath("(//div[@id='main']//table)[2]//td")
104
+ opening, high, low, finish, turnover = tds[1..5].map(&:text)
105
+ {
106
+ code: html.css("div#divAddPortfolio + dl dt").text,
107
+ name: html.css('table.stocksTable th.symbol h1').inner_text,
108
+ market: html.css('div.stocksDtlWp dd')[0].content,
109
+ industry: html.css("div.stocksDtl dd.category a").text,
110
+ opening: opening,
111
+ high: high,
112
+ low: low,
113
+ finish: finish
114
+ }
115
+ end
116
+
117
+ def yahoo_url(code)
118
+ "http://stocks.finance.yahoo.co.jp/stocks/detail/?code=#{code}"
119
+ end
120
+
121
+ def yahoo_url_with_date(code, date)
122
+ year, month, day = date.strftime("%Y,%m,%d").split(",")
123
+ month.delete!("0")
124
+ "http://info.finance.yahoo.co.jp/history/?code=#{code}.T&sy=#{year}&sm=#{month}&sd=#{day}&ey=#{year}&em=#{month}&ed=#{day}&tm=d"
125
+ end
126
+
127
+ end
128
+ end
@@ -1,3 +1,3 @@
1
1
  module SorryYahooFinance
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,9 +1,51 @@
1
1
  require "sorry_yahoo_finance/version"
2
- require "sorry_yahoo_finance/get"
2
+ require 'sorry_yahoo_finance/info'
3
+ require 'utils/all_stock_codes'
4
+ require 'utils/converter'
5
+ require 'utils/hash_accessor'
6
+ require 'utils/extended_hash'
7
+
3
8
  module SorryYahooFinance
4
9
  class << self
5
- def GET(code, date=nil)
6
- SorryYahooFinance::GET.new(code, date)
10
+
11
+ def GET(code_or_codes_or_all, date_or_year=nil, month=nil, day=nil)
12
+ date = build_date(date_or_year, month, day)
13
+ code_ary = build_code_ary(code_or_codes_or_all)
14
+
15
+ SorryYahooFinance::GET.code_ary_and_date(code_ary, date)
7
16
  end
17
+
18
+ def build_code_ary(code_or_codes_or_all)
19
+ if code_or_codes_or_all == :all
20
+ AllStockCodes::CODES
21
+ elsif code_or_codes_or_all.is_a? Array
22
+ code_or_codes_or_all
23
+ else
24
+ [code_or_codes_or_all]
25
+ end
26
+ end
27
+
28
+ def build_date(date_or_year, month, day)
29
+ if month && day
30
+ Date.new(date_or_year, month, day)
31
+ else
32
+ date_or_year
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ module GET
39
+ def code_ary_and_date(code_ary, date)
40
+ infos = code_ary.map do |code|
41
+ SorryYahooFinance::Info.new(code, date)
42
+ end
43
+
44
+ infos.count == 1 ? infos.first : infos
45
+ end
46
+
47
+ module_function :code_ary_and_date
8
48
  end
9
- end
49
+
50
+ end
51
+ Stock = SorryYahooFinance
File without changes
@@ -0,0 +1,7 @@
1
+ class Hash
2
+ def map_to_hash &block
3
+ ret = {}
4
+ each { |k,v| ret[k] = block.call(k,v) }
5
+ ret
6
+ end
7
+ end
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorry_yahoo_finance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-20 00:00:00.000000000 Z
11
+ date: 2014-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -111,12 +111,13 @@ files:
111
111
  - README.md
112
112
  - Rakefile
113
113
  - bin/sorry_yahoo_finance
114
- - lib/all_stock_codes.rb
115
- - lib/converter.rb
116
- - lib/hash_accessor.rb
117
114
  - lib/sorry_yahoo_finance.rb
118
- - lib/sorry_yahoo_finance/get.rb
115
+ - lib/sorry_yahoo_finance/info.rb
119
116
  - lib/sorry_yahoo_finance/version.rb
117
+ - lib/utils/all_stock_codes.rb
118
+ - lib/utils/converter.rb
119
+ - lib/utils/extended_hash.rb
120
+ - lib/utils/hash_accessor.rb
120
121
  - sorry_yahoo_finance.gemspec
121
122
  - spec/sorry_yahoo_finance_spec.rb
122
123
  - spec/spec_helper.rb
@@ -1,132 +0,0 @@
1
- require 'all_stock_codes'
2
- require 'converter'
3
- require 'hash_accessor'
4
-
5
- module SorryYahooFinance
6
- class Hash
7
- def map_to_hash &block
8
- ret = {}
9
- each { |k,v| ret[k] = block.call(k,v) }
10
- ret
11
- end
12
- end
13
- class GET
14
- extend HashAccessor
15
- hash_accessor :values, :code, :name, :market, :industry, :price, :previousprice, :opening, :high, :low, :turnover, :trading_volume, :price_limit, :margin_buying, :margin_selling, :d_margin_buying, :d_margin_selling, :margin_rate, :chart_image
16
- class << self
17
- def get_infos(url)
18
- html = Converter.do(url)
19
- previousprice, opening, high, low, turnover, trading_volume, price_limit = html.css('div.innerDate dd').map{|x| x.css('strong').inner_text }
20
- margin_deal = html.css("div.ymuiDotLine div.yjMS dd.ymuiEditLink strong").map(&:text)
21
- {
22
- code: html.css("div#divAddPortfolio + dl dt").text,
23
- name: html.css('table.stocksTable th.symbol h1').inner_text,
24
- market: html.css('div.stocksDtlWp dd')[0].content,
25
- industry: html.css("div.stocksDtl dd.category a").text,
26
- price: html.css('table.stocksTable td.stoksPrice')[1].content,
27
- previousprice: previousprice,
28
- opening: opening,
29
- high: high,
30
- low: low,
31
- turnover: turnover,
32
- trading_volume: trading_volume,
33
- price_limit: price_limit,
34
- margin_buying: margin_deal[0],
35
- margin_selling: margin_deal[3],
36
- d_margin_buying: margin_deal[1],
37
- d_margin_selling: margin_deal[4],
38
- margin_rate: margin_deal[2],
39
- chart_image: html.css("div.styleChart img")[0][:src],
40
- }
41
- end
42
-
43
- def get_infos_with_date(url)
44
- html = Converter.do(url)
45
- tds = html.xpath("(//div[@id='main']//table)[2]//td")
46
- opening, high, low, finish, turnover = tds[1..5].map(&:text)
47
- {
48
- code: html.css("div#divAddPortfolio + dl dt").text,
49
- name: html.css('table.stocksTable th.symbol h1').inner_text,
50
- market: html.css('div.stocksDtlWp dd')[0].content,
51
- industry: html.css("div.stocksDtl dd.category a").text,
52
- opening: opening,
53
- high: high,
54
- low: low,
55
- finish: finish
56
- }
57
- end
58
-
59
- def get_by_codes(codes)
60
- if codes.class == Array
61
- codes.map{|code| self.new(code)}
62
- else
63
- raise "codes #{codes} must be a Array."
64
- end
65
- end
66
-
67
- def get_all
68
- get_from_codes(AllStockCodes::CODES)
69
- end
70
- end
71
-
72
- # 急場の時はアラート出したい
73
- def initialize(code, date=nil)
74
- if code.class == Fixnum && code.to_s.size == 4
75
- begin
76
- @values = if date
77
- year, month, day = date.strftime("%Y,%m,%d").split(",")
78
- month.delete!("0")
79
- url = "http://info.finance.yahoo.co.jp/history/?code=#{code}.T&sy=#{year}&sm=#{month}&sd=#{day}&ey=#{year}&em=#{month}&ed=#{day}&tm=d"
80
- self.class.get_infos_with_date(url)
81
- else
82
- self.class.get_infos("http://stocks.finance.yahoo.co.jp/stocks/detail/?code=#{code}")
83
- end
84
- rescue => ex
85
- raise "code #{code} stock dont exist. #{ex}"
86
- end
87
- else
88
- raise "code #{code} must be a four-digit number."
89
- end
90
- end
91
-
92
- def values
93
- @values
94
- end
95
-
96
- def to_range
97
- # str is like 183〜201
98
- price_limit = @values[:price_limit]
99
- price_limit.delete!(",")
100
- price_limit =~ /(\d+)~(\d+)/
101
- @values[:price_limit] = Range.new($1.to_i,$2.to_i)
102
- end
103
-
104
- def formalize_values
105
- int_keys = [
106
- :code,
107
- :price,
108
- :previousprice,
109
- :opening,
110
- :high,
111
- :low,
112
- :turnover,
113
- :trading_volume,
114
- :margin_buying,
115
- :margin_selling,
116
- :d_margin_buying,
117
- :d_margin_selling,
118
- :finish
119
- ]
120
- @values = @values.map_to_hash do |k,v|
121
- if int_keys.include?(k) && v.class == String
122
- v.delete(",").to_i
123
- else
124
- v
125
- end
126
- end
127
- to_range
128
- @values[:margin_rate] = @values[:margin_rate].to_f
129
- end
130
- end
131
- end
132
- Stock = SorryYahooFinance