]>
code.delx.au - monosys/blob - scripts/virginmobile.rb
4 # ROOTCA = "/etc/ssl/certs/ca-certificates.crt"
5 # SMTPSERVER = "mail.example.com"
6 # FROMADDR = "cron@example.com"
9 # ["person1@example.com", "0499999999", "000000", 100, 30],
10 # ["person2@example.com", "0499999998", "000000", 100, 30],
20 def initialize(sleep_time
, username
, password
)
21 @sleep_time = sleep_time
27 def do_request(path
, form_data
=nil)
28 sleep(@sleep_time) # Don't look like a bot
29 http
= Net
::HTTP.new("www.virginmobile.com.au", 443)
33 http
.verify_mode
= OpenSSL
::SSL::VERIFY_PEER
37 req
= Net
::HTTP::Get.new(path
)
38 req
["Cookie"] = @cookie
41 req
= Net
::HTTP::Post.new(path
)
42 req
.form_data
= form_data
44 return http
.request(req
)
49 "username" => @username,
50 "password" => @password,
52 res
= do_request("/selfcare/MyAccount/LogoutLoginPre.jsp", form_data
)
53 @cookie = res
.fetch("Set-Cookie")
55 while location
= res
.get_fields("Location")
56 location
= URI
.parse(location
[0])
57 res
= do_request(location
.path
)
62 do_request("/selfcare/dispatch/Logout")
65 def request_bill_preview
66 res
= do_request("/selfcare/dispatch/PostPayUnbilledUsage")
67 usage
= res
.body
.scan(/\$([\d\.]*)/).flatten
68 last_bill_date
= res
.body
.gsub(/\n/, '').scan(/Last bill date:.*>(\d\d?\/\d\d
?\
/\d\d\d\d)/).flatten
69 return usage
, last_bill_date
72 def request_mobile_browsing
73 res
= do_request("/selfcare/dispatch/DataUsageRequest")
74 data_usage
= res
.body
.scan(/USAGE: ([\d\.]*)MB/).flatten
79 res
.each_capitalized
do |key
, value
|
80 print
"#{key}: #{value}\n"
86 def check_usage(sleep_time
, ignore_limits
, destination
, username
, password
, usageLimit
, dataLimit
)
88 data = VirginMobile
.new(sleep_time
, username
, password
)
91 usage
, last_bill_date
= data.request_bill_preview
93 if ignore_limits
or (usageLimit
>= 0 and x
.to_f
>= usageLimit
)
94 message
+= "Unbilled usage: $#{x}\n"
98 data_usage
= data.request_mobile_browsing
99 data_usage
.each
do |x
|
100 if ignore_limits
or (dataLimit
>= 0 and x
.to_f
>= dataLimit
)
101 message
+= "Data usage: #{x} MiB\n"
107 if message
.length
> 0
108 return destination
, <<EOT
111 Subject: Virgin Mobile Usage
116 Previous bill: #{last_bill_date}
119 https://www.virginmobile.com.au/selfcare/MyAccount/login.jsp
126 def send_emails(emails
)
127 Net
::SMTP.start(SMTPSERVER
, 25) do |smtp
|
128 emails
.each
do |destination
, message
|
129 smtp
.send_message(message
, FROMADDR
, destination
)
134 def do_email(sleep_time
, ignore_limits
)
136 ACCOUNTS
.each
do |args
|
137 if ret
= check_usage(sleep_time
, ignore_limits
, *args
)
144 def do_print(sleep_time
, ignore_limits
)
146 ACCOUNTS
.each
do |args
|
147 if ret
= check_usage(sleep_time
, ignore_limits
, *args
)
154 ignore_limits
= dry_run
= fast
= false
155 OptionParser
.new
do |opts
|
156 opts
.banner
= "Usage: #{$0} [options] config\n"
157 opts
.on("--fast", "Don't sleep between connections") do |v
|
160 opts
.on("--dry-run", "Don't send emails, just print them") do |v
|
163 opts
.on("--ignore-limits", "Treat all limits as 0") do |v
|
170 eval File
.open(config
).read
172 $stderr.print
"Error parsing config file!\n\n"
179 sleep_time
= SLEEPTIME
183 do_print(sleep_time
, ignore_limits
)
185 do_email(sleep_time
, ignore_limits
)