/// <summary> /// Queues a <see cref="PlayedTrack"/> for scrobbling, then /// scrobbles it on another thread. Or at least tries to. /// The <see cref="PlayedTrack"/> info is cached locally firt, so /// there's no fear of losing precious scrobbles. /// </summary> /// <param name="track"> /// A <see cref="PlayedTrack"/> /// </param> public void Queue(Entry track) { // Append the scrobble line to the file StreamWriter writer = new StreamWriter(CacheDir + cacheFileName, true, Encoding.Unicode); writer.WriteLine(track.getParameters().serialize()); writer.Flush(); writer.Close(); // Try and submit the whole file now, in another thread. Submit(); }
/// <summary> /// Public scrobble function. Scrobbles a PlayedTrack object. /// </summary> /// <param name="track"> /// A <see cref="PlayedTrack"/> /// </param> public void Scrobble(Entry track) { RequestParameters np = new RequestParameters(); RequestParameters p = track.getParameters(); foreach(string key in p.Keys) np[key + "[0]"] = p[key]; // This scrobbles the collection of parameters no matter what they belong to. this.Scrobble(np); }
void worker_DoWork(object sender, DoWorkEventArgs e) { InvokeStarted(null); Trace.WriteLine("Started"); Track lastPlayingTrack = null; Track lastScrobbledTrack = null; Track lastTrack = null; var lastPlayedTrackStarted = DateTime.Now; var cycles = 0; while (!worker.CancellationPending) { if (cycles == 0) { cycles = 15; // 15 seconds approx. var track = GetCurrentTrack(feedUrl); var lastPlayedTrackHasChanged = (lastPlayingTrack != null && !lastPlayingTrack.Equals(track)); var isFirstTime = (lastTrack == null || !lastTrack.Equals(track)); if (lastPlayedTrackHasChanged) { var newScrobbledTrack = (lastScrobbledTrack == null || !lastScrobbledTrack.Equals(lastPlayingTrack)); var duration = DateTime.Now - lastPlayedTrackStarted; if (newScrobbledTrack && duration > new TimeSpan(0, 0, 0, 30)) { var entry = new Entry( lastPlayingTrack.Artist, lastPlayingTrack.Title, lastPlayedTrackStarted, PlaybackSource.NonPersonalizedBroadcast, duration, ScrobbleMode.Played ); scrobbleManager.Queue(entry); lastScrobbledTrack = lastPlayingTrack; InvokeScrobbled(new TrackEventArgs() {Track = lastPlayingTrack}); Trace.WriteLine(String.Format("Scrobbled: {0} ({1})", lastPlayingTrack, duration)); } lastPlayedTrackStarted = DateTime.Now; } if (track.Status == TrackStatus.Playing) { var duration = DateTime.Now - lastPlayedTrackStarted; NotifyMsnMessenger(track); if (configuration.NotifyLastFm) { var nowplayingTrack = new NowplayingTrack(track.Artist, track.Title, duration); scrobbleManager.ReportNowplaying(nowplayingTrack); } Trace.WriteLine(String.Format("Now playing: {0} ({1})", track, duration)); lastPlayingTrack = track; lastPlayedTrack = track; } else { NotifyMsnMessenger(null); lastPlayingTrack = null; if (lastPlayedTrack.Status == TrackStatus.Playing) { lastPlayedTrack.Status = TrackStatus.Played; } Trace.WriteLine(String.Format("{0}: {1}", track.Status, track)); } InvokePlaying(new PlayingEventArgs { Track = track, IsFirstTime = isFirstTime }); lastTrack = track; } cycles--; Thread.Sleep(1000); } InvokeStopped(null); Trace.WriteLine("Stopped"); }
public void Submit(YouTubeEntry song) { if (!IsLoged) return; try { string Artist = string.Empty; string Title = string.Empty; int length = 0; string _title = song.Title.Text; if (_title.Contains("-")) { Artist = _title.Split('-')[0].Trim(); Title = _title.Split('-')[1].Trim(); } if (song.Duration != null && song.Duration.Seconds != null) { length = Convert.ToInt32(song.Duration.Seconds, 10); } if (!string.IsNullOrEmpty(Artist) && !string.IsNullOrEmpty(Title) && length > 0) { Entry entry = new Entry(Artist, Title, DateTime.Now, PlaybackSource.User, new TimeSpan(0, 0, length), ScrobbleMode.Played); manager.Queue(entry); //manager.Submit(); } } catch (Exception exception) { Log.Error(exception); } }
void myPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) { if (doneLoadingSong && e.newState == 3) { songDuration.Maximum = (int)player.Ctlcontrols.currentItem.duration; lblSongDuration.Text = TimeTools.SecondsToString(songDuration.Maximum); TimeTools.SecondsToString((int)player.Ctlcontrols.currentItem.duration); /* Incorrect way of scrobbling. Should ReportNowPlaying first, then when the song is finished add a new entry to the queue */ if (scrobblingEnabled) { Entry entry = new Entry( listSongs[listMusic.SelectedIndices[0]].Artist, listSongs[listMusic.SelectedIndices[0]].Title, DateTime.Now, PlaybackSource.NonPersonalizedBroadcast, TimeSpan.FromSeconds(player.currentMedia.duration), ScrobbleMode.Played); scrobbleManager.Queue(entry); } //scrobbleManager.ReportNowplaying(new NowplayingTrack(listSongs[listMusic.SelectedIndices[0]].Artist, listSongs[listMusic.SelectedIndices[0]].Title)); doneLoadingSong = false; } }