Skip to content

Instantly share code, notes, and snippets.

@nkmrgk
Created March 1, 2012 20:59
Show Gist options
  • Save nkmrgk/1953195 to your computer and use it in GitHub Desktop.
Save nkmrgk/1953195 to your computer and use it in GitHub Desktop.
Downloader for Google
#!/usr/bin/perl
#
# googleget.pl - Downloader for Google
# * Export Google Bookmarks
# $ googleget.pl "https://www.google.com/bookmarks/bookmarks.html?hl=ja"
# * Export Google Reader Subscription
# $ googleget.pl "http://www.google.com/reader/subscriptions/export?hl=en"
#
use strict;
use warnings;
use Encode;
use utf8;
use URI;
use HTTP::Cookies;
use WWW::Mechanize;
use File::Basename;
my $account = 'your-google-account';
my $password = 'your-google-password';
my $app = basename($0, '.pl');
my $cache = $app . '.cache_cookie';
my $cache_expire = 60*60*24*3;
my $http_timeout = 300;
my $login_max_retry = 3;
sub get {
my ($uri) = @_;
my $mech = WWW::Mechanize->new(timeout => $http_timeout);
my $cookie = HTTP::Cookies->new(file => $cache, autosave => 1);
$mech->agent_alias('Windows IE 6');
my $content;
for (my $i=1; $i<=$login_max_retry; $i++) {
if (-f $cache && time - scalar((stat($cache))[9]) < $cache_expire) {
$mech->cookie_jar($cookie);
}
else {
unlink $cache;
$mech->cookie_jar($cookie);
$mech->get('https://www.google.co.jp/accounts/Login');
$mech->form_number(1);
$mech->field(Email => $account);
$mech->field(Passwd => $password);
$mech->click();
}
warn "GET $uri\n";
my $res;
eval { $res = $mech->get($uri) };
die $@ if ($@);
$content = $res->decoded_content;
Encode::_utf8_off($content);
# Success
last;
}
return $content;
}
my $uri = shift @ARGV;
my $file = shift @ARGV || "$app.out";
die "usage: $app {URI} [output_file]\n" if !$uri;
my $content = get($uri);
open my $out, '>', $file or die;
print $out $content;
close $out;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment