#!/usr/bin/perl -w
###############################################################################
# $Log: fetch.pl.new,v $
# Revision 1.20 2015/10/22 12:58:44 nick
# Added the ability for Sunday only comics. Stonesoup is no longer weekdays, this has been added to Sunday only. I also added Foxtrot Classics for weekdays and Foxtrot for Sundays.
#
# Revision 1.19 2015/07/13 12:56:58 nick
# Added Sally Forth and Pearls Before Swine. Adding Sally Forth required a change in the 'wget' command for fetching the index file to include 'user-agent' and 'referer'.
#
# Revision 1.18 2015/05/07 12:31:43 nick
# Added favicon
#
# Revision 1.17 2015/02/19 14:56:10 nick
# Fixed a problem that forced everything to JPG. This would kill GIF animations, but would not display the gifs either because 'convert' appends an index number to the end of the file name for each from of the GIF animation. I fixed this to maintain GIF compatibilty as well as rewritting how the script fetches the size of the file. Additionally, I updated the configuration for Questionable Content to search for GIF or JPG, which is what triggered this entire update.
#
# Revision 1.16 2015/02/05 18:05:58 nick
# Changed the background and added a fancy title.
#
# Revision 1.15 2015/01/19 13:46:19 nick
# *** empty log message ***
#
###############################################################################
use strict;
use File::Path;
use Data::Dumper;
use Pod::Usage;
use Getopt::Long;
use Date::Calc qw/Date_to_Text_Long Today/;
##
## Some default values
##
my $ver = '$Id: fetch.pl.new,v 1.20 2015/10/22 12:58:44 nick Exp $';
my $comicFile = "comics.conf";
my %comics = &readComicConfig ( $comicFile );
my %opts = &fetchOptions( );
my $days_ago = $opts{'days'} || 0;
my %dates = &fetchDates();
my $baseDir = $comics{'configs'}{'base_directory'} || ".";
my $imageDir = $baseDir . "/" . ( $comics{'configs'}{'image_directory'} || "images" ) .
"/$dates{'mon2'}$dates{'year2'}";
my $indexDir = $baseDir . "/" . ( $comics{'configs'}{'index_directory'} || "indexes" );
my $USER_AGENT = "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110628 Ubuntu/10.10 (maverick) Firefox/3.6.18";
my @days = qw/ Sunday Monday Tuesday Wednesday Thursday Friday Saturday /;
my $DATE=`date`; chomp $DATE;
print STDOUT "Starting comic fetch at $DATE\n";
##
## Main program starts here
##
&checkDir ( [ $imageDir, $indexDir ] );
&writeTitle ( \%dates );
foreach my $comic ( sort keys %comics ) {
## Skip if this is Sunday and the comic is weekdays only
next if ( $comic =~ m/config/ );
if (($dates{'day2'} eq "Sunday") &&
($comics{$comic}{'sunday'} == 0)) {
print "Skipping '$comic'; Weekdays only.\n";
next;
}
## Skip if Sunday only comic and it's not Sunday.
if (($dates{'day2'} ne "Sunday") &&
($comics{$comic}{'sunday_only'} == 1)) {
print "Skipping '$comic'; Sunday only.\n";
next
}
$comics{$comic}{'error'} = &downloadComic ( \%comics, $comic, \%dates );
&writeComic ( \%comics, $comic, \%dates );
my $file = "$imageDir/$comic-$dates{'day2'}.$comics{$comic}{'ext'}";
my $size = 0;
my $cmd = "/usr/bin/identify -verbose $file|";
open(IMG, $cmd) || die ("Can't open: $!\n");
while() {
if ($_ =~ m/^\s+geometry:\s+(\d+)x\d+.*/i) {
$size = $1 if ( $size == 0);
}
}
close(IMG);
system( "/usr/bin/convert -resize 640 $file $file" )
if ( $size > 640 )
}
## &writeMainIndex ( \%dates );
&writeFooter( \%dates );
$DATE=`date`; chomp( $DATE );
print STDOUT "Completed comic fetch at $DATE\n";
## End
#######################################################################
## Function : downloadComic
##
## Description :
## This function determines the download method being used to
## retrieve the comic and calls the apprioriate function.
##
## If the mode is invalid an error will be returned.
##
#######################################################################
sub downloadComic ($$) {
my ( $comics, $comic, $date ) = @_;
SWITCH: {
if ( $comics->{$comic}{'mode'} eq 1 ) {
return indexDownload ( \%comics, $comic, $date );
last SWITCH;
}
if ( $comics->{$comic}{'mode'} eq 2 ) {
return directDownload ( \%comics, $comic, $date );
last SWITCH;
}
}
return "ERROR: Unknown download method specified for $comics->{$comic}{'fullName'}.";
}
#######################################################################
#######################################################################
sub readComicConfig ($$) {
my ( $comicFile ) = @_;
my %comicConfig = ( );
my %config = ( );
my ($year, $mon, $day) =( localtime(time))[5,4,3];
$year += 1900;
$mon = sprintf("%02d", ($mon + 1));
$day = sprintf("%02d", $day);
open FILEN, "<$comicFile";
while () {
if ( ( $_ !~ m/^#/ ) && ( $_ =~ m/,.*,/) ){
$_ =~ s/__YEAR__/$year/g;
$_ =~ s/__MON__/$mon/g;
$_ =~ s/__DAY__/$day/g;
my @res = split /,/, $_;
$comicConfig{$res[0]}{'url'} = $res[1];
$comicConfig{$res[0]}{'search'} = $res[2];
$comicConfig{$res[0]}{'mode'} = $res[3];
$comicConfig{$res[0]}{'fullName'} = $res[4];
$comicConfig{$res[0]}{'ext'} = $res[5];
$comicConfig{$res[0]}{'sunday'} = $res[6] || 1;
$comicConfig{$res[0]}{'sunday_only'} = $res[7] || 0;
$comicConfig{$res[0]}{'error'} = 0;
}
elsif ( $_ =~ m/(.*)\s+=\s+(.*)/ ) {
$comicConfig{'configs'}{$1} = $2;
}
}
close (FILEN);
return %comicConfig;
}
#######################################################################
#######################################################################
sub writeComic ($$) {
my ( $comics, $comic, $date ) = @_;
my $sd = substr( join( '', $days[$date->{'dow'}] ), 0, 3 );
my $indexFile = $indexDir . "/index-" . $date->{'year2'} .
$date->{'mon2'} . $date->{'day2'} . "-" .
$sd . ".html";
my $content = <{$comic}{'fullName'}) ******* -->