[banshee] Support the amz deluxe trackList XSPF extension



commit 0d4651d6c8d3d52463a1e65a8a7182ca7d099d4d
Author: Aaron Bockover <abockover novell com>
Date:   Mon Aug 2 17:54:50 2010 -0400

    Support the amz deluxe trackList XSPF extension
    
    Enables the downloading of non-MP3 types (like digital booklets/PDF)
    that come as part of the album purchase. These extension types are not
    supported yet internally in Banshee, but at least the bonus non-audio
    content you pay for will be downloaded.

 .../Banshee.AmazonMp3/AmzMp3Downloader.cs          |   32 +++++++++++++-------
 .../Banshee.AmazonMp3/AmzXspfPlaylist.cs           |   10 ++++++
 .../Banshee.AmazonMp3/UserJobDownloadManager.cs    |   27 +++++++++++++++-
 3 files changed, 56 insertions(+), 13 deletions(-)
---
diff --git a/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/AmzMp3Downloader.cs b/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/AmzMp3Downloader.cs
index bf43c9b..ca738b2 100644
--- a/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/AmzMp3Downloader.cs
+++ b/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/AmzMp3Downloader.cs
@@ -47,10 +47,12 @@ namespace Banshee.AmazonMp3
         {
             UserAgent = String.Format ("Amazon MP3 Downloader (Linux {0} en_US)", AmazonMp3DownloaderCompatVersion);
             TempPathRoot = Path.Combine (Path.GetTempPath (), "banshee-amz-downloader");
-            FileExtension = "mp3";
             Uri = track.Locations[0];
             Track = track;
             Name = String.Format ("{0} ({1})", Track.Title, Track.Creator);
+
+            var meta = track.FindMetaEntry (new Uri ("http://www.amazon.com/dmusic/trackType";));
+            FileExtension = !meta.Equals (Xspf.MetaEntry.Zero) ? meta.Value : "mp3";
         }
 
         protected override void OnFileFinished ()
@@ -65,19 +67,27 @@ namespace Banshee.AmazonMp3
                 return;
             }
 
-            using (var file = TagLib.File.Create (LocalPath, "taglib/mp3", TagLib.ReadStyle.Average)) {
-                var artist = StringUtil.EscapeFilename (file.Tag.JoinedPerformers);
-                var album = StringUtil.EscapeFilename (file.Tag.Album);
-                var title = StringUtil.EscapeFilename (file.Tag.Title);
+            string track_dir = null;
+            string track_path = null;
 
-                var track_dir = Path.Combine (OutputPath, Path.Combine (artist, album));
-                var track_path = Path.Combine (track_dir, String.Format ("{0:00}. {1}.mp3",
-                    file.Tag.Track, title));
+            if (FileExtension == "mp3") {
+                using (var file = TagLib.File.Create (LocalPath, "taglib/mp3", TagLib.ReadStyle.Average)) {
+                    var artist = StringUtil.EscapeFilename (file.Tag.JoinedPerformers);
+                    var album = StringUtil.EscapeFilename (file.Tag.Album);
+                    var title = StringUtil.EscapeFilename (file.Tag.Title);
 
-                Directory.CreateDirectory (track_dir);
-                File.Copy (LocalPath, track_path, true);
-                File.Delete (LocalPath);
+                    track_dir = Path.Combine (OutputPath, Path.Combine (artist, album));
+                    track_path = Path.Combine (track_dir, String.Format ("{0:00}. {1}.mp3",
+                        file.Tag.Track, title));
+                }
+            } else {
+                track_dir = Path.Combine (OutputPath, Path.Combine (Track.Creator, Track.Album));
+                track_path = Path.Combine (track_dir, Track.Title + "." + FileExtension);
             }
+
+            Directory.CreateDirectory (track_dir);
+            File.Copy (LocalPath, track_path, true);
+            File.Delete (LocalPath);
         }
     }
 }
diff --git a/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/AmzXspfPlaylist.cs b/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/AmzXspfPlaylist.cs
index 3475713..bea6911 100644
--- a/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/AmzXspfPlaylist.cs
+++ b/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/AmzXspfPlaylist.cs
@@ -26,6 +26,7 @@
 
 using System;
 using System.IO;
+using System.Xml;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Security.Cryptography;
@@ -106,6 +107,15 @@ namespace Banshee.AmazonMp3
             }
         }
 
+        protected override void LoadExtensionNode (XmlNode extensionNode, XmlNamespaceManager xmlns)
+        {
+            // Digital Booklets (pdf) are stuffed under extension/deluxe, so we need
+            // to support this XSPF extension that is specific to the .amz format.
+            foreach (XmlNode node in extensionNode.SelectNodes ("xspf:deluxe/xspf:trackList", xmlns)) {
+                LoadTrackListFromNode (node, xmlns);
+            }
+        }
+
         public ReadOnlyCollection<Xspf.Track> DownloadableTracks {
             get { return new ReadOnlyCollection<Xspf.Track> (downloadable_tracks); }
         }
diff --git a/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/UserJobDownloadManager.cs b/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/UserJobDownloadManager.cs
index 9740390..8186e80 100644
--- a/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/UserJobDownloadManager.cs
+++ b/src/Extensions/Banshee.AmazonMp3/Banshee.AmazonMp3/UserJobDownloadManager.cs
@@ -25,6 +25,7 @@
 // THE SOFTWARE.
 
 using System;
+using System.IO;
 
 using Hyena;
 using Hyena.Downloader;
@@ -105,11 +106,33 @@ namespace Banshee.AmazonMp3
         {
             base.OnDownloaderFinished (downloader);
 
+            var amz_downloader = (AmzMp3Downloader)downloader;
+            var track = amz_downloader.Track;
+
             if (downloader.State.Success) {
-                import_manager.Enqueue (((HttpFileDownloader)downloader).LocalPath);
+                if (amz_downloader.FileExtension == "mp3") {
+                    import_manager.Enqueue (((HttpFileDownloader)downloader).LocalPath);
+                } else {
+                    // FIXME: this just ensures that we download any extension content (e.g. pdf)
+                    // but we do not actually import it since it's not an MP3. However, we should
+                    // support the digital booklets (pdf) by keeping a special track in the
+                    // database. Such a track would never be played, but manual activation would
+                    // open the file in the default document viewer.
+                    //
+                    // Also, computing a path like this is not great, since it is possible that
+                    // the booklet may not actually end up in the same album directory. We should
+                    // probably wait to copy extensions until all tracks are downloaded, and we
+                    // choose the most common album path out of all downloaded tracks as the final
+                    // resting place for the extension content. Make sense? GOOD.
+                    var base_path = ServiceManager.SourceManager.MusicLibrary.BaseDirectory;
+                    var dir = Path.Combine (base_path, Path.Combine (track.Creator, track.Album));
+                    var path = Path.Combine (dir, track.Title + "." + amz_downloader.FileExtension);
+                    Directory.CreateDirectory (dir);
+                    File.Copy (amz_downloader.LocalPath, path, true);
+                    File.Delete (amz_downloader.LocalPath);
+                }
             }
 
-            var track = ((AmzMp3Downloader)downloader).Track;
             Log.InformationFormat ("Finished downloading \"{0}\" by {1}", track.Title, track.Creator);
 
             lock (SyncRoot) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]