Uncategorized

Script to retrieve voicemail from Vonage

A while back, I wrote a perl script to retrieve my voicemail from Vonage for archival purposes. It connects to their web interface and downloads the voicemail as wav files. I had it run every 4 hours from cron so it would grab everything from my Inbox and Trash (since items sit in the trash for a few hours before being permanently deleted. If I had more time to mess with it, I would have had it convert the wav files to mp3s and store them that way.
I’m sure it’s slightly bloated and can be cleaned up, but it works for me.
I must give most of the credit for this (and almost all of my scraping scripts) to the O’Reilly book, Spidering Hacks.
I bought this book about a year and a half ago and it is worth its weight in gold.

Here you go, use it at your own risk…

#!/usr/bin/perl
use strict;
use WWW::Mechanize;
use File::Basename;
use HTTP::Response;
use HTTP::Headers;
use HTML::TokeParser;

my $UserName = 'vonageusername';
my $Password = 'vonagepassword';
my $AcctDesc = 'vonage';
my $link;
my $fields;
my $url;
my $filename;
my $response;
my $headers;
my $mech = WWW::Mechanize->new();

print "Logging in...\n";
$mech->agent_alias( 'Linux Mozilla' );
$mech->get( 'https://secure.vonage.com/vonage-web/public/login.htm' );
#print $mech->content;
$fields = {
'username' => $UserName,
'password' => $Password,
'goToSelection' => 'voicemail',
};
$mech->submit_form(
form_name => 'logonForm',
fields => $fields,
);
print "Going to Voicemail page...\n";
#Find the link for "Voicemail"
$mech->follow_link( text_regex => qr/Voicemail/ );
$mech->success or die "post failed: ", $mech->response->status_line;

print "Checking Inbox...\n";

#for future cleanup, the voicemail table is the 10th table on the page
#$stream->get_tag("table");
my @links = $mech->find_all_links( text_regex => qr/Listen/ );
for $link ( @links ) {
$url = $link->url_abs;
print "retrieving " . $url . "\n";
$mech->get( $url );
$response = $mech->response();
$filename = $response->header( 'content-disposition' );
$filename =~ s/attachment;filename=//;
print $filename . " saved\n";
open(outfile, ">$filename");
print outfile $mech->content;
close(outfile);
$mech->back();
}

print "Checking Trash...\n";
$fields = {
'folder' => 'Deleted Items',
};
$mech->submit_form(
form_name => 'ListMessages',
fields => $fields
);

my @links = $mech->find_all_links( text_regex => qr/Listen/ );
for $link ( @links ) {
$url = $link->url_abs;
print "retrieving " . $url . "\n";
$mech->get( $url );
$response = $mech->response();
$filename = $response->header( 'content-disposition' );
$filename =~ s/attachment;filename=//;
print $filename . " saved\n";
open(outfile, ">$filename");
print outfile $mech->content;
close(outfile);
$mech->back();
}