#!/usr/bin/perl
# Time-stamp: "2021-02-23 22:54:40 MST"
# See http://www.interglacial.com/rss/ for info on RSS feeds

use strict;
use constant DEBUG => $ENV{'CRON'} ? 0 : 15;
  # to test whether we're under cron

#BEGIN { DEBUG and *XML::RSS::SimpleGen::DEBUG = sub {1}; }
$| = 1;
use XML::RSS::SimpleGen 12;

my $me = 'bob_the_angry_flower';

my $url =  "https://angryflower.com/";
rss_new( $url, "Bob the Angry Flower", );
rss_daily();
rss_item_limit( 200 );
rss_self_url(      "https://interglacial.com/rss/$me.rss" );
rss_generator_url( "https://interglacial.com/rss/$me.pl"  );
rss_history_file(  "$me.dat" );
rss_livejournal( 'bobangryflower' );

get_url($url);

use URI;

my $count = 0;
while(
   m@
     (?:
        #QQ
        <a \s+ href=\"
           ( [^\"/:]+\.(?:html?|gif|png|jpe?g ))
           \".*?
        >
          (.*?)
        </a>
      )
    | 
     (?:
        #QQ
        <a \s+  href="( \d{4,5}\.html )"
         (?: \s* target="_blank")?
        >
        \s*
        <img \s+
           src="
             (
                [^\"\[\]\<\>]{1,80}
                 \.(?: gif | jpg | jpeg | png )
             )
           "
        \s+
        alt=\s*
        \"
          ( [^\"<>\cm\cj\t]{1,90} )
        \"
      )
    @igx
) {

=for
  <a href="1385.html" target="_blank"><img src="1trillion1.gif" alt=
  "$1 Trillion in Pennies" border="0"></a>

=cut

  $count++;  
  my($bare_url, $title,    $numeric_href, $image_url, $title_2) = map  {; $_ // "" }  ($1,$2,$3,$4,$5);
  if( $title =~ m/^<img/i ) {
    $title = $bare_url;
    $title =~ s/\.\w+//s;
  }
  if( DEBUG ) {
    print "* #$count: 1($bare_url) => 2($title)\n\t3($numeric_href) => 4($image_url) => 5($title_2)\n";
    print "* #$count: bare_url($bare_url) => title($title)\n\tnumeric_href($numeric_href) => image_url($image_url) => title_2($title_2)\n";
  }

  foreach ($bare_url, $numeric_href, $image_url ) {
    next unless $_;
    next if m/^http/;
    $_ = "$url$_";
    DEBUG && print "\t\t>> $_\n";
  }

  if( $bare_url ) {
    DEBUG > 10 and print "\t\t~~ bare_url{$bare_url} => title{$title}\n";
    rss_item( $bare_url, $title );
    
  } elsif( $numeric_href ) {
    DEBUG > 10 and print "\t\t%% numeric_href{$numeric_href} => title_2{$title_2}\n";  
    rss_item(
      $numeric_href,
      #$image_url,
      $title_2
    );
  } else {
    DEBUG > 10 and print "\t\t^^ Nothing applies.\n";
  }

  print "\n";
}

rss_item_count or die "What, no items in $url ?";
if( rss_save("$me.rss", 20) ) {
  DEBUG and print "Updated\n";
} else {
  DEBUG and print "Didn't need updating.\n";
}

__END__
