Jul 12, 2009

Uploading photos to Facebook groups with Perl

here we go again, YAFS (yet another facebook script)

a little different this time since it's in Perl, which is new to me. This script takes a list of file names as arguments, and uploads them to a specified group on facebook. It uses WWW::Mechanize (which is awesome) to do the heavy lifting, and also depends on imagemagick (to resize images)

do note however that this was NOT written to be platform independent so windows users might encounter some problemns. However it should be fixable with a little modification.

USAGE

you'll need to edit the lines for your user-name and password.

perl gup.pl GID [file1] [file2] ...
where GID is the concerned groupID. You need to be a member of the group, and should have upload rights to its photo album.

#!/usr/bin/perl
# -----------------------------------------------
# Script to upload photos to a Facebook group.
# 2009 - kudanai, http://kudanai.blogspot.com
#
# This script can act as a nautilus extension.
# (preset GID and set $i=0 on line 59)
#
# 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.
# -----------------------------------------------

use strict;
use WWW::Mechanize;
use HTTP::Cookies;

#edit your username, password and group_id here
#these values MUST BE CORRECT for the script
#to work properly
my $username = 'username@example.com';
my $password = 'password';
my $gid = $ARGV[0];

#-----no need to edit beyond this point-----

#url for login page + redirect instruction
my $url = "https://login.facebook.com/login.php?" .
"&next=http%3A%2F%2Fwww.facebook.com%2Feditalbum.php%3Foid%3D".
$gid."%26add%3D1%26htmlup%3D1";

#initialize objects
my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());

#notify
#system("notify-send -i fbnotif UPLOADING \"Logging in\"");

$mech->get($url);
$mech->submit_form(
form_number => 1,
fields =>{
email=>$username,
pass=>$password
},
button=>'login'
);

#notify - if you have libnotify installed, uncomment
#these notify commands to get progress notifications
#system("notify-send -i fbnotif UPLOADING \"Uploading photos\"");


#loop through args and post the files
#five at a time.

my $i=1;
while(defined $ARGV[$i])
{

$mech->form_name('upload');
for(my $j=1;$j<6;$j++,$i++)
{
if(defined $ARGV[$i])
{
system("convert -resize 604 \"".$ARGV[$i]."\" /tmp/".$j.".fgup.jpg");
$mech->field("file$j"=>"/tmp/$j.fgup.jpg");
}   
}
$mech->tick('agree','1');
$mech->submit();
if(defined $ARGV[$i]) {
$mech->back()
}
}

#remove temporary images
system( "find /tmp -name '*.fgup.jpg' -exec rm {} \\;>/dev/null" );

#notify
#system("notify-send -i fbnotif UPLOADING \"Finished Uploads\"");

14 Comments:

ari_goodman said...

this is helpful. Thanks

Anonymous said...

what kind of "modifications" are needed to make this work in windows?

meekaaku said...

Its good. but its more of a C program written in perl. You are not using the real power of perl. you can convert all the files and grab a list in two lines:

foreach (@ARGV) {system ("convert $_ $_.fgup.jpg");}
@upfiles = <*.fgup.jpg>;

now @upfile contains the name list of files that have been converted and ready to upload. Now you can loop and upload in sets of 5.

to remove temp storage no need for find command. just do system ("rm *.fgup.jpg");

to run no need to give perl command.
just script name will do (since the shebang line is given) if you have execute permission.

SoE said...

thanks for the suggestions! I'll put that in with the next update. I'm new to Perl so I'm still discovering its abilities..

and I suppose you're right about it being a C program written in Perl, I was brought up on C :)

meekaaku said...

A perlish way of doing it recursively...


foreach (@ARGV) {system ("cp $_ $_.fgup.jpg");}
@upfiles = <*.fgup.jpg>;
upload (@upfiles);
system ("rm *.fgup.jpg");

sub upload () {
return if ($#_ < 0);
my $numfiles = ($#_ < 4 ? $#_ : 4);
foreach $i (1..$numfiles+1) {print "mech->field$i => " . shift . "\n";}
print "mech->submit\n";
upload (@_);
}

SoE said...

I'm a bit lost there since I'm not familiar with Perl's lexicon. could you walk me through it a little?

meekaaku said...

line 1:
Perls foreach loops thru an array doing something over the whole list. Perl has special variables such as $_, @_ and others which makes things a bit cryptic, but very useful. $_ refers to the current argument being processed (in this case the foreach loop). Hence 'convert $_ $_.fgup.jpg' is equivalent to 'convert 1.jpg 1.fgup.jpg'.

sub upload():
It is called with @upfiles as arguments. Within the sub @_ contains the arguments passed. $#_ is number of items in @_ (starting with 0). In the sub first check if a null list was passed (otherwise it will recurse forever). the line
$numfiles = ($#_ < 4 ? $#_ : 4)
sets $numfiles to 4 (0-4 is five items), or $#_ whichever is less. This syntax is similar to C.

the line:
foreach $i (1..$numfiles+1) {print "mech->field$i => " . shift . "\n";}
is a bit tricky.

foreach $i (1..10) {} is same as for ($i=1; $i<=10; $++){}
The 'shift' is an array operator like pop and push. it grabs the item at the top of the array and REDUCES the number of items in the array.

if @list = {'1', '2', '3'}, then shift(@list) pulls out '1' leaving @list with '2' and '3' only.

now giving just 'shift' without an argument makes it operate on @_ (the argument list passed to the sub).
So basically its shift(@_). Now that @_ is reduced by 5 items, upload is called recursively with the remaining items in @_ passed.

Hope that clears. Refer to perl special variables and array operators.

SoE said...

awesome help dude really appreciate it. Are you using this btw?

meekaaku said...

sorry, not using it. not much into fb.

Anonymous said...

yesss!!! finally

Stan G said...

great stuff dude! this will save me hours of tedious sorting

Anonymous said...

still works...

high school cheerleader true sex stories said...

Ziggy took the packed cake and walked down the aislewith Stephanie lagging behind him in deep thought. The echo of Gingers heels marked her departure.
horrible rape stories
erotic mind control stories
porn stories com
schoolgirl spanking stories
gay daddy sex stories
Ziggy took the packed cake and walked down the aislewith Stephanie lagging behind him in deep thought. The echo of Gingers heels marked her departure.

stories incest said...

They were all in close by the time John withdrew. Mywife complains and screams and I go pick up the phone and start dialinga number.
free male beastiality stories
true sex stories
true sex stories teen
interracial lesbian love stories
free stories teen incest
They were all in close by the time John withdrew. Mywife complains and screams and I go pick up the phone and start dialinga number.

Post a Comment