Facebook Album Downloader

» 08 September 2010 » In Perl, Programming »

Last July, I posted the Multiply Album Downloader. To be honest, I didn’t know that I posted that here. All I know is I posted that script on my old blog. I just discovered that few minutes ago. Anyway, I coded this script last week, because I need to download a lot of pictures from my friends’ Facebook albums.

I had a great time coding this, so now I want to share this with you. Just like the last downloader, I coded this in PERL:

#/usr/bin/perl
#
#    Copyright (C) 2010 Ruel Pagayon <ruel@ruel.me>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    Facebook Album Downloader

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Cookies;

my $ua = LWP::UserAgent->new(agent => 'Ruel - ruel@ruel.me - Facebook Album Downloader');
my $cookie_jar = HTTP::Cookies->new(
   file     => "fb-cookies.txt",
   autosave => 1,
);
$ua->cookie_jar( $cookie_jar );

#TODO: Change these (Your Facebook account details):
my $user = 'user@email.com';
my $pass = 'password';

print "Initializing Downloader..\n";
my $init = $ua->post('https://login.facebook.com/login.php?login_attempt=1',
{
    'charset_test'        =>    '%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84',
    'locale'            =>    'en_US',
    'email'                =>    $user,
    'pass'                =>    $pass,
    'charset_test'        =>    '%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84',
    'lsd'                =>    'lcUCH'
});

$cookie_jar->extract_cookies($init);

print "\nEnter the Album URL: ";
my $aurl = <STDIN>;
$aurl =~ s/^\s+//;
$aurl =~ s/\s+$//;
print "Enter the Owner: ";
my $own = <STDIN>;
$own =~ s/^\s+//;
$own =~ s/\s+$//;
mkdir($own);
my $ares = $ua->get($aurl);
my $acon = $ares->content;
my $i = 0;
my ($gp, $un);
while ($acon =~ m/(http:\/\/photos-.*?\.jpg)/g) {
    $i = $i + 1;
    print "Getting Photo " . $i . "..\n";
    $un = $1;
    $un =~ s/(photos\-\w\.ak\.)/sphotos.ak./g;
    $un =~ s/_\w\.jpg$/_n.jpg/g;
    $gp = $ua->get($un);
    open (SV, '>>' , $own . '\\' . $i . '_' . int(rand(1000)) . '.jpg');
    binmode (SV);
    print SV $gp->content;
    close (SV);
}

You can freely modify and redistribute this, as long as you notify me. It’s no problem, really. Thanks for reading. :)

Tags: , , ,

Trackback URL

  • Ramaglia Michele

    It’s asking me to enable cookies:
    ” Cookies are not enabled on your browser. Please adjust this in your security preferences before continuing. “

    • http://ruel.me Ruel

      Are you running the script through the command line? or through your browser?

  • Pingback: Downloading Facebook Albums with C#