Ejemplo n.º 1
0
        private async Task <bool> UpdateTrackInfoUsingWebApi()
        {
            if (this.Web?.API == null)
            {
                return(false);
            }

            // Pre-emptively waiting a little bit to let the remote Spotify server update its own information
            await Task.Delay(TimeSpan.FromMilliseconds(50)).ConfigureAwait(false);

            var currentlyPlayingObject = await this.Web.API.GetCurrentlyPlayingTrackAsync().ConfigureAwait(false);

            if (currentlyPlayingObject?.Track == null || !currentlyPlayingObject.Track.IsValid())
            {
                return(false);
            }

            ISpotifyTrack newTrack = currentlyPlayingObject.Track;

            if (!SpotifyTrack.Equal(this.CurrentTrack, newTrack))
            {
                ISpotifyTrack oldTrack = this.CurrentTrack;
                this.CurrentTrack = newTrack;
                await this.OnTrackChanged(oldTrack).ConfigureAwait(false);
            }

            await this.OnPlayStateChanged(currentlyPlayingObject.IsPlaying).ConfigureAwait(false);

            return(true);
        }
Ejemplo n.º 2
0
 public SpotifyStateEventArgs(ISpotifyTrack currentTrack, bool playing, double trackTime, double volume)
 {
     this.CurrentTrack = currentTrack;
     this.Playing      = playing;
     this.TrackTime    = trackTime;
     this.Volume       = volume;
 }
 public CurrentlyPlayingObject(int progressMs, bool isPlaying, ISpotifyTrack track, SpotifyTrackType type)
 {
     this.ProgressMs = progressMs;
     this.IsPlaying  = isPlaying;
     this.Track      = track;
     this.Type       = type;
 }
Ejemplo n.º 4
0
        public JsonTrack(ISpotifyTrack track)
        {
            this.Type   = track?.Type ?? SpotifyTrackType.Unknown;
            this.Title  = track?.Title;
            this.Length = track?.Length ?? 0;

            if (track?.Type == SpotifyTrackType.Song)
            {
                ISong song = track as ISong;
                this.Artists = song?.Artists.ToList();
                this.Album   = song?.Album;
            }
        }
Ejemplo n.º 5
0
        public static bool Equal(ISpotifyTrack s1, ISpotifyTrack s2)
        {
            if (ReferenceEquals(s1, s2))
            {
                return(true);
            }
            if (s1 == null || s2 == null)
            {
                return(false);
            }

            return(s1.Equals((object)s2));
        }
Ejemplo n.º 6
0
        private async void SpotifyWindowTitleWatcher_TitleChanged(object sender, WindowTitleChangedEventArgs e)
        {
            // TODO: Refactor this method
            try
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug($"Spotify's window title changed: \"{e.NewTitle}\". Fetching song info...");
                }

                if (!(Settings.Current.EnableSpotifyWebApi && this.IsWebApiRunning &&
                      await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false)))
                {
                    // If the WebAPIs are disabled or they weren't able to retrieve the song info, fallback to
                    // the old method based on the title of Spotify's window.

                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug("Fetching song info using old method based on the title of Spotify's window...");
                    }

                    bool updateSong = true;
                    if (SpotifyWindow.PausedTitles.Contains(e.NewTitle, StringComparer.InvariantCulture))
                    {
                        await this.OnPlayStateChanged(false).ConfigureAwait(false);

                        updateSong = false;
                    }
                    else if (!this.IsPlaying)
                    {
                        await this.OnPlayStateChanged(true).ConfigureAwait(false);
                    }

                    if (updateSong)
                    {
                        ISpotifyTrack newSong = Song.FromSpotifyWindowTitle(e.NewTitle);
                        if (!SpotifyTrack.Equal(this.CurrentTrack, newSong))
                        {
                            ISpotifyTrack oldSong = this.CurrentTrack;
                            this.CurrentTrack = newSong;
                            await this.OnTrackChanged(oldSong).ConfigureAwait(false);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                logger.Error($"Unhandled exception in {nameof(this.SpotifyWindowTitleWatcher_TitleChanged)}.", exception);
            }
        }
