smartermeter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ require 'mechanize'
2
+ require 'csv'
3
+
4
+ module SmarterMeter
5
+ class Service
6
+ LOGIN_URL = "http://www.pge.com/myhome/"
7
+ OVERVIEW_URL = "https://www.pge.com/csol/actions/login.do?aw"
8
+ ENERGYGUIDE_AUTH_URL = "https://www.energyguide.com/LoadAnalysis/LoadAnalysis.aspx?Referrerid=154"
9
+
10
+ def initialize
11
+ @agent = WWW::Mechanize.new { |agent|
12
+ agent.user_agent_alias = 'Mac Safari'
13
+ }
14
+ end
15
+
16
+ # Returns true upon succesful login and false otherwise
17
+ def login(username, password)
18
+ @agent.get(LOGIN_URL) do |page|
19
+ logged_in_page = page.form_with(:action => 'https://www.pge.com/eum/login') do |login|
20
+ login.USER = username
21
+ login.PASSWORD = password
22
+ end.submit
23
+ end
24
+
25
+ # There is a crazy meta-reload thing here that mechanize doesn't handle
26
+ # correctly by itself so let's help it along...
27
+ @agent.get(OVERVIEW_URL) do |page|
28
+
29
+ return false if page.title =~ /PG&E Login/
30
+
31
+ # Load the PG&E Terms of Use page
32
+ tou_page = @agent.click(page.link_with(:href => '/csol/actions/billingDisclaimer.do?actionType=hourly'))
33
+ form = tou_page.forms().first
34
+ agree_button = form.button_with(:value => 'I Understand - Proceed')
35
+
36
+ # Agree to the terms of use
37
+ form['agreement'] = 'yes'
38
+
39
+ # Load up the PG&E frame page for historical data
40
+ hourly_usage_container = form.submit(agree_button)
41
+
42
+ # Now load up the frame with the content
43
+ hourly_usage = @agent.click(hourly_usage_container.frames.select{|f| f.href == "/csol/nexus/content.jsp"}.first)
44
+
45
+ # Now post the authentication information from PG&E to energyguide.com
46
+ @data_page = hourly_usage.form_with(:action => ENERGYGUIDE_AUTH_URL).submit
47
+ end
48
+ true
49
+ end
50
+
51
+ def fetch_csv(date)
52
+ # TODO: Check if the authentication has been called
53
+
54
+ # Now we almost actually have data. However we need to setup the desired
55
+ # parameters first before we can get the exportable data. This really shouldn't
56
+ # be necessary.
57
+ hourly_data = @data_page.form_with(:action => "/LoadAnalysis/LoadAnalysis.aspx") do |form|
58
+ form['__EVENTTARGET'] = "objChartSelect$butSubmit"
59
+ form['objTimePeriods$objExport$hidChart'] = "Hourly Usage"
60
+ form['objTimePeriods$objExport$hidChartID'] = 8
61
+ form['objChartSelect$ddChart'] = 8 # Hourly usage
62
+
63
+ form['objTimePeriods$objExport$hidTimePeriod'] = "Week"
64
+ form['objTimePeriods$objExport$hidTimePeriodID'] = 3
65
+ form['objTimePeriods$rlPeriod'] = 3
66
+
67
+ form['objChartSelect$ccSelectedDate1'] = date.strftime("%m/%d/%Y")
68
+ end.submit
69
+
70
+ # Now the beautiful data...
71
+ hourly_csv = hourly_data.form_with(:action => "/LoadAnalysis/LoadAnalysis.aspx") do |form|
72
+ form['__EVENTTARGET'] = "objTimePeriods$objExport$butExport"
73
+ end.submit
74
+
75
+ hourly_csv.body
76
+ end
77
+ end
78
+ end