Here's another new feature for podcast support. It allows you to 
automatically download the podcast's artwork and add it to the 
podcast's files.

Due to limitations in the current implementation you can't add
different pieces of artwork during one run of gnupod_addsong. So you 
can't add different artwork for different podcasts in one go yet. 
But I guess that most people run gnupod_addsong in a script anyway.
( I would like a hand here from somebody who understands the artwork 
related code in gnupod.)


To cleanly apply you'll first need the patch as attached to this 
email: fix_a156511d_Change_size_tolerance_for_podcast_cache.patch


The patch for podcast-artwork itself is attached as
feat_1ace2709..54586471_new_podcast-artwork_option.patch


cheers
-henrik

PS: You've got to love "git" it can generate and process patches in
emails (I'm not using this yet) and even export changes you've made 
to your local repository, back to the cvs server where you imported from. 


PPS: here are the commit messages for the podcast-artwork feature patch
for the curious ones:
...................
commit 54586471b4b2deb9f924ecee5b8b48b33fc666b0
Author: Heinrich Langos <[EMAIL PROTECTED]>
Date:   Tue Apr 15 09:50:47 2008 +0200

    Mentioned "--podcast-artwork" feature in help text.

commit a5e5516981b4eaa08e1cbf008c8801d0c3965feb
Author: Heinrich Langos <[EMAIL PROTECTED]>
Date:   Tue Apr 15 03:49:16 2008 +0200

    Source code formating change only.

commit 6977c451f8699da5689fbd4bf73aab2a20cea2f2
Author: Heinrich Langos <[EMAIL PROTECTED]>
Date:   Tue Apr 15 03:42:22 2008 +0200

    Added "--podcast-artwork" option to download and add artwork to podcasts.

    It will try to get the picture referenced by the channel element
    "itunes:image" or "image".

    I had to make the "DropUnseen" option that removes unused artwork from
    the artwork database depend on the non-usage of "--podcast-artwork" because
    otherwise every cover but the last would get deleted.

    Detail: The information about artwork usage is read during parsing
    of GNUtunesDB.xml meaning that at this point the artwork database has
    to be loaded. However you can only add artwork BEFORE loading the
    database. This works well on the commandline where you already have
    the artwork in a file. For podcasts you have to work with data from the
    GNUtunesDB.xml before you even download the artwork. Impact on normal
    users should me minimal.

commit 37989ae79f8881c1b75ba7f13b6bfeac7249b4be
Author: Heinrich Langos <[EMAIL PROTECTED]>
Date:   Tue Apr 15 01:38:54 2008 +0200

    Moved adding of artwork to a sub.
....................



commit a156511de4a1c8588574c045323c5cf237a850e5
Author: Heinrich Langos <[EMAIL PROTECTED]>
Date:   Mon Apr 14 23:03:24 2008 +0200

    Changed size tolerance for podcast cache usage to +/-5 percent.

diff --git a/src/gnupod_addsong.pl b/src/gnupod_addsong.pl
index 3ffec29..492e5b9 100644
--- a/src/gnupod_addsong.pl
+++ b/src/gnupod_addsong.pl
@@ -399,15 +399,15 @@ sub PODCAST_fetch_media {
 		push @cachefilecandidates, $flatcachefile;
 
 		foreach my $cachefile (@cachefilecandidates) {
-			if ( -e $cachefile && -r $cachefile && $length > (stat($cachefile))[7] ) {
+			if ( -e $cachefile && -r $cachefile ) {
 				my $sizedelta = int($length) - int((stat($cachefile))[7]) ;
-				print "* [HTTP] Not using cached file $cachefile ... (size:".(stat($cachefile))[7]." (".$sizedelta." bytes too small))\n";
-			}
-			if ( -e $cachefile && -r $cachefile && $length <= (stat($cachefile))[7] ) {
-				my $sizedelta = int((stat($cachefile))[7]) - int($length);
-				print "* [HTTP] Using cached file $cachefile ... (size:".(stat($cachefile))[7]."".
-					($sizedelta ? " (".$sizedelta." bytes bigger than expected)" : "(matches expected length)").")\n";
-				return {file=>$cachefile, status=>0};
+				if (abs($sizedelta) > ($length * 0.05)) {
+					print "* [HTTP] Not using cached file $cachefile ... (".abs($sizedelta)." bytes too ".($sizedelta > 0 ? "small" : "big").")\n";
+				} else {
+					print "* [HTTP] Using cached file $cachefile (size:".(stat($cachefile))[7].") ...".
+						($sizedelta ? " (even though it is ".abs($sizedelta)." bytes too " . ($sizedelta>0?"small":"big") . ")" : "")."\n";
+					return {file=>$cachefile, status=>0};
+				}
 			}
 		}
 		print "* [HTTP] Downloading $url ...\n";
diff --git a/src/gnupod_addsong.pl b/src/gnupod_addsong.pl
index 492e5b9..298da79 100644
--- a/src/gnupod_addsong.pl
+++ b/src/gnupod_addsong.pl
@@ -47,11 +47,13 @@ GetOptions(\%opts, "version", "help|h", "mount|m=s", "decode|x=s", "restore|r",
                    "set-title|t=s", "set-artist|a=s", "set-album|l=s", "set-genre|g=s", "set-rating=i", "set-playcount=i",
                    "set-bookmarkable|b", "set-shuffleskip", "artwork=s",
                    "set-songnum", "playlist|p=s@", "reencode|e=i",
-                   "min-vol-adj=i", "max-vol-adj=i", "playlist-is-podcast", "podcast-files-limit=i", "podcast-cache-dir=s", "set-compilation");
+                   "min-vol-adj=i", "max-vol-adj=i", "playlist-is-podcast", "podcast-files-limit=i", "podcast-cache-dir=s",
+                   "podcast-artwork", "set-compilation");
 
 GNUpod::FooBar::GetConfig(\%opts, {'decode'=>'s', mount=>'s', duplicate=>'b', model=>'s',
                                    'disable-v1'=>'b', 'disable-v2'=>'b', 'set-songnum'=>'b',
-                                   'min-vol-adj'=>'i', 'max-vol-adj'=>'i', 'automktunes'=>'b', 'podcast-files-limit'=>'i', 'podcast-cache-dir'=>'s' },
+                                   'min-vol-adj'=>'i', 'max-vol-adj'=>'i', 'automktunes'=>'b', 
+                                   'podcast-files-limit'=>'i', 'podcast-cache-dir'=>'s', 'podcast-artwork'=>'b' },
                                    "gnupod_addsong");
 
 
@@ -84,8 +86,8 @@ else {
 my $connection = GNUpod::FooBar::connect(\%opts);
 usage($connection->{status}."\n") if $connection->{status} || [EMAIL PROTECTED];
 
-my $AWDB  = GNUpod::ArtworkDB->new(Connection=>$connection, DropUnseen=>1);
-
+my $AWDB  = GNUpod::ArtworkDB->new(Connection=>$connection, DropUnseen=>($opts{'podcast-artwork'}?0:1));
+my $awdb_image_prepared = 0 ;
 
 my $exit_code = startup($connection,@XFILES);
 exit($exit_code);
@@ -112,10 +114,7 @@ sub startup {
 	}
 	else {
 		if($opts{artwork}) {
-			if( $AWDB->PrepareImage(File=>$opts{artwork}, Model=>$opts{model}) ) {
-				$AWDB->LoadArtworkDb or die "Failed to load artwork database\n";
-			}
-			else {
+			if ( ! add_image_to_awdb($opts{artwork})) {
 				warn "$0: Could not load $opts{artwork}, skipping artwork\n";
 				delete($opts{artwork});
 			}
@@ -187,7 +186,7 @@ sub startup {
 		$fh->{playcount}    = $opts{'set-playcount'}   if $opts{'set-playcount'};
 		$fh->{title}        = $opts{'set-title'}       if $opts{'set-title'};
 		$fh->{songnum}      = 1+$addcount              if $opts{'set-songnum'};
-		if(defined($opts{artwork})) {
+		if($awdb_image_prepared) {
 			$fh->{has_artwork} = 1;
 			$fh->{artworkcnt}  = 1;
 			$fh->{dbid_1}      = $AWDB->InjectImage;
@@ -328,8 +327,21 @@ sub startup {
 	return $fatal_error;
 }
 
-
-
+#############################################################
+# Preapare and add image to artwork database
+sub add_image_to_awdb {
+	my ($filename) = @_;
+	if( $awdb_image_prepared ) {
+		warn "! [****] Skipping $filename because there is already one prepared.\n";
+		return 0;
+	}
+	my $count = $AWDB->PrepareImage(File=>$filename, Model=>$opts{model});
+	if( $count ) {; 
+		$AWDB->LoadArtworkDb or die "Failed to load artwork database\n";
+		$awdb_image_prepared = 1;
+	}
+	return $count;
+}
 #############################################################
 # Add item to playlist
 sub create_playlist_now {
@@ -401,11 +413,11 @@ sub PODCAST_fetch_media {
 		foreach my $cachefile (@cachefilecandidates) {
 			if ( -e $cachefile && -r $cachefile ) {
 				my $sizedelta = int($length) - int((stat($cachefile))[7]) ;
-				if (abs($sizedelta) > ($length * 0.05)) {
+				if ( ($length != 0) && (abs($sizedelta) > ($length * 0.05)) ) {
 					print "* [HTTP] Not using cached file $cachefile ... (".abs($sizedelta)." bytes too ".($sizedelta > 0 ? "small" : "big").")\n";
 				} else {
 					print "* [HTTP] Using cached file $cachefile (size:".(stat($cachefile))[7].") ...".
-						($sizedelta ? " (even though it is ".abs($sizedelta)." bytes too " . ($sizedelta>0?"small":"big") . ")" : "")."\n";
+						(($length != 0 && $sizedelta) ? " (even though it is ".abs($sizedelta)." bytes too " . ($sizedelta>0?"small":"big") . ")" : "")."\n";
 					return {file=>$cachefile, status=>0};
 				}
 			}
@@ -597,6 +609,25 @@ sub resolve_podcasts {
 
 	foreach my $key (keys(%podcast_infos)) {
 		my $cref = $podcast_infos{$key};
+		my $channel = $podcast_channel_infos{$key}[0]; # assuming for now that there's only one channel in the feed
+		# get the artwork
+		if (defined($opts{'podcast-artwork'})) {
+			my $channel_image_url = ( $channel->{"itunes:image"}->{"href"} or
+				$channel->{"image"}->{"url"}->{"\0"} ) ;
+			if ( $channel_image_url ) {
+				my $channel_image = PODCAST_fetch_media($channel_image_url, "/tmp/gnupodcast_image", 0);
+				if($channel_image->{status} or (!(-f $channel_image->{file}))) {
+					warn "! [HTTP] Failed to download $channel_image to ".$channel_image->{file}."\n";
+				}
+				else {
+					add_image_to_awdb($channel_image->{file});
+					if ( ! $opts{'podcast-cache-dir'} ) {
+						unlink($channel_image->{file}) or warn "Could not unlink ".$channel_image->{file}.", $!\n";
+					}
+				}
+			}
+		}
+
 		foreach my $podcast_item (@$cref) {
 			my $c_title = $podcast_item->{title}->{"\0"};
 			my $c_author = $podcast_item->{author}->{"\0"};
@@ -683,6 +714,7 @@ Usage: gnupod_addsong.pl [-h] [-m directory] File1 File2 ...
    -d, --duplicate                  Allow duplicate files
    -p, --playlist=string            Add songs to this playlist, can be used multiple times
        --playlist-is-podcast        Set podcast flag for playlist(s) created using '--playlist'
+       --podcast-artwork            Download and install artwork for podcasts from their channel.
        --podcast-cache-dir=string   Set a directory in which podcast media files will be cached.
        --podcast-files-limit=int    Limit the number of files that are downloaded.
                                     0 = download all (default), -X = download X oldest items, X = download X newest items
_______________________________________________
Bug-gnupod mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/bug-gnupod

Reply via email to