Annotation of comics/fetch.pl.new, revision 1.11

1.1       nick        1: #!/usr/bin/perl -w
                      2: 
                      3: use strict;
                      4: use File::Path;
                      5: use Data::Dumper;
1.8       nick        6: use Pod::Usage;
                      7: use Getopt::Long;
1.1       nick        8: 
                      9: ## 
                     10: ## Some default values
                     11: ##
1.11    ! nick       12: my $ver                = q/$Id: fetch.pl.new,v 1.10 2013-02-26 14:24:14 nick Exp $/;
1.1       nick       13: my $comicFile   = "comics.conf";
                     14: my %comics     = &readComicConfig ( $comicFile );
1.8       nick       15: my %opts        = &fetchOptions( );
                     16: my $days_ago    = $opts{'days'} || 0;
1.1       nick       17: my %dates      = &fetchDates();
                     18: my $baseDir     = $comics{'configs'}{'base_directory'} || ".";
                     19: my $imageDir    = $baseDir . "/" . ( $comics{'configs'}{'image_directory'} || "images" ) . 
                     20:                  "/$dates{'mon2'}$dates{'year2'}";
                     21: my $indexDir    = $baseDir . "/" . ( $comics{'configs'}{'index_directory'} || "indexes" );
1.2       nick       22: 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";
1.8       nick       23: my @days        = qw/ Sunday Monday Tuesday Wednesday Thursday Friday Saturday /;
1.1       nick       24: 
                     25: 
                     26: my $DATE=`date`; chomp $DATE;
                     27: print STDOUT "Starting comic fetch at $DATE\n";
                     28: 
                     29: ##
                     30: ## Main program starts here
                     31: ##
                     32: &checkDir ( [ $imageDir, $indexDir ] );
                     33: 
1.5       nick       34: &writeTitle ( \%dates );
1.1       nick       35: 
                     36: foreach my $comic ( sort keys %comics ) {
                     37:   next if ( $comic =~ m/config/ );
                     38:   $comics{$comic}{'error'} = &downloadComic ( \%comics, $comic, \%dates );
                     39:   &writeComic ( \%comics, $comic, \%dates );
                     40: }
                     41: 
1.8       nick       42: print "Finding in $imageDir/*-$dates{'day2'}.jpg\n";
                     43: foreach my $file ( glob( "$imageDir/*-$dates{'day2'}.jpg" ) ) 
1.4       nick       44: {
                     45:        my $size = `/usr/bin/identify $file`;
                     46:        $size =~ s/.*\s(\d+)x\d+.*/$1/;
                     47: 
                     48:        system( "/usr/bin/convert -resize 640 $file $file" )
                     49:                if ( $size > 640 ) 
                     50: }
                     51: 
1.1       nick       52: ## &writeMainIndex ( \%dates );
                     53: 
                     54: &writeFooter( \%dates );
                     55: 
                     56: $DATE=`date`;  chomp( $DATE );
                     57: print STDOUT "Completed comic fetch at $DATE\n";
                     58: 
                     59: ## End
                     60: 
                     61: #######################################################################
                     62: ## Function :  downloadComic
                     63: ##
                     64: ##   Description :
                     65: ##     This function determines the download method being used to 
                     66: ##      retrieve the comic and calls the apprioriate function.
                     67: ##
                     68: ##      If the mode is invalid an error will be returned.
                     69: ##
                     70: #######################################################################
                     71: sub downloadComic ($$) {
                     72:        my ( $comics, $comic, $date ) = @_;
                     73: 
                     74:        SWITCH: {
                     75:                if ( $comics->{$comic}{'mode'} eq 1 ) { 
                     76:                        return indexDownload ( \%comics, $comic, $date );
                     77:                        last SWITCH;
                     78:                }
                     79:                if ( $comics->{$comic}{'mode'} eq 2 ) { 
                     80:                        return directDownload ( \%comics, $comic, $date );
                     81:                        last SWITCH;
                     82:                }
                     83:        }
                     84:         
                     85:        return "ERROR: Unknown download method specified for $comics->{$comic}{'fullName'}.";
                     86: }
                     87: 
                     88: #######################################################################
                     89: #######################################################################
                     90: sub readComicConfig ($$) {
                     91:        my ( $comicFile ) = @_;
                     92:        my %comicConfig   = ( );
                     93:        my %config        = ( );
                     94: 
                     95:        open FILEN, "<$comicFile";
                     96:                while (<FILEN>) {
                     97:                        if ( ( $_ !~ m/^#/ ) && ( $_ =~ m/,.*,/) ){
                     98:                                my @res = split /,/, $_;
                     99:                                $comicConfig{$res[0]}{'url'}      = $res[1];
                    100:                                $comicConfig{$res[0]}{'search'}   = $res[2];
                    101:                                $comicConfig{$res[0]}{'mode'}     = $res[3];
                    102:                                $comicConfig{$res[0]}{'fullName'} = $res[4];
                    103:                                $comicConfig{$res[0]}{'ext'}      = $res[5];
                    104:                                $comicConfig{$res[0]}{'error'}    = 0;
                    105:                        }
                    106:                        elsif ( $_ =~ m/(.*)\s+=\s+(.*)/ ) {
                    107:                                $comicConfig{'configs'}{$1} = $2;
                    108:                        }
                    109:                }
                    110:        close (FILEN);
                    111: 
                    112:        return %comicConfig;
                    113: }
                    114: 
                    115: #######################################################################
                    116: #######################################################################
                    117: sub writeComic ($$) {
                    118:        my ( $comics, $comic, $date ) = @_;
1.11    ! nick      119:        my $sd = substr( join( '', $days[$date->{'dow'}] ), 0, 3 );
        !           120:        my $indexFile = $indexDir . "/index-" . $sd .
1.9       nick      121:                         "-" . $date->{'year2'} . $date->{'mon2'} .
1.1       nick      122:                         $date->{'day2'} . ".html";
                    123:        my $content = <<EOF;
                    124: 
                    125: <!-- ********* Begin $comic ($comics->{$comic}{'fullName'}) ******* -->
                    126:   <tr>
                    127:     <td align="left">
                    128: <font color="blue"><b>$comics->{$comic}{'fullName'}</b></font> &nbsp; &nbsp; 
                    129: <font size="-2">
                    130:        <a href="$comics->{$comic}{'url'}">
                    131:                $comics->{$comic}{'url'}
                    132:        </a>
                    133: </font><br/>
                    134: <img src="../images/$date->{'mon2'}$date->{'year2'}/$comic-$date->{'day2'}.jpg" alt="$comic-$date->{'day2'}" />
                    135: <br/><br/>
                    136: </td></tr>
                    137: <!-- ********* Finish $comic ($comics->{$comic}{'fullName'}) ******* -->
                    138: 
                    139: EOF
                    140:        open INDEX, ">>$indexFile";
                    141: 
                    142:        print INDEX $content if ( ! $comics->{$comic}{'error'} );
                    143: 
                    144:        print INDEX <<EOF
                    145: <font color="blue"><b>$comics->{$comic}{'fullName'}</b></font> &nbsp; &nbsp;
                    146: <font size="-2"><
                    147:         <a href="$comics->{$comic}{'url'}">
                    148:                 $comics->{$comic}{'url'}
                    149:         </a>
                    150: </font><br/>
                    151: <font color="red"><b>$comic :  $comics->{$comic}{'error'}</b></font><br/>
                    152:   </td>
                    153: </tr>
                    154: EOF
                    155:                if ( $comics->{$comic}{'error'} );
                    156: 
                    157:        close (INDEX);
                    158: 
                    159:        return 0;
                    160: }
                    161: 
                    162: 
                    163: #######################################################################
                    164: #######################################################################
                    165: sub writeMainIndex ($$) {
                    166:        my ( $date ) = @_;
                    167: 
                    168: }
                    169: 
                    170: 
                    171: #######################################################################
                    172: #######################################################################
                    173: sub writeFooter {
                    174:        my ( $date ) = @_;
1.11    ! nick      175:        my $sd = substr( join( '', $days[$date->{'dow'}] ), 0, 3 );
        !           176:        my $indexFile = $indexDir . "/index-" . $sd .
1.10      nick      177:                        "-" . $date->{'year2'} . $date->{'mon2'} .
1.1       nick      178:                         $date->{'day2'} . ".html";
                    179:        my $sysDate = `date`;
                    180: 
                    181:        open INDEX, ">>$indexFile";
                    182:        print INDEX <<EOF;
                    183: </table>
1.3       nick      184: <center>
                    185: <font size="2">
                    186: Generated on: <font color="green">$sysDate</font><br/>
1.7       nick      187: Version: <font color="green">$ver</font><br />
                    188: CVS: <a href="http://demandred.dyndns.org/cgi-bin/cvsweb/comics/">http://demandred.dyndns.org/cgi-bin/cvsweb/comics/</a>
1.1       nick      189:   <p>
                    190:     <a href="http://validator.w3.org/check?uri=referer"><img
                    191:       src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" border="0" /></a>
                    192:   </p>
                    193: </center>
                    194: 
                    195: </body>
                    196: </html>
                    197: EOF
                    198:        close( INDEX );
                    199: }
                    200: 
                    201: #######################################################################
                    202: #######################################################################
                    203: sub checkDir ($$) {
                    204:        my @dir = @_;
                    205: 
                    206:        foreach ( @dir ) {
                    207:                if ( ! -d $_ ) { mkpath( $_ ); }
                    208:        }
                    209: }
                    210: 
                    211: #######################################################################
                    212: #######################################################################
                    213: sub writeTitle ($$) {
                    214:        my ( $date ) = @_;
1.11    ! nick      215:        my $sd = substr( join( '', $days[$date->{'dow'}] ), 0, 3 );
        !           216:        my $indexFile = $indexDir . "/index-" . $sd .
1.10      nick      217:                        "-" . $date->{'year2'} . $date->{'mon2'} .
1.1       nick      218:                          $date->{'day2'} . ".html";
1.8       nick      219:        my $today     = $days[$date->{'dow'}] . " " . $date->{'mon'} . "/" . $date->{'day'} . "/" . $date->{'year'};
1.1       nick      220: 
                    221:        open INDEX, ">$indexFile";
                    222:        print INDEX <<EOF;
                    223: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    224: 
                    225: <html xmlns="http://www.w3.org/1999/xhtml">
                    226: <head>
                    227: <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
                    228:     <title>Daily Comics for $today</title>
                    229:   </head>
                    230: <body bgcolor="#FFFFFF">
                    231: <h1>Daily Comics for $today</h1>
                    232: <table align="center" cellpadding="5" cellspacing="0">
                    233: EOF
                    234:        close (INDEX);
                    235: }
                    236: 
                    237: #######################################################################
                    238: #######################################################################
                    239: sub directDownload ($$) {
                    240:        my ( $comics, $comic, $date ) = @_;
                    241:        my $file = &parseComic ( $comics, $comic, $date );
                    242: 
                    243:         ##
                    244:         ## Save the file to the appropriate directory
                    245:         ##
                    246:         my $cDir  = $date->{'mon2'} . $date->{'year2'};
                    247:         my $cDate = $date->{'day2'};
                    248: 
1.3       nick      249:        my $cmd = "wget -q $file --referer=\"" . $comics->{$comic}{'url'} ."\" --user-agent=\"$USER_AGENT\" -O - | /usr/bin/convert - jpeg:images/$cDir/$comic-$cDate.jpg";
1.1       nick      250:         return system($cmd);
                    251: }
                    252: 
                    253: #######################################################################
                    254: #######################################################################
                    255: sub indexDownload ($$) {
                    256:        my ( $comics, $comic, $date ) = @_;
                    257:        my ( @lines, $comicLine, $mainURL );
                    258:        my $comicIndex = "indexes/index.$comic";
                    259: 
                    260:        `wget -q $comics->{$comic}{'url'} -O $comicIndex`;
                    261: 
                    262:        if ( ! open FILEN, "<$comicIndex" ) {  
                    263:                return "ERROR: Can't open index file for " . $comics->{$comic}{'fullName'} . 
                    264:                       " (" . $comics->{$comic}{'url'} . ")"; 
                    265:        } 
                    266:                @lines = <FILEN>;
                    267:        close (FILEN);  
                    268: 
                    269:        unlink ("$comicIndex");
                    270: 
                    271:        $mainURL = $comics->{$comic}{'url'};
                    272:        ## I need to figure out how to merge these two in to one regex.
                    273:        $mainURL =~ s/(http:\/\/.*)(?:\/.*\/){1,}.*/$1/;
                    274:        $mainURL =~ s/([a-z])\/.*/$1/i;
                    275: 
                    276:        ##
                    277:        ## Find the comic strip URL based on the specified regex in the search
                    278:        ##
                    279:        foreach my $line (@lines) {
                    280:                if ( $line =~ m/$comics->{$comic}{'search'}/ ) {
                    281:                        $comicLine = $1; chomp $comicLine;
                    282:                }
                    283:         }
                    284: 
                    285:        ##
                    286:        ## Save the file to the appropriate directory
                    287:        ##
                    288:        my $cDir    = $date->{'mon2'} . $date->{'year2'};
                    289:        my $cDate   = $date->{'day2'};
                    290: 
                    291:        if ( $comicLine ) {
                    292:                if ( $comicLine =~ m/(gif|jpg|png)/i ) { $comics->{$comic}{'ext'} = $1; }
                    293:                my $comicURL = ( $comicLine =~ m/http/ ) ? $comicLine : $mainURL . $comicLine;
1.3       nick      294:                my $cmd = "wget --user-agent=\"$USER_AGENT\" --referer=\"" . $comics->{$comic}{'url'} . "\" -q $comicURL -O - | /usr/bin/convert - jpeg:images/$cDir/$comic-$cDate.jpg";
1.1       nick      295:                system( $cmd );
                    296:                return 0;
                    297:        }
                    298: 
                    299:        unlink "index.html";
                    300: 
                    301:        return "ERROR: Could not download comic $comics->{$comic}{'fullName'}";
                    302: }
                    303: 
                    304: #######################################################################
                    305: #######################################################################
                    306: sub parseComic ($$) {
                    307:        my ( $comics, $comic, $date ) = @_;
                    308:        my $string = $comics->{$comic}{'search'};
                    309: 
                    310:        $string =~ s/__year__/$date->{'year'}/g;
                    311:        $string =~ s/__year2__/$date->{'year2'}/g;
                    312:        $string =~ s/__mon__/$date->{'mon'}/g;
                    313:        $string =~ s/__mon2__/$date->{'mon2'}/g;
                    314:        $string =~ s/__day__/$date->{'day'}/g;
                    315:        $string =~ s/__day2__/$date->{'day2'}/g;
                    316:        $string =~ s/__ext__/$comics->{$comic}{'ext'}/g;
                    317:        chomp $string;
                    318: 
                    319:        return $string;
                    320: }
                    321: 
                    322: #######################################################################
                    323: #######################################################################
                    324: sub fetchDates () {
                    325:        my %dates = ();
                    326: 
1.8       nick      327:        ($dates{'day'}, $dates{'mon'}, $dates{'year'}, $dates{'dow'}) = (localtime(time - (86400 * $days_ago )))[3,4,5,6];
1.1       nick      328: 
                    329:        $dates{'year'} += 1900;
                    330:        $dates{'year2'} = substr $dates{'year'}, 2, 2;
                    331:        $dates{'day2'}  = ( $dates{'day'} < 10 ) ? "0" . $dates{'day'} : $dates{'day'}; 
                    332:        $dates{'mon'}++;
                    333:        $dates{'mon2'}  = ( $dates{'mon'} < 10 ) ? "0".$dates{'mon'} : $dates{'mon'};
                    334: 
                    335:        return %dates;
                    336: }
1.8       nick      337: 
                    338: ###############################################################################
                    339: ##
                    340: ## &fetchOptions( );
                    341: ##
                    342: ##      Grab our command line arguments and toss them in to a hash
                    343: ##
                    344: ###############################################################################
                    345: sub fetchOptions {
                    346:         my %opts;
                    347: 
                    348:         &GetOptions(
                    349:                         "days:i"        => \$opts{'days'},
                    350:                         "help|?"        => \$opts{'help'},
                    351:                         "man"           => \$opts{'man'},
                    352:                    ) || &pod2usage( );
                    353:         &pod2usage( ) if defined $opts{'help'};
                    354:         &pod2usage( { -verbose => 2, -input => \*DATA } ) if defined $opts{'man'};
                    355: 
                    356:         return %opts;
                    357: }
                    358: 
                    359: __END__
                    360: 
                    361: =head1 NAME
                    362: 
                    363: fetch.pl - Fetches comics and places them all locally in a single html file.
                    364: 
                    365: =head1 SYNOPSIS
                    366: 
                    367: fetch.pl [options]
                    368: 
                    369: Options:
                    370:         --days,d        Fetch comics from X days ago
                    371:         --help,?        Display the basic help menu
                    372:         --man,m         Display the detailed man page
                    373: 
                    374: =head1 DESCRIPTION
                    375: 
                    376: =head1 HISTORY
                    377: 
                    378: =head1 AUTHOR
                    379: 
                    380: Nicholas DeClario <nick@declario.com>
                    381: 
                    382: =head1 BUGS
                    383: 
                    384: This is a work in progress.  Please report all bugs to the author.
                    385: 
                    386: =head1 SEE ALSO
                    387: 
                    388: =head1 COPYRIGHT
                    389: 
                    390: =cut
                    391: 
                    392: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>