[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] load random URLs



Someone asked recently (not here) if he could have AppleScript regularly
load random URLs into his browser.  I think he had some mechanism that paid
him to surf, and he wanted it to surf while he was away.  The best way is
to use an external process to feed random URLs, so you don't need to have
your own list.  AppleScript could do it, but the problems are twofold:

  * You would have to go to the same URL each time, and then get redirected
    to the random URL.  The company paying you to surf could see this
    behavior and be unhappy.

  * If an URL is unreachable, Netscape may put up a dialog box that needs
    to be dismissed before continuing.

Both problems are surmountable with AppleScript, but not much fun.  Perl is
easier.  :D

This script requires the Mac::InternetConfig that comes with cpan-mac,
which provides the GetURL function (which is the same as "use
Mac::InternetConfig qw(:DEFAULT $ICInstance);
ICGeneralFindConfigFile($ICInstance); ICLaunchURL($ICInstance, 0, $url)").

We avoid the problem of Netscape hanging by having MacPerl check the URL
first with head(), and using alarm() to timeout requests after 20 seconds.
This way we weed out bad URLs before sending them to Netscape.

#!perl -w
use strict;
use Mac::InternetConfig;
use LWP::Simple;
use LWP::UserAgent;

my $ua = new LWP::UserAgent;
my $req = new HTTP::Request GET => 'http://random.yahoo.com/bin/ryl';

print "\nStarting ...\n";

while (1) {
	my($url, $res);
	eval {
		local $SIG{ALRM} = sub { die "alarm\n" };
		alarm 20;   # give it 20 seconds to fail
		$res = $ua->simple_request($req);
		$url = $res->header('location') or die "bad url\n";
		GetURL($url) if head $url;
	};

	alarm 0;	   # reset alarm

	unless ($@) {  # silently ignore bad URLs and move on
		print $url, "\n";
		sleep 60;
	}
}

__END__

-- 
Chris Nandor       |     pudge@pobox.com      |     http://pudge.net/
Andover.Net        | chris.nandor@andover.net | http://slashcode.com/

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org