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

Re: [MacPerl] https request ...



On Tue, 7 Mar 2000 11:43:33 -0500, Chris Nandor <pudge@pobox.com> wrote:

>  At 17.16 +0100 2000.03.07, Carl Johan Berglund wrote:
>>At 13.12 -0500 0-03-03, schinder@pobox.com wrote:
>>>2) use an AppleEvent to ask a browser to do it for you.    How you get t=
he
>>>information out of the browser back to your script I don't know.

[snip]

>
>  The only real problem with this is you don't know for sure when the
>  downloading is done.  If you want to do user interaction, you can do
>  (instead of a sleep):
>
>    print "Hit <return> when download is complete:"; <>;
>
>  Netscape has a way to tell if a window is busy or whatever, but the dial=
og
>  used for saving URLs to files doesn't do that.

I know this is a bit late, but this is not quite true. It is=20
perfectly possible to have Netscape inform your=20
script/MacPerl/AppleScript that a download is completed.
The following script was written long before Chris' AE Glue was born:


use Mac::Files;
use Mac::AppleEvents;


#####PARAMETERS#####

$multConCount =3D 4; 
=09=09=09=09#max number of multiple connections

%filenames =3D ();
@urls =3D ();

#####SUBROUTINES#####

sub MOSSRegURLEcho {

=09my $be =3D 
AEBuildAppleEvent('WWW!','RGUE',typeApplSignature,'MOSS',0,0,"'----':McPL")=
=20
|| die "$!\n";
=09AESend($be, kAEWaitReply) or die $^E;
=09AEDisposeDesc $be;
}

sub MOSSUnregURLEcho {

=09my $be  =3D 
AEBuildAppleEvent('WWW!','UNRU',typeApplSignature,'MOSS',0,0,"'----':McPL")=
=20
|| die "$!\n";
=09AESend($be, kAEWaitReply) or die $^E;
=09AEDisposeDesc $be;
}

sub saveURLinFile($$){

=09my ($URL, $file) =3D @_;
=09$obj =3D "'----':"$URL", dest:obj{form:name, want:type(file), 
seld:"$file", from:'null'()}";
=09$be =3D 
AEBuildAppleEvent('GURL','GURL',typeApplSignature,'MOSS',0,0,$obj) ||=20
die "$^E\n";
=09AESend($be, kAEWaitReply) or die $^E;
=09AEDisposeDesc $be;
=09print "Called Netscape to save "$URL"\n     in file "$file"=8A\n";

}


sub URLEcallback {
=09my($event) =3D @_;
=09$desc =3D AEGetParamDesc($event, keyDirectObject());
=09$filepath =3D AEPrint($desc);
=09AEDisposeDesc($desc);
=09$filepath =3D~ s/^"(.*)"$/$1/;=09#get rid of curly quotes=8A
=09$filepath =3D~ s/\/?$//;=09#and possible trailing slashes
=09#an array where urls are keys, value 0 is 'first download 
window shown', value 1 is
=09#'second download window shown'
=09#Necessary since Netscape uses 2 windows for every download.
=09if ($filenames{$filepath} =3D=3D 1){
=09=09delete $filenames{$filepath};
=09=09--$fileCount;=09#One file less to download.
=09=09print "($fileCount) "$filepath" downloaded.\n";
=09} else {
=09=09$filenames{$filepath} =3D 1;
=09}

=090;
}


#####MAIN STARTS HERE#####

MacPerl::Quit(0);
while (<DATA>){
=09next if /^\n$/;
=09chomp;
=09push(@urls, (split(/\s+/, $_)));
}


#####CREATE DOWNLOAD FOLDER#####

@thistime =3D localtime(time);
$thistime[5] =3D~ s/\d*?(\d\d)$/\1/m;
$date =3D "$thistime[3]/" . ++$thistime[4] . "/$thistime[5]";
$rootFolder =3D "Download HD:Downloads=9F:htmls $date:";
while (!-d $rootFolder) {FSpDirCreate $rootFolder ||
=09die "Can't create rootFolder hierarchy: $!\n"};


#####DOWNLOAD######=09=09#ASSUME THAT Netscape is up and running!

MOSSRegURLEcho;=09=09=09#Get events from Netscape after 
completed downloads.

$AppleEvent{'WWW?', 'URLE'} =3D \&URLEcallback;=09#Install URLEhandler.


$fileCount =3D (scalar @urls) / 2;=09#Define a counter for the 
number of links to download.

while ($fileCount){=09=09=09#Download the urls. Details 
handled by URLEcallback.

=09if ((scalar keys %filenames < $multConCount) && @urls){
=09=09$thisURL =3D shift @urls;
=09=09$thisfile =3D $rootFolder . shift @urls;
=09=09saveURLinFile($thisURL, $thisfile);
=09=09$filenames{$thisURL} =3D 0;
=09} else {

=09sleep(1);

=09}
}

MOSSUnregURLEcho;=09#No longer get events from Netscape.

MacPerl::Reply($rootFolder);=09#Return container of downloaded stuff 
for further processing.



__DATA__
http://www.versiontracker.com/=09versiontracker.html
http://www.macfixit.com macfixit.html
http://www.macnn.com macnn.html
http://www.macintouch.com macintouch.html
--=20
__Peter Hartmann ________

mailto:hphartmann@arcormail.de

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