#!/usr/bin/perl -w ############################################################################### # $Log: fetch.pl.new,v $ # 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.18 2015/05/07 12:31:43 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 ) { next if ( $comic =~ m/config/ ); if ( ( $dates{'day2'} eq "Sunday" ) && ( $comics{$comic}{'sunday'} == 0 ) ) { print "Skipping.\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]}{'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'}) ******* --> $comics->{$comic}{'fullName'} $comics->{$comic}{'url'} EOF open INDEX, ">>$indexFile"; print INDEX $content if ( ! $comics->{$comic}{'error'} ); print INDEX <$comics->{$comic}{'fullName'} < $comics->{$comic}{'url'} $comic : $comics->{$comic}{'error'} EOF if ( $comics->{$comic}{'error'} ); close (INDEX); return 0; } ####################################################################### ####################################################################### sub writeMainIndex ($$) { my ( $date ) = @_; } ####################################################################### ####################################################################### sub writeFooter { my ( $date ) = @_; my $sd = substr( join( '', $days[$date->{'dow'}] ), 0, 3 ); my $indexFile = $indexDir . "/index-" . $date->{'year2'} . $date->{'mon2'} . $date->{'day2'} . "-" . $sd . ".html"; my $sysDate = `date`; open INDEX, ">>$indexFile"; print INDEX < Generated on: $sysDate Version: $ver CVS: http://demandred.dyndns.org/cgi-bin/cvsweb/comics/