Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scripts for using Selenium to check your Phone Power international minutes automatically
#!/bin/bash -e
# Cron wrapper script for phonepower-usage.pl.
#
# Copyright (c) 2014 Jonathan Kamens <jik@kamens.us>. You can do
# whatever you want with this script, as long as you give me credit
# and don't make any money from it.
#
# The home page of this script is http://blog.kamens.us/2014/04/20/using-selenium-to-monitor-phone-power-international-call-usage/.
#
# There's a donate link there if you want to show your
# appreciation. :-) Or you can post comments or questions there or
# just email them to me.
#
# $Id: phonepower-usage-cron.sh,v 1.3 2014/04/20 20:08:40 jik Exp $
# Set to "false" to use current X server (i.e., to see Selenium doing
# its stuff on your screen) rather than a virtual server (which you'll
# need when you're running this script out of cron).
XVFB=true
# Modify if you want to use a different port number, e.g., because you
# need to run another Selenium instance on port 4444 and you don't
# want this one to conflict with it or vice versa.
PORT=4444
# Modify if you have something else running on X display number 99.
DISPLAY=99
if [ $(nmap -p T:$PORT localhost | grep -cw open) != 0 ]; then
echo "ERROR: Something is already listening on port $PORT" 1>&2
exit 1
fi
if $XVFB; then
((dp = 6000 + DISPLAY))
if [ $(nmap -p T:$dp localhost | grep -cw open) != 0 ]; then
echo "ERROR: Something is already listening on port $dp" 1>&2
exit 1
fi
Xvfb :$DISPLAY >/tmp/$$.X.log 2>&1 &
X_PID=$!
export DISPLAY=:$DISPLAY
fi
# Note: The perl script that this script calls does not work with
# version 2.28 or earlier of Selenium. It does work with version 2.41
# or newer. I don't know when exactly between 2.28 and 2.41 it started
# working, so your best bet is using 2.41 or newer.
java -jar ~/random/selenium-server-standalone-2.41.0.jar \
-port $PORT >/tmp/$$.selenium.log 2>&1 &
S_PID=$!
tries=0
while [ $(nmap -p T:$PORT localhost | grep -cw open) != 1 ]; do
((tries += 1))
if [ $tries = 10 ]; then
echo "ERROR: Selenium took too long to start up" 1>&2
kill $X_PID $S_PID || :
exit 1
fi
sleep 1
done
if $XVFB; then
tries=0
while [ $(nmap -p T:$dp localhost | grep -cw open) != 1 ]; do
((tries += 1))
if [ $tries = 10 ]; then
echo "ERROR: X took too long to start up" 1>&2
kill $X_PID $S_PID || :
exit 1
fi
sleep 1
done
fi
# Omit --debug to only generate output if there are warnings.
~/scripts/phonepower-usage.pl --port $PORT
kill $X_PID $S_PID
rm -f /tmp/$$.*
#!/usr/bin/perl
# Use Selenium to fetch your Phone Power international free minutes
# usage.
#
# Copyright (c) 2014 Jonathan Kamens <jik@kamens.us>. You can do
# whatever you want with this script, as long as you give me credit
# and don't make any money from it.
#
# Prints a message if you have used more than 75% of your free
# international minutes for the current month. Otherwise exits without
# printing anything.
#
# The home page of this script is http://blog.kamens.us/2014/04/20/using-selenium-to-monitor-phone-power-international-call-usage/.
#
# There's a donate link there if you want to show your
# appreciation. :-) Or you can post comments or questions there or
# just email them to me.
#
# $Id: phonepower-usage.pl,v 1.3 2014/04/20 19:28:56 jik Exp $
use strict;
use warnings;
use Date::Parse;
use Getopt::Long;
use WWW::Selenium;
use vars qw($pp_username $pp_password);
my $debug = undef;
my $port = 4444;
die "Bad usage" if (! GetOptions("username=s" => \$pp_username,
"password=s" => \$pp_password,
"port=i" => \$port,
"debug" => \$debug));
do "$ENV{HOME}/closed/pp-config.pl";
my $sel = WWW::Selenium->new(browser => '*firefox',
browser_url => 'https://seecure.phonepower.com',
port => $port) or die;
$sel->start or die;
$sel->open("https://secure.phonepower.com/Account/Login.aspx") or die;
$sel->wait_for_page_to_load(30000) or die;
$sel->type("identifier=ctl00_Account_ContentPlaceHolder_MyAccountLogin_UserName", $pp_username) or die;
$sel->type("identifier=ctl00_Account_ContentPlaceHolder_MyAccountLogin_Password", $pp_password) or die;
$sel->click("identifier=ctl00_Account_ContentPlaceHolder_MyAccountLogin_LoginButton") or die;
$sel->wait_for_page_to_load(30000) or die;
$sel->open("https://secure.phonepower.com/account/calllog.aspx") or die;
$sel->wait_for_page_to_load(30000) or die;
$sel->click("identifier=ctl00_Account_ContentPlaceHolder_btnGetFreeIntUsage") or die;
$sel->wait_for_text_present('minutes for the current bill cycle', 30000) or die;
my $used_text = $sel->get_text("identifier=ctl00_Account_ContentPlaceHolder_pnlDisplayFreeIntUsage");
if ($used_text !~ m,(\d+)\s*/\s*(\d+)\s+minutes for the current bill cycle,) {
die "Usage text doesn't match expected pattern:\n$used_text";
}
my($minutes_used, $free_minutes) = ($1, $2);
my $pct = int($minutes_used / $free_minutes * 100);
my $msg = "$pct% ($minutes_used / $free_minutes) international minutes used";
if ($pct > 75) {
print STDERR "Warning: $msg\n";
}
elsif ($debug) {
print "$msg\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.