Skip to content

Instantly share code, notes, and snippets.

@natefanaro
Created November 24, 2014 04:11
Show Gist options
  • Save natefanaro/340eb3ee53e041f38c98 to your computer and use it in GitHub Desktop.
Save natefanaro/340eb3ee53e041f38c98 to your computer and use it in GitHub Desktop.
Downloads voicemails from google voice.
#!/usr/bin/perl
# Downloads voicemails from google voice.
# This is old code, and I'm not 100% sure it still works, but I wanted to save this.
# natefanaro@gmail.com
use strict;
use WWW::Mechanize;
use DBI;
use HTTP::Cookies;
use JSON -support_by_pp;
use Data::Dumper;
# Login information for Google Voice
use constant USERNAME => 'username@gmail.com';
use constant PASSWORD => '';
use constant FILE_PATH => '/var/tmp/voicemail/';
use constant MYSQL_HOST => 'localhost';
use constant MYSQL_USER => 'root';
use constant MYSQL_PASS => '';
use constant MYSQL_DB => 'test';
use constant MYSQL_TABLE => 'voicemail';
# Won't insert to mysql or save to file
use constant TEST => 0;
my $dbh;
if (MYSQL_DB) {
$dbh = DBI->connect('DBI:mysql:'.MYSQL_DB.';host='.MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
if (!$dbh) {
die('can not connect to db');
}
}
my $url = 'https://www.google.com/accounts/ServiceLogin?hl=en&service=grandcentral&nui=1&continue=https://www.google.com%2Fvoice%2Faccount%2Fsignin%2F%3Fprev%3D%252F';
my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->form_number(1);
$mech->field(Email => USERNAME);
$mech->field(Passwd => PASSWORD);
$mech->click();
my $i = 0;
my $page = 1;
my $json_text;
$json_text->{'totalSize'} = 1;
my $sql = q[
REPLACE INTO ].MYSQL_TABLE.q[
SET id = ?,
date_time = from_unixtime(?),
note = ?,
phone_number = ?,
duration = ?,
message_text = ?
];
while ($i < $json_text->{'totalSize'}) {
#print "Page: $page\n";
#Go to the next link, now that we are logged in.
$url = 'https://www.google.com/voice/inbox/recent/voicemail/?page=p'.$page;
$mech->get($url);
my $output_page = $mech->content();
$output_page =~ s/\<\!\[CDATA\[//;
$output_page =~ /\<json\>(.*?)\]\]\>\<\/json\>/;
my $json_content = $1;
#print $json_content;
my $json = JSON->new();
$json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($json_content);
foreach my $message_id (keys %{$json_text->{'messages'}}) {
$i++;
my %msg = %{$json_text->{'messages'}->{$message_id}};
$msg{'phoneNumber'} = undef if ($msg{'phoneNumber'} =~ /unknown/i);
$msg{'phoneNumber'} =~ s/[^\d]//g;
$msg{'startTime'} =~ s/000$//;
print "$i - $message_id $msg{'displayStartDateTime'} $msg{'phoneNumber'} $msg{'note'}\n";
#print Dumper(\%msg);
if (MYSQL_DB && !TEST) {
my $sth = $dbh->prepare($sql);
$sth->execute($message_id, $msg{'startTime'}, $msg{'note'}, $msg{'phoneNumber'}, $msg{'duration'}, $msg{'messageText'}) or die($dbh->errstr);
}
my $filename = FILE_PATH.'/'.$message_id.'.mp3';
if (!-e $filename) {
print 'Downloading voicemail'."\n";
$mech->get("https://www.google.com/voice/media/send_voicemail/$message_id");
if ( !$mech->success ) {
print "Can't download mp3\n";
next;
}
if (!TEST) {
open(FH, '>', $filename);
print FH $mech->content();
close(FH);
}
}
}
$page++;
}
# CREATE TABLE `voicemail` (
# `id` varchar(50) NOT NULL,
# `date_time` datetime NOT NULL,
# `note` varchar(500) DEFAULT NULL,
# `phone_number` bigint(15) DEFAULT NULL,
# `duration` decimal(7,3) DEFAULT NULL,
# `message_text` longtext,
# PRIMARY KEY (`id`)
# ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment