lav-outils/podcasts/scripts/feed2lav.pl

113 lines
4.5 KiB
Perl
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#! /usr/bin/perl -CAS
# Gérald Sédrati-Dinet
# Frédéric Couchet
# License : WTFPL
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
use 5.014;
use utf8;
use open qw(:std :encoding(UTF-8));
use LWP::Simple;
use XML::RSS;
use DateTime;
use DateTime::Format::DateParse;
# Configuration variables
my $url = "https://www.april.org/taxonomy_all_items/term/1917/all/feed";
my $filename = "/usr/share/drupal6/lav.xml";
my $linkurl = "https://april.org/libre-a-vous";
my $feedurl = "https://april.org/lav.xml";
# build RSS object
my $rss = XML::RSS->new;
# Fetch Drupal feed
my $in_file = get($url);
die "Couldn't get $url\n!" unless defined $in_file;
# Parse RSS
open(my $fh, '>:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!";
$rss->parse($in_file);
# Add description
$rss->{channel}->{description} = "<p>Chaque mardi de 15 h 30 à 17 h 00, l'April, l'association de promotion et de défense du logiciel libre, propose une émission de radio d'explications et d'échanges concernant les dossiers politiques et juridiques que l'association traite et les actions qu'elle mène : <a href=\"https://april.org/libre-a-vous\">Libre à vous !</a>. Une partie de l'émission est également consacrée aux actualités et actions de type sensibilisation. L'émission est diffusée sur radio Cause Commune sur la bande FM en Île-de-France (93.1) et <a href=\"https://cause-commune.fm\">sur le site web de la radio</a>.</p><p>Donner à chacun et chacune, de manière simple et accessible les clefs pour comprendre les enjeux mais aussi proposer des moyens d'action, tel est l'objectif de l'émission.</p>";
# Add image
$rss->image(title=>'Libre à vous !', url=>'https://april.org/files/libre-a-vous/banniere-libre-a-vous-flux-rss-1400x1400.png', link=>$linkurl);
$rss->{channel}->{link} = $linkurl;
# Parse items
my @new_items;
foreach my $item (@{$rss->{'items'}}) {
# Add mp3 enclosure
my $ogg_enclosure = $item->{enclosure};
if ($ogg_enclosure and ($ogg_enclosure->{type} eq 'audio/ogg' or $ogg_enclosure->{type} eq 'application/ogg')) {
my $mp3_enclosure;
$mp3_enclosure->{type} = 'audio/mpeg';
$mp3_enclosure->{length} = $ogg_enclosure->{length};
($mp3_enclosure->{url} = $ogg_enclosure->{url}) =~ s/\.ogg$/.mp3/;
$item->{enclosure} = [$ogg_enclosure, $mp3_enclosure];
}
# Add items for each topic
if ($item->{description} =~ /Podcasts des différents sujets abordés(?:<\/strong><\/p>|<\/h2>)?\s*<ul>(.+?)<\/ul>/s) {
# Extract topics
my $topics = $1;
# Add current item to items list so new item for each topic goes after this one
push @new_items, $item;
# Parse each topic
while ($topics =~ /<li><a href="([^"]+)">([^>]+)<\/a>/g) {
# Extract ogg url for enclosures
my ($ogg_url, $title) = ($1, $2);
(my $mp3_url = $ogg_url) =~ s/\.ogg$/.mp3/;
# Extract date for description
my $date = DateTime::Format::DateParse->parse_datetime($item->{pubDate})->set_locale('fr')->strftime("%A %d %B %Y");
# Build new item for this topic
my %new_item = %$item;
$new_item{title} = $title;
$new_item{description} = <<"__HTML__";
<p><a href="https://april.org/libre-a-vous"><em>Libre à vous !</em></a>, l'émission pour comprendre et agir avec l'April, chaque mardi de 15h30 à 17h sur la radio <em>Cause commune</em> (93.1 FM en Île-de-France et sur <a href="https://causecommune.fm">Internet</a>.</p>
<p><em>Émission « Libre à vous ! »</em> diffusée $date, partie « $title »</p>
__HTML__
$new_item{category} = 'Podcast Libre à vous !';
$new_item{enclosure} = [{length => 0, type => 'audio/ogg', url => $ogg_url}, {length => 0, type => 'audio/mpeg', url => $mp3_url}];
$new_item{guid} = $item->{guid} . "- $title";
# Add new item in items list
push @new_items, \%new_item;
}
} else {
# Add current item to items list
push @new_items, $item;
}
}
# Replace list of items
$rss->{items} = \@new_items;
# Output RSS
my $rsscontent = $rss->as_string;
$rsscontent =~s#https://www.april.org/taxonomy_all_items/term/1917/all#$feedurl#g;
print $fh $rsscontent;
close $fh;