bank_scrap 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 68b5810bd6747ebe4b3f073747092768f5d16019
4
+ data.tar.gz: 413f9dc6ab905d1c5ec32de0bd3bb76a0a84025f
5
+ SHA512:
6
+ metadata.gz: 6fc2147bb41666fcfc2cceb6f571dd7ae517ad398c117b38c650c8119b78c450b32471e9f98c6569c1e934ecd02985a6b31ebf2b70661c533a9c943b8e171de2
7
+ data.tar.gz: fa7b34b93ef33f86f005ba79f6d46533b15c4eda928945d45eb08307e6b64946fec77dd6eaba08f4c93907fe889e416d4310372179aed311c97ac8cdaea78853
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bankscrap.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Bankscrap
2
+
3
+ Ruby gem to extract balance and transactions from banks. You can use it either as command line tool or as a library.
4
+
5
+ The aim of this project is to build a library which interacts with some of the most used banks. We already know how boring is to look for your balance on the web, so let's do it with the command line.
6
+
7
+ Feel free to contribute and add your bank if it isn't supported.
8
+
9
+ ## Supported banks
10
+ - Bankinter
11
+ - BBVA
12
+
13
+ ## Requirements
14
+
15
+ Some banks needs a JavaScript runtime in order to work. So if you find an error like "Could not find JavasScript runtime" try to install one. It has been tested with nodejs.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'bank_scrap'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install bank_scrap
32
+
33
+ ## Usage
34
+
35
+ Retrieve balance account
36
+
37
+ $ bank_scrap balance BANK_NAME --user YOUR_USER --password YOUR_PASSWORD
38
+
39
+ BANK_NAME should be in underscore case (`bankinter`, `bbva`).
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/[my-github-username]/bankscrap/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
48
+
49
+ ## Thanks
50
+
51
+ Thanks to Javier Cuevas (@javiercr) for his Bbva gem.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -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 'bank_scrap/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bank_scrap"
8
+ spec.version = BankScrap::VERSION
9
+ spec.authors = ["Ismael Sánchez", "Javier Cuevas", "Fernando Blat", "Raúl Marcos"]
10
+ spec.email = ["root@ismagnu.com"]
11
+ spec.summary = %q{Get your bank account details.}
12
+ spec.description = %q{Command line tools to get bank account details from some banks.}
13
+ spec.homepage = "https://github.com/ismaGNU/bank_scrap"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '~> 2.1'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+
26
+ spec.add_dependency 'thor', "~> 0.19"
27
+ spec.add_dependency 'nokogiri', "~> 1.6"
28
+ spec.add_dependency 'execjs', "~> 2.2"
29
+ spec.add_dependency 'curb', "~> 0.8"
30
+ spec.add_dependency 'activesupport', "~> 4.1"
31
+ end
data/bin/bank_scrap ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
+
4
+ require 'bank_scrap'
5
+ BankScrap::Cli.start(ARGV)
@@ -0,0 +1,64 @@
1
+ require 'curb'
2
+
3
+ module BankScrap
4
+ class Bank
5
+
6
+ WEB_USER_AGENT = 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'
7
+
8
+ private
9
+
10
+ def get(url)
11
+ @curl.url = url
12
+ @curl.get
13
+
14
+ @curl.body_str
15
+ end
16
+
17
+ def post(url, fields)
18
+ @curl.url = url
19
+
20
+ # If hash, transform to Curl::PostField objects
21
+ if fields.is_a? Hash
22
+ fields = fields.collect {|key, value| Curl::PostField.content(key, value)}
23
+ end
24
+
25
+ @curl.post(fields)
26
+ @curl.body_str
27
+ end
28
+
29
+ def set_header(name, value)
30
+ @curl.headers[name] = value
31
+ end
32
+
33
+ def set_headers(headers)
34
+ headers.each { |key, value| set_header(key, value) }
35
+ end
36
+
37
+ def get_headers
38
+ @curl.header_str
39
+ end
40
+
41
+ def initialize_cookie(url)
42
+ log 'Initialize cookie'
43
+
44
+ @curl.url = url
45
+ @curl.get
46
+
47
+ @curl.body_str
48
+
49
+ end
50
+
51
+ def initialize_connection
52
+ @curl = Curl::Easy.new
53
+ @curl.follow_location = true
54
+ @curl.ssl_verify_peer = false
55
+ @curl.verbose = true if @debug
56
+ @curl.enable_cookies = true
57
+ @curl.headers["User-Agent"] = WEB_USER_AGENT
58
+ end
59
+
60
+ def log(msg)
61
+ puts msg if @log
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,88 @@
1
+
2
+ require 'nokogiri'
3
+ require 'execjs'
4
+
5
+ module BankScrap
6
+ class Bankinter < Bank
7
+
8
+ BASE_ENDPOINT = "https://movil.bankinter.es"
9
+ LOGIN_ENDPOINT = "/mov/es-es/cgi/ebkmovil+md+login"
10
+
11
+ def initialize(user, password, log: false, debug: false)
12
+ @user = user
13
+ @password = password
14
+ @log = log
15
+ @debug = debug
16
+
17
+ initialize_connection
18
+ parse_html(initialize_cookie(BASE_ENDPOINT + '/'))
19
+ login
20
+ end
21
+
22
+ def get_balance
23
+ if @dashboard_doc
24
+ if links = @dashboard_doc.css('div.textoresaltado_md a')
25
+ links[0].text
26
+ end
27
+ end
28
+ end
29
+
30
+ def get_transactions
31
+ response = get(BASE_ENDPOINT + "/mov/es-es/cgi/" + @transactions_endpoint)
32
+
33
+ html_doc = Nokogiri::HTML(response)
34
+
35
+ html_transactions = html_doc.css('ul#listamovimiento li')
36
+
37
+ # puts html_transactions[0]
38
+ html_transactions.each do |x|
39
+ puts x.css('div.fechaimagen span').text
40
+ puts x.css('div.altoMaximo').text
41
+ puts x.css('div.importesproductosflecha_md').text
42
+ end
43
+
44
+ end
45
+
46
+ private
47
+
48
+ def login
49
+ fields = [
50
+ Curl::PostField.content('bkcache',''),
51
+ Curl::PostField.content('destino', ''),
52
+ Curl::PostField.content(@id_field, 'username,password,psi'),
53
+ Curl::PostField.content(@login_field,@login_param)
54
+ ]
55
+
56
+ response = post(BASE_ENDPOINT + LOGIN_ENDPOINT, fields)
57
+
58
+ @dashboard_doc = Nokogiri::HTML(response)
59
+
60
+ @transactions_endpoint = @dashboard_doc.xpath('//div[@class="floatIzquierdo_md"]/form/@action')[0];
61
+
62
+ end
63
+
64
+ def parse_html(html)
65
+ log 'Parsing Html'
66
+
67
+ html_doc = Nokogiri::HTML(html)
68
+
69
+ html_form = html_doc.css('form#fLogSecurity')
70
+ #TODO: Check if form is present if not retry initialize cookie for 5 attemps
71
+
72
+ @id_field = html_form.xpath('input[3]/@id')
73
+
74
+ js_source = 'var document = {};' + html_form.text
75
+
76
+ @login_field = js_source.match('eval\(doc_Login\+\'(.+)\'\+')[1]
77
+
78
+ js_function = js_source.match('sty_Login\)\.value=(.+)\(')[1]
79
+
80
+ server_id = js_source.match('Array\((.+),')[1]
81
+
82
+ js_context = ExecJS.compile(js_source)
83
+ psi_actions = 'S(' + (Time.now.to_f * 1000).to_i.to_s + ')S(12946;server:' + server_id.to_i(16).to_s + ')'
84
+
85
+ @login_param = js_context.call(js_function, @user, @password, psi_actions)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,66 @@
1
+ module BankScrap
2
+ class Bbva < Bank
3
+ BASE_ENDPOINT = 'https://bancamovil.grupobbva.com'
4
+ LOGIN_ENDPOINT = '/DFAUTH/slod/DFServletXML'
5
+ BALANCE_ENDPOINT = '/ENPP/enpp_mult_web_mobility_02/products/v1'
6
+ USER_AGENT = 'Android;LGE;Nexus 5;1080x1776;Android;4.4.4;BMES;4.0.4'
7
+
8
+ def initialize(user, password, log: false, debug: false)
9
+ @user = format_user(user.dup)
10
+ @password = password
11
+ @log = log
12
+ @debug = debug
13
+
14
+ initialize_connection
15
+
16
+ set_headers({
17
+ "User-Agent" => USER_AGENT,
18
+ 'BBVA-User-Agent' => USER_AGENT,
19
+ 'Accept-Language' => 'spa',
20
+ 'Content-Language' => 'spa',
21
+ 'Accept' => 'application/json',
22
+ 'Accept-Charset' => 'UTF-8',
23
+ 'Connection' => 'Keep-Alive',
24
+ 'Host' => 'bancamovil.grupobbva.com',
25
+ })
26
+
27
+ login
28
+ end
29
+
30
+ def get_balance
31
+ log 'get_balance'
32
+ response = post(BASE_ENDPOINT + BALANCE_ENDPOINT, nil)
33
+
34
+ json = JSON.parse(response)
35
+ json["balances"]["personalAccounts"]
36
+ end
37
+
38
+ private
39
+
40
+ # As far as we know there are two types of identifiers BBVA uses
41
+ # 1) A number of 7 characters that gets passed to the API as it is
42
+ # 2) A DNI number, this needs to transformed before it get passed to the API
43
+ # Example: "49021740T" will become "0019-049021740T"
44
+ def format_user(user)
45
+ user.upcase!
46
+
47
+ if user.match /^[0-9]{8}[A-Z]$/
48
+ # It's a DNI
49
+ "0019-0#{user}"
50
+ else
51
+ user
52
+ end
53
+ end
54
+
55
+ def login
56
+ log 'login'
57
+ post(BASE_ENDPOINT + LOGIN_ENDPOINT, {
58
+ 'origen' => 'enpp',
59
+ 'eai_tipoCP' => 'up',
60
+ 'eai_user' => @user,
61
+ 'eai_password' => @password,
62
+ 'eai_URLDestino' => '/ENPP/enpp_mult_web_mobility_02/sessions/v1'
63
+ })
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,50 @@
1
+ require 'thor'
2
+ require 'active_support/core_ext/string'
3
+
4
+ module BankScrap
5
+ class Cli < Thor
6
+
7
+ desc "balance BANK", "get account's balance"
8
+ option :user, default: ENV['USER']
9
+ option :password, default: ENV['PASSWORD']
10
+ option :log, default: false
11
+ option :debug, default: false
12
+ def balance(bank)
13
+ @user = options[:user]
14
+ @password = options[:password]
15
+ @log = options[:log]
16
+ @debug = options[:debug]
17
+
18
+ bank_class = find_bank_class_for(bank)
19
+ @client = bank_class.new(@user, @password, log: @log, debug: @debug)
20
+
21
+ balance = @client.get_balance
22
+ puts "Balance: #{balance}"
23
+ end
24
+
25
+ desc "transactions BANK", "get account's transactions"
26
+ option :user, default: ENV['USER']
27
+ option :password, default: ENV['PASSWORD']
28
+ option :log, default: false
29
+ option :debug, default: false
30
+ def transactions(bank)
31
+ @user = options[:user]
32
+ @password = options[:password]
33
+ @log = options[:log]
34
+ @debug = options[:debug]
35
+
36
+ bank_class = find_bank_class_for(bank)
37
+ @client = bank_class.new(@user, @password, log: @log, debug: @debug)
38
+
39
+ transactions = @client.get_transactions
40
+ end
41
+
42
+ private
43
+
44
+ def find_bank_class_for(bank_name)
45
+ Object.const_get("BankScrap::" + bank_name.classify)
46
+ rescue NameError
47
+ raise ArgumentError.new('Invalid bank name')
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module BankScrap
2
+ VERSION = "0.0.2"
3
+ end
data/lib/bank_scrap.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'bank_scrap/version'
2
+ require 'bank_scrap/cli'
3
+ require 'bank_scrap/bank'
4
+
5
+ # TODO: load banks dynamically
6
+ require 'bank_scrap/banks/bankinter'
7
+ require 'bank_scrap/banks/bbva'
8
+
9
+ module BankScrap
10
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bank_scrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ismael Sánchez
8
+ - Javier Cuevas
9
+ - Fernando Blat
10
+ - Raúl Marcos
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2014-11-01 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: bundler
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '1.7'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.7'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: '10.0'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '10.0'
44
+ - !ruby/object:Gem::Dependency
45
+ name: thor
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '0.19'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '0.19'
58
+ - !ruby/object:Gem::Dependency
59
+ name: nokogiri
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '1.6'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '1.6'
72
+ - !ruby/object:Gem::Dependency
73
+ name: execjs
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - "~>"
77
+ - !ruby/object:Gem::Version
78
+ version: '2.2'
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '2.2'
86
+ - !ruby/object:Gem::Dependency
87
+ name: curb
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - "~>"
91
+ - !ruby/object:Gem::Version
92
+ version: '0.8'
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '0.8'
100
+ - !ruby/object:Gem::Dependency
101
+ name: activesupport
102
+ requirement: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - "~>"
105
+ - !ruby/object:Gem::Version
106
+ version: '4.1'
107
+ type: :runtime
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '4.1'
114
+ description: Command line tools to get bank account details from some banks.
115
+ email:
116
+ - root@ismagnu.com
117
+ executables:
118
+ - bank_scrap
119
+ extensions: []
120
+ extra_rdoc_files: []
121
+ files:
122
+ - ".gitignore"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bank_scrap.gemspec
128
+ - bin/bank_scrap
129
+ - lib/bank_scrap.rb
130
+ - lib/bank_scrap/bank.rb
131
+ - lib/bank_scrap/banks/bankinter.rb
132
+ - lib/bank_scrap/banks/bbva.rb
133
+ - lib/bank_scrap/cli.rb
134
+ - lib/bank_scrap/version.rb
135
+ homepage: https://github.com/ismaGNU/bank_scrap
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '2.1'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.2.2
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Get your bank account details.
159
+ test_files: []