#!/usr/bin/perl

use strict;
use warnings;

system('rm -f /tmp/cont /tmp/cont_proc; wget -q -O /tmp/cont https://www.ardaudiothek.de/sendung/11km-der-tagesschau-podcast/12200383/index.html');
system(' sed -e \'s,<,\n<,g\' -e \'s/"title/\n"title,/g\' -e \'s,"summary,\n"summary,g\' -e \'s,"download,\n"download,g\' /tmp/cont |grep -B19 downloadUrl|egrep \'summary|downloadUrl\'|grep -v publicationService >/tmp/cont_proc ');

my $header=<<"END_HEADER";
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
  <title>11KM: der tagesschau-Podcast</title>
  <link>https://www.ardaudiothek.de/sendung/11km-der-tagesschau-podcast/12200383/</link>
  <description>My feed of contents..</description>
END_HEADER
my $footer=<<"END_FOOTER";
</channel>
</rss>
END_FOOTER

my $title;
my $desc;

print $header;

my $filename = '/tmp/cont_proc';
open(my $fh, '<', $filename)
	or die "Could not open file '$filename' $!";

foreach ( <$fh> ) {
	chomp;

	# print "full: $_\n";

	if ( s/.*summary":"// ) {
		$desc=$_;
		$desc=~s/".*//;
		next;
	};
	if ( s/.*downloadUrl":"// ) {
		$title=$_;
		$title=~s/".*//;
	};

	print "  <item>\n";
	print "    <title>$title</title>\n";
	print "    <link>$title</link>\n";
	print "    <description>$desc</description>\n";
	print "  </item>\n";
} 

print $footer;
