This post continues from Part I - Login to Facebook
By now, we are able to login to our Facebook account, so it is time to do more things with it. Facebook lets you publish your status, a short text descsribing what you are doing any time. It has a 160-char limit, just like the Twitter thing.
To get or set the status, we only need the homepage html page. To retrieve our status we just match it against a pattern and to set the status we send a POST request with the appropriate arguments. Here is the code:
#go to the homepage
$response = $browser->get('http://www.facebook.com/home.php',@header);
#Let's now post a new status
my $newStatus = "Testing DOL Code..";
$response->content =~ /id="post_form_id" name="post_form_id" value="(.+?)"/;
my $postformid = $1;
my %post_data = ("status"=>$newStatus,"post_form_id"=>$postformid);
$response = $browser->post('http://www.facebook.com/updatestatus.php',\%post_data,@header);
##Ok! The status was posted...
##Let's now see what we have done...
$response = $browser->get('http://www.facebook.com/home.php',@header);
if($response->content =~ /<span id="chat_su_text">(.+?)<\/span>/)
{
#Here is the new status
print "My status: ",$1;
}
else
{
print "Could retrieve status...";
}
This piece of code should be used in conjuction with the previous post. It assumes we have already setup the brower and the response object and that we have already logged in to Facebook. The code first posts a new status ('Testing DOL code...') and then tests if everything was setup ok.
Wednesday, October 29, 2008
Sunday, October 26, 2008
Using Perl Against Facebook - Part I: Login
After a small intervention about the economic crisis, it is time we get back to some hacking. This time we shall use Perl against Facebook, to do anything that Fb will not let u
s do. In this post I will just show how to login to Facebook. In the next post, I will dump the code on how to update your status.
For the history, Fb has indeed an API for developers that wish to build their application on the Facebook platform. You can start here, but this is not the place for me. The API does give you some choice but it was built with a different view in mind: 3rd party developers accessing peoples' accounts. This is why the API is very restrictive and it makes sense not letting an application do much with your data. There is also a plethora of the so-called Facebook clients, which in the majority are just a wrapper for the Facebook API (like this google code project). In most cases also you get redirected to Facebook pages. Shame..
So long for the Facebook API, let's get down on how to login to Facebook. Of course
, language of choice is...what else, Perl (because simple things should be easy, and complex not impossible) In order to login to Facebook we have to follow these steps:
1. Go to 'http://www.facebook.com/login.php', and rest our virtual browser there to collect the cookies (GET request)
2. Visit 'https://login.facebook.com/login.php' with the proper parameters (POST request)
3. If we succeed then we can safely browse to 'http://www.facebook.com/home.php' to get our profile.
To do this we will need the LWP::UserAgent class (de facto in latest Perl distros) that will be our virtual browser. If we want to store the cookies we can use Http::Cookies. We will also need Crypt::SSLeay package so that our agent supports HTTPS. Ok. Let's do this.
Here is the complete code. Explanation follows.
my $email; #stores our mail
my $password; #stores our password
my $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6';
$email = <>; #read the login e-mail
$password=<>; #read the password
chomp($email); #remove last line
chomp($password);
my %postLoginData; #necessary post data for login
$postLoginData{'email')=$email;
$postLoginData{'pass'}=$password;
$postLoginData{'persistent'}=1;
$postLoginData{'login'}='Login';
our $response; #holds the response the HTTP requests
#set the headers, let's make this a Firefox browser!
our @header = ('Referer'=>'http://www.facebook.com', 'User-Agent'=>$user_agent);
our $cookie_jar = HTTP::Cookies->new(file=>'fbkCookies.dat',autosave=>1, ignore_discard=>1);
our $browser = LWP::UserAgent->new; #init browser
$browser->cookie_jar($cookie_jar);
$browser->get('http://www.facebook.com/login.php',@header);
#here we actually login!
$browser->post('https://login.facebook.com/login.php',\%postLoginData,@header);
#was login successful?
if($response->content =~ /Incorrect Email/)
{
print "Login Failed...Quitting..\n";
}
else {
print "..and we are in!";
#let's go to the homepage
$response = $browser->get('http://www.facebook.com/home.php',@header);
}
Upon execution of the script we either get a Login Failure error or a message of success. In the subsequent article we will move on to how to get and set the Facebook status. Stay around because this will get more interesting. The final Perl script has about 500 lines of code and can send messages, retrieve inbox and chat among others!
s do. In this post I will just show how to login to Facebook. In the next post, I will dump the code on how to update your status.For the history, Fb has indeed an API for developers that wish to build their application on the Facebook platform. You can start here, but this is not the place for me. The API does give you some choice but it was built with a different view in mind: 3rd party developers accessing peoples' accounts. This is why the API is very restrictive and it makes sense not letting an application do much with your data. There is also a plethora of the so-called Facebook clients, which in the majority are just a wrapper for the Facebook API (like this google code project). In most cases also you get redirected to Facebook pages. Shame..
So long for the Facebook API, let's get down on how to login to Facebook. Of course
, language of choice is...what else, Perl (because simple things should be easy, and complex not impossible) In order to login to Facebook we have to follow these steps:1. Go to 'http://www.facebook.com/login.php', and rest our virtual browser there to collect the cookies (GET request)
2. Visit 'https://login.facebook.com/login.php' with the proper parameters (POST request)
3. If we succeed then we can safely browse to 'http://www.facebook.com/home.php' to get our profile.
To do this we will need the LWP::UserAgent class (de facto in latest Perl distros) that will be our virtual browser. If we want to store the cookies we can use Http::Cookies. We will also need Crypt::SSLeay package so that our agent supports HTTPS. Ok. Let's do this.
Here is the complete code. Explanation follows.
my $email; #stores our mail
my $password; #stores our password
my $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6';
$email = <>; #read the login e-mail
$password=<>; #read the password
chomp($email); #remove last line
chomp($password);
my %postLoginData; #necessary post data for login
$postLoginData{'email')=$email;
$postLoginData{'pass'}=$password;
$postLoginData{'persistent'}=1;
$postLoginData{'login'}='Login';
our $response; #holds the response the HTTP requests
#set the headers, let's make this a Firefox browser!
our @header = ('Referer'=>'http://www.facebook.com', 'User-Agent'=>$user_agent);
our $cookie_jar = HTTP::Cookies->new(file=>'fbkCookies.dat',autosave=>1, ignore_discard=>1);
our $browser = LWP::UserAgent->new; #init browser
$browser->cookie_jar($cookie_jar);
$browser->get('http://www.facebook.com/login.php',@header);
#here we actually login!
$browser->post('https://login.facebook.com/login.php',\%postLoginData,@header);
#was login successful?
if($response->content =~ /Incorrect Email/)
{
print "Login Failed...Quitting..\n";
}
else {
print "..and we are in!";
#let's go to the homepage
$response = $browser->get('http://www.facebook.com/home.php',@header);
}
Upon execution of the script we either get a Login Failure error or a message of success. In the subsequent article we will move on to how to get and set the Facebook status. Stay around because this will get more interesting. The final Perl script has about 500 lines of code and can send messages, retrieve inbox and chat among others!
Tuesday, October 14, 2008
Nouriel Roubini: The Prophet Of The Global Economic Crisis
In a recent article, Mr Richard Posner, tries to understand the most disturbing with this global economic crisis: "How come we didn't see it coming?" It is still disturbing but since it happened, Mr Posner argues we should be better prepared next time, perhaps
with a CIA-type financial institution which will gather information from disperse places and shield markets against such kind of disasters.
However, Mr Posner is fair. Not all of us didn't see it coming. There were many warnings, the most notable coming from a teacher in the University of New York, Mr Nouriel Roubini (photo), who has been predicting for years and with 'uncanny accuracy' what has already happened: housing bubble burst, oil shock, consumer confidence decline and recession..And this is where our story begins.
Mr Roubini got his PhD from the Harvard University studying international markets and concentrated on recessions of emerging economies, which were typical in 90's. His methodology is distinguished for taking history and cultural evidence into account. Along the way, Mr Roubini defined the fundamental and common characteristics of these economies at the peak of the recession: huge deficits (spending more than production), borrowing, exposed banking system, and government corruption. These were the clues he was looking for in the next economy to suffer a crisis. And guess what: it was US' turn!
Still in 2006, Roubini predicted the abandonment of the dollar and its decline, the burst of the housing bubble, the meltdown of hedge funds and ...well all what we are seeing now. When the first bailouts for Federal Reserve were announced in Spring of this year, there was no doubt for Mr Roubini: crisis was upon and the worst were yet to come. The days of dismissive comments towards Roubini, like perma-bear (always predicting the worst), or pessimist have gone, and people are listening sharp of what he has to say.
So what really happened? Mr Roubini talks about the biggest asset bubble and credit bubble in the history of humanity, not only on the US but in many other countries as well, and not only in one sector (mortgage) but across many different sectors. So, in fact, we can talk about a housing bubble, a mortgage bubble, an equity bubble, a credit bubble,etc, all breaking at once!
What is Roubini's view of the bailout plans? He is really making a point I can really understand. Before pouring money into these companies, you first have to wipe out corrupt managers and shareholders. Just throw them out. Otherwise it is just a scandalous way to waste taxpayers' money buying for gold the very (toxic) trash that these guys invented.
This was as far as I could go. For your further information just follow the articles linked here. I am also embedding a highly interesting interview of Mr Roubini on Bloomberg TV, talking about the coming recession, shot on July 2008.
Roubini's blog - Roubini Global Economic Monitor: http://www.rgemonitor.com/blog/
Article in NY Times - Dr. Doom: http://www.nytimes.com/2008/08/17/magazine/17pessimist-t.html
with a CIA-type financial institution which will gather information from disperse places and shield markets against such kind of disasters.However, Mr Posner is fair. Not all of us didn't see it coming. There were many warnings, the most notable coming from a teacher in the University of New York, Mr Nouriel Roubini (photo), who has been predicting for years and with 'uncanny accuracy' what has already happened: housing bubble burst, oil shock, consumer confidence decline and recession..And this is where our story begins.
Mr Roubini got his PhD from the Harvard University studying international markets and concentrated on recessions of emerging economies, which were typical in 90's. His methodology is distinguished for taking history and cultural evidence into account. Along the way, Mr Roubini defined the fundamental and common characteristics of these economies at the peak of the recession: huge deficits (spending more than production), borrowing, exposed banking system, and government corruption. These were the clues he was looking for in the next economy to suffer a crisis. And guess what: it was US' turn!
Still in 2006, Roubini predicted the abandonment of the dollar and its decline, the burst of the housing bubble, the meltdown of hedge funds and ...well all what we are seeing now. When the first bailouts for Federal Reserve were announced in Spring of this year, there was no doubt for Mr Roubini: crisis was upon and the worst were yet to come. The days of dismissive comments towards Roubini, like perma-bear (always predicting the worst), or pessimist have gone, and people are listening sharp of what he has to say.
So what really happened? Mr Roubini talks about the biggest asset bubble and credit bubble in the history of humanity, not only on the US but in many other countries as well, and not only in one sector (mortgage) but across many different sectors. So, in fact, we can talk about a housing bubble, a mortgage bubble, an equity bubble, a credit bubble,etc, all breaking at once!
What is Roubini's view of the bailout plans? He is really making a point I can really understand. Before pouring money into these companies, you first have to wipe out corrupt managers and shareholders. Just throw them out. Otherwise it is just a scandalous way to waste taxpayers' money buying for gold the very (toxic) trash that these guys invented.
This was as far as I could go. For your further information just follow the articles linked here. I am also embedding a highly interesting interview of Mr Roubini on Bloomberg TV, talking about the coming recession, shot on July 2008.
Roubini's blog - Roubini Global Economic Monitor: http://www.rgemonitor.com/blog/
Article in NY Times - Dr. Doom: http://www.nytimes.com/2008/08/17/magazine/17pessimist-t.html
Subscribe to:
Posts (Atom)