Ejemplo n.º 1
0
        private void UpdateSpotifyTrackInformation_Elapsed(object sender, ElapsedEventArgs e)
        {
            // Get a list of all spotify.exe processes
            Process[] processes = Process.GetProcessesByName("spotify");

            // Array must be greater than 0 to continue (as in a process was successfully found)
            if (processes.Length > 0)
            {
                // We know Spotify is running at least
                this.spotifyRunning = true;

                // Only loop through and get the process handle if it's not set
                if (this.spotifyHandle == IntPtr.Zero)
                {
                    // Loop through all processes that matched spotify.exe
                    foreach (Process localSpotifyProcess in processes)
                    {
                        StringBuilder className = new StringBuilder(64);

                        UnsafeNativeMethods.GetClassName(localSpotifyProcess.MainWindowHandle, className, className.Capacity);

                        string classNameText = className.ToString();

                        // Spotify's main window that has the song name in the titlebar uses the Chrome_WidgetWin_0 class
                        // We also need to verify that the titlebar has something in it due to multiple processes sharing the same class
                        // From everything I've seen the titlebar will _always_ have at least some text
                        if (classNameText == "Chrome_WidgetWin_0" && localSpotifyProcess.MainWindowTitle.Length > 0)
                        {
                            this.spotifyHandle = localSpotifyProcess.MainWindowHandle;
                        }
                    }
                }
            }
            else
            {
                this.ResetSnipSinceSpotifyIsNotPlaying();
            }

            if (this.spotifyRunning == true && this.spotifyHandle != IntPtr.Zero)
            {
                // We'll limit the window title string to 256 characters
                // The length really doesn't matter since we're just storing what appears and comparing it
                // Even if it gets cut off it should compare well up to 256 characters
                StringBuilder stringBuilder = new StringBuilder(256);

                // Get the window title of Spotify and store it in stringBuilder
                UnsafeNativeMethods.GetWindowText(this.spotifyHandle, stringBuilder, stringBuilder.Capacity);

                // Convert the StringBuilder to a regular string
                string spotifyTitle = stringBuilder.ToString();

                if (spotifyTitle.Length > 0)
                {
                    if (spotifyTitle != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                    {
                        Globals.RewriteUpdatedOutputFormat = false;

                        if (spotifyTitle == "Spotify" || spotifyTitle == "Spotify Free" || spotifyTitle == "Spotify Premium")
                        {
                            if (Globals.SaveAlbumArtwork)
                            {
                                this.SaveBlankImage();
                            }

                            TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                        }
                        else
                        {
                            string downloadedJson = this.DownloadJson("https://api.spotify.com/v1/me/player/currently-playing", SpotifyAddressContactType.API);

                            if (!string.IsNullOrEmpty(downloadedJson))
                            {
                                dynamic jsonSummary = SimpleJson.DeserializeObject(downloadedJson);

                                string currentlyPlayingType = (string)jsonSummary.currently_playing_type;

                                // Spotify does not provide any detailed information for anything other than actual songs.
                                // Podcasts have types of "episode" but do not contain any useful data unfortunately.
                                if (currentlyPlayingType == "track")
                                {
                                    bool   isPlaying = (bool)jsonSummary.is_playing;
                                    string trackId   = (string)jsonSummary.item.id;

                                    if (isPlaying)
                                    {
                                        if (Globals.CacheSpotifyMetadata)
                                        {
                                            downloadedJson = this.ReadCachedJson(trackId);
                                        }

                                        // If there are multiple artists we want to join all of them together for display
                                        string artists = string.Empty;

                                        foreach (dynamic artist in jsonSummary.item.artists)
                                        {
                                            artists += artist.name.ToString() + ", ";
                                        }

                                        artists = artists.Substring(0, artists.LastIndexOf(',')); // Removes last comma

                                        TextHandler.UpdateText(
                                            jsonSummary.item.name.ToString(),
                                            artists,
                                            jsonSummary.item.album.name.ToString(),
                                            trackId,
                                            downloadedJson);

                                        if (Globals.SaveAlbumArtwork)
                                        {
                                            this.DownloadSpotifyAlbumArtwork(jsonSummary.item.album);
                                        }
                                    }
                                    else
                                    {
                                        this.ResetSnipSinceSpotifyIsNotPlaying();
                                    }
                                }
                                else
                                {
                                    this.ResetSnipSinceSpotifyIsNotPlaying();
                                }

                                // Reset timer after it was potentially changed by rate limit
                                // Since we should only reach this point if valid JSON was obtained this means
                                // that the timer shouldn't reset unless there was a success.
                                this.updateSpotifyTrackInformation.Enabled  = false;
                                this.updateSpotifyTrackInformation.Interval = updateSpotifyTrackInformationDefaultInterval;
                                this.updateSpotifyTrackInformation.Enabled  = true;
                            }
                        }

                        this.LastTitle = spotifyTitle;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public override void Update()
        {
            if (!this.Found)
            {
                this.Handle = UnsafeNativeMethods.FindWindow("SpotifyMainWindow", null);

                this.Found      = true;
                this.NotRunning = false;
            }
            else
            {
                // Make sure the process is still valid.
                if (this.Handle != IntPtr.Zero && this.Handle != null)
                {
                    int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                    string spotifyTitle = this.Title.ToString();

                    this.Title.Clear();

                    // If the window title length is 0 then the process handle is not valid.
                    if (windowTextLength > 0)
                    {
                        // Only update if the title has actually changed.
                        // This prevents unnecessary calls and downloads.
                        if (spotifyTitle != this.LastTitle)
                        {
                            if (spotifyTitle == "Spotify")
                            {
                                if (Globals.SaveAlbumArtwork)
                                {
                                    this.SaveBlankImage();
                                }

                                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                            }
                            else
                            {
                                this.DownloadJson(spotifyTitle);

                                dynamic jsonSummary = SimpleJson.DeserializeObject(this.json);

                                if (jsonSummary != null)
                                {
                                    var numberOfResults = jsonSummary.tracks.total;

                                    if (numberOfResults > 0)
                                    {
                                        jsonSummary = SimpleJson.DeserializeObject(jsonSummary.tracks["items"].ToString());

                                        TextHandler.UpdateText(
                                            jsonSummary[0].name.ToString(),
                                            jsonSummary[0].artists[0].name.ToString(),
                                            jsonSummary[0].album.name.ToString());

                                        if (Globals.SaveAlbumArtwork)
                                        {
                                            this.HandleSpotifyAlbumArtwork(jsonSummary[0].name.ToString());
                                        }
                                    }
                                    else
                                    {
                                        // In the event of an advertisement (or any song that returns 0 results)
                                        // then we'll just write the whole title as a single string instead.
                                        TextHandler.UpdateText(spotifyTitle);
                                    }
                                }
                            }

                            this.LastTitle = spotifyTitle;
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            this.ResetSinceSpotifyIsNotRunning();
                        }
                    }
                }
                else
                {
                    if (!this.NotRunning)
                    {
                        this.ResetSinceSpotifyIsNotRunning();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public override void Update()
        {
            if (!this.Found)
            {
                System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("foobar2000");

                if (processes.Length > 0)
                {
                    this.Handle = processes[0].MainWindowHandle;
                }

                foreach (var process in processes)
                {
                    process.Dispose();
                }

                processes = null;

                this.Found      = true;
                this.NotRunning = false;
            }
            else
            {
                // Make sure the process is still valid.
                if (this.Handle != IntPtr.Zero && this.Handle != null)
                {
                    int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                    string foobar2000Title = this.Title.ToString();

                    this.Title.Clear();

                    // If the window title length is 0 then the process handle is not valid.
                    if (windowTextLength > 0)
                    {
                        // Only update the system tray text and text file text if the title changes.
                        if (foobar2000Title != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                        {
                            Globals.RewriteUpdatedOutputFormat = false;

                            if (foobar2000Title.StartsWith("foobar2000", StringComparison.OrdinalIgnoreCase))
                            {
                                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                            }
                            else
                            {
                                // Winamp window titles look like "[%album artist% - ]['['%album%[ CD%discnumber%][ #%tracknumber%]']' ]%title%[ '//' %track artist%]".
                                // Require that the user use ATF and replace the format with something like:
                                // %artist% – %title%
                                string   windowTitleFull = System.Text.RegularExpressions.Regex.Replace(foobar2000Title, @"\s+\[foobar2000.+\]", string.Empty);
                                string[] windowTitle     = windowTitleFull.Split('–');

                                // Album artwork not supported by foobar2000
                                if (Globals.SaveAlbumArtwork)
                                {
                                    this.SaveBlankImage();
                                }

                                if (windowTitle.Length > 1)
                                {
                                    string artist    = windowTitle[0].Trim();
                                    string songTitle = windowTitle[1].Trim();

                                    TextHandler.UpdateText(songTitle, artist);
                                }
                                else
                                {
                                    TextHandler.UpdateText(windowTitle[0].Trim());
                                }
                            }

                            this.LastTitle = foobar2000Title;
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            if (Globals.SaveAlbumArtwork)
                            {
                                this.SaveBlankImage();
                            }

                            TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("foobar2000IsNotRunning"));

                            this.Found      = false;
                            this.NotRunning = true;
                        }
                    }
                }
                else
                {
                    if (!this.NotRunning)
                    {
                        if (Globals.SaveAlbumArtwork)
                        {
                            this.SaveBlankImage();
                        }

                        TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("foobar2000IsNotRunning"));

                        this.Found      = false;
                        this.NotRunning = true;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public override void Update()
        {
            if (!this.Found)
            {
                this.Handle = UnsafeNativeMethods.FindWindow("Winamp v1.x", null);

                this.Found      = true;
                this.NotRunning = false;
            }
            else
            {
                // Make sure the process is still valid.
                if (this.Handle != IntPtr.Zero && this.Handle != null)
                {
                    int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                    string winampTitle = this.Title.ToString();

                    this.Title.Clear();

                    // If the window title length is 0 then the process handle is not valid.
                    if (windowTextLength > 0)
                    {
                        // Only update the system tray text and text file text if the title changes.
                        if (winampTitle != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                        {
                            Globals.RewriteUpdatedOutputFormat = false;

                            if (winampTitle.Contains("- Winamp [Stopped]") || winampTitle.Contains("- Winamp [Paused]"))
                            {
                                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                            }
                            else
                            {
                                // Winamp window titles look like "#. Artist - Track - Winamp".
                                // Require that the user use ATF and replace the format with something like:
                                // %artist% – %title%
                                string   windowTitleFull = winampTitle.Replace("- Winamp", string.Empty);
                                string[] windowTitle     = windowTitleFull.Split('–');

                                // Album artwork not supported by Winamp
                                if (Globals.SaveAlbumArtwork)
                                {
                                    this.SaveBlankImage();
                                }

                                if (windowTitle.Length > 1)
                                {
                                    string artist    = windowTitle[0].Trim();
                                    string songTitle = windowTitle[1].Trim();

                                    TextHandler.UpdateText(songTitle, artist);
                                }
                                else
                                {
                                    TextHandler.UpdateText(windowTitle[0].Trim());
                                }
                            }

                            this.LastTitle = winampTitle;
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            if (Globals.SaveAlbumArtwork)
                            {
                                this.SaveBlankImage();
                            }

                            TextHandler.UpdateTextAndEmptyFilesMaybe(
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    Globals.ResourceManager.GetString("PlayerIsNotRunning"),
                                    Globals.ResourceManager.GetString("Winamp")));

                            this.Found      = false;
                            this.NotRunning = true;
                        }
                    }
                }
                else
                {
                    if (!this.NotRunning)
                    {
                        if (Globals.SaveAlbumArtwork)
                        {
                            this.SaveBlankImage();
                        }

                        TextHandler.UpdateTextAndEmptyFilesMaybe(
                            string.Format(
                                CultureInfo.InvariantCulture,
                                Globals.ResourceManager.GetString("PlayerIsNotRunning"),
                                Globals.ResourceManager.GetString("Winamp")));

                        this.Found      = false;
                        this.NotRunning = true;
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public override void Update()
        {
            if (!this.Found)
            {
                this.Handle = UnsafeNativeMethods.FindWindow("QWidget", "VLC media player");

                this.Found      = true;
                this.NotRunning = false;
            }
            else
            {
                // Make sure the process is still valid.
                if (this.Handle != IntPtr.Zero && this.Handle != null)
                {
                    int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                    string vlcTitle = this.Title.ToString();

                    this.Title.Clear();

                    // If the window title length is 0 then the process handle is not valid.
                    if (windowTextLength > 0)
                    {
                        // Only update the system tray text and text file text if the title changes.
                        if (vlcTitle != this.LastTitle)
                        {
                            if (vlcTitle.Equals("VLC media player"))
                            {
                                if (Globals.EmptyFileIfNoTrackPlaying)
                                {
                                    TextHandler.UpdateTextAndEmptyFile(Globals.ResourceManager.GetString("NoTrackPlaying"));
                                }
                                else
                                {
                                    TextHandler.UpdateText(Globals.ResourceManager.GetString("NoTrackPlaying"));
                                }
                            }
                            else
                            {
                                string windowTitleFull = vlcTitle.Replace(" - VLC media player", string.Empty);

                                if (Globals.SaveAlbumArtwork)
                                {
                                    this.SaveBlankImage();
                                }

                                TextHandler.UpdateText(windowTitleFull.Trim());
                            }

                            this.LastTitle = vlcTitle;
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            if (Globals.SaveAlbumArtwork)
                            {
                                this.SaveBlankImage();
                            }

                            if (Globals.EmptyFileIfNoTrackPlaying)
                            {
                                TextHandler.UpdateTextAndEmptyFile(Globals.ResourceManager.GetString("VLCIsNotRunning"));
                            }
                            else
                            {
                                TextHandler.UpdateText(Globals.ResourceManager.GetString("VLCIsNotRunning"));
                            }

                            this.Found      = false;
                            this.NotRunning = true;
                        }
                    }
                }
                else
                {
                    if (!this.NotRunning)
                    {
                        if (Globals.SaveAlbumArtwork)
                        {
                            this.SaveBlankImage();
                        }

                        if (Globals.EmptyFileIfNoTrackPlaying)
                        {
                            TextHandler.UpdateTextAndEmptyFile(Globals.ResourceManager.GetString("VLCIsNotRunning"));
                        }
                        else
                        {
                            TextHandler.UpdateText(Globals.ResourceManager.GetString("VLCIsNotRunning"));
                        }

                        this.Found      = false;
                        this.NotRunning = true;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public override void Update()
        {
            if (!this.Found)
            {
                this.Handle = UnsafeNativeMethods.FindWindow("SpotifyMainWindow", null);

                this.Found      = true;
                this.NotRunning = false;
            }
            else
            {
                // Make sure the process is still valid.
                if (this.Handle != IntPtr.Zero && this.Handle != null)
                {
                    int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                    string spotifyTitle = this.Title.ToString();

                    this.Title.Clear();

                    // If the window title length is 0 then the process handle is not valid.
                    if (windowTextLength > 0)
                    {
                        // Only update the system tray text and text file text if the title changes.
                        if (spotifyTitle != this.LastTitle)
                        {
                            if (spotifyTitle == "Spotify")
                            {
                                if (Globals.SaveAlbumArtwork)
                                {
                                    this.SaveBlankImage();
                                }

                                if (Globals.EmptyFileIfNoTrackPlaying)
                                {
                                    TextHandler.UpdateTextAndEmptyFile(Globals.ResourceManager.GetString("NoTrackPlaying"));
                                }
                                else
                                {
                                    TextHandler.UpdateText(Globals.ResourceManager.GetString("NoTrackPlaying"));
                                }
                            }
                            else
                            {
                                // Spotify window titles look like "Spotify - Artist – Song Title".
                                string   windowTitleFull = spotifyTitle.Replace("Spotify - ", string.Empty);
                                string[] windowTitle     = windowTitleFull.Split('–');

                                string artist    = windowTitle[0].Trim();
                                string songTitle = windowTitle[1].Trim();
                                songTitle = songTitle.Replace(" - Explicit Album Version", string.Empty);

                                if (Globals.SaveAlbumArtwork || Globals.SaveSeparateFiles)
                                {
                                    this.DownloadJson(artist, songTitle);
                                }

                                if (Globals.SaveAlbumArtwork)
                                {
                                    this.HandleSpotifyAlbumArtwork(songTitle);
                                }

                                if (Globals.SaveSeparateFiles)
                                {
                                    string album = this.HandleSpotifyAlbumName();
                                    TextHandler.UpdateText(songTitle, artist, album);
                                }
                                else
                                {
                                    TextHandler.UpdateText(songTitle, artist);
                                }
                            }

                            this.LastTitle = spotifyTitle;
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            if (Globals.SaveAlbumArtwork)
                            {
                                this.SaveBlankImage();
                            }

                            if (Globals.EmptyFileIfNoTrackPlaying)
                            {
                                TextHandler.UpdateTextAndEmptyFile(Globals.ResourceManager.GetString("SpotifyIsNotRunning"));
                            }
                            else
                            {
                                TextHandler.UpdateText(Globals.ResourceManager.GetString("SpotifyIsNotRunning"));
                            }

                            this.Found      = false;
                            this.NotRunning = true;
                        }
                    }
                }
                else
                {
                    if (!this.NotRunning)
                    {
                        if (Globals.SaveAlbumArtwork)
                        {
                            this.SaveBlankImage();
                        }

                        if (Globals.EmptyFileIfNoTrackPlaying)
                        {
                            TextHandler.UpdateTextAndEmptyFile(Globals.ResourceManager.GetString("SpotifyIsNotRunning"));
                        }
                        else
                        {
                            TextHandler.UpdateText(Globals.ResourceManager.GetString("SpotifyIsNotRunning"));
                        }

                        this.Found      = false;
                        this.NotRunning = true;
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public override void Update()
        {
            // There's no sense in doing anything anymore without a valid token.
            if (!string.IsNullOrEmpty(this.token))
            {
                if (!this.Found)
                {
                    this.Handle = UnsafeNativeMethods.FindWindow("SpotifyMainWindow", null);

                    this.Found      = true;
                    this.NotRunning = false;
                }
                else
                {
                    // Make sure the process is still valid.
                    if (this.Handle != IntPtr.Zero && this.Handle != null)
                    {
                        int windowTextLength = UnsafeNativeMethods.GetWindowText(this.Handle, this.Title, this.Title.Capacity);

                        string spotifyTitle = this.Title.ToString();

                        this.Title.Clear();

                        // If the window title length is 0 then the process handle is not valid.
                        if (windowTextLength > 0)
                        {
                            // Only update if the title has actually changed.
                            // This prevents unnecessary calls and downloads.
                            if (spotifyTitle != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                            {
                                Globals.RewriteUpdatedOutputFormat = false;

                                if (spotifyTitle == "Spotify")
                                {
                                    if (Globals.SaveAlbumArtwork)
                                    {
                                        this.SaveBlankImage();
                                    }

                                    TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                                }
                                else
                                {
                                    this.DownloadJson(spotifyTitle);

                                    if (!string.IsNullOrEmpty(this.json))
                                    {
                                        dynamic jsonSummary = SimpleJson.DeserializeObject(this.json);

                                        if (jsonSummary != null)
                                        {
                                            var numberOfResults = jsonSummary.tracks.total;

                                            if (numberOfResults > 0)
                                            {
                                                jsonSummary = SimpleJson.DeserializeObject(jsonSummary.tracks["items"].ToString());

                                                int mostPopular = SelectTrackByPopularity(jsonSummary, spotifyTitle);

                                                TextHandler.UpdateText(
                                                    jsonSummary[mostPopular].name.ToString(),
                                                    jsonSummary[mostPopular].artists[0].name.ToString(),
                                                    jsonSummary[mostPopular].album.name.ToString(),
                                                    jsonSummary[mostPopular].id.ToString());

                                                if (Globals.SaveAlbumArtwork)
                                                {
                                                    this.DownloadSpotifyAlbumArtwork(jsonSummary[mostPopular].album);
                                                }
                                            }
                                            else
                                            {
                                                // In the event of an advertisement (or any song that returns 0 results)
                                                // then we'll just write the whole title as a single string instead.
                                                TextHandler.UpdateText(spotifyTitle);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // For whatever reason the JSON file couldn't download
                                        // In the event this happens we'll just display Spotify's window title as the track
                                        TextHandler.UpdateText(spotifyTitle);
                                    }
                                }

                                this.LastTitle = spotifyTitle;
                            }
                        }
                        else
                        {
                            if (!this.NotRunning)
                            {
                                this.ResetSinceSpotifyIsNotRunning();
                            }
                        }
                    }
                    else
                    {
                        if (!this.NotRunning)
                        {
                            this.ResetSinceSpotifyIsNotRunning();
                        }
                    }
                }
            }
        }