sbi-security 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +48 -0
- data/Rakefile +6 -0
- data/bin/sbisec +32 -0
- data/lib/sbi/security/client.rb +17 -0
- data/lib/sbi/security/crawler.rb +55 -0
- data/lib/sbi/security/format.rb +19 -0
- data/lib/sbi/security/portfolio.rb +9 -0
- data/lib/sbi/security/stock.rb +20 -0
- data/lib/sbi/security/version.rb +5 -0
- data/lib/sbi/security.rb +16 -0
- data/sbi-security.gemspec +31 -0
- metadata +171 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9c15e62feb0bb186b99e360f5101ce96844bbf3d
|
4
|
+
data.tar.gz: bf508a8de86f2e43e18b8a3e27761dcf0f9e4d08
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 135bdabd31793c34efbe9579a645890ed6ac994f0635ff0dbdc98d9aa47ee827419c024fa724a2d0fb39c3fa473aa9f247f81f4c8fa0c85d6f295c0521e2c68f
|
7
|
+
data.tar.gz: d75eff6628835ea07ac251b2929cf4dab555a113acd9581fbcd117bcc6f0ae32f6a9bcf8998b11a52ae4c3cd666bb5ed384336a469c0a829d20dd51c4aef2fb5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# SBI Security
|
2
|
+
|
3
|
+
Ruby Client for SBI Security
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Note: You need to install Google Chrome and [ChromeDriver](https://sites.google.com/a/chromium.org/chromedriver/downloads) for crawling.
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'sbi-security'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sbi-security
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Note: You need to environment variables `SBI_SECURITY_USER_ID` and `SBI_SECURITY_PASSWORD`)
|
26
|
+
|
27
|
+
### Show portfolio
|
28
|
+
|
29
|
+
```
|
30
|
+
sbisec
|
31
|
+
+--------+--------------+------+----------+--------+--------+---------+---------+----------+-----------+
|
32
|
+
| コード | 銘柄 | 数量 | 参考単価 | 現在値 | 前日比 | 前日(%) | 損益 | 損益(%) | 評価額 |
|
33
|
+
+--------+--------------+------+----------+--------+--------+---------+---------+----------+-----------+
|
34
|
+
| 3633 | GMOぺパボ | 100 | 3,333.57 | 3,385 | +5 | +0.15 | +500 | +1.54 | 338,500 |
|
35
|
+
+--------+--------------+------+----------+--------+--------+---------+---------+----------+-----------+
|
36
|
+
```
|
37
|
+
|
38
|
+
## TODO
|
39
|
+
|
40
|
+
- Implement `Sbi::Security::Client#buy`
|
41
|
+
- Implement `Sbi::Security::Client#sell`
|
42
|
+
- Update cli options
|
43
|
+
- Like `sbisec portfolio` and `sbisec buy 3633 100`
|
44
|
+
- Add test
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/camelmasa/sbi-security.
|
data/Rakefile
ADDED
data/bin/sbisec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
|
4
|
+
require 'sbi/security'
|
5
|
+
require 'sbi/security/format'
|
6
|
+
require 'terminal-table'
|
7
|
+
require 'colorize'
|
8
|
+
|
9
|
+
include Sbi::Security::Format
|
10
|
+
|
11
|
+
client = Sbi::Security::Client.new(ENV["SBI_SECURITY_USER_ID"], ENV["SBI_SECURITY_PASSWORD"])
|
12
|
+
|
13
|
+
rows = []
|
14
|
+
rows << %w(コード 銘柄 数量 参考単価 現在値 前日比 前日(%) 損益 損益(%) 評価額)
|
15
|
+
rows << :separator
|
16
|
+
client.portfolio.stocks.each do |stock|
|
17
|
+
rows << [
|
18
|
+
stock.code,
|
19
|
+
stock.name,
|
20
|
+
number_to_currency(stock.count),
|
21
|
+
number_to_currency(stock.value),
|
22
|
+
number_to_currency(stock.price),
|
23
|
+
format(stock.price_ratio),
|
24
|
+
format(stock.price_ratio_percentage),
|
25
|
+
format(stock.profit),
|
26
|
+
format(stock.profit_percentage),
|
27
|
+
number_to_currency(stock.total_value)
|
28
|
+
]
|
29
|
+
end
|
30
|
+
|
31
|
+
puts Terminal::Table.new(rows: rows)
|
32
|
+
exit 0
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Sbi::Security
|
2
|
+
class Client
|
3
|
+
def initialize(user_id, password)
|
4
|
+
@user_id, @password = user_id, password
|
5
|
+
end
|
6
|
+
|
7
|
+
def portfolio
|
8
|
+
crawler.portfolio
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def crawler
|
14
|
+
@crawler ||= Crawler.new(@user_id, @password)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Sbi::Security
|
2
|
+
class Crawler
|
3
|
+
include Capybara::DSL
|
4
|
+
|
5
|
+
TOP_PAGE = "https://www.sbisec.co.jp/ETGate"
|
6
|
+
|
7
|
+
Capybara.register_driver :headless_chromium do |app|
|
8
|
+
options = { args: %w{headless no-sandbox disable-gpu} }
|
9
|
+
caps = Selenium::WebDriver::Remote::Capabilities.chrome(chromeOptions: options)
|
10
|
+
Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: caps)
|
11
|
+
end
|
12
|
+
|
13
|
+
Capybara.default_driver = :headless_chromium
|
14
|
+
|
15
|
+
def initialize(user_id, password)
|
16
|
+
login(user_id, password)
|
17
|
+
end
|
18
|
+
|
19
|
+
def portfolio
|
20
|
+
find("img[title='ポートフォリオ']").click
|
21
|
+
|
22
|
+
stocks = page.all(:xpath, '//table[@width="100%"]/tbody/tr[@align="center"]').each_with_index.map do |tr, i|
|
23
|
+
# Ignore title row
|
24
|
+
next if i == 0
|
25
|
+
|
26
|
+
_, code_and_name, _, count, value, price, price_ratio, price_ratio_percentage, profit, profit_percentage,
|
27
|
+
total_value = tr.all("td").map(&:text)
|
28
|
+
|
29
|
+
Stock.new(
|
30
|
+
code: code_and_name.split(" ").first,
|
31
|
+
name: code_and_name.split(" ").last,
|
32
|
+
count: count.gsub(/,/, ""),
|
33
|
+
value: value.gsub(/,/, ""),
|
34
|
+
price: price.gsub(/,/, ""),
|
35
|
+
price_ratio: (price_ratio == "--" ? nil : price_ratio.gsub(/,/, "").to_i),
|
36
|
+
price_ratio_percentage: (price_ratio_percentage == "--" ? nil : price_ratio_percentage.gsub(/,/, "").to_f),
|
37
|
+
profit: profit.gsub(/,/, ""),
|
38
|
+
profit_percentage: profit_percentage.gsub(/,/, ""),
|
39
|
+
total_value: total_value.gsub(/,/, "")
|
40
|
+
)
|
41
|
+
end.compact
|
42
|
+
|
43
|
+
Portfolio.new(stocks)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def login(user_id, password)
|
49
|
+
visit TOP_PAGE
|
50
|
+
fill_in :user_id, with: user_id
|
51
|
+
fill_in :user_password, with: password
|
52
|
+
find_button(class: "ov").click
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Sbi::Security
|
2
|
+
module Format
|
3
|
+
def format(number)
|
4
|
+
if number.nil? || number == 0
|
5
|
+
return number.to_s
|
6
|
+
end
|
7
|
+
|
8
|
+
if number > 0
|
9
|
+
"+#{number_to_currency(number)}".colorize(:red)
|
10
|
+
else
|
11
|
+
"-#{number_to_currency(number)}".colorize(:blue)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def number_to_currency(number)
|
16
|
+
number.to_s.gsub(/(\d)(?=(\d{3})+(?!\d))/, '\1,')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Sbi::Security
|
2
|
+
class Stock
|
3
|
+
include Virtus.model(strict: true)
|
4
|
+
|
5
|
+
attribute :code, Integer
|
6
|
+
attribute :name, String
|
7
|
+
attribute :count, Integer
|
8
|
+
attribute :value, Float
|
9
|
+
attribute :price, Integer
|
10
|
+
|
11
|
+
# Basically, price_ratio is Integer and price_ratio_percentage is Float.
|
12
|
+
# Those are nil after 9:00 JST so we can't define type.
|
13
|
+
attribute :price_ratio
|
14
|
+
attribute :price_ratio_percentage
|
15
|
+
|
16
|
+
attribute :profit, Integer
|
17
|
+
attribute :profit_percentage, Float
|
18
|
+
attribute :total_value, Integer
|
19
|
+
end
|
20
|
+
end
|
data/lib/sbi/security.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "capybara"
|
2
|
+
require 'capybara/dsl'
|
3
|
+
require 'selenium-webdriver'
|
4
|
+
require 'virtus'
|
5
|
+
require 'money'
|
6
|
+
|
7
|
+
module Sbi
|
8
|
+
module Security
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require "sbi/security/client"
|
13
|
+
require "sbi/security/crawler"
|
14
|
+
require "sbi/security/portfolio"
|
15
|
+
require "sbi/security/stock"
|
16
|
+
require "sbi/security/version"
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "sbi/security/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sbi-security"
|
8
|
+
spec.version = Sbi::Security::VERSION
|
9
|
+
spec.authors = ["camelmasa"]
|
10
|
+
spec.email = ["camelmasa@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{SBI Security client}
|
13
|
+
spec.description = %q{SBI Security client}
|
14
|
+
spec.homepage = "https://github.com/camelmasa/sbi-security"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "capybara", "~> 2.14"
|
23
|
+
spec.add_dependency "selenium-webdriver", "~> 3.4"
|
24
|
+
spec.add_dependency "virtus", "~> 1.0"
|
25
|
+
spec.add_dependency "terminal-table", "~> 1.8"
|
26
|
+
spec.add_dependency "colorize", "~> 0.8"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sbi-security
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- camelmasa
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: capybara
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.14'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: selenium-webdriver
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: virtus
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: terminal-table
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.8'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: colorize
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.8'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.8'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: bundler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.15'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.15'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '10.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '10.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.0'
|
125
|
+
description: SBI Security client
|
126
|
+
email:
|
127
|
+
- camelmasa@gmail.com
|
128
|
+
executables:
|
129
|
+
- sbisec
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".travis.yml"
|
136
|
+
- Gemfile
|
137
|
+
- README.md
|
138
|
+
- Rakefile
|
139
|
+
- bin/sbisec
|
140
|
+
- lib/sbi/security.rb
|
141
|
+
- lib/sbi/security/client.rb
|
142
|
+
- lib/sbi/security/crawler.rb
|
143
|
+
- lib/sbi/security/format.rb
|
144
|
+
- lib/sbi/security/portfolio.rb
|
145
|
+
- lib/sbi/security/stock.rb
|
146
|
+
- lib/sbi/security/version.rb
|
147
|
+
- sbi-security.gemspec
|
148
|
+
homepage: https://github.com/camelmasa/sbi-security
|
149
|
+
licenses: []
|
150
|
+
metadata: {}
|
151
|
+
post_install_message:
|
152
|
+
rdoc_options: []
|
153
|
+
require_paths:
|
154
|
+
- lib
|
155
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
requirements: []
|
166
|
+
rubyforge_project:
|
167
|
+
rubygems_version: 2.6.11
|
168
|
+
signing_key:
|
169
|
+
specification_version: 4
|
170
|
+
summary: SBI Security client
|
171
|
+
test_files: []
|