[banshee] Last.fm: update response stream handling



commit 29b3d73199d36f7aff1f98bb31006afc6d260de8
Author: Phil Trimble <PhilTrimble gmail com>
Date:   Sat Jul 7 18:44:10 2012 -0500

    Last.fm: update response stream handling
    
    Currently there are two methods in LastFmRequest that attempt
    to read the response stream. If both methods are called during
    the same request then the second read returns nothing from the
    stream since it was already consumed with the first read. This
    change stores the stream contents in the request object instead
    to avoid this situation.
    
    Also updates the number of tracks we submit at a time from 50 to
    30. With the change to the 2.0 API it seems we receive '414' HTTP
    errors if our post url is too large.
    
    Signed-off-by: Alexander Kojevnikov <alexk gnome org>

 .../Banshee.Lastfm.Audioscrobbler/Queue.cs         |    6 +++-
 src/Libraries/Lastfm/Lastfm/LastfmRequest.cs       |   28 +++++++++++++------
 2 files changed, 23 insertions(+), 11 deletions(-)
---
diff --git a/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs b/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs
index 7f896df..72bc159 100644
--- a/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs
+++ b/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs
@@ -4,6 +4,7 @@
 // Author:
 //   Chris Toshok <toshok ximian com>
 //   Alexander Hixon <hixon alexander mediati org>
+//   Phil Trimble <philtrimble gmail com>
 //
 // Copyright (C) 2005-2008 Novell, Inc.
 //
@@ -258,8 +259,9 @@ namespace Banshee.Lastfm.Audioscrobbler
 
         public List<IQueuedTrack> GetTracks ()
         {
-            /* Last.fm can handle up to 50 songs in one request */
-            return queue.GetRange (0, Math.Min (queue.Count, 50));
+            // Last.fm can technically handle up to 50 songs in one request
+            // but seems to throw errors if our submission is too long.
+            return queue.GetRange (0, Math.Min (queue.Count, 30));
         }
 
         public void Add (object track, DateTime started_at)
diff --git a/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs b/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs
index 769c205..e22dc4d 100644
--- a/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs
+++ b/src/Libraries/Lastfm/Lastfm/LastfmRequest.cs
@@ -3,6 +3,7 @@
 //
 // Authors:
 //   Bertrand Lorentz <bertrand lorentz gmail com>
+//   Phil Trimble <philtrimble gmail com>
 //
 // Copyright (C) 2009 Bertrand Lorentz
 //
@@ -59,6 +60,7 @@ namespace Lastfm
 
         private Dictionary<string, string> parameters = new Dictionary<string, string> ();
         private Stream response_stream;
+        private string response_string;
 
         public LastfmRequest ()
         {}
@@ -119,7 +121,10 @@ namespace Lastfm
             if (response_stream == null) {
                 return null;
             }
-            Deserializer deserializer = new Deserializer (response_stream);
+
+            SetResponseString ();
+
+            Deserializer deserializer = new Deserializer (response_string);
             object obj = deserializer.Deserialize ();
             JsonObject json_obj = obj as Hyena.Json.JsonObject;
 
@@ -152,14 +157,11 @@ namespace Lastfm
         {
             StationError error = StationError.None;
 
-            string response;
-            using (StreamReader sr = new StreamReader (response_stream)) {
-                response = sr.ReadToEnd ();
-            }
+            SetResponseString ();
 
-            if (response.Contains ("<lfm status=\"failed\">")) {
+            if (response_string.Contains ("<lfm status=\"failed\">")) {
                 // XML reply indicates an error
-                Match match = Regex.Match (response, "<error code=\"(\\d+)\">");
+                Match match = Regex.Match (response_string, "<error code=\"(\\d+)\">");
                 if (match.Success) {
                     error = (StationError) Int32.Parse (match.Value);
                     Log.WarningFormat ("Lastfm error {0}", error);
@@ -167,9 +169,9 @@ namespace Lastfm
                     error = StationError.Unknown;
                 }
             }
-            if (response_format == ResponseFormat.Json && response.Contains ("\"error\":")) {
+            if (response_format == ResponseFormat.Json && response_string.Contains ("\"error\":")) {
                 // JSON reply indicates an error
-                Deserializer deserializer = new Deserializer (response);
+                Deserializer deserializer = new Deserializer (response_string);
                 JsonObject json = deserializer.Deserialize () as JsonObject;
                 if (json != null && json.ContainsKey ("error")) {
                     error = (StationError) json["error"];
@@ -249,6 +251,14 @@ namespace Lastfm
             return sb.ToString ();
         }
 
+        private void SetResponseString () {
+            if (response_string == null) {
+                using (StreamReader sr = new StreamReader (response_stream)) {
+                    response_string = sr.ReadToEnd ();
+                }
+            }
+        }
+
 #region HTTP helpers
 
         private Stream Get (string uri)



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