#!/usr/bin/perl -w

use strict;
use LWP::Simple;

# The script takes one parameter - the URL to grab.
my $uri = shift;
my $page = get ($uri);

my $uris;

foreach (split ("\n", $page))
{
	# Scrape the URL of the page containing the image.
	if (m!(/popupWindow/[^.]*.html)!i)
	{
		$uris .= "http://www.thesun.co.uk$1 ";
	}
	elsif (m!window.open\('(http://images.thesun.co.uk/picture/0,,[^,]*,00.[gj][ip][fg])'!i)
	{
		$uris .= "$1 ";
	}
}
# Grab each page for viewing
# This directory is specific to the way I keep files (dis)organised
`cd /home/jimmy/.download && wget --referer=$uri -x $uri $uris`;

foreach (split (" ", $uris))
{
	if (/htm[l]+$/i)
	{
		$page = get ($_);
		foreach (split ("\r\n", $page))
		{
			# Find the image
			if (m!(http://images.thesun.co.uk/picture[s]?/0,,[^,]*,00.[gj][ip][fg])!i)
			{
				# Another directory specific to me
				`cd /home/jimmy/Pictures && wget $1`;
			}
		}
	}
}



