M6 KVM avatar M6 KVM committed b777a25 Merge

Add configuration file for user-defined streams (not just Infowars)
Branch merge: f/201210-conf

Comments (0)

Files changed (2)

 use LWP::Simple qw(get);
 use XML::Feed;
 use Data::Dumper;
+use Daybo::ConfReader;
 use Getopt::Std;
 use strict;
 use warnings;
 use diagnostics;
 
-use constant URL              => 'http://xml.nfowars.net/Alex.rss';
-use constant LOCAL_STORE_PATH => '/home/overlord/infowars';
-
 my $Debug = 0;
 
 sub FileFromURI($)
 	return $uri_parts[-1];
 }
 
-sub ReadStream($)
+sub ReadStream($$$)
 {
 	my @entries = ( );
-	my $Url = shift;
+	my ( $Feeds, $Url, $Name ) = @_;
 	my $feed = XML::Feed->parse(URI->new($Url));
 	foreach my $entry ($feed->entries) {
 		my $filename = FileFromURI($entry->link);
 	return @entries;
 }
 
-sub DownloadStream($)
+sub DownloadStream($$$)
 {
 	my $data;
-	my $entry = shift;
-	my $local_podcast = LOCAL_STORE_PATH() . '/' . $entry->{filename};
+	my ( $Feeds, $entry, $Name ) = @_;
+	my $local_podcast = $Feeds->{$Name}->{localpath} . '/' . $entry->{filename};
 	if ( -f $local_podcast ) {
 		printf(STDERR "%s already exists\n", $local_podcast);
 		return;
 	$data = get($entry->{uri});
 	print(STDERR "done.\n");
 
+	if ( $Feeds->{_main}->{noop} ) {
+		printf(STDERR "\nSkipping write of %s (DRY-RUN).\n", $local_podcast);
+		return;
+	}
+
 	printf(STDERR "\nWriting %s ... ", $local_podcast);
 	if ( open(my $f, '>', $local_podcast) ) {
 		binmode($f);
 	}
 }
 
+sub ProcessTags($$)
+{
+	my ( $Feeds, $V ) = @_;
+	foreach my $c ( keys(%{ $Feeds->{_main} }) ) {
+		my $ucc = '$' . uc($c);
+		while ( my $idx = index($V, $ucc) > -1 ) {
+			substr($V, $idx-1, length($ucc), $Feeds->{_main}->{$c});
+		}
+	}
+	return $V;
+}
+
 sub main()
 {
+	my $conf;
 	my @entries = ( );
+	my @defKeys;
+	my %feeds = ( );
 	my %opts = ( );
+	my $confSeen = 0;
+	my @confFiles = (
+		'dlpodget.rc',
+		"$ENV{HOME}/.dlpodget.rc"
+	);
+
 	return 1 if ( !getopts('d', \%opts) );
 	$Debug = 1 if ( $opts{'d'} );
 	print(STDERR "Explicit debug mode enabled by -d\n") if ( $Debug );
-	@entries = ReadStream(URL());
-	foreach my $entry ( @entries ) {
-		print STDERR 'Processing entry: ' . Dumper $entry if ( $Debug );
-		DownloadStream($entry);
+
+	$conf = Daybo::ConfReader->new();
+	foreach my $confFile ( @confFiles ) {
+		next unless ( -f $confFile );
+		if ( $conf->SetFn($confFile) ) {
+			$conf->Reload();
+			$confSeen++;
+			last;
+		}
 	}
+	$conf = undef if ( !$confSeen );
+
+	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);
+
+		# First load the generation configuration information
+		$feeds{_main} = { %ENV }; # Include environment variables.
+		if ( scalar(@defKeys) ) {
+			foreach my $mk ( @defKeys ) {
+				$feeds{_main}->{$mk} = $conf->GetDatum(undef, $mk);
+			}
+			# Set debug flag via config if it existed.
+			if ( $feeds{_main}->{debug} ) {
+				$Debug = $feeds{_main}->{debug};
+				printf(STDERR "Set Debug via config: %s\n", $Debug);
+			}
+		}
+
+		for ( my $secI = 0; $secI < $secC; $secI++ ) {
+			my @secKeys;
+			my $keyC = $conf->GetKeys($sections[$secI], \@secKeys);
+			foreach ( my $keyI = 0; $keyI < $keyC; $keyI++ ) {
+				next if ( $sections[$secI] eq '_main' ); # Skip reserved section
+				my $v = $conf->GetDatum($sections[$secI], $secKeys[$keyI]);
+				$v = ProcessTags(\%feeds, $v);
+				printf(
+					STDERR
+					'[%s] %s -> %s'."\n",
+					$sections[$secI],
+					$secKeys[$keyI],
+					$v
+				) if ( $Debug );
+				$feeds{ $sections[$secI] }->{ $secKeys[$keyI] } = $v;
+			}
+		}
+	}
+
+	foreach my $feedName ( keys(%feeds) ) {
+		my $feed = $feeds{$feedName};
+		next if ( !$feed->{enable} );
+		next if ( !$feed->{download} );
+		@entries = ReadStream(\%feeds, $feed->{rss}, $feedName);
+		foreach my $entry ( @entries ) {
+			print STDERR 'Processing entry: ' . Dumper $entry if ( $Debug );
+			DownloadStream(\%feeds, $entry, $feedName);
+		}
+	}
+
 	return 0;
 }
 
+;
+; This configuration file holds the RSS feeds for the dlpodget application
+; the section is the nominal tag or name you want to assign the download,
+; the supported keys are:
+;
+;   localpath = <path to a directory whuich holds the downloads>
+;   rss       = <URI to RSS source>
+;   check     = <1|0>, default 1, whether to automatically check for new files on each run
+;   download  = <1|0>, default 1, whether to automatically download new files
+;   enable    = <1|0>, default 1, set to 0 to disable the section, or comment it out.
+;
+; More generally, we have a default section, which contains the following options:
+;
+;   enable    = <1|0>, default 1, set to 0 for a convenient way to ignore the entire file.
+;   localpfx  = <prefix which will be exported to sections as $LOCALPFX>
+;   noop      = <1|0>, default 0, set to 1 to disable modification of local files.
+
+enable    = 1
+localpfx  = $HOME/podcasts
+noop      = 0
+debug     = 0
+
+[infowars]
+localpath = $LOCALPFX/infowars
+rss       = http://xml.nfowars.net/Alex.rss
+check     = 1
+download  = 1
+enable    = 1
+
+[bsec]
+localpath = $LOCALPFX/bsec
+rss       = http://feeds.feedburner.com/BradleyStokeEvangelicalChurchSermons?format=xml
+check     = 1
+download  = 0
+enable    = 1
+
+[bbcaq]
+localpath = $LOCALPFX/bbcaq
+rss       = http://downloads.bbc.co.uk/podcasts/radio4/bbcaq/rss.xml
+check     = 1
+download  = 1
+enable    = 1
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.