za_bank_scraper 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTQxMDlmMDc0NWM1ZmViY2ZjYTkxNjI0YjU1NzFiNmVkNzU0ZGE0NA==
5
+ data.tar.gz: !binary |-
6
+ MmMxMjgwMTM4ZThhMjRhNDA5YmY5YTY5ZDU4ZDBjNmU4MThkZDIyYw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjBiNmRkZThlNWU4MTc5NDM1MjQ1MzcyNGE5MzFmYTZiNDI3MmQzZTU0MmI2
10
+ MmJhODZiODU1MzcyNDBjNzc4Yzc4Y2NiMjllZDVhYmY3MWJhNTIzZmQyZjgy
11
+ NjA3YjY1NTRjYjFlOWQ3ZGZlZjhjZTI1NTVmODgzMTVlYzgwODU=
12
+ data.tar.gz: !binary |-
13
+ YmNlNzBlNTJkOGQyZGI0ZTM4ODU3NjA1NjVmODFjYTM4NWRiYWE5OTdlNjNm
14
+ ZTRiNGJhYWRkYzgzZjBlZjE3YjgwYmVkYzU1OWI5OGNkMmM0NGNiNGNkZjFj
15
+ MDllMzA3MmI3MWY5YmQyMjI3ZDdhYmJjOGExNWZkOWFkMmJmNGU=
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in za_bank_scraper.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # ZaBankScraper
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/za_bank_scraper`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'za_bank_scraper'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install za_bank_scraper
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/za_bank_scraper/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "za_bank_scraper"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,154 @@
1
+ module ZaBankScraper
2
+ class Absa
3
+
4
+ attr_accessor :base_url, :account_number, :pin, :password, :user_number, :date_from, :date_to
5
+
6
+ def initialize(options={})
7
+ @base_url = "https://ib.absa.co.za/ib/AuthenticateW.do"
8
+
9
+ @agent = Mechanize.new
10
+ @account_number = options[:account_number]
11
+ @pin = options[:pin]
12
+ @user_number = options[:user_number]
13
+ @password = options[:password]
14
+ @date_from = options[:date_from]
15
+ @date_to = options[:date_to]
16
+ end
17
+
18
+ def pretty_print
19
+ array = []
20
+ array << [ "Date", "T.Num", "Acc Num", "Description", "Dep Id", "Amount", "Balance"]
21
+ scrape.each do |t|
22
+ array << [t[:remittance_date], t[:transaction_number_for_day], t[:account_number],t[:description], t[:dep_id], t[:amount], t[:account_balance_after_transaction] ]
23
+ end
24
+
25
+ puts array.to_table
26
+ end
27
+
28
+ def scrape
29
+ agent = Mechanize.new
30
+ agent.get(base_url) # mobi
31
+ agent.keep_alive = false
32
+
33
+ # Page 1 of login form
34
+ agent.page.forms[0]['AccessAccount'] = account_number
35
+ agent.page.forms[0]['PIN'] = pin
36
+ agent.page.forms[0]['user'] = user_number
37
+ agent.submit(agent.page.forms[0], agent.page.forms[0].button_with(:name => 'button_processPIN'))
38
+
39
+ # Page 2 of login form
40
+ (1..password.length).each do |digit|
41
+ if agent.page.forms[0]["DIGITOUT#{digit-1}"].nil?
42
+ agent.page.forms[0]["DIGIT#{digit-1}"] = password[digit-1]
43
+ end
44
+ end
45
+
46
+ agent.submit(agent.page.forms[0], agent.page.forms[0].button_with(:name => 'button_processPassword'))
47
+
48
+ # Go to My Account
49
+
50
+ agent.page.links[0].click
51
+
52
+ # Go to Statement
53
+
54
+ agent.page.links[1].click
55
+
56
+ # Go to a specific account for last 2 days
57
+
58
+ select_list = agent.page.forms[0].field_with(:name => "SelectAccount")
59
+
60
+ html_transactions = []
61
+ select_list.options.select.each do |select|
62
+
63
+ account_number = select.text[0,10]
64
+ # p "--------------BEGIN--------------"
65
+ html_transactions.push get_transactions_for_account(agent, account_number, date_from, date_to)
66
+ # p "---------------END---------------"
67
+ agent.submit(agent.page.forms[0], agent.page.forms[0].button_with(:name => 'button_promptSelectionCriteria'))
68
+
69
+ end
70
+
71
+ html_transactions.flatten
72
+ end
73
+
74
+ def get_transactions_for_account(agent, account_number, date_from, date_to)
75
+ search_page = agent.page.form_with(:action => 'https://ib.absa.co.za/ib/TranHistoryW.do') do |f|
76
+ f.field_with(:name => "SelectAccount").value = f.field_with(:name => "SelectAccount").options.select {|o| o.text.start_with? account_number}[0]
77
+ f.field_with(:name => "dateRange").value = f.field_with(:name => "dateRange").options.select {|o| o.text.start_with? 'Custom'}[0]
78
+ f.fromDate = date_from
79
+ f.toDate = date_to
80
+ end.submit agent.page.forms[0].button_with(:name => 'button_View')
81
+
82
+ # fetch transactions
83
+ results = []
84
+ array = []
85
+ button = true
86
+
87
+ temp_date = Date.strptime(date_from, "%Y%m%d").to_s
88
+ count = 1
89
+
90
+ while button
91
+
92
+ html_transactions = agent.page.parser.xpath("//p[contains(@class, 'alt')]")
93
+
94
+ html_transactions.map do |transaction|
95
+ array = transaction.children.select {|child| child.name == "text"}.map {|child| child.content.gsub(/\t/, '').gsub(/\r\n/, '').strip}
96
+
97
+ if array.length == 5
98
+ dep_id = ''
99
+ if array.select {|elem| elem.empty?}.empty?
100
+ dep_id = array[-3]
101
+ amount = array[-2].gsub("R", '').gsub(".", '').gsub(" ", '').to_i.to_s
102
+ else
103
+ amount = array[-3].gsub("R", '').gsub(".", '').gsub(" ", '').to_i.to_s
104
+ end
105
+ account_balance_after_transaction = array[-1].gsub("R", '').gsub(".", '').gsub(" ", '').to_i.to_s
106
+ elsif array.length == 7
107
+ dep_id = array[2] + array[3]
108
+ amount = array[-3].gsub("R", '').gsub(".", '').gsub(" ", '').to_i.to_s
109
+ account_balance_after_transaction = array[-1].gsub("R", '').gsub(".", '').gsub(" ", '').to_i.to_s
110
+ elsif array.length == 4
111
+ # ["2015-07-28", "CASH DEP BRANCH RICHARDS B( 123,00 )", "R 8 000.00", "69 155.95"]
112
+ dep_id = ""
113
+ amount = array[-2].gsub("R", '').gsub(".", '').gsub(" ", '').to_i.to_s
114
+ account_balance_after_transaction = array[-1].gsub("R", '').gsub(".", '').gsub(" ", '').to_i.to_s
115
+ else
116
+ if (array[-2] =~ /^R\s-?[0-9\s]*.[0-9]{2}$/)
117
+ amount = array[-2].gsub("R", '').gsub(".", '').gsub(" ", '').to_i.to_s
118
+ dep_id = array[-4] + array[-3]
119
+ else
120
+ dep_id = array[2]
121
+ amount = array[-3].gsub("R", '').gsub(".", '').gsub(" ", '').to_i.to_s
122
+ end
123
+ account_balance_after_transaction = array[-1].gsub("R", '').gsub(".", '').gsub(" ", '').to_i.to_s
124
+ end
125
+
126
+ if temp_date != array[0]
127
+ count = 1
128
+ temp_date = array[0]
129
+ end
130
+
131
+ results <<
132
+ {
133
+ :transaction_number_for_day => count,
134
+ :account_number => account_number,
135
+ :remittance_date => array[0],
136
+ :description => array[1],
137
+ :dep_id => dep_id,
138
+ :amount => amount,
139
+ :account_balance_after_transaction => account_balance_after_transaction
140
+ }
141
+
142
+ count += 1
143
+ end
144
+
145
+ button = agent.page.forms[0].button_with(:name => "button_buttonNext")
146
+
147
+ agent.submit(agent.page.forms[0], agent.page.forms[0].button_with(:name => 'button_buttonNext')) unless button.nil?
148
+
149
+ end
150
+ # results.each {|x| puts x}
151
+ results
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,3 @@
1
+ module ZaBankScraper
2
+ VERSION = "0.1.6"
3
+ end
@@ -0,0 +1,12 @@
1
+ require "za_bank_scraper/version"
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+ require 'mechanize'
5
+ require 'logger'
6
+ require 'text-table'
7
+
8
+ module ZaBankScraper
9
+ # Your code goes here...
10
+ end
11
+
12
+ require 'za_bank_scraper/absa'
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'za_bank_scraper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "za_bank_scraper"
8
+ spec.version = ZaBankScraper::VERSION
9
+ spec.authors = ["Jeffrey van Aswegen"]
10
+ spec.email = ["jeffmess@gmail.com"]
11
+
12
+ spec.summary = %q{Scrapes South African Bank website for ofx statements (ABSA, STD Bank, FNB).}
13
+ spec.homepage = "http://github.com/jeffmess/za_bank_scraper"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"#{}"TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
22
+ end
23
+
24
+ spec.add_dependency "nokogiri", "1.5.5"
25
+ spec.add_dependency "mechanize", "2.7.3"
26
+ spec.add_dependency "text-table"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.8"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: za_bank_scraper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ platform: ruby
6
+ authors:
7
+ - Jeffrey van Aswegen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: mechanize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.7.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 2.7.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: text-table
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ type: :development
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: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description:
84
+ email:
85
+ - jeffmess@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .travis.yml
91
+ - Gemfile
92
+ - README.md
93
+ - Rakefile
94
+ - bin/console
95
+ - bin/setup
96
+ - lib/za_bank_scraper.rb
97
+ - lib/za_bank_scraper/absa.rb
98
+ - lib/za_bank_scraper/version.rb
99
+ - za_bank_scraper.gemspec
100
+ homepage: http://github.com/jeffmess/za_bank_scraper
101
+ licenses: []
102
+ metadata:
103
+ allowed_push_host: https://rubygems.org
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.8
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Scrapes South African Bank website for ofx statements (ABSA, STD Bank, FNB).
124
+ test_files: []