Source

dlpodget / dlpodget

The branch 'f/201210-conf' does not exist.
#!/usr/bin/perl -w

package main;
use LWP::Simple qw(get);
use XML::Feed;
use Data::Dumper;
use Daybo::ConfReader;
use strict;
use warnings;
use diagnostics;

sub FileFromURI($)
{
	my $Url = shift;
	my @uri_parts = split('/', $Url);
	return $uri_parts[-1];
}

sub ReadStream($)
{
	my $Url = shift;

	my $feed = XML::Feed->parse(URI->new($Url));

	foreach my $podcast ($feed->entries) {
		my $data;
		my $filename = FileFromURI($podcast->link);
		my $local_podcast = LOCAL_STORE_PATH() . '/' . $filename;
		if ( -f $local_podcast ) {
			printf(STDERR "%s already exists\n", $local_podcast);
			next;
		}
		printf(STDERR "Downloading %s ... ", $podcast->link);
		$data = get($podcast->link);
		print(STDERR "done.\n");

		printf(STDERR "\nWriting %s ... ", $local_podcast);
		if ( open(my $f, '>', $local_podcast) ) {
			binmode($f);
			print($f $data);
			close($f);
			print(STDERR "done.\n");
		} else {
			printf(STDERR "Failed -- %s\n", $!);
		}
	}

}

sub main()
{
	my @defKeys;
	my $conf = Daybo::ConfReader->new();
	my $conffile = "$ENV{HOME}/.dlpodget.rc";
	if ( -f $conffile ) {
		if ( $conf->SetFn($conffile) ) {
			$conf->Reload();
		}
	} else {
		$conf = undef;
	}

	if ( $conf && $conf->GetKeys(undef, \@defKeys) ) {
		if ( 'enable' ~~ @defKeys ) {
			if ( !$conf->GetDatum(undef, 'enable') ) {
				print(STDERR "Configuration disabled.\n");
				$conf = undef;
			}
		}
	}
	if ( $conf ) {
		my ( @sections, $secC );
		$secC = $conf->GetSections(\@sections);
		for ( my $secI = 0; $secI < $secC; $secI++ ) {
			my @secKeys;
			my $keyC = $conf->GetKeys($sections[$secI], \@secKeys);
			foreach ( my $keyI = 0; $keyI < $keyC; $keyI++ ) {
				printf(
					'[%s] %s -> %s'."\n",
					$sections[$secI],
					$secKeys[$keyI],
					$conf->GetDatum($sections[$secI], $secKeys[$keyI])
				);
			}
		}
	}

	#ReadStream(URL());
	return 0;
}

exit(main());