#!/usr/bin/perl -w
#
# (C) 2002 Draco D. 
# email: draco_d@gmx.net
#
# script to download Trailers from www.apple.com/trailers
#
# pass the URL of the HTML File which contains the embedded QT Mov file as 
# argument to this skript.
# example:
#     getMov.pl http://www.apple.com/trailers/mgm/die_another_day/tr3_lg.html
#
#
# I wrote this script by analysing some sample-trailer-pages; if it doesn't
# work with one you're trying, you can send me the link, *maybe* I can and want to fix it...
#
# Or, even better, you fix it and send me a patch ;-)
#
# How this works:
# 1. the script uses 'wget' to download the HTML file
# 2. the HTML File is parsed for appearance of an <EMBED> tag. the first one is taken
# 3. the embedded filename in the HREF is extracted
# 4. if it ends in \d*.mov (some digits, then ".mov") insert the "m" and download. done!
# if it doesn't end with digits+.mov then use second strategy, described below, where it starts...
#
# NO WARRANTY! I can't guarantee that this script always works... It shouldn't delete some of your
# files either, since it doesn't use any command which may overwrite files. Even wget creates new ones
# if the target already exists!
#
# You need Perl, wget and strings installed for this script to work!
#
 

if ($#ARGV != 0) {
	print "Start with one argument, which is the URL to the HTML file containing the embedded mov\n";
	die;
}

# 1. get HTML File
open HTMLFILE, "wget -q -O - $ARGV[0] |" or die "can't read output from wget";
@html=<HTMLFILE>;

# 2. parse file, search for EMBED tag... Break out if found
$embed="";
foreach $line (@html) {
  if (($line=~/embed/) || ($line=~/EMBED/)) {
    $embed=$line;
    last;
  }
}
if ($embed eq "") {
  die "didn't find any EMBED";
}
# 3. Extract movie filename
$embed=~s/.*HREF//i;
$embed=~s/\.mov.*/.mov/;
$embed=~s/.*http/http/i;
if ($embed=~/\d*.mov/) {
  $filen=$embed;
  $filen=~s/(\d*).mov/m$1.mov/;
  chomp $filen;
  system "wget $filen";
  $fn=$filen;
  $fn=~s|.*/||g;
  $size= -s $fn;
  if ($size > "1000000") {
	print "Done!\n";
	exit 0;
  }
}

# this didn't work, so let's take the SECOND STRATEGY....
# a1. download the .mov file. this isn't the movie, but contains the URL of it...
# a2. use 'strings' to get filenames, the ones which end with .mov and don't have http:// 
# a3. there may be different sizes. this script only uses the 480 ones...
# a4. get the file which was referenced in the first mov
# a5. if it's smaller than 1MB then it's another file which contains URLs, so "goto a2"
#     the file is already downloaded...

# save url of directory (without filename, but with http://) for later use
$dir=$embed;
$dir=~s|(.*/).*|$1|g; #remove filename
chomp $dir;

# a1 get .mov file
$tmpfilename="tmp.mov";
system "wget -q -O $tmpfilename $embed";

$done="0";
while ($done < "10") { # < 10 to prevent infinite loop
	# a2. use strings to extract the filenames out of the .mov
	open MOVFILE1, "strings $tmpfilename |" or die "can't read from strings";
	@mov1=<MOVFILE1>;

	@movienames = ();
	foreach $something (@mov1) {
	  if (($something=~/.mov/) && (not($something=~/http/i))) {
	    @movienames = (@movienames, $something);
	  }
	}
	# this isn't very efficient, it could be done in one loop only.
	# but I wanted to write it so that it can be changed easily later, eg
	# if someone wants to download a smaller size *if* the 480 doesn't exist
	$movie="";
	#try to find the movie with 480 in its name. it's the large one...
	foreach $m (@movienames) {
	  if ($m=~/480/ || (@movienames == "1")) {
	    # if there's only 1 movie, take this!
	    $movie=$m;
	    last;
	  }
	}
	die "No link to Movie found " if ($movie eq "");
	
	$movie=~s/(^\W)|(^\d)//;
	$completename=$dir.$movie;
	
	print "Downloading $completename\n";
	# 7: download the file
	$fn=$completename;
	$fn=~s|.*/||g;
	chomp $fn;
	system "wget $completename";

	# 8. if it's too small it isn't the movie but yet another "link"...
	$size = -s $fn;
	$done="100" if ($size > "1000000");
	$done++; #to prevent infinite loop
	$tmpfilename=$fn;
}
if ($done < "100") {
	print "Downloading probably failed :( Sorry.\n";
} else {
	print "Done!\n";
}

