Technology Feed Lab

technology site

Archive for the ‘Perl’ Category

Facebook login using Perl and Mechanize

Posted by TechGeek on June 28, 2011

This article will explain how to login into Facebook using Perl language? We will be using WWW::Mechanize again for this. You can use mechanize library for automating interaction with websites. The mechanize supports lot of different methods to handle text, HTML or images. It also automatically stores cookies, follow redirections etc. You can even parse links, follow links or submit web based forms easily.

Accessing the Facebook homepage requires you to be a valid user and must be logged in to do further activity. Simply fetching a webpage will not work as it will redirect you to a login page since you are not authenticated to Facebook. So you need to store credential cookies and will have to send that cookies on each requests. For this, we will use another Perl module called HTTP::Cookies. The method will be,

$cookie_jar = HTTP::Cookies->new

The above method will save our cookies. Here is the complete code for Facebook login,
-----------------------------------------
#!/usr/bin/perl -w

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

my $url = "http://www.facebook.com";
# credentials
my $username = 'Enter User Name';
my $password = 'Enter Password';

my $mech_request;

sub login
{
$mech_request = WWW::Mechanize->new(autocheck => 0);
$mech_request->cookie_jar(HTTP::Cookies->new());
my $res = $mech_request->get($url);
$mech_request->get($url);

$mech_request->form_name('menubar_login');
$mech_request->field(email => $username);
$mech_request->field(pass => $password);
$mech_request->click();

my $contents = $mech_request->content();
return $contents;
}

#access Facebook URL's after Login
sub fb_pages()
{
my $url = shift;
my $res = $mech_request->get($url);
$mech_request->get($url);
my $data = $mech_request->content();
print $data;
}

# Start - Login to Facebook
my $data = login();
print $data;

# Now you have logged in and saved a cookie
# access anything

#my $page_url = "http://www.facebook.com/games?ap=1";
# fb_pages($page_url);

-----------------------------------------

 

Posted in Best, Perl, Technology, Top Tips | Tagged: , , , | Leave a Comment »

How to find mime type of a file using Perl

Posted by TechGeek on June 22, 2011

A nice Perl module called “File::MimeInfo” can be used to find mime type of a file. This module uses shared MIME Database and can be useful in many cases. You need to just call a method “mimetype($file)” to find the mime type of static file. Before that make sure you have installed “File::MimeInfo” Perl module.

If you don’t know how to install then follow the simple steps,

1)      Download File::MimeInfo module from the search.cpan.org.

2)      Use command: tar -xzvf File-MimeInfo-0.15.tar.gz

3)      Use command: cd File-MimeInfo-0.15

4)      Use command: perl Makefile.PL

5)      Use command: make

6)      Use command: make test

7)      Use command: make install

This way you can install the module manually and use it. You can also use CPAN command like “install File::MimeInfo” to automatically install this module. Here is the sample Perl code for doing this,

----------------------------------------------
#!/usr/bin/perl

use strict;
#Perl module
use File::MimeInfo;

sub find_mimeType
{
my $filename = shift;

my $mime_type = mimetype($filename);

print “Mime-Type = $mime_typen”;
}

# Command line arguments
my $argc = scalar @ARGV;
my $FILE = ${ARGV[0]};

if($argc{
print “n”;
die “tUSAGE: mime_type.pl n”;
}

# find mime type
find_mimeType($FILE);

———————————————-

Posted in How-To, Perl, Technology | Tagged: , | Leave a Comment »

How to fetch password protected URL using Perl and Mechanize

Posted by TechGeek on June 20, 2011

Sometimes, we require fetching or grabbing the webpage which is password protected. One of the best modules is WWW::Mechanize. You can use mechanize library for automating interaction with websites. The mechanize supports lot of different methods to handle text, HTML or images. It also automatically stores cookies, follow redirections etc. You can even parse links, follow links or submit web based forms easily.

The mechanize support different methods for credentials based webpage’s. Here are the methods,

$mech->credentials($username, $password) = Provide credentials to be used for HTTP Basic authentication for all sites and realms until further notice.

$mech->get_basic_credentials( $realm, $uri, $isproxy ) = Returns the credentials for the realm and URI.

$mech->clear_credentials() = Remove any credentials set up with credentials().

The code is very simple to write with the help of Perl. Here is the simple code to grab or fetch any webpage which is password protected,

---------------------------------------------------------------------------
#!/usr/bin/perl -w

use strict;
use WWW::Mechanize;

sub grab_page
{
# using shift to accept paramter passed to method
my $urlString = shift;

#username and password
my $username = "enter username";
my $password = "enter password";

my $mech = WWW::Mechanize->new(autocheck => 0);

$mech->agent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1');

$mech->credentials($username, $password);

my $response = $mech->get($urlString);

my $page_contents = $mech->content();
print $page_contents;
}

# URL to grab
my $url = "password protected website";

# call to method
grab_page($url);

---------------------------------------------------------------------------

Just replace URL parameter in the above code to fetch any webpage. Once you fetch the contents, you can print it to the screen, store it into a variable or file for later parsing of the contents. The above code shows a simple way to fetch the contents which are password protected.

Posted in Perl, Technology | Tagged: , | Leave a Comment »

How to fetch URL using Perl and Mechanize

Posted by TechGeek on June 20, 2011

Many times, we require fetching or grabbing the webpage from Perl for doing different stuff. Perl is an easy language for most of the automation process. With few lines of simple code, you can able to do anything related to data on the web. Once fetched you can then able to parse or play with the data. There are different Perl modules for fetching the web data.  You can search different Perl modules on http://search.cpan.org.

One of the best modules is WWW::Mechanize. You can use mechanize library for automating interaction with websites. The mechanize supports lot of different methods to handle text, HTML or images. It also automatically stores cookies, follow redirections etc. You can even parse links, follow links or submit web based forms easily.

The code is very simple to write with the help of Perl. Here is the simple code to grab or fetch any webpage,

---------------------------------------------------------------------------
#!/usr/bin/perl -w

use strict;
use WWW::Mechanize;

sub grab_page
{
# using shift to accept paramter passed to method
my $urlString = shift;

my $mech = WWW::Mechanize->new(autocheck => 0);

$mech->agent(‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1′);

my $response = $mech->get($urlString);

if($response->is_success())
{
my $page_contents = $mech->content();
print $page_contents;
}
else
{
print “Check failed Responsen”;
}
}

# URL to grab
my $url = “http://www.google.com”;

# call to method
grab_page($url);

—————————————————————————

Copy paste above code and save it as “grabPage.pl”. Just replace URL parameter in the above code to fetch any webpage. Once you fetch the contents, you can print it to the screen, store it into a variable or file for later parsing of the contents. The above code shows a simple way to fetch the contents. Using mechanize, you can even able to fetch the pages which requires authentication or password protected websites. We will post another entry for fetching password protected webpage’s.

pixel How to fetch URL using Perl and Mechanize

Posted in Perl, Technology | Tagged: , | Leave a Comment »