autility 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -10,6 +10,7 @@ For now it supports downloading invoices from:
10
10
  * Endesa
11
11
  * Vodafone (Spain)
12
12
  * Movistar (Spain)
13
+ * La Caixa (messages)
13
14
 
14
15
  ## Install
15
16
 
@@ -67,3 +67,25 @@ command :movistar do |c|
67
67
  end
68
68
  end
69
69
 
70
+ command :lacaixa do |c|
71
+ c.syntax = 'autility lacaixa [options]'
72
+ c.summary = 'Downloads invoices from lacaixa.'
73
+ c.description = 'Downloads invoices from lacaixa.'
74
+ c.example 'Download the invoice for the current month of user "foo" with password "bar"', 'utilities lacaixa --user foo --password bar'
75
+ c.option '--user USER', String, 'The username for lacaixa Online'
76
+ c.option '--password PASSWORD', String, 'The password for lacaixa Online'
77
+ c.option '--month MONTH_NUMBER', Integer, 'The month (e.g. 5, 9, 12...). By default it\'s the current month'
78
+ c.option '--output-folder FOLDER_NAME', String, 'The output folder to store the PDF in. Defaults to ~/utilities'
79
+ c.action do |args, options|
80
+ options.default :month => Time.now.month
81
+ options.default :output_folder => File.expand_path("~/utilities")
82
+
83
+ raise "Both --user and --password are required options." unless options.user && options.password
84
+
85
+ result = Autility::LaCaixa.scrape(options.user, options.password, options.month, options.output_folder)
86
+ result.each do |document_path|
87
+ puts "Path: #{document_path}"
88
+ end
89
+ end
90
+ end
91
+
@@ -6,6 +6,7 @@ require_relative 'autility/cookie'
6
6
  require_relative 'autility/endesa'
7
7
  require_relative 'autility/vodafone_spain'
8
8
  require_relative 'autility/movistar_spain'
9
+ require_relative 'autility/lacaixa'
9
10
 
10
11
  module Autility
11
12
  end
@@ -0,0 +1,166 @@
1
+ # encoding: utf-8
2
+ require 'capybara'
3
+ require 'capybara/dsl'
4
+ require 'show_me_the_cookies'
5
+ require 'fileutils'
6
+ require 'pry'
7
+
8
+ Capybara.register_driver :selenium do |app|
9
+ Capybara::Selenium::Driver.new(app, :browser => :firefox)
10
+ end
11
+ Capybara.default_wait_time = 20
12
+ Capybara.ignore_hidden_elements = false
13
+
14
+ module Autility
15
+ # Public: A scraper for all the utility invoices in LaCaixa.
16
+ #
17
+ # Examples
18
+ #
19
+ # # Download the invoice from September this year and store it in /tmp.
20
+ # LaCaixa.scrape("user", "password", 9, "/tmp") # Download all invoices from September this year
21
+ #
22
+ class LaCaixa
23
+ include Capybara::DSL
24
+ include ShowMeTheCookies
25
+
26
+ # Public: Instantiates a new scraper and fires it to download the utility
27
+ # invoices from LaCaixa.
28
+ #
29
+ # Returns nothing.
30
+ def self.scrape(*args)
31
+ new(*args).scrape
32
+ end
33
+
34
+ def initialize(user, password, month, folder)
35
+ @user = user
36
+ @password = password
37
+ @month = month
38
+ @folder = folder
39
+ @year = Time.now.year
40
+ end
41
+
42
+ # Public: Scrapes the lacaixa website and gets the invoice for the current
43
+ # month, saving it to @folder.
44
+ #
45
+ # Returns the String path of the saved document.
46
+ def scrape(index=nil)
47
+ setup_capybara
48
+ log_in
49
+
50
+ FileUtils.mkdir_p(@folder)
51
+ if index
52
+ filename = "#{@folder}/lacaixa_#{month}_#{@year}__#{index}.pdf"
53
+ document(index).save(filename)
54
+ else
55
+ return document
56
+ end
57
+ filename
58
+ end
59
+
60
+ private
61
+
62
+ # Internal: Logs in to the LaCaixa website.
63
+ #
64
+ # Returns nothing.
65
+ def log_in
66
+ visit "/home/empreses_ca.html"
67
+ fill_in "u", :with => @user
68
+ fill_in "p", :with => @password
69
+ first(".loginbut").click
70
+ end
71
+
72
+ # Internal: Gets the latest invoice and returns it as a Document (not
73
+ # fetched yet).
74
+ #
75
+ # Returns the Document to be fetched.
76
+ def document(index=nil)
77
+ @document ||= begin
78
+ params = {}
79
+ url = "https://loc6.lacaixa.es"
80
+
81
+ wait_until { find('frame') }
82
+
83
+ within_frame(all('frame')[1][:name]) do
84
+ within_frame(find('frame')[:name]) do
85
+ find('#buzon1.msn a').click
86
+ end
87
+ end
88
+
89
+ wait_until { find('frame') }
90
+
91
+ within_frame(all('frame')[1][:name]) do
92
+ within_frame('Cos') do
93
+ sleep 3
94
+ find("#lbl_Varios a").click
95
+ sleep 3
96
+ wait_until { find('#enlaceDescr') }
97
+ rows = all('.table_generica tr').select do |tr|
98
+ tr.find("td").text =~ /#{month}\/#{@year}/
99
+ end
100
+
101
+ if index
102
+ rows[index].find("a").click
103
+ else
104
+ docs = []
105
+ rows.each_with_index do |row, idx|
106
+ scraper = LaCaixa.new(@user, @password, @month, @folder)
107
+ docs << scraper.scrape(idx)
108
+ end
109
+ return docs
110
+ end
111
+ end
112
+ end
113
+
114
+ wait_until { find('frame') }
115
+
116
+ within_frame(all('frame')[1][:name]) do
117
+ within_frame('Cos') do
118
+ within_frame("Fr1") do
119
+ url = find("form[name=\"datos\"]")[:action]
120
+ within("form[name=\"datos\"]") do
121
+ guardar = {
122
+ "PN" => "COM",
123
+ "PE" => "39",
124
+ "RESOLUCION" => "300",
125
+ "CANAL_MOVIMIENTO" => "INT",
126
+ "target" => "Fr1",
127
+ "PAGINA_SOLICITADA" => "00001",
128
+ "FLAG_PDF_INICIAL" => "S",
129
+ "FLUJO" => "COM,10,:COM,51:SCP,23:GFI,7,''",
130
+ "CLICK_ORIG" => "FLX_COM_4",
131
+ "OPCION" => ""
132
+ }
133
+
134
+ params = all('input').reduce({}) do |h, i|
135
+ h.update({ i[:name] => i[:value] })
136
+ end.update(guardar)
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ Capybara.app_host = "https://loc6.lacaixa.es"
143
+ cookie = Cookie.new("JSESSIONID_CTX", get_me_the_cookie("JSESSIONID_CTX")[:value])
144
+
145
+ Document.new(url, :post, cookie, params)
146
+ end
147
+ end
148
+
149
+ # Internal: Returns the String current month padded with zeros to the left.
150
+ def month
151
+ @month.to_s.rjust(2, '0')
152
+ end
153
+
154
+ # Internal: Sets the configuration for capybara to work with the LaCaixa
155
+ # website.
156
+ #
157
+ # Returns nothing.
158
+ def setup_capybara
159
+ Capybara.run_server = false
160
+ Capybara.current_driver = :selenium
161
+ Capybara.app_host = 'http://empresa.lacaixa.es'
162
+ Capybara.default_wait_time = 20
163
+ Capybara.ignore_hidden_elements = false
164
+ end
165
+ end
166
+ end
@@ -1,3 +1,3 @@
1
1
  module Autility
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autility
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
16
- requirement: &70096387494420 !ruby/object:Gem::Requirement
16
+ requirement: &70133155093360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70096387494420
24
+ version_requirements: *70133155093360
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: capybara-webkit
27
- requirement: &70096387493420 !ruby/object:Gem::Requirement
27
+ requirement: &70133155091420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70096387493420
35
+ version_requirements: *70133155091420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: show_me_the_cookies
38
- requirement: &70096387492300 !ruby/object:Gem::Requirement
38
+ requirement: &70133155089040 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70096387492300
46
+ version_requirements: *70133155089040
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: commander
49
- requirement: &70096387489320 !ruby/object:Gem::Requirement
49
+ requirement: &70133155088360 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70096387489320
57
+ version_requirements: *70133155088360
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: mocha
60
- requirement: &70096387488100 !ruby/object:Gem::Requirement
60
+ requirement: &70133155087420 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70096387488100
68
+ version_requirements: *70133155087420
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: minitest
71
- requirement: &70096387487580 !ruby/object:Gem::Requirement
71
+ requirement: &70133155086440 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70096387487580
79
+ version_requirements: *70133155086440
80
80
  description: Downloads utility invoices from common spanish firms such as Endesa or
81
81
  Vodafone.
82
82
  email:
@@ -99,6 +99,7 @@ files:
99
99
  - lib/autility/data.rb
100
100
  - lib/autility/document.rb
101
101
  - lib/autility/endesa.rb
102
+ - lib/autility/lacaixa.rb
102
103
  - lib/autility/movistar_spain.rb
103
104
  - lib/autility/version.rb
104
105
  - lib/autility/vodafone_spain.rb