# -*- mode: Perl; -*-

package NewsClipper::Handler::Acquisition::unitedmediacomic;

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

$handlerInfo{'Author_Name'}              = 'David Coppit';
$handlerInfo{'Author_Email'}             = 'coppit@cs.virginia.edu';
$handlerInfo{'Maintainer_Name'}          = 'David Coppit';
$handlerInfo{'Maintainer_Email'}         = 'coppit@cs.virginia.edu';
$handlerInfo{'Description'}              = <<'EOF';
Get any United Media comic strip. The image is cached locally according to the
configuration of the cacheimages filter.
EOF
$handlerInfo{'Category'}                 = 'Comics';
$handlerInfo{'URL'}                      = <<'EOF';
http://www.unitedmedia.com/comics/
EOF
$handlerInfo{'License'}                  = 'GPL';
$handlerInfo{'For_News_Clipper_Version'} = '1.18';
$handlerInfo{'Language'}                 = 'English';
$handlerInfo{'Notes'}                    = <<'EOF';
original idea from Tanner Lovelace <lovelace@cs.unc.edu>
EOF
$handlerInfo{'Syntax'}                   = <<'EOF';
<input name=unitedmediacomic strip=W>
  Returns a string
  W=one of (adam, alleyoop, andycapp, arlonjanis, askshagg, bc, ballardst,
    betty, bignate, bizarro, bliss, boondocks, bornloser, buckets, cathy,
    cheapthrills, citizendog, closehome, committed, cornered, crabbyrd,
    crankshaft, dilbert, doonesbury, drabble, duplex, eeknmeek, fatcats,
    fifthwave, forbetter, forheavens, foxtrot, franknernest, fuscobros,
    garfield, geech, getfuzzy, grandave, grizzwells, heartcity, heathcliff,
    herman, bleachers, jumpstart, katies, kitncarlyle, liberty, luann, lupo,
    marmaduke, meatloaf, meg, mrboffo, momma, motley, mspeach, nancy,
    nohuddle, nonsequitur, hedge, onebighappy, overboard, pcnpixel, peanuts,
    penmen, pickles, rawmaterial, reallife, reality, ripleys, robotman,
    roseisrose, rubes, rugrats, secchances, speedbump, stonesoup,
    strangebrew, tmcnamara, tarzan, tmnt, thatslife, toby, tombug,
    topofworld, tradinpaint, warped, weepals, wizardofid, ziggy)
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=('1.11.1'=~/\d+/g);sprintf "%d."."%02d"x$#r,@r};

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

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

  my %comicMap = (
    'adam' => 'http://www.unitedmedia.com/universal/adam/ab.html',
    'alleyoop' => 'http://www.unitedmedia.com/comics/alleyoop/ab.html',
    'andycapp' => 'http://www.unitedmedia.com/comics/andycapp/ab.html',
    'arlonjanis' => 'http://www.unitedmedia.com/comics/arlonjanis/ab.html',
    'askshagg' => 'http://www.unitedmedia.com/comics/askshagg/ab.html',
    'bc' => 'http://www.unitedmedia.com/comics/bc/ab.html',
    'ballardst' => 'http://www.unitedmedia.com/comics/ballardst/ab.html',
    'betty' => 'http://www.unitedmedia.com/comics/betty/ab.html',
    'bignate' => 'http://www.unitedmedia.com/comics/bignate/ab.html',
    'bizarro' => 'http://www.unitedmedia.com/universal/bizarro/ab.html',
    'bliss' => 'http://www.unitedmedia.com/universal/bliss/ab.html',
    'boondocks' => 'http://www.unitedmedia.com/universal/boondocks/ab.html',
    'bornloser' => 'http://www.unitedmedia.com/comics/bornloser/ab.html',
    'buckets' => 'http://www.unitedmedia.com/comics/buckets/ab.html',
    'cathy' => 'http://www.unitedmedia.com/universal/cathy/ab.html',
    'cheapthrills' => 'http://www.unitedmedia.com/comics/cheapthrills/ab.html',
    'citizendog' => 'http://www.unitedmedia.com/universal/citizendog/ab.html',
    'closehome' => 'http://www.unitedmedia.com/universal/closehome/ab.html',
    'committed' => 'http://www.unitedmedia.com/comics/committed/ab.html',
    'cornered' => 'http://www.unitedmedia.com/universal/cornered/ab.html',
    'crabbyrd' => 'http://www.unitedmedia.com/universal/crabbyrd/ab.html',
    'crankshaft' => 'http://www.unitedmedia.com/universal/crankshaft/ab.html',
    'dilbert' => 'http://www.unitedmedia.com/comics/dilbert/',
    'doonesbury' => 'http://www.unitedmedia.com/universal/doonesbury/ab.html',
    'drabble' => 'http://www.unitedmedia.com/comics/drabble/ab.html',
    'duplex' => 'http://www.unitedmedia.com/universal/duplex/ab.html',
    'eeknmeek' => 'http://www.unitedmedia.com/comics/eeknmeek/ab.html',
    'fatcats' => 'http://www.unitedmedia.com/comics/fatcats/ab.html',
    'fifthwave' => 'http://www.unitedmedia.com/universal/fifthwave/ab.html',
    'forbetter' => 'http://www.fborfw.com/',
    'forheavens' => 'http://www.unitedmedia.com/comics/forheavens/ab.html',
    'foxtrot' => 'http://www.unitedmedia.com/universal/foxtrot/ab.html',
    'franknernest' => 'http://www.unitedmedia.com/comics/franknernest/ab.html',
    'fuscobros' => 'http://www.unitedmedia.com/universal/fuscobros/ab.html',
    'garfield' => 'http://www.garfield.com/comics/pages/index.html',
    'geech' => 'http://www.unitedmedia.com/universal/geech/ab.html',
    'getfuzzy' => 'http://www.unitedmedia.com/comics/getfuzzy/ab.html',
    'grandave' => 'http://www.unitedmedia.com/comics/grandave/ab.html',
    'grizzwells' => 'http://www.unitedmedia.com/comics/grizzwells/ab.html',
    'heartcity' => 'http://www.unitedmedia.com/universal/heartcity/ab.html',
    'heathcliff' => 'http://www.unitedmedia.com/comics/heathcliff/ab.html',
    'herman' => 'http://www.unitedmedia.com/comics/herman/ab.html',
    'bleachers' => 'http://www.unitedmedia.com/universal/bleachers/ab.html',
    'jumpstart' => 'http://www.unitedmedia.com/comics/jumpstart/ab.html',
    'katies' => 'http://www.unitedmedia.com/comics/katies/ab.html',
    'kitncarlyle' => 'http://www.unitedmedia.com/comics/kitncarlyle/ab.html',
    'liberty' => 'http://www.unitedmedia.com/comics/liberty/ab.html',
    'luann' => 'http://www.unitedmedia.com/comics/luann/ab.html',
    'lupo' => 'http://www.unitedmedia.com/comics/lupo/ab.html',
    'marmaduke' => 'http://www.unitedmedia.com/comics/marmaduke/ab.html',
    'meatloaf' => 'http://www.unitedmedia.com/comics/meatloaf/ab.html',
    'meg' => 'http://www.unitedmedia.com/comics/meg/ab.html',
    'mrboffo' => 'http://www.unitedmedia.com/universal/mrboffo/ab.html',
    'momma' => 'http://www.unitedmedia.com/comics/momma/ab.html',
    'motley' => 'http://www.unitedmedia.com/comics/motley/ab.html',
    'mspeach' => 'http://www.unitedmedia.com/comics/mspeach/ab.html',
    'nancy' => 'http://www.unitedmedia.com/comics/nancy/ab.html',
    'nohuddle' => 'http://www.unitedmedia.com/comics/nohuddle/ab.html',
    'nonsequitur' => 'http://www.unitedmedia.com/comics/nonsequitur/ab.html',
    'hedge' => 'http://www.unitedmedia.com/comics/hedge/ab.html',
    'onebighappy' => 'http://www.unitedmedia.com/comics/onebighappy/ab.html',
    'overboard' => 'http://www.unitedmedia.com/universal/overboard/ab.html',
    'pcnpixel' => 'http://www.unitedmedia.com/comics/pcnpixel/ab.html',
    'peanuts' => 'http://www.unitedmedia.com/comics/peanuts/',
    'penmen' => 'http://www.unitedmedia.com/comics/penmen/ab.html',
    'pickles' => 'http://www.unitedmedia.com/comics/pickles/ab.html',
    'rawmaterial' => 'http://www.unitedmedia.com/comics/rawmaterial/ab.html',
    'reallife' => 'http://www.unitedmedia.com/universal/reallife/ab.html',
    'reality' => 'http://www.unitedmedia.com/comics/reality/ab.html',
    'ripleys' => 'http://www.unitedmedia.com/comics/ripleys/ab.html',
    'robotman' => 'http://www.unitedmedia.com/comics/robotman/ab.html',
    'roseisrose' => 'http://www.unitedmedia.com/comics/roseisrose/ab.html',
    'rubes' => 'http://www.unitedmedia.com/comics/rubes/ab.html',
    'rugrats' => 'http://www.unitedmedia.com/comics/rugrats/ab.html',
    'secchances' => 'http://www.unitedmedia.com/universal/secchances/ab.html',
    'speedbump' => 'http://www.unitedmedia.com/comics/speedbump/ab.html',
    'stonesoup' => 'http://www.unitedmedia.com/universal/stonesoup/ab.html',
    'strangebrew' => 'http://www.unitedmedia.com/comics/strangebrew/ab.html',
    'tmcnamara' => 'http://www.unitedmedia.com/universal/tmcnamara/ab.html',
    'tarzan' => 'http://www.unitedmedia.com/comics/tarzan/ab.html',
    'tmnt' => 'http://www.unitedmedia.com/comics/tmnt/ab.html',
    'thatslife' => 'http://www.unitedmedia.com/comics/thatslife/ab.html',
    'toby' => 'http://www.unitedmedia.com/comics/toby/ab.html',
    'tombug' => 'http://www.unitedmedia.com/universal/tombug/ab.html',
    'topofworld' => 'http://www.unitedmedia.com/comics/topofworld/ab.html',
    'tradinpaint' => 'http://www.unitedmedia.com/universal/tradinpaint/ab.html',
    'warped' => 'http://www.unitedmedia.com/comics/warped/ab.html',
    'weepals' => 'http://www.unitedmedia.com/comics/weepals/ab.html',
    'wizardofid' => 'http://www.unitedmedia.com/comics/wizardofid/ab.html',
    'ziggy' => 'http://www.unitedmedia.com/universal/ziggy/ab.html',
  );

  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} = 'ripleys' 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;

  @$data = grep { $$_ =~ /\d{6}\.gif/ } @$data;
  $data = $$data[0];

  # 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,4,6 EST'];
}

1;
