banshee r3310 - in trunk/banshee: . src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler src/Libraries/Lastfm/Lastfm



Author: ahixon
Date: Sat Feb 23 09:36:13 2008
New Revision: 3310
URL: http://svn.gnome.org/viewvc/banshee?rev=3310&view=rev

Log:
2008-02-22  Alexander Hixon  <hixon alexander mediati org>
    * src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs:
      Use a slightly less brain-dead method of checking the total length of the track
      played. Skipping through a track but playing the correct length for inclusion on
      a Last.fm profile now works correctly.
      
      Also added support for sending NowPlaying information.
    
    * src/Libraries/Lastfm/Lastfm/Account.cs: Create an 'Instance' property for use via
      multiple modules.
    
    * src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs: Uncommented and exposed
      NowPlaying method, so that Last.fm lists the current track as playing.


Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs
   trunk/banshee/src/Libraries/Lastfm/Lastfm/Account.cs
   trunk/banshee/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs

Modified: trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs
==============================================================================
--- trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs	(original)
+++ trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs	Sat Feb 23 09:36:13 2008
@@ -63,7 +63,9 @@
         
         private bool song_started = false; /* if we were watching the current song from the beginning */
         private bool queued; /* if current_track has been queued */
-        private bool sought; /* if the user has sought in the current playing song */
+        
+        private DateTime song_start_time;
+        private TrackInfo last_track;
     
         public AudioscrobblerService ()
         {
@@ -79,6 +81,7 @@
             connection = new AudioscrobblerConnection (account, queue);
             
             ServiceManager.PlayerEngine.EventChanged += OnPlayerEngineEventChanged;
+            ServiceManager.PlayerEngine.StateChanged += OnPlayerEngineStateChanged;
             
             action_service = ServiceManager.Get<InterfaceActionService> ("InterfaceActionService");
             InterfaceInitialize ();
@@ -122,6 +125,7 @@
         public void Dispose ()
         {
             ServiceManager.PlayerEngine.EventChanged -= OnPlayerEngineEventChanged;
+            ServiceManager.PlayerEngine.StateChanged -= OnPlayerEngineStateChanged;
             
             connection.Stop ();
         
@@ -130,47 +134,62 @@
             actions = null;
         }
         
+        void OnPlayerEngineStateChanged (object o, PlayerEngineStateArgs args)
+        {
+            if (ServiceManager.PlayerEngine.CurrentState == PlayerEngineState.Paused && 
+                    ServiceManager.PlayerEngine.LastState == PlayerEngineState.Playing) {
+                st.Stop ();
+            } 
+            else if (ServiceManager.PlayerEngine.CurrentState == PlayerEngineState.Playing &&
+                ServiceManager.PlayerEngine.LastState == PlayerEngineState.Paused) {
+                st.Start ();
+            }
+        }
+        
+        // We need to time how long the song has played
+        internal class SongTimer
+        {
+            private DateTime start_time;
+            public int PlayTime = 0;
+            public void Start() { start_time = DateTime.Now; }
+            public void Stop() { PlayTime += (int) (DateTime.Now - start_time).TotalSeconds;}
+            public void Reset() { PlayTime = 0; }
+        }
+        
+        SongTimer st = new SongTimer ();
+        
+        private void Queue (TrackInfo track) {
+            if (song_started && !queued && track.Duration.TotalSeconds > 30 && 
+                track.ArtistName != "" && track.TrackTitle != "" &&
+               (st.PlayTime >  track.Duration.TotalSeconds / 2 || st.PlayTime > 240)) {
+                  queue.Add (track, song_start_time);
+                  queued = true;
+            }
+        }
+        
         private void OnPlayerEngineEventChanged (object o, PlayerEngineEventArgs args)
         {
             switch (args.Event) {
-                /* Queue if we're watching this song from the beginning,
-                 * it isn't queued yet and the user didn't seek until now,
-                 * we're actually playing, song position and length are greater than 0
-                 * and we already played half of the song or 240 seconds */
-                 
-                case PlayerEngineEvent.Iterate:
-                    if (song_started && !queued && !sought &&
-                        ServiceManager.PlayerEngine.CurrentState == PlayerEngineState.Playing &&
-                        ServiceManager.PlayerEngine.Length > 0 &&
-                        ServiceManager.PlayerEngine.Position > 0 &&
-                        (ServiceManager.PlayerEngine.Position > ServiceManager.PlayerEngine.Length / 2 || ServiceManager.PlayerEngine.Position > (240 * 1000))) {
-                            TrackInfo track = ServiceManager.PlayerEngine.CurrentTrack;
-                            if (track == null) {
-                                queued = sought = false;
-                            } else {
-                                if ((actions["AudioscrobblerEnableAction"] as ToggleAction).Active) {
-                                    queue.Add (track, DateTime.Now - TimeSpan.FromSeconds (ServiceManager.PlayerEngine.Position));
-                                }
-                                queued = true;
-                            }
-                    }
-                    
-                    break;
-                 
-                /* Start of Stream: new song started */
                 case PlayerEngineEvent.StartOfStream:
-                    queued = sought = false;
+                    // Queue the previous track in case of a skip
+                    st.Stop ();
+                    Queue (last_track);
+                
+                    st.Reset (); st.Start ();
+                    song_start_time = DateTime.Now;
+                    last_track = ServiceManager.PlayerEngine.CurrentTrack;
+                    queued = false;
                     song_started = true;
+
+                    // Queue as now playing
+                    connection.NowPlaying (last_track.ArtistName, last_track.TrackTitle,
+                        last_track.AlbumTitle, last_track.Duration.TotalSeconds, last_track.TrackNumber);
                     break;
                 
-                /* End of Stream: song finished */
                 case PlayerEngineEvent.EndOfStream:
-                    song_started = queued = sought = false;
-                    break;
-                
-                /* Did the user seek? */
-                case PlayerEngineEvent.Seek:
-                    sought = true;
+                    st.Stop ();
+                    Queue (ServiceManager.PlayerEngine.CurrentTrack);
+                    queued = true;
                     break;
             }
         }

Modified: trunk/banshee/src/Libraries/Lastfm/Lastfm/Account.cs
==============================================================================
--- trunk/banshee/src/Libraries/Lastfm/Lastfm/Account.cs	(original)
+++ trunk/banshee/src/Libraries/Lastfm/Lastfm/Account.cs	Sat Feb 23 09:36:13 2008
@@ -35,6 +35,17 @@
 {
     public class Account
     {
+        private static Account instance;
+        public static Account Instance {
+            get {
+                if (instance == null) {
+                    instance = new Account ();
+                }
+                
+                return instance;
+            }
+        }
+    
         public event EventHandler Updated;
 
         private string username;

Modified: trunk/banshee/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs
==============================================================================
--- trunk/banshee/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs	(original)
+++ trunk/banshee/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs	Sat Feb 23 09:36:13 2008
@@ -65,6 +65,7 @@
         string post_url;
         string session_id = null;
         string now_playing_url;
+        bool now_playing_submitted = false;
         
         bool started = false;
         public bool Started {
@@ -79,11 +80,6 @@
         
         int hard_failures = 0;
         int hard_failure_retry_sec = 60;
- 
-        bool now_playing_submitted;
-        
-        DateTime song_start_time;
-        //TrackInfo last_track;
         
         WebRequest now_playing_post;
         WebRequest current_web_req;
@@ -484,24 +480,24 @@
         //
         // Async code for now playing
         
-        /*void NowPlaying (TrackInfo track)
+        public void NowPlaying (string artist, string title, string album, double duration, int tracknum)
 
         {
-            if (session_id != null && track.Artist != "" && track.Title != "") {
+            if (session_id != null && artist != "" && title != "") {
                 
                 string str_track_number = "";
-                if (track.TrackNumber != 0)
-                    str_track_number = track.TrackNumber.ToString();
+                if (tracknum != 0)
+                    str_track_number = tracknum.ToString();
                 
                 string uri = String.Format ("{0}?s={1}&a={2}&t={3}&b={4}&l={5}&n={6}&m={7}",
                                             now_playing_url,
                                             session_id,
-                                            HttpUtility.UrlEncode(track.Artist),
-                                            HttpUtility.UrlEncode(track.Title),
-    			                            HttpUtility.UrlEncode(track.Album),
-                                            track.Duration.TotalSeconds.ToString(),
+                                            HttpUtility.UrlEncode(artist),
+                                            HttpUtility.UrlEncode(title),
+    			                            HttpUtility.UrlEncode(album),
+                                            duration.ToString(),
                                             str_track_number,
-    			                            "" *//* musicbrainz id *//*);
+    			                            "" /* musicbrainz id */);
 
                 now_playing_post = WebRequest.Create (uri);
                 now_playing_post.Method = "POST";
@@ -523,9 +519,9 @@
 
                 string line = sr.ReadLine ();
                 if (line.StartsWith ("BADSESSION")) {
-                    Hyena.Log.Warning ("Audioscrobbler NowPlaying failed", "Session ID sent was invalid", false);*/
+                    Hyena.Log.Warning ("Audioscrobbler NowPlaying failed", "Session ID sent was invalid", false);
                     /* attempt to re-handshake on the next interval */
-                    /*session_id = null;
+                    session_id = null;
                     next_interval = DateTime.Now + new TimeSpan (0, 0, RETRY_SECONDS);
                     state = State.NEED_HANDSHAKE;
                     return;
@@ -541,6 +537,6 @@
                 Hyena.Log.Error ("Audioscrobbler NowPlaying failed", 
                               String.Format("Failed to post NowPlaying: {0}", e));
             }
-        }*/
+        }
     }
 }



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