bin-utils/opml2feed.pl
2015-05-30 18:42:42 +02:00

82 lines
2.8 KiB
Perl
Executable File

#!/usr/bin/perl -w
# ########################################################################
# This program converts the the output from a OPML file to the format
# used by feed2imap. You could for example export a opml file from
# Google reader and convert it to a .feedrc file.
#
# #########################################################################
# This file is part of opml2feed
#
# opml2feed is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# opml2feed is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with opml2feed. If not, see <http://www.gnu.org/licenses/>.
# http://www.gnu.org/licenses/agpl-3.0.html
# #########################################################################
use strict;
use XML::Twig;
use Data::Dumper;
use URI::Escape;
binmode STDOUT, ":utf8";
my $imap_username = 'rss@example.com';
my $imap_password = 'myultracoolpassword';
my $imap_server = 'imap.example.com';
my $imap_base = 'INBOX.Feeds';
#my $maildir_base = '/home/meutel/.mail/feeds';
my $maildir_base = '/home/meutel/tmp/feeds';
# You shouldn't need to change anything after here - If you do please contribute back.
$imap_username = uri_escape_utf8( $imap_username );
$imap_password = uri_escape_utf8( $imap_password );
my $title_parent = "";
my $opmlfile= $ARGV[0] || 'google-reader-subscriptions.xml';
my $feed2imapfile= $ARGV[1] || '.feed2imaprc';
die "ERROR: $opmlfile is missing" unless -e $opmlfile;
die "ERROR: $feed2imapfile already exists" if -e $feed2imapfile;
open FH, ">>:utf8", $feed2imapfile or die "can't open '$feed2imapfile': $!";
print FH "feeds:\n";
my $twig= new XML::Twig(
twig_handlers =>
{ outline => \&outline }
);
$twig->parsefile( $opmlfile);
close FH;
sub outline
{ my( $twig, $outline)= @_;
$title_parent = $outline->parent->att( 'text') || "#" ;
if ( $title_parent !~ /^\#/ )
{
my $title = $outline->att( 'text');
$title =~ s/[^a-zA-Z0-9_ .]*//g;
$title =~ s/ {1,}/ /g;
$title_parent =~ s/[^a-zA-Z0-9_ .]*//g;
$title_parent =~ s/ /_/g;
my $xmlUrl = $outline->att( 'xmlUrl');
print FH " - name: ${title}\n";
print FH " url: ${xmlUrl}\n";
print FH " target: maildir://${maildir_base}/${title_parent}\n";
#print FH " target: imap://${imap_username}:${imap_password}\@${imap_server}/${imap_base}.${title_parent}\n";
print FH " include-images: true\n\n";
}
}