Google Answers Logo
View Question
 
Q: Logging in to Yahoo using Perl and LWP ( Answered,   4 Comments )
Question  
Subject: Logging in to Yahoo using Perl and LWP
Category: Computers
Asked by: aroth816-ga
List Price: $10.00
Posted: 21 Jun 2002 15:58 PDT
Expires: 21 Jul 2002 15:58 PDT
Question ID: 31379
Logging onto Yahoo using Perl and LWP::UserAgent / LWP is diffucult
because Yahoo has a low of forwards/weird things/etc that occur during
the sign in process. I once had code to do this, but it no longer
works. If anyone can provide me with the code (again, using Perl and
LWP) I will pay generously (if your quick, ill pay above the initial
$10).

Thanks,
Adam Roth
Answer  
Subject: Re: Logging in to Yahoo using Perl and LWP
Answered By: runix-ga on 21 Jun 2002 19:35 PDT
 
Hello adam

I've coded an example script that logs into yahoo mail and gets the
number of new mails on the folders.
It can be easily extended, because the hard part (the login, with the
md5 tricks) is inside a function.

Here's the script:

--------------------
#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request::Common;
use MD5;
use strict;
our	$ua = new LWP::UserAgent;
$ua->cookie_jar(HTTP::Cookies->new);
$ua->agent("AgentName/0.1 " . $ua->agent);

my $mu="http://mail.yahoo.com"; 
my $user="USERNAME";
my $pass="PASSWORD";

if (my $mainPage=login($mu,$user,$pass)){
	print "login ok!\n";
	getFolders($mainPage);
}else{
	print "finished!\n";
}
sub login($$$){
	my ($mu,$user,$pass)=@_;
	my $req=new HTTP::Request GET => $mu;
	my $res=$ua->request($req);

	#some yahoo login tricks :)
	my ($challenge)=$res->content=~ /<input type=hidden
name=\"\.challenge\" value=\"([^\"]*?)\" >/;
	my $hexpass=MD5->hexhash($pass);
	my $passwd=MD5->hexhash($hexpass.$challenge);
	my $loginURL="http://login.yahoo.com/config/login?passwd=$passwd&user=$user&login=$user&.js=1&.challenge=$challenge&.hash=1&.src=ym";
	my $to=$loginURL;
    $res=$ua->simple_request(GET $loginURL); # 
	if ($res->content=~ /Invalid Password/im){print "Passwd
incorrect!\n"; return 0;}
	if ($res->content=~ /This Yahoo! ID does not exist/im){print "Login
incorrect!\n"; return 0;}
	while ($res->header('location')){
		$to=$res->header('location');
		$res=$ua->simple_request(GET $to);
	}
	return $to;
}

sub getFolders(){
		my $mainPage=$_[0];
		my $res=$ua->simple_request(GET $mainPage);
		my $text=$res->content;
		(my @mb)=$text=~/><a
href=\"\/ym\/ShowFolder\?YY=[0-9]*&BOX=.*?&yn=1\">(.*?)&nbsp;\(([0-9]+)\)<\/a>/igm;
		my $i=0;
		for($i=0; $i<$#mb; $i+=2){
			print "New mail:  ".($mb[$i])." (".$mb[$i+1].")!!!\n";
		}
}

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

Hope it works!


Additional links:

Here's an explanation of how to write web robots
https://answers.google.com/answers/main?cmd=threadview&id=28442


Search Strategy

Personal experience
Comments  
Subject: Re: Logging in to Yahoo using Perl and LWP
From: mohsen-ga on 21 Jun 2002 17:44 PDT
 
Hello Adam,

If you are going to download your emails from yahoo using a perl
script,
the good news for you is that they have established a project on
freshmeat, called fetchyahoo, dealing exactly with the same matter.
The script is fully
configurable, can use secure connections as well as proxy:

http://freshmeat.net/projects/fetchyahoo/?topic_id=861%2C32

However, if your need is something diffrent you can see the script to
find out how they have logged into yahoo using LWP. should you require
more information, feel free to ask.

Google search term: yahoo login perl update script

Hope it was helpful,
mohsen,
Subject: Re: Logging in to Yahoo using Perl and LWP
From: lighthousej-ga on 21 Jun 2002 20:56 PDT
 
I think on line 7:
    our $ua = new LWP::UserAgent;

is incorrect, you should use a 'my' instead of 'our' because 'my'
properly declares the local variable.

If it's not correct, I'd like to know what perl interpreter you use
because I have 5.005_03 for linux.  Other than that, nice tight code.
Subject: Re: Logging in to Yahoo using Perl and LWP
From: runix-ga on 21 Jun 2002 23:53 PDT
 
yeah, yeah :) but this version is the public version of this script:
before , I used global variables and ugly things like that :)
Subject: Re: Logging in to Yahoo using Perl and LWP
From: pne-ga on 24 Jun 2002 04:19 PDT
 
"our" is new with Perl 5.6.0.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy