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

1.1       nick        1: #!/usr/bin/perl -w
                      2: 
                      3: use strict;
                      4: use File::Path;
                      5: use Data::Dumper;
                      6: 
                      7: ## 
                      8: ## Some default values
                      9: ##
1.2     ! nick       10: my $ver                = q/$Id: fetch.pl.new,v 1.1.1.1 2011-08-18 12:39:12 nick Exp $/;
1.1       nick       11: my $comicFile   = "comics.conf";
                     12: my %comics     = &readComicConfig ( $comicFile );
                     13: my %dates      = &fetchDates();
                     14: my $baseDir     = $comics{'configs'}{'base_directory'} || ".";
                     15: my $imageDir    = $baseDir . "/" . ( $comics{'configs'}{'image_directory'} || "images" ) . 
                     16:                  "/$dates{'mon2'}$dates{'year2'}";
                     17: my $indexDir    = $baseDir . "/" . ( $comics{'configs'}{'index_directory'} || "indexes" );
1.2     ! nick       18: 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.1       nick       19: 
                     20: 
                     21: my $DATE=`date`; chomp $DATE;
                     22: print STDOUT "Starting comic fetch at $DATE\n";
                     23: 
                     24: ##
                     25: ## Main program starts here
                     26: ##
                     27: &checkDir ( [ $imageDir, $indexDir ] );
                     28: 
                     29: &writeTitle ( \%dates );
                     30: 
                     31: foreach my $comic ( sort keys %comics ) {
                     32:   next if ( $comic =~ m/config/ );
                     33:   $comics{$comic}{'error'} = &downloadComic ( \%comics, $comic, \%dates );
                     34:   &writeComic ( \%comics, $comic, \%dates );
                     35: }
                     36: 
                     37: ## &writeMainIndex ( \%dates );
                     38: 
                     39: &writeFooter( \%dates );
                     40: 
                     41: $DATE=`date`;  chomp( $DATE );
                     42: print STDOUT "Completed comic fetch at $DATE\n";
                     43: 
                     44: ## End
                     45: 
                     46: #######################################################################
                     47: ## Function :  downloadComic
                     48: ##
                     49: ##   Description :
                     50: ##     This function determines the download method being used to 
                     51: ##      retrieve the comic and calls the apprioriate function.
                     52: ##
                     53: ##      If the mode is invalid an error will be returned.
                     54: ##
                     55: #######################################################################
                     56: sub downloadComic ($$) {
                     57:        my ( $comics, $comic, $date ) = @_;
                     58: 
                     59:        SWITCH: {
                     60:                if ( $comics->{$comic}{'mode'} eq 1 ) { 
                     61:                        return indexDownload ( \%comics, $comic, $date );
                     62:                        last SWITCH;
                     63:                }
                     64:                if ( $comics->{$comic}{'mode'} eq 2 ) { 
                     65:                        return directDownload ( \%comics, $comic, $date );
                     66:                        last SWITCH;
                     67:                }
                     68:        }
                     69:         
                     70:        return "ERROR: Unknown download method specified for $comics->{$comic}{'fullName'}.";
                     71: }
                     72: 
                     73: #######################################################################
                     74: #######################################################################
                     75: sub readComicConfig ($$) {
                     76:        my ( $comicFile ) = @_;
                     77:        my %comicConfig   = ( );
                     78:        my %config        = ( );
                     79: 
                     80:        open FILEN, "<$comicFile";
                     81:                while (<FILEN>) {
                     82:                        if ( ( $_ !~ m/^#/ ) && ( $_ =~ m/,.*,/) ){
                     83:                                my @res = split /,/, $_;
                     84:                                $comicConfig{$res[0]}{'url'}      = $res[1];
                     85:                                $comicConfig{$res[0]}{'search'}   = $res[2];
                     86:                                $comicConfig{$res[0]}{'mode'}     = $res[3];
                     87:                                $comicConfig{$res[0]}{'fullName'} = $res[4];
                     88:                                $comicConfig{$res[0]}{'ext'}      = $res[5];
                     89:                                $comicConfig{$res[0]}{'error'}    = 0;
                     90:                        }
                     91:                        elsif ( $_ =~ m/(.*)\s+=\s+(.*)/ ) {
                     92:                                $comicConfig{'configs'}{$1} = $2;
                     93:                        }
                     94:                }
                     95:        close (FILEN);
                     96: 
                     97:        return %comicConfig;
                     98: }
                     99: 
                    100: #######################################################################
                    101: #######################################################################
                    102: sub writeComic ($$) {
                    103:        my ( $comics, $comic, $date ) = @_;
                    104:        my $indexFile = $indexDir . "/index-" . $date->{'year2'} . $date->{'mon2'} .
                    105:                         $date->{'day2'} . ".html";
                    106:        my $content = <<EOF;
                    107: 
                    108: <!-- ********* Begin $comic ($comics->{$comic}{'fullName'}) ******* -->
                    109:   <tr>
                    110:     <td align="left">
                    111: <font color="blue"><b>$comics->{$comic}{'fullName'}</b></font> &nbsp; &nbsp; 
                    112: <font size="-2">
                    113:        <a href="$comics->{$comic}{'url'}">
                    114:                $comics->{$comic}{'url'}
                    115:        </a>
                    116: </font><br/>
                    117: <img src="../images/$date->{'mon2'}$date->{'year2'}/$comic-$date->{'day2'}.jpg" alt="$comic-$date->{'day2'}" />
                    118: <br/><br/>
                    119: </td></tr>
                    120: <!-- ********* Finish $comic ($comics->{$comic}{'fullName'}) ******* -->
                    121: 
                    122: EOF
                    123:        open INDEX, ">>$indexFile";
                    124: 
                    125:        print INDEX $content if ( ! $comics->{$comic}{'error'} );
                    126: 
                    127:        print INDEX <<EOF
                    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: <font color="red"><b>$comic :  $comics->{$comic}{'error'}</b></font><br/>
                    135:   </td>
                    136: </tr>
                    137: EOF
                    138:                if ( $comics->{$comic}{'error'} );
                    139: 
                    140:        close (INDEX);
                    141: 
                    142:        return 0;
                    143: }
                    144: 
                    145: 
                    146: #######################################################################
                    147: #######################################################################
                    148: sub writeMainIndex ($$) {
                    149:        my ( $date ) = @_;
                    150: 
                    151: }
                    152: 
                    153: 
                    154: #######################################################################
                    155: #######################################################################
                    156: sub writeFooter {
                    157:        my ( $date ) = @_;
                    158:        my $indexFile = $indexDir . "/index-" . $date->{'year2'} . $date->{'mon2'} .
                    159:                         $date->{'day2'} . ".html";
                    160:        my $sysDate = `date`;
                    161: 
                    162:        open INDEX, ">>$indexFile";
                    163:        print INDEX <<EOF;
                    164: </table>
1.2     ! nick      165: <center>Generated at $sysDate</br>
        !           166: Version: $ver
1.1       nick      167:   <p>
                    168:     <a href="http://validator.w3.org/check?uri=referer"><img
                    169:       src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" border="0" /></a>
                    170:   </p>
                    171: </center>
                    172: 
                    173: </body>
                    174: </html>
                    175: EOF
                    176:        close( INDEX );
                    177: }
                    178: 
                    179: #######################################################################
                    180: #######################################################################
                    181: sub checkDir ($$) {
                    182:        my @dir = @_;
                    183: 
                    184:        foreach ( @dir ) {
                    185:                if ( ! -d $_ ) { mkpath( $_ ); }
                    186:        }
                    187: }
                    188: 
                    189: #######################################################################
                    190: #######################################################################
                    191: sub writeTitle ($$) {
                    192:        my ( $date ) = @_;
                    193:        my $indexFile = $indexDir . "/index-" . $date->{'year2'} . $date->{'mon2'} .
                    194:                          $date->{'day2'} . ".html";
                    195:        my $today     = $date->{'mon'} . "/" . $date->{'day'} . "/" . $date->{'year'};
                    196: 
                    197:        open INDEX, ">$indexFile";
                    198:        print INDEX <<EOF;
                    199: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    200: 
                    201: <html xmlns="http://www.w3.org/1999/xhtml">
                    202: <head>
                    203: <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
                    204:     <title>Daily Comics for $today</title>
                    205:   </head>
                    206: <body bgcolor="#FFFFFF">
                    207: <h1>Daily Comics for $today</h1>
                    208: <table align="center" cellpadding="5" cellspacing="0">
                    209: EOF
                    210:        close (INDEX);
                    211: }
                    212: 
                    213: #######################################################################
                    214: #######################################################################
                    215: sub directDownload ($$) {
                    216:        my ( $comics, $comic, $date ) = @_;
                    217:        my $file = &parseComic ( $comics, $comic, $date );
                    218: 
                    219:         ##
                    220:         ## Save the file to the appropriate directory
                    221:         ##
                    222:         my $cDir  = $date->{'mon2'} . $date->{'year2'};
                    223:         my $cDate = $date->{'day2'};
                    224: 
                    225:        my $cmd = "wget -q $file --referer=\"" . $comics->{$comic}{'url'} ."\" --user-agent=\"$USER_AGENT\" -O - | /usr/bin/convert -resize 640 - jpeg:images/$cDir/$comic-$cDate.jpg";
                    226:         return system($cmd);
                    227: }
                    228: 
                    229: #######################################################################
                    230: #######################################################################
                    231: sub indexDownload ($$) {
                    232:        my ( $comics, $comic, $date ) = @_;
                    233:        my ( @lines, $comicLine, $mainURL );
                    234:        my $comicIndex = "indexes/index.$comic";
                    235: 
                    236:        `wget -q $comics->{$comic}{'url'} -O $comicIndex`;
                    237: 
                    238:        if ( ! open FILEN, "<$comicIndex" ) {  
                    239:                return "ERROR: Can't open index file for " . $comics->{$comic}{'fullName'} . 
                    240:                       " (" . $comics->{$comic}{'url'} . ")"; 
                    241:        } 
                    242:                @lines = <FILEN>;
                    243:        close (FILEN);  
                    244: 
                    245:        unlink ("$comicIndex");
                    246: 
                    247:        $mainURL = $comics->{$comic}{'url'};
                    248:        ## I need to figure out how to merge these two in to one regex.
                    249:        $mainURL =~ s/(http:\/\/.*)(?:\/.*\/){1,}.*/$1/;
                    250:        $mainURL =~ s/([a-z])\/.*/$1/i;
                    251: 
                    252:        ##
                    253:        ## Find the comic strip URL based on the specified regex in the search
                    254:        ##
                    255:        foreach my $line (@lines) {
                    256:                if ( $line =~ m/$comics->{$comic}{'search'}/ ) {
                    257:                        $comicLine = $1; chomp $comicLine;
                    258:                }
                    259:         }
                    260: 
                    261:        ##
                    262:        ## Save the file to the appropriate directory
                    263:        ##
                    264:        my $cDir    = $date->{'mon2'} . $date->{'year2'};
                    265:        my $cDate   = $date->{'day2'};
                    266: 
                    267:        if ( $comicLine ) {
                    268:                if ( $comicLine =~ m/(gif|jpg|png)/i ) { $comics->{$comic}{'ext'} = $1; }
                    269:                my $comicURL = ( $comicLine =~ m/http/ ) ? $comicLine : $mainURL . $comicLine;
                    270:                my $cmd = "wget --user-agent=\"$USER_AGENT\" --referer=\"" . $comics->{$comic}{'url'} . "\" -q $comicURL -O - | /usr/bin/convert -resize 640 - jpeg:images/$cDir/$comic-$cDate.jpg";
                    271:                system( $cmd );
                    272:                return 0;
                    273:        }
                    274: 
                    275:        unlink "index.html";
                    276: 
                    277:        return "ERROR: Could not download comic $comics->{$comic}{'fullName'}";
                    278: }
                    279: 
                    280: #######################################################################
                    281: #######################################################################
                    282: sub parseComic ($$) {
                    283:        my ( $comics, $comic, $date ) = @_;
                    284:        my $string = $comics->{$comic}{'search'};
                    285: 
                    286:        $string =~ s/__year__/$date->{'year'}/g;
                    287:        $string =~ s/__year2__/$date->{'year2'}/g;
                    288:        $string =~ s/__mon__/$date->{'mon'}/g;
                    289:        $string =~ s/__mon2__/$date->{'mon2'}/g;
                    290:        $string =~ s/__day__/$date->{'day'}/g;
                    291:        $string =~ s/__day2__/$date->{'day2'}/g;
                    292:        $string =~ s/__ext__/$comics->{$comic}{'ext'}/g;
                    293:        chomp $string;
                    294: 
                    295:        return $string;
                    296: }
                    297: 
                    298: #######################################################################
                    299: #######################################################################
                    300: sub fetchDates () {
                    301:        my %dates = ();
                    302: 
                    303:        ($dates{'day'}, $dates{'mon'}, $dates{'year'}, $dates{'dow'}) = (localtime)[3,4,5,6];
                    304: 
                    305:        ## If you missed a day or two, reflect it here:
                    306:        # $dates{'day'}-=1;  ## <-- 5 days ago
                    307: 
                    308:        $dates{'year'} += 1900;
                    309:        $dates{'year2'} = substr $dates{'year'}, 2, 2;
                    310:        $dates{'day2'}  = ( $dates{'day'} < 10 ) ? "0" . $dates{'day'} : $dates{'day'}; 
                    311:        $dates{'mon'}++;
                    312:        $dates{'mon2'}  = ( $dates{'mon'} < 10 ) ? "0".$dates{'mon'} : $dates{'mon'};
                    313: 
                    314:        return %dates;
                    315: }

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