[banshee] [MiroGuide] Handle Listen/Watch buttons



commit 75255b90b054906340a03bc5dbb0c55723d0d750
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Wed Jul 14 11:49:11 2010 -0700

    [MiroGuide] Handle Listen/Watch buttons
    
    When you click Listen/Watch in MiroGuide, we grab the actual media URL
    and start streaming it in Banshee.

 .../Banshee.MiroGuide/MiroGuideView.cs             |   82 ++++++++++++++++----
 src/Hyena                                          |    2 +-
 2 files changed, 66 insertions(+), 18 deletions(-)
---
diff --git a/src/Extensions/Banshee.MiroGuide/Banshee.MiroGuide/MiroGuideView.cs b/src/Extensions/Banshee.MiroGuide/Banshee.MiroGuide/MiroGuideView.cs
index 9f411a8..0568fa6 100644
--- a/src/Extensions/Banshee.MiroGuide/Banshee.MiroGuide/MiroGuideView.cs
+++ b/src/Extensions/Banshee.MiroGuide/Banshee.MiroGuide/MiroGuideView.cs
@@ -30,6 +30,7 @@ using System;
 using Gtk;
 
 using Hyena;
+using Hyena.Json;
 using Hyena.Downloader;
 
 using Banshee.Base;
@@ -83,11 +84,6 @@ namespace Banshee.MiroGuide
             // We only explicitly accept (render) text/html types, and only
             // download audio/x-amzxml - everything else is ignored.
             switch (mimetype) {
-                //case "audio/x-mpegurl":
-                //
-                // TODO:
-                //   media mimetypes - ship to gst/PlayerEngine
-                //   application/rss+xml
                 case "application/x-miro":
                     return OssiferNavigationResponse.Download;
                 default:
@@ -104,10 +100,6 @@ namespace Banshee.MiroGuide
                     for (int i = 1; File.Exists (dest_uri);
                         dest_uri = new SafeUri (String.Format ("{0} ({1})", dest_uri_base, ++i)));
                     return dest_uri.AbsoluteUri;
-                /*case "audio/x-mpegurl":
-                    Banshee.Streaming.RadioTrackInfo.OpenPlay (uri);
-                    Banshee.ServiceStack.ServiceManager.PlaybackController.StopWhenFinished = true;
-                    return null;*/
             }
 
             return null;
@@ -115,29 +107,85 @@ namespace Banshee.MiroGuide
 
         protected override OssiferNavigationResponse OnNavigationPolicyDecisionRequested (string uri)
         {
-            Log.Information ("ResourceRequestStarting for", uri);
+            try {
+                if (TryBypassRedirect (uri) || TryInterceptListenWatch (uri)) {
+                    return OssiferNavigationResponse.Ignore;
+                }
+            } catch (Exception e) {
+                Log.Exception ("MiroGuide caught error trying to shortcut navigation", e);
+            }
 
-            // Avoid the whole redirect-before-downloading page if possible
+            return OssiferNavigationResponse.Unhandled;
+        }
+
+        // The download and add-to-sidebar buttons take the user to a page that then redirects to the
+        // actual media URL or .miro OPML file.  But the URL to that redirection page contains all the
+        // info we need to start downloading or subscribe immediately.
+        private bool TryBypassRedirect (string uri)
+        {
             if (uri != null && uri.StartsWith ("http://subscribe.getmiro.com/";) && uri.Contains ("url1")) {
                 int a = uri.IndexOf ("url1") + 5;
                 int l = Math.Min (uri.Length - 1, uri.IndexOf ('&', a)) - a;
                 if (l > 0 && a + l < uri.Length) {
                     var direct_uri = System.Web.HttpUtility.UrlDecode (uri.Substring (a, l));
                     if (uri.Contains ("/download")) {
-                        // Go straight to the destination URL
-                        Banshee.Streaming.RadioTrackInfo.OpenPlay (direct_uri);
-                        Banshee.ServiceStack.ServiceManager.PlaybackController.StopWhenFinished = true;
-                        Log.DebugFormat ("MiroGuide: playing straight away {0}", direct_uri);
+                        // FIXME download and import
+                        //Banshee.Streaming.RadioTrackInfo.OpenPlay (direct_uri);
+                        //Banshee.ServiceStack.ServiceManager.PlaybackController.StopWhenFinished = true;
+                        Log.DebugFormat ("MiroGuide: downloading {0}", direct_uri);
+                        Log.Information ("Downloading not yet implemented.  URL", direct_uri, true);
                     } else {
                         // Subscribe to it straight away, don't redirect at all
                         ServiceManager.Get<DBusCommandService> ().PushFile (direct_uri);
                         Log.DebugFormat ("MiroGuide: subscribing straight away to {0}", direct_uri);
                     }
-                    return OssiferNavigationResponse.Ignore;
+                    return true;
                 }
             }
 
-            return OssiferNavigationResponse.Unhandled;
+            return false;
+        }
+
+        // The listen/watch links take the user to another page with an embedded player.  Instead of
+        // going there, find the direct media URL and send it to Banshee's PlayerEngine.
+        private bool TryInterceptListenWatch (string uri)
+        {
+            bool ret = false;
+            if (uri != null && uri.StartsWith ("http://miroguide.com/items/";)) {
+                int i = Uri.LastIndexOf ('/') + 1;
+                int channel_id = Int32.Parse (Uri.Substring (i, Uri.Length - i));
+
+                i = uri.LastIndexOf ('/') + 1;
+                var item_id = uri.Substring (i, uri.Length - i);
+
+                // Get the actual media URL via the MiroGuide API
+                new Hyena.Downloader.HttpStringDownloader () {
+                    Uri = new Uri (String.Format ("http://www.miroguide.com/api/get_channel?datatype=json&id={0}";, channel_id)),
+                    Finished = (d) => {
+                        if (d.State.Success) {
+                            string media_url = null;
+                            var obj = new Deserializer (d.Content).Deserialize ();
+                            var ary = ((JsonObject)obj)["item"] as JsonArray;
+                            foreach (JsonObject item in ary) {
+                                if ((item["playback_url"] as string).EndsWith (item_id)) {
+                                    media_url = item["url"] as string;
+                                    break;
+                                }
+                            }
+
+                            if (media_url != null) {
+                                Log.DebugFormat ("MiroGuide: streaming straight away {0}", media_url);
+                                Banshee.Streaming.RadioTrackInfo.OpenPlay (media_url);
+                                Banshee.ServiceStack.ServiceManager.PlaybackController.StopWhenFinished = true;
+                                ret = true;
+                            }
+                        }
+                    },
+                    AcceptContentTypes = new [] { "text/javascript" }
+                }.StartSync ();
+            }
+
+            return ret;
         }
 
         protected override void OnDownloadStatusChanged (OssiferDownloadStatus status, string mimetype, string destinationUri)
diff --git a/src/Hyena b/src/Hyena
index e3198fc..f9373d4 160000
--- a/src/Hyena
+++ b/src/Hyena
@@ -1 +1 @@
-Subproject commit e3198fc8dfe306ff2ec4fee00ce2abb43b319889
+Subproject commit f9373d450c1566d94c11a8c054db6f33ed68c244



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