fortum_reader 0.0.3

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.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/fortum_reader.rb +164 -0
  3. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzRjZDJmM2VlYjNlYWYxMTdjNDI4NDczNTdjM2MzMGNlM2ZkMTgyMg==
5
+ data.tar.gz: !binary |-
6
+ MjFhNDUzNjk4MWZhYmZlYTBlOTFlZDQxYTkzMWQzYWM4ZDIzZjQwNA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YjVjYTczMWY4NmNiMjZlM2ZjZGYxMmZkOWNmZDc3NmMwOTZmNzgxMGZiYmY0
10
+ Yzk2OThjNDQ1OGUyNjMwNGJlMzgzOGIwMDc1YWNjMGI2OTdiMjhjYWJhZDRl
11
+ YzAxOGVmNGExNGE0MjdiZjM4YjM1ZmY0Yjc0YTE2OTI5ZGE1NjU=
12
+ data.tar.gz: !binary |-
13
+ MjY4MGFjMjU5ZTAwYmZmMDU2OWZhNGYxODQxYTM4ZGE2YTYyMDNlMTI3YTUy
14
+ Y2U1MmVjYzcxMjViYzM5YmM4OGQ4ODg3NjkzZDVkNTJmZTViZTllODc0Mzc2
15
+ ZjBkMDRkMzQyYzgxYzNmZGE2OTM5OGY1MTk4NzFlZWMyNGU5ZmY=
@@ -0,0 +1,164 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'rubygems'
3
+ require 'mechanize'
4
+ require 'logger'
5
+ class FortumReader
6
+
7
+ START_URL = 'https://www.fortum.com/countries/fi/yksityisasiakkaat/omat-palvelut/kulutuksen-seuranta/lukemahistoria/pages/default.aspx'
8
+ AGREEMENTS_URL = 'https://www.fortum.com/countries/fi/yksityisasiakkaat/omat-palvelut/sopimukset/pages/default.aspx'
9
+
10
+ def initialize(username, password)
11
+ @rds=[]
12
+ @username=username
13
+ @password=password
14
+ raise "Username and password must exist!" unless @username && @password
15
+ @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
16
+ end
17
+
18
+ # Check that login works with given params.
19
+ def login_ok?
20
+ new_agent
21
+ welcome_page=login
22
+ welcome_page.uri.to_s.include?("login") ? false : true
23
+ end
24
+
25
+ # Read readings! Returns array of readings.
26
+ def read
27
+ new_agent
28
+ @cups=[]
29
+ welcome_page=login
30
+ if welcome_page.uri.to_s.include?("login")
31
+ raise "Cannot read readings: login was not OK. Is there problem with username(#{@username}) and password?"
32
+ end
33
+ select_cup_form=navigate_to_select_cup_page(welcome_page).form
34
+ # read available cups
35
+ read_cups(select_cup_form)
36
+ # Iterate each cup.
37
+ @cups.each_with_index do |cup, index|
38
+ @logger.info "Reading usage point #{index+1} #{cup[:usage_point_id]}..."
39
+ usages_page=navigate_to_usages_page(select_cup_form, index+1, cup[:usage_point_id])
40
+ # Find each page and load readings
41
+ usages_page.links.each do |link|
42
+ if link.href.include?("ReadingReport")
43
+ reading_page=link.click
44
+ @logger.info "Reading from page with title '#{link.text.chomp}'"
45
+ collect_readings(reading_page, cup[:usage_point_id])
46
+ end
47
+ end
48
+
49
+ if index < @cups.size-1
50
+ logout
51
+ welcome_page=login
52
+ select_cup_form=navigate_to_select_cup_page(welcome_page).form
53
+ end
54
+ end
55
+ @logger.info "Ready! Got #{@rds.size} readings."
56
+ @rds
57
+ end
58
+
59
+ def logout
60
+ @agent=nil
61
+ end
62
+
63
+ def readings
64
+ @rds
65
+ end
66
+
67
+ # Reads basic information for customer targets. Example address. Returns hash.
68
+ def basic_information
69
+ vals={}
70
+
71
+ new_agent
72
+ welcome_page=login(AGREEMENTS_URL)
73
+ bodypage=welcome_page.iframe.click
74
+ ps=bodypage.search("//p")
75
+ ps.each do |node|
76
+ if node.text.include?("Käyttöpaikka:")
77
+ parts=node.text.split("\n")
78
+ vals[:street_address]=parts[1].chomp.strip
79
+ vals[:postno]=parts[2].chomp.strip
80
+ vals[:city]=parts[3].chomp.strip.capitalize
81
+ end
82
+ end
83
+
84
+ vals
85
+ end
86
+
87
+ # Reads usage point information. Returns list of usage point information hashes.
88
+ def usage_point_information
89
+ list=[]
90
+ # TODO: Implement
91
+ list
92
+ end
93
+
94
+ # Nagigagtes from welcome page to select cup page.
95
+ def navigate_to_select_cup_page(welcome_page)
96
+ lukemahistoria_page=welcome_page.link_with(text: "Lukemahistoria").click
97
+ select_cup_page=lukemahistoria_page.iframe.click
98
+ select_cup_page
99
+ end
100
+
101
+ def new_agent
102
+ @agent=Mechanize.new
103
+ @agent.user_agent_alias="Linux Firefox"
104
+ @agent
105
+ end
106
+
107
+ def agent
108
+ new_agent unless @agent
109
+ @agent
110
+ end
111
+
112
+ # Logins user. Return page after login.
113
+ def login(url= START_URL)
114
+ page=agent.get(url)
115
+ login_form=page.form
116
+ login_form.username=@username
117
+ login_form.password=@password
118
+ page=agent.submit(login_form)
119
+ @logger.info "After login, result page url is #{page.uri}"
120
+ page
121
+ end
122
+
123
+ def read_cups(select_cup_form)
124
+ @cups=[]
125
+ @logger.info "Reading usage point information..."
126
+ select_cup_form.page.search("//table//tr").each_with_index do |cup, index|
127
+ infos=cup.children[1].text.gsub(/\r\n/m, "\n").split("\n")
128
+ @cups<<{usage_point_id: infos[1].strip, street_address: infos[2].strip, postno: infos[3].strip, city: infos[4].strip}
129
+ end
130
+ @logger.info "Found #{@cups.size} usage points."
131
+ end
132
+
133
+ def navigate_to_usages_page(select_cup_form, no, usage_point_id)
134
+ @logger.info "Reading usage point #{usage_point_id} readings..."
135
+ select_cup_form.radiobuttons[no-1].check
136
+ #select_cup_form["cmdSelectCP"]="Jatka"
137
+ usages_page=select_cup_form.click_button
138
+ raise "error: submit select did not work." if usages_page.content.include?("Valitse k")
139
+ raise "error: Did not get given usage_point_id #{usage_point_id} page." unless usages_page.content.include?(usage_point_id)
140
+ usages_page
141
+ end
142
+
143
+ def collect_readings(page, usage_point_id)
144
+ readings=page.search("//tr[@name='NormalRow']") + page.search("//tr[@name='HighLightRow']")
145
+ @logger.debug "Got #{readings.size} possible elements for readings..."
146
+ readings.each do |reading|
147
+ if reading.children.size >= 4
148
+ comment=reading.children[1].children.text.chomp
149
+ #@logger.debug "comment is #{comment}"
150
+ reading_hash={read_at: reading.children[0].children.text.chomp,
151
+ reading: reading.children[3].children.text.chomp,
152
+ usage_point_id: usage_point_id,
153
+ comment: reading.children[1].children.text.chomp}
154
+ @logger.info "Reading hash: #{reading_hash}"
155
+ if comment.downcase.include?("siirto") || comment.downcase.include?("kauko")
156
+ @rds<<reading_hash
157
+ else
158
+ @logger.error "Got reading, with unknown comment: #{comment} cannot process."
159
+ end
160
+ end
161
+ end
162
+ end
163
+
164
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fortum_reader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - kulutusseuranta.fi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mechanize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Fortum Reader can read readings from Fortum Web site using screen scraping
28
+ techniques. Credentials needed.
29
+ email: support@kulutussseuranta.fi
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/fortum_reader.rb
35
+ homepage: https://github.com/kulutusseuranta/fortum_reader
36
+ licenses: []
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.8.7
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.0.3
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Fortum Reader can read readings from Fortum Web site using screen scraping
58
+ techniques.
59
+ test_files: []