banshee r3335 - in trunk/banshee: . src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler src/Libraries/Lastfm/Lastfm
- From: ahixon svn gnome org
- To: svn-commits-list gnome org
- Subject: banshee r3335 - in trunk/banshee: . src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler src/Libraries/Lastfm/Lastfm
- Date: Wed, 27 Feb 2008 08:50:05 +0000 (GMT)
Author: ahixon
Date: Wed Feb 27 08:50:05 2008
New Revision: 3335
URL: http://svn.gnome.org/viewvc/banshee?rev=3335&view=rev
Log:
2008-02-27 Alexander Hixon <hixon alexander mediati org>
* src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs: Support lazy
authentication. Also implement retrying if sending a 'Now Playing' track
failed. Slightly more detailed logging is also added.
* src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs:
Removed a useless boolean value. Also implemented lazy connecting before
queuing tracks.
Modified:
trunk/banshee/ChangeLog
trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.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 Wed Feb 27 08:50:05 2008
@@ -62,7 +62,6 @@
private Queue queue;
private Account account;
- 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 DateTime song_start_time;
@@ -89,7 +88,6 @@
LastfmCore.AudioscrobblerQueue = queue;
connection = LastfmCore.Audioscrobbler;
- // This auto-connects for us if we start off connected to the network.
connection.UpdateNetworkState (NetworkDetect.Instance.Connected);
NetworkDetect.Instance.StateChanged += delegate (object o, NetworkStateChangedArgs args) {
connection.UpdateNetworkState (args.Connected);
@@ -204,14 +202,19 @@
return;
}
- Log.DebugFormat ("Track {4} had playtime of {0} msec ({5}sec), duration {1} msec, started: {2}, queued: {3}",
- st.PlayTime, track.Duration.TotalMilliseconds, song_started, queued, track, st.PlayTime / 1000);
+ Log.DebugFormat ("Track {3} had playtime of {0} msec ({4}sec), duration {1} msec, queued: {2}",
+ st.PlayTime, track.Duration.TotalMilliseconds, queued, track, st.PlayTime / 1000);
- if (song_started && !queued && track.Duration.TotalSeconds > 30 &&
+ if (!queued && track.Duration.TotalSeconds > 30 &&
track.ArtistName != "" && track.TrackTitle != "" &&
(st.PlayTime > track.Duration.TotalMilliseconds / 2 || st.PlayTime > 240 * 1000)) {
- queue.Add (track, song_start_time);
- queued = true;
+ if (!connection.Started) {
+ // Lazy-connect.
+ connection.Connect ();
+ }
+
+ queue.Add (track, song_start_time);
+ queued = true;
}
}
@@ -226,7 +229,6 @@
song_start_time = DateTime.Now;
last_track = ServiceManager.PlayerEngine.CurrentTrack;
queued = false;
- song_started = true;
// Queue as now playing
if (last_track != null) {
@@ -246,7 +248,6 @@
case PlayerEngineEvent.EndOfStream:
Queue (ServiceManager.PlayerEngine.CurrentTrack);
- //queued = true;
break;
}
}
@@ -276,10 +277,6 @@
get { return EngineEnabledSchema.Get (); }
set {
EngineEnabledSchema.Set (value);
- if (!connection.Started) {
- connection.Connect ();
- }
-
(actions["AudioscrobblerEnableAction"] as ToggleAction).Active = value;
}
}
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 Wed Feb 27 08:50:05 2008
@@ -63,8 +63,7 @@
string post_url;
string session_id = null;
- string now_playing_url;
- bool now_playing_submitted = false;
+ string now_playing_url = "%now_playing_uri%"; /* default placeholder url */
bool connected = false;
public bool Connected {
get { return connected; }
@@ -85,6 +84,7 @@
int hard_failure_retry_sec = 60;
HttpWebRequest now_playing_post;
+ string now_playing_uri;
HttpWebRequest current_web_req;
IAsyncResult current_async_result;
State state;
@@ -109,34 +109,24 @@
if (!started) {
Start ();
}
-
- if (session_id == null && started) {
- if (connected) {
- state = State.NEED_HANDSHAKE;
- Handshake ();
- } else {
- Hyena.Log.Debug ("Not connecting to Audioscrobbler", "Not connected to network.");
- }
- }
}
public void UpdateNetworkState (bool connected)
{
Log.DebugFormat ("Changing Audioscrobbler connected state: {0}", connected ? "connected" : "disconnected");
this.connected = connected;
- if (connected) {
- Connect ();
- }
}
private void Start ()
- {
+ {
started = true;
queue.TrackAdded += delegate(object o, EventArgs args) {
StartTransitionHandler ();
};
queue.Load ();
+
+ StartTransitionHandler ();
}
private void StartTransitionHandler ()
@@ -182,7 +172,7 @@
private void StateTransitionHandler (object o, ElapsedEventArgs e)
{
- Hyena.Log.DebugFormat ("State transition handler running; state: {0}", state);
+ Hyena.Log.DebugFormat ("State transition handler running; state: {0}, connected {1}", state, connected);
/* if we're not connected, don't bother doing anything
* involving the network. */
@@ -203,10 +193,15 @@
state = State.NEED_HANDSHAKE;
} else {
- if (queue.Count > 0)
+ if (queue.Count > 0) {
state = State.NEED_TRANSMIT;
- else if (now_playing_submitted)
+ } else if (now_playing_uri != null) {
+ // Now playing info needs to be sent
+ NowPlaying (now_playing_uri);
+ } else {
+ Hyena.Log.DebugFormat ("State transition handler going to sleep.");
StopTransitionHandler ();
+ }
}
break;
@@ -369,10 +364,6 @@
/* we succeeded, pop the elements off our queue */
queue.RemoveRange (0, ts.Count);
queue.Save ();
- if (queue.Count == 0) {
- // Don't wake up all the time - sleep for a while.
- StopTransitionHandler ();
- }
state = State.IDLE;
}
@@ -499,45 +490,86 @@
// Async code for now playing
public void NowPlaying (string artist, string title, string album, double duration,
- int tracknum)
+ int tracknum)
{
NowPlaying (artist, title, album, duration, tracknum, "");
}
public void NowPlaying (string artist, string title, string album, double duration,
- int tracknum, string mbrainzid)
-
+ int tracknum, string mbrainzid)
{
- if (session_id != null && artist != "" && title != "") {
-
- string str_track_number = "";
- 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(artist),
- HttpUtility.UrlEncode(title),
- HttpUtility.UrlEncode(album),
- duration.ToString(),
- str_track_number,
- mbrainzid);
+ if (artist == "" || title == "") {
+ return;
+ }
+
+ string str_track_number = "";
+ if (tracknum != 0) {
+ str_track_number = tracknum.ToString();
+ }
+
+ string session_string = String.IsNullOrEmpty(session_id) ? "%session_id%" : session_id;
+ Console.WriteLine ("Session string: {0}", session_string);
+
+ string uri = String.Format ("{0}?s={1}&a={2}&t={3}&b={4}&l={5}&n={6}&m={7}",
+ now_playing_url,
+ session_string,
+ HttpUtility.UrlEncode(artist),
+ HttpUtility.UrlEncode(title),
+ HttpUtility.UrlEncode(album),
+ duration.ToString(),
+ str_track_number,
+ mbrainzid);
+
+ NowPlaying (uri);
+ }
+
+ public void NowPlaying (string uri)
+ {
+ if (now_playing_post != null) {
+ Hyena.Log.DebugFormat ("Now-playing submission already started - aborting.");
+ now_playing_post.Abort ();
+ }
+
+ // Fill in placeholder text if NowPlaying was called when
+ // we weren't authenticated.
+ string fillin = "%now_playing_uri%?s=%session_id%";
+
+ // We prefer not to use replace with placeholders due to security
+ // risks - so we substring to a predetermined length.
+ if (uri.StartsWith (fillin) && session_id != null) {
+ uri = String.Format ("{0}?s={1}&{2}", now_playing_url,
+ session_id,
+ uri.Substring (fillin.Length + 1));
+ }
+
+ now_playing_uri = uri;
+
+ if (session_id == null) {
+ // Go connect - we'll come back later in main timer loop.
+ Connect ();
+ return;
+ }
+ try {
now_playing_post = (HttpWebRequest) WebRequest.Create (uri);
now_playing_post.UserAgent = LastfmCore.UserAgent;
now_playing_post.Method = "POST";
now_playing_post.ContentType = "application/x-www-form-urlencoded";
now_playing_post.ContentLength = uri.Length;
now_playing_post.BeginGetResponse (NowPlayingGetResponse, null);
- now_playing_submitted = true;
+ state = State.WAITING_FOR_RESP;
+ } catch (Exception ex) {
+ Hyena.Log.Warning ("Audioscrobbler NowPlaying failed",
+ String.Format ("Exception while creating request: {0}", ex), false);
+
+ // Reset now_playing_uri if it was the problem.
+ now_playing_uri = null;
}
}
void NowPlayingGetResponse (IAsyncResult ar)
{
try {
-
WebResponse my_resp = now_playing_post.EndGetResponse (ar);
Stream s = my_resp.GetResponseStream ();
@@ -559,6 +591,10 @@
else if (line.StartsWith ("OK")) {
// NowPlaying submitted
Hyena.Log.DebugFormat ("Submitted NowPlaying track to Audioscrobbler");
+ now_playing_post = null;
+ now_playing_uri = null;
+ state = State.IDLE;
+ return;
}
else {
Hyena.Log.Warning ("Audioscrobbler NowPlaying failed", "Unexpected or no response", false);
@@ -568,6 +604,17 @@
Hyena.Log.Error ("Audioscrobbler NowPlaying failed",
String.Format("Failed to post NowPlaying: {0}", e), false);
}
+
+ // NowPlaying error/success is non-crutial.
+ hard_failures++;
+ if (hard_failures < 3) {
+ NowPlaying (now_playing_uri);
+ } else {
+ // Give up - NowPlaying status information is non-critical.
+ now_playing_uri = null;
+ now_playing_post = null;
+ state = State.IDLE;
+ }
}
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]