# -*- mode: Perl; -*-

package NewsClipper::Handler::Acquisition::uexpresscomic;

use vars qw( @ISA $VERSION %handlerInfo );

$handlerInfo{'Author_Name'}              = 'David Coppit';
$handlerInfo{'Author_Email'}             = 'david@coppit.org';
$handlerInfo{'Maintainer_Name'}          = 'David Coppit';
$handlerInfo{'Maintainer_Email'}         = 'david@coppit.org';
$handlerInfo{'Description'}              = <<'EOF';
Any UExpress comic. The image is cached locally according to the
configuration of the cacheimages filter.
EOF
$handlerInfo{'Category'}                 = 'Comics';
$handlerInfo{'URL'}                      = <<'EOF';
http://www.uexpress.com/ups/comics/
EOF
$handlerInfo{'License'}                  = 'GPL';
$handlerInfo{'For_News_Clipper_Version'} = '1.18';
$handlerInfo{'Language'}                 = 'English';
$handlerInfo{'Notes'}                    = <<'EOF';
based on schema by john@www.llamacom.com <john@www.llamacom.com>
EOF
$handlerInfo{'Syntax'}                   = <<'EOF';
<input name=uexpresscomic strip=W>
  Returns a string containing the image link
  W is one of the following: adamathome, bliss, cathy, citizendog,
    crankshaft, doonesbury, duplex, fluff, fusco, geech,
    misterboffo, overboard, secondchances, stonesoup, tankmcnamara,
    tradinpaint, usandthem, bizarro, closetohome, crabbyroad, cornered,
    fairgame, thefifthwave, inthebleachers, qualitytime, reallifead, tombug,
    boondocks, ziggy (default is doonesbury)
This handler uses the cacheimages filter to store a local copy of the image.
See the cacheimages documentation for more information.
EOF

use strict;
use NewsClipper::Handler;
@ISA = qw(NewsClipper::Handler);

# - The first number should be incremented when a change is made to the
#   handler that will break people's input files.
# - The second number should be incremented when a change is made that won't
#   break people's input files, but changes the functionality.
# - The third number should be incremented when only a bugfix is applied.

$VERSION = do {my @r=('0.74.1'=~/\d+/g);sprintf "%d."."%02d"x$#r,@r};

# ------------------------------------------------------------------------------

sub ComputeURL
{
  my $self = shift;
  my $attributes = shift;

  my %comicMap = (
    'adamathome' => 'http://www.uexpress.com/ups/comics/ad/',
    'bliss' => 'http://www.uexpress.com/ups/comics/sh/',
    'boondocks' => 'http://www.uexpress.com/ups/comics/bo/',
    'cathy' => 'http://www.uexpress.com/ups/comics/ca/',
    'citizendog' => 'http://www.uexpress.com/ups/comics/cd/',
    'crankshaft' => 'http://www.uexpress.com/ups/comics/cs/',
    'doonesbury' => 'http://www.doonesbury.com/strip/dailydose/',
    'duplex' => 'http://www.uexpress.com/ups/comics/dp/',
    'fluff' => 'http://www.uexpress.com/ups/comics/fl/',
    'fusco' => 'http://www.uexpress.com/ups/comics/fu/',
    'geech' => 'http://www.uexpress.com/ups/comics/ge/',
    'misterboffo' => 'http://www.uexpress.com/ups/comics/mb/',
    'overboard' => 'http://www.uexpress.com/ups/comics/ob/',
    'secondchances' => 'http://www.uexpress.com/ups/comics/se/',
    'stonesoup' => 'http://www.uexpress.com/ups/comics/ss/',
    'tankmcnamara' => 'http://www.uexpress.com/ups/comics/tm/',
    'tradinpaint' => 'http://www.uexpress.com/ups/comics/tp/',
    'usandthem' => 'http://www.uexpress.com/ups/comics/ut/',
    'bizarro' => 'http://www.uexpress.com/ups/comics/bz/',
    'closetohome' => 'http://www.uexpress.com/ups/comics/cl/',
    'crabbyroad' => 'http://www.uexpress.com/ups/comics/cr/',
    'cornered' => 'http://www.uexpress.com/ups/comics/co/',
    'fairgame' => 'http://www.uexpress.com/ups/comics/fg/',
    'thefifthwave' => 'http://www.uexpress.com/ups/comics/fw/',
    'inthebleachers' => 'http://www.uexpress.com/ups/comics/bl/',
    'qualitytime' => 'http://www.uexpress.com/ups/comics/qt/',
    'reallifead' => 'http://www.uexpress.com/ups/comics/rl/',
    'tombug' => 'http://www.uexpress.com/ups/comics/td/',
    'ziggy' => 'http://www.uexpress.com/ups/comics/zi'
  );

  my $url = $comicMap{$attributes->{strip}};

  return $url;
}

# ------------------------------------------------------------------------------

# This subroutine checks the handler's attributes to make sure they are valid,
# and sets any default attributes if necessary.

sub ProcessAttributes
{
  my $self = shift;
  my $attributes = shift;
  my $handlerRole = shift;

  # Set defaults here. You can safely delete this function if your handler has
  # no attributes with default values.

  # $attributes->{'some_attribute'} = 'default_value'
  #   unless defined $attributes->{'some_attribute'};
  $attributes->{strip} = 'doonesbury' unless defined $attributes->{strip};

  # Verify any attributes you need to here. Output an error and return undef
  # if something is wrong.

  # unless ($attributes->{somevalue} > 0)
  # {
  #   error "The \"somevalue\" attribute for handler \"HANDLERNAME\" " .
  #     "should be greater than 0.\n";
  #   return undef;
  # }

  return $attributes;
}

# ------------------------------------------------------------------------------

# This function is used to get the raw data from the URL.
sub Get
{
  my $self = shift;
  my $attributes = shift;

  my $url = $self->ComputeURL($attributes);

  my $data = GetImages($url,'^','$');

  return undef unless defined $data;

  # Grab the link that has the image. It's the one that has 6 or more digits.
  @$data = grep { $$_ =~ /[^\d]\d{6,11}\./ } @$data;
  $data = $$data[0];

  # Strip out the alt text
  $$data =~ s/ ALT=".*?"//;

  # Cache the image locally
  $data = RunHandler('cacheimages','filter',$data,$attributes);

  return $data;
}

# ------------------------------------------------------------------------------

sub GetDefaultHandlers
{
  my $self = shift;
  my $inputAttributes = shift;

  my $returnVal = <<'  EOF';
    <output name='string'>
  EOF

  return $returnVal;
}

# ------------------------------------------------------------------------------

sub GetUpdateTimes
{
  return ['1,3 EST'];
}

1;
