#!/usr/bin/perl
# Filename:	diFilm
# Author:	David Ljung Madison <DaveSource.com>
# See License:	http://MarginalHacks.com/License/
# Description:	Download iFilm movies given a film ID.
  my $VERSION=  '1.00';
#
# Requires that ifilm's obfuscation doesn't change drastically.
use strict;

die("diFilm has been renamed/updated to 'myfilm'\nPlease see http://MarginalHacks.com/ for details.\n");

##################################################
# Setup the variables
##################################################
my $PROGNAME = $0; $PROGNAME =~ s|.*/||;
my ($BASENAME,$PROGNAME) = ($0 =~ m|(.*)/(.+)|) ? ($1?$1:'/',$2) : ('.',$0);

# This will probably change..
my $IFILM = "http://www.ifilm.com/player/mac.jsp?ifilmId=";

# How to fetch web files - pick one
# Actual Safari agent example:
# Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/416.12 (KHTML, like Gecko) Safari/416.13
my $GET = "GET -H 'user-agent: Mozilla/5.0'";
my $LYNX = "lynx -source";

my $GET_URL = $LYNX;

my $POST = "mov";

##################################################
# Usage
##################################################
sub fatal {
	foreach my $msg (@_) { print STDERR "[$PROGNAME] ERROR:  $msg\n"; }
	exit(-1);
}

sub usage {
	foreach my $msg (@_) { print STDERR "ERROR:  $msg\n"; }
	print STDERR <<USAGE;

Usage:\t$PROGNAME [-d] <ifilm URL or ifilm id>
  Download an ifilm movie
  -o <file>         Save to file
  -bw <56,200,300>	Force a bandwidth other than your iFilm settings
  -d                Set debug mode

USAGE
	exit -1;
}

sub parse_args {
	my $opt = {};
	while (my $arg=shift(@ARGV)) {
		if ($arg =~ /^-h$/) { usage(); }
		if ($arg =~ /^-d$/) { $MAIN::DEBUG=1; next; }
		if ($arg =~ /^-bw$/) { $opt->{bw} = shift @ARGV; next; }
		if ($arg =~ /^-o$/) { $opt->{save} = shift @ARGV; next; }
		if ($arg =~ /^-/) { usage("Unknown option: $arg"); }
		if ($arg =~ /(\d+)/) { $opt->{film} = $1; next; }
		usage("Unknown film id.  I need a URL that contains a iFilm number.");
	}
	usage("No film found.  I need a URL that contains a iFilm number.") unless $opt->{film};
	print STDERR "Fetching iFilm ID: $opt->{film}\n";
	
	$opt;
}

sub debug {
	return unless $MAIN::DEBUG;
	foreach my $msg (@_) { print STDERR "[$PROGNAME] $msg\n"; }
}

##################################################
# Main code
##################################################
sub main {
	my $opt = parse_args();
	
	# Read the ifilm HTML
	my $url = $IFILM.$opt->{film};
	debug("URL: $url");
	my $open = "$GET_URL $url";
	debug("GET: $open");
	open(FILM,"$open |") || usage("Couldn't get URL: $url\nMake sure [$GET_URL] is installed");
	
	# Find the film location
	my $dl;
	my $title = $opt->{film};
	my $bw = $opt->{bw};
	my $smil;
	while(<FILM>) {
		$dl = $1 if /var\s+dl\s*=\s*"?(-?\d+)"?/;
		$bw = $1 if !$bw && /var\s+bw\s*=\s*"?(\d+)"?/;
		$smil = $1 if /<embed.*(http:[^>"]+)/i;
		$title = $1 if /span class="title">([^<]+)/;
	}
		print "YO; $smil\n\n";
	close(FILM);
	usage("Couldn't find 'dl' key in URL [$url]") unless $dl;
	usage("Couldn't find 'bw' key in URL [$url]") unless $bw;
	usage("Couldn't find 'embed' tag in URL [$url]") unless $smil;
	$smil =~ s/'\s+\+\s+dl\s+\+\s+'/$dl/g;
	$smil =~ s/'\s+\+\s+bw\s+\+\s+'/$bw/g;
	$smil =~ s/"$//;
	debug("SMIL: $smil");

	# Get the smil file
	$open = "$GET_URL \Q$smil\E";
	open(SMIL,"$open |") || usage("Couldn't get SMIL: $smil");
	my $video;
	while (<SMIL>) {
		$video = $1 if /video src="(http[^"]+)"/;
	}
	usage("Couldn't find video source key in SMIL [$smil]") unless $video;
	close SMIL;

	debug("Found: $video");
	$title =~ s/\s+/_/g;
	my $save = $opt->{save} || "$title.$POST";
	print "Saving to: $save\n";
	system("$GET_URL \Q$video\E > \Q$save\E");
}
main();
