#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp;
use JSON;
use XML::OPML::SimpleGen;
use Data::Dumper;
print "$ARGV[0] -> $ARGV[1]\n";
my $json = read_file($ARGV[0]);
my $data = decode_json($json);
my $opml = new XML::OPML::SimpleGen();
$opml->head(title => 'Popular news feed sources of the countries');
for my $cc (keys %$data) {
	my $country = $data->{$cc}->{name};
	my $feeds = $data->{$cc}->{sources};
	$opml->add_group(title => $country, text => $country, id => $cc);
	for my $feed (@$feeds) {
		print "$country $feed->{name} $feed->{feedlink}\n";
		$opml->insert_outline(
			group => $country,
			title => $feed->{name},
			text => $feed->{name},
			xmlUrl => $feed->{feedlink},
			htmlUrl => $feed->{url},
		);
	}
}
unlink $ARGV[1];
$opml->save($ARGV[1]);