Ejemplo n.º 7
0
        public bool Equals(ISpotifyTrack other)
        {
            if (other == null)
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            return(this.Type == other.Type &&
                   string.Equals(this.Title, other.Title, StringComparison.InvariantCulture) &&
                   this.Length == other.Length);
        }
Ejemplo n.º 8
0
 private async Task OnTrackChanged(ISpotifyTrack previousTrack)
 {
     this.TrackChanged?.Invoke(this, new SpotifyTrackChangedEventArgs(previousTrack, this.CurrentTrack));
     await this.Broadcaster.BroadcastCurrentTrack(this.CurrentTrack).ConfigureAwait(false);
 }
 public SpotifyTrackChangedEventArgs(ISpotifyTrack previousTrack, ISpotifyTrack newTrack)
 {
     this.PreviousTrack = previousTrack;
     this.NewTrack      = newTrack;
 }
Ejemplo n.º 10
0
        private async void SpotifyWindowTitleWatcher_TitleChanged(object sender, WindowTitleChangedEventArgs e)
        {
            // TODO: Refactor this method
            try
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug($"Spotify's window title changed: \"{e.NewTitle}\". Fetching song info...");
                }

                bool shouldUpdateUsingWindowTitle = !(Settings.Current.EnableSpotifyWebApi && this.IsWebApiRunning);

                this.apiTrackDelayedUpdateTimer?.Dispose();
                bool tooFast = this.songChangesInTimespan >= 3;
                if (!shouldUpdateUsingWindowTitle && tooFast)
                {
                    logger.Debug($"Songs are being changed too fast ({this.songChangesInTimespan} times in {this.songChangeBuffer.TotalMilliseconds} ms)!");
                    this.apiTrackDelayedUpdateTimer = new Timer(async state =>
                    {
                        logger.Debug($"Executing delayed track update using WebAPI (\"{state}\")");
                        await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false);
                    }, e.NewTitle, this.songChangeBuffer, TimeSpan.FromMilliseconds(-1));
                    shouldUpdateUsingWindowTitle = true;
                }

                if (!shouldUpdateUsingWindowTitle)
                {
                    shouldUpdateUsingWindowTitle = !await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false);
                }

                if (shouldUpdateUsingWindowTitle)
                {
                    // If the WebAPIs are disabled or they weren't able to retrieve the song info, fallback to
                    // the old method based on the title of Spotify's window.

                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug("Fetching song info using old method based on the title of Spotify's window...");
                    }

                    bool updateSong = true;
                    if (SpotifyWindow.PausedTitles.Contains(e.NewTitle, StringComparer.InvariantCulture))
                    {
                        await this.OnPlayStateChanged(false).ConfigureAwait(false);

                        updateSong = false;
                    }
                    else if (!this.IsPlaying)
                    {
                        await this.OnPlayStateChanged(true).ConfigureAwait(false);
                    }

                    if (updateSong)
                    {
                        ISpotifyTrack newSong = Song.FromSpotifyWindowTitle(e.NewTitle);
                        if (!SpotifyTrack.Equal(this.CurrentTrack, newSong))
                        {
                            ISpotifyTrack oldSong = this.CurrentTrack;
                            this.CurrentTrack = newSong;
                            await this.OnTrackChanged(oldSong).ConfigureAwait(false);
                        }
                    }
                }

                this.songChangesInTimespan++;
                if (DateTime.Now - this.lastSongChange > this.songChangeBuffer)
                {
                    this.songChangesInTimespan = 0;
                }
                this.lastSongChange = DateTime.Now;
            }
            catch (Exception exception)
            {
                logger.Error($"Unhandled exception in {nameof(this.SpotifyWindowTitleWatcher_TitleChanged)}.", exception);
            }
        }