ispusage 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -11,6 +11,10 @@ begin
11
11
  gem.homepage = "http://github.com/aussiegeek/ispusage"
12
12
  gem.authors = ["Alan Harper"]
13
13
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency 'json'
15
+ gem.add_dependency 'nokogiri'
16
+ gem.add_dependency 'mechanize'
17
+
14
18
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
19
  end
16
20
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'mechanize'
3
+
4
+ class IspUsage
5
+ class Fetchers
6
+ class AUOptus < Fetcher
7
+ def initialize(options)
8
+ super(options)
9
+ end
10
+
11
+ def fetch_usage
12
+ agent = Mechanize.new
13
+
14
+ page = agent.get('https://my.optus.com.au/web/oscportal.portal?site=personal')
15
+ form = page.form('Login')
16
+ form['USER'] = self.username
17
+ form['PASSWORD'] = self.password
18
+
19
+ page = agent.submit(form, form.buttons.first)
20
+
21
+ account_no = page.search('h2').text.match(/\d+/)
22
+ services = page.search('ol.service_list')
23
+ services.each do |service|
24
+ phone_no = service.search('div>a').text.strip
25
+ page = agent.get("https://my.optus.com.au/web/oscportal.portal?_nfpb=true&_pageLabel=redirectPage&redirectTo=deeplink_myusage_postpaid&site=personal&pageName=unbilledUsage&virAcctNum=#{phone_no}&accNum=#{account_no}&_activeBA=#{account_no}&_activeSI=#{phone_no}")
26
+
27
+ plan_text = page.search("html>body>div>div>div:nth-of-type(3)>div>div:nth-of-type(2)>div:nth-of-type(4)>div>table tr:nth-of-type(5)>td>table tr:nth-of-type(2)>td>div>table tr:nth-of-type(2)>td:nth-of-type(2)").text
28
+
29
+
30
+ downloads = UsagePeriod.new
31
+ self.usage_periods << downloads
32
+
33
+ downloads.label = phone_no
34
+ downloads.quota = plan_text.match(/(\d+)MB/)[1].to_i
35
+
36
+ usagesummary = page.search('table.usagesummary')
37
+ usagesummary.search('tr').each do |row|
38
+ row_name = row.search('td[1]').text
39
+ case row_name
40
+ when 'Data - Mobile Internet'
41
+ downloads.used = row.search('td[2]').text.match(/\d+\./)[0].to_i
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'mechanize'
3
+
4
+ class IspUsage
5
+ class Fetchers
6
+ class AUThree < Fetcher
7
+ def initialize(options)
8
+ super(options)
9
+ end
10
+
11
+ def fetch_usage
12
+ agent = Mechanize.new
13
+ page = agent.get('https://www.my.three.com.au/My3/jfn')
14
+ login_form = page.form('login')
15
+ login_form.login = self.username
16
+ login_form.password = self.password
17
+ page = agent.submit(login_form, login_form.buttons.first)
18
+ pin_form = page.form('myForm')
19
+ if pin_form.nil?
20
+ puts "Authentication failure"
21
+ exit(1)
22
+ end
23
+ pin_form.pin = options[:pin]
24
+ page = agent.submit(pin_form, pin_form.buttons.first)
25
+
26
+ downloads = UsagePeriod.new
27
+ self.usage_periods << downloads
28
+
29
+ downloads.quota = page.search("td[width='190px;']").text.strip.to_i
30
+ downloads.used = downloads.quota - page.search('br+strong.largeclass').text.strip.to_i
31
+ end
32
+ end
33
+ end
34
+ end
@@ -4,12 +4,12 @@ require 'json'
4
4
  class IspUsage
5
5
  class Fetchers
6
6
  class Fetcher
7
- attr_accessor :usage_periods, :username, :password
7
+ attr_accessor :usage_periods, :username, :password, :options
8
8
 
9
9
  def initialize(options)
10
10
  @username = options[:username]
11
11
  @password = options[:password]
12
-
12
+ self.options = options
13
13
  self.usage_periods = []
14
14
  end
15
15
 
@@ -1,3 +1,5 @@
1
1
  require 'ispusage/fetchers/fetcher'
2
+ require 'ispusage/fetchers/au_optus'
3
+ require 'ispusage/fetchers/au_three'
2
4
  require 'ispusage/fetchers/iinet'
3
5
  require 'ispusage/fetchers/internode'
data/lib/ispusage.rb CHANGED
@@ -7,9 +7,10 @@ require 'json'
7
7
  class IspUsage
8
8
 
9
9
  ISPS = [
10
- 'AUBigpond',
11
10
  'AUIinet',
12
- 'AUInternode'
11
+ 'AUInternode',
12
+ 'AUOptus',
13
+ 'AUThree'
13
14
  ]
14
15
 
15
16
  class Cli
@@ -20,6 +21,7 @@ class IspUsage
20
21
  opts.on('-i', '--isp isp', 'ISP') {|i| options[:isp] = i}
21
22
  opts.on('-u', '--username username', 'Username') {|u| options[:username] = u}
22
23
  opts.on('-p', '--password password', 'Password') {|p| options[:password] = p}
24
+ opts.on('-q', '--pin ', 'PIN') {|pin| options[:pin] = pin}
23
25
  opts.on('-j', '--json', 'JSON Output') {|j| options[:output] = :json}
24
26
  end
25
27
  optparse.parse!
@@ -35,7 +37,9 @@ class IspUsage
35
37
  fetcher.fetch_usage
36
38
  case options[:output]
37
39
  when :text
38
- puts fetcher.used.to_s + '/' + fetcher.quota.to_s
40
+ fetcher.usage_periods.each do |usage_period|
41
+ puts usage_period.label + ':' + usage_period.used.to_s + '/' + usage_period.quota.to_s
42
+ end
39
43
  when :json
40
44
  puts fetcher.to_json
41
45
  end
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe IspUsage::Fetchers::AUOptus do
4
+ describe "single usage period" do
5
+ before do
6
+ signon = File.read(File.dirname(__FILE__) + '/../fixtures/au_optus/signon.html')
7
+ FakeWeb.register_uri(:get, 'https://my.optus.com.au/web/oscportal.portal?site=personal', :response => signon)
8
+ my_account = File.read(File.dirname(__FILE__) + '/../fixtures/au_optus/my_account.html')
9
+ FakeWeb.register_uri(:post, 'https://my.optus.com.au/signon/Optus/login_ext.sec', :response => my_account)
10
+ my_usage = File.read(File.dirname(__FILE__) + '/../fixtures/au_optus/my_usage.html')
11
+ FakeWeb.register_uri(:get, 'https://my.optus.com.au/web/oscportal.portal?_nfpb=true&_pageLabel=redirectPage&redirectTo=deeplink_myusage_postpaid&site=personal&pageName=unbilledUsage&virAcctNum=0123456789&accNum=01234567890123&_activeBA=01234567890123&_activeSI=0123456789', :response => my_usage)
12
+
13
+ options = {:username => 'user', :password => 'password'}
14
+ @usage = IspUsage::Fetchers::AUOptus.new(options)
15
+ @usage.fetch_usage
16
+ end
17
+
18
+ it "should return usage correctly" do
19
+ @usage.usage_periods.first.used.should == 151
20
+ end
21
+
22
+ it "should return quota correctly" do
23
+ @usage.usage_periods.first.quota.should == 500
24
+ end
25
+
26
+ it "should return total correctly" do
27
+ @usage.usage_periods.first.total.should == 500
28
+ end
29
+
30
+ it "should set label to phone number" do
31
+ @usage.usage_periods.first.label.should == "0123456789"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe IspUsage::Fetchers::AUThree do
4
+ describe "single usage period" do
5
+ before do
6
+ signon = File.read(File.dirname(__FILE__) + '/../fixtures/au_three/signon.html')
7
+ FakeWeb.register_uri(:get, 'https://www.my.three.com.au/My3/jfn', :response => signon)
8
+ pin = File.read(File.dirname(__FILE__) + '/../fixtures/au_three/pin.html')
9
+ my_usage = File.read(File.dirname(__FILE__) + '/../fixtures/au_three/usage.html')
10
+ FakeWeb.register_uri(:post, 'https://www.my.three.com.au/My3/jfn', [{:response => pin, :times => 1}, {:response => my_usage}])
11
+
12
+ options = {:username => 'user', :password => 'password', :pin => '1234'}
13
+ @usage = IspUsage::Fetchers::AUThree.new(options)
14
+ @usage.fetch_usage
15
+ end
16
+
17
+ it "should return usage correctly" do
18
+ @usage.usage_periods.first.used.should == 61
19
+ end
20
+
21
+ it "should return quota correctly" do
22
+ @usage.usage_periods.first.quota.should == 500
23
+ end
24
+
25
+ it "should return total correctly" do
26
+ @usage.usage_periods.first.total.should == 500
27
+ end
28
+ end
29
+ end