UpdateText() 공개 정적인 메소드

public static UpdateText ( string text ) : void
text string
리턴 void
예제 #1
0
파일: iTunes.cs 프로젝트: jaylapham/Snip
        private void App_OnPlayerPlayingTrackChangedEvent(object sender)
        {
            IITTrack track = this.iTunesApplication.CurrentTrack;

            if (!string.IsNullOrEmpty(track.Artist) && !string.IsNullOrEmpty(track.Name) && string.IsNullOrEmpty(this.iTunesApplication.CurrentStreamTitle))
            {
                if (Globals.SaveAlbumArtwork)
                {
                    try
                    {
                        IITArtworkCollection artworkCollection = track.Artwork;
                        IITArtwork           artwork           = artworkCollection[1];

                        artwork.SaveArtworkToFile(this.DefaultArtworkFilePath);
                    }
                    catch
                    {
                        this.SaveBlankImage();
                        throw;
                    }
                }

                TextHandler.UpdateText(track.Name, track.Artist, track.Album);
            }
            else if (!string.IsNullOrEmpty(this.iTunesApplication.CurrentStreamTitle))
            {
                TextHandler.UpdateText(this.iTunesApplication.CurrentStreamTitle);
            }
        }
예제 #2
0
파일: Spotify.cs 프로젝트: neviim/Snip
        private void UpdateSpotifyTrackInformation_Elapsed(object sender, ElapsedEventArgs e)
        {
            string downloadedJson = this.DownloadJson("https://api.spotify.com/v1/me/player/currently-playing", SpotifyAddressContactType.API);

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

                bool   isPlaying = (bool)jsonSummary.is_playing;
                string trackId   = (string)jsonSummary.item.id;

                if (isPlaying)
                {
                    // Only update if the track ID has changed or the user updates how the output format should look
                    if (trackId != this.lastTrackId || Globals.RewriteUpdatedOutputFormat)
                    {
                        Globals.RewriteUpdatedOutputFormat = false;
                        this.lastTrackId = trackId;

                        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(),
                            jsonSummary.item.id.ToString(),
                            downloadedJson);

                        if (Globals.SaveAlbumArtwork)
                        {
                            this.DownloadSpotifyAlbumArtwork(jsonSummary.item.album);
                        }
                    }
                }
                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;
            }
        }
예제 #3
0
        public override void Update()
        {
            Process[] processes = Process.GetProcessesByName("vlc");

            if (processes.Length > 0)
            {
                string vlcTitle = string.Empty;

                foreach (Process process in processes)
                {
                    vlcTitle = process.MainWindowTitle;
                }

                // Check for a hyphen in the title. If a hyphen exists then we need to cut all of the text after the last
                // hyphen because that's the "VLC media player" text, which can vary based on language.
                // If no hyphen exists then VLC is not playing anything.
                int lastHyphen = vlcTitle.LastIndexOf("-", StringComparison.OrdinalIgnoreCase);

                if (lastHyphen > 0)
                {
                    vlcTitle = vlcTitle.Substring(0, lastHyphen).Trim();

                    string vlcExtension = System.IO.Path.GetExtension(vlcTitle);

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

                    // Filter file extension
                    // VLC doesn't always have file extensions show in the title.
                    // The previous code would sometimes cut song titles prematurely if there was no extension in the title.
                    // If there's an extension, we'll trim it. Otherwise, we won't do anything to the string.
                    if (vlcExtension.Length > 0)
                    {
                        int lastDot = vlcTitle.LastIndexOf(vlcExtension);

                        if (lastDot > 0)
                        {
                            vlcTitle = vlcTitle.Substring(0, lastDot).Trim();
                        }
                    }

                    TextHandler.UpdateText(vlcTitle);
                }
                else
                {
                    TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                }
            }
            else
            {
                if (Globals.SaveAlbumArtwork)
                {
                    this.SaveBlankImage();
                }

                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("VLCIsNotRunning"));
            }
        }
예제 #4
0
        public override void Update()
        {
            Process[] processes = Process.GetProcessesByName("vlc");

            if (processes.Length > 0)
            {
                string vlcTitle = string.Empty;

                foreach (Process process in processes)
                {
                    vlcTitle = process.MainWindowTitle;
                }

                // Check for a hyphen in the title. If a hyphen exists then we need to cut all of the text after the last
                // hyphen because that's the "VLC media player" text, which can vary based on language.
                // If no hyphen exists then VLC is not playing anything.
                int lastHyphen = vlcTitle.LastIndexOf("-", StringComparison.OrdinalIgnoreCase);

                if (lastHyphen > 0)
                {
                    vlcTitle = vlcTitle.Substring(0, lastHyphen).Trim();

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

                    // Filter file extension
                    int lastDot = vlcTitle.LastIndexOf('.');

                    if (lastDot > 0)
                    {
                        vlcTitle = vlcTitle.Substring(0, lastDot).Trim();
                    }

                    TextHandler.UpdateText(vlcTitle);
                }
                else
                {
                    TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                }
            }
            else
            {
                if (Globals.SaveAlbumArtwork)
                {
                    this.SaveBlankImage();
                }

                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("VLCIsNotRunning"));
            }
        }
예제 #5
0
파일: iTunes.cs 프로젝트: ToppleTheNun/Snip
        private void App_OnPlayerStopEvent(object o)
        {
            if (Globals.SaveAlbumArtwork)
            {
                this.SaveBlankImage();
            }

            if (Globals.EmptyFileIfNoTrackPlaying)
            {
                TextHandler.UpdateTextAndEmptyFile(Globals.ResourceManager.GetString("NoTrackPlaying"));
            }
            else
            {
                TextHandler.UpdateText(Globals.ResourceManager.GetString("NoTrackPlaying"));
            }
        }
예제 #6
0
파일: iTunes.cs 프로젝트: ToppleTheNun/Snip
        private void App_OnPlayerQuittingEvent()
        {
            if (Globals.SaveAlbumArtwork)
            {
                this.SaveBlankImage();
            }

            if (Globals.EmptyFileIfNoTrackPlaying)
            {
                TextHandler.UpdateTextAndEmptyFile(Globals.ResourceManager.GetString("iTunesNotRunning"));
            }
            else
            {
                TextHandler.UpdateText(Globals.ResourceManager.GetString("iTunesNotRunning"));
            }
        }
예제 #7
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();
                    }
                }
            }
        }
예제 #8
0
파일: Spotify.cs 프로젝트: dxball/Snip
        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;
                    }
                }
            }
        }
예제 #9
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;
                    }
                }
            }
        }
예제 #10
0
파일: Winamp.cs 프로젝트: piccolo456/Snip
        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;
                    }
                }
            }
        }
예제 #11
0
        private void ContactSpotifyLocalServerTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            string trackInformation = this.GetTrackInformation();

            // We should only be here if Spotify actually is running
            if (!string.IsNullOrEmpty(trackInformation))
            {
                // Get the handle so that hotkeys can be used
                this.GetSpotifyWindowHandle();

                dynamic jsonSummary = SimpleJson.DeserializeObject(trackInformation);

                if (jsonSummary != null)
                {
                    bool spotifyPlaying = Convert.ToBoolean(jsonSummary.playing);

                    if (!spotifyPlaying)
                    {
                        this.ResetSnipSinceSpotifyIsNotPlaying();
                    }
                    else
                    {
                        string fullTrackId = jsonSummary.track.track_resource.uri.ToString();
                        string trackId     = fullTrackId.Substring(fullTrackId.LastIndexOf(':') + 1); // + 1 to not include :

                        // Only update if the title has changed or the user updates how the output format should look
                        if (trackId != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                        {
                            Globals.RewriteUpdatedOutputFormat = false;

                            string json = string.Empty;

                            if (Globals.CacheSpotifyMetadata)
                            {
                                json = this.ReadCachedJson(trackId);
                            }
                            else
                            {
                                json = this.DownloadJson(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        "https://api.spotify.com/v1/tracks/{0}",
                                        trackId),
                                    SpotifyAddressContactType.API);
                            }

                            jsonSummary = SimpleJson.DeserializeObject(json);

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

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

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

                            TextHandler.UpdateText(
                                jsonSummary.name.ToString(),
                                artists,
                                jsonSummary.album.name.ToString(),
                                jsonSummary.id.ToString());

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

                            // Set the last title to the track id as these are unique values that only change when the track changes
                            this.LastTitle = trackId;

                            this.snipReset = false;
                        }
                    }
                }
            }
        }
예제 #12
0
파일: Spotify.cs 프로젝트: Deddie94/Snip
        private void UpdateSpotifyTrackTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // Locate and detect Spotify and get handles
            this.SetUpSpotifyHandles();

            // If the process ID is greater than 0 then we found the process
            if (this.processId > 0)
            {
                // The track ID is 22 bytes long
                byte[] trackIdInBytes = new byte[22];

                // We can use this to determine if Spotify is playing or not.
                // If Spotify is not playing the titlebar will be "Spotify". Otherwise it
                // will contain the artist and track title.
                string windowTitle = ProcessFunctions.GetProcessWindowTitle(this.processId);

                byte[] searchBytes      = Encoding.Default.GetBytes(this.searchString);
                int    addressOfTrackId = ProcessFunctions.FindInMemory(this.processId, this.moduleBaseAddress, this.moduleLength, searchBytes);
                // 14 is the offset where the trackId begins, trackId is 22 chars long
                trackIdInBytes = ProcessFunctions.ReadMemory(this.Handle, (IntPtr)addressOfTrackId + 14, 22);

                if (windowTitle == "Spotify Premium" || windowTitle == "Spotify Free" || windowTitle == "Spotify")
                {
                    // Because we search for this first there's a brief moment on startup where it may display
                    // that no track is playing before it states that Spotify is not running.
                    this.ResetSnipSinceSpotifyIsNotPlaying();
                }
                else
                {
                    string trackId = Encoding.Default.GetString(trackIdInBytes);

                    // Only update if the title has changed or the user updates how the output format should look
                    if (trackId != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                    {
                        Globals.RewriteUpdatedOutputFormat = false;

                        string json = string.Empty;

                        if (Globals.CacheSpotifyMetadata)
                        {
                            json = this.ReadCachedJson(trackId);
                        }
                        else
                        {
                            json = this.DownloadJson(
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    "https://api.spotify.com/v1/tracks/{0}",
                                    trackId),
                                SpotifyAddressContactType.API);
                        }

                        // This shouldn't happen... but you never know.
                        if (!string.IsNullOrEmpty(json))
                        {
                            dynamic jsonSummary = SimpleJson.DeserializeObject(json);

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

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

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

                            TextHandler.UpdateText(
                                jsonSummary.name.ToString(),
                                artists,
                                jsonSummary.album.name.ToString(),
                                jsonSummary.id.ToString(),
                                jsonSummary.ToString());

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

                            // Set the last title to the track id as these are unique values that only change when the track changes
                            this.LastTitle = trackId;
                        }
                    }
                }
            }
        }
예제 #13
0
파일: VLC.cs 프로젝트: jaylapham/Snip
        public override void Update()
        {
            Process[] processes = Process.GetProcessesByName("vlc");

            if (processes.Length > 0)
            {
                string vlcTitle = string.Empty;

                foreach (Process process in processes)
                {
                    vlcTitle = process.MainWindowTitle;
                }


                // Check for a hyphen in the title. If a hyphen exists then we need to cut all of the text after the last
                // hyphen because that's the "VLC media player" text, which can vary based on language.
                // If no hyphen exists then VLC is not playing anything.
                int lastHyphen = vlcTitle.LastIndexOf("-", StringComparison.OrdinalIgnoreCase);

                if (lastHyphen > 0)
                {
                    vlcTitle = vlcTitle.Substring(0, lastHyphen).Trim();

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

                    // Filter file extension
                    // Using the previous method of using System.IO.Path to grab the file extension caused some problems.
                    // It treated the title as a path, restricting what characters were allowed in the titles.
                    // I changed it back to a similar version of the old method.
                    // Now we'll check if the section of the title after the dot is greater than 4 characters, and less than 5 (the dot is included)
                    // This is done because common file extensions are typically 3 characters long, with some exceptions like flac and aiff being 4.
                    // Additionally, we'll check if there's a space anywhere after the last dot. Extensions will not have spaces in them.
                    //
                    // Alternatively, you can use System.IO.Path, make it system dependent, and replace characters like " and | with
                    // equivalents.
                    //
                    // TODO:
                    // It may be best to just remove common extensions by name, i.e. cut off ".mp3", ".flac", etc.
                    int lastDot = vlcTitle.LastIndexOf(".", StringComparison.OrdinalIgnoreCase);
                    if (lastDot > 0)
                    {
                        string vlcTitleExtension = vlcTitle.Substring(lastDot);
                        if (vlcTitleExtension.Length >= 4 && vlcTitleExtension.Length <= 5 && !vlcTitleExtension.Contains(" "))
                        {
                            vlcTitle = vlcTitle.Substring(0, lastDot).Trim();
                        }
                    }

                    TextHandler.UpdateText(vlcTitle);
                }
                else
                {
                    TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("NoTrackPlaying"));
                }
            }
            else
            {
                if (Globals.SaveAlbumArtwork)
                {
                    this.SaveBlankImage();
                }

                TextHandler.UpdateTextAndEmptyFilesMaybe(Globals.ResourceManager.GetString("VLCIsNotRunning"));
            }
        }
예제 #14
0
파일: Spotify.cs 프로젝트: PlumpMath/Snip
        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();
                        }
                    }
                }
            }
        }
예제 #15
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;
                    }
                }
            }
        }
예제 #16
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;
                    }
                }
            }
        }
예제 #17
0
파일: Spotify.cs 프로젝트: hylianux/Snip
        private void UpdateSpotifyTrackInformation_Elapsed(object sender, ElapsedEventArgs e)
        {
            Process[] processes = Process.GetProcessesByName("spotify");
            foreach (Process localSpotifyProcess in processes)
            {
                // There's probably a better way to do this but for the sake of getting this going this will work
                // Spotify creates a lot of processes... but only one is actually going to have text in the window title.
                if (localSpotifyProcess.MainWindowTitle.Length > 0)
                {
                    this.spotifyProcess = localSpotifyProcess;
                }
            }

            string spotifyTitle = this.spotifyProcess.MainWindowTitle;

            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;
                }
            }
        }
예제 #18
0
        private void CheckPlaybackTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // There's really no way of knowing if the player is running or not
            // Just check the JSON file for playback status repeatedly

            // Do nothing if the playback file doesn't exist
            if (File.Exists(this.pathToPlaybackJson))
            {
                string json = string.Empty;

                FileStream fileStream = null;
                try
                {
                    fileStream = File.Open(pathToPlaybackJson, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    using (StreamReader streamReader = new StreamReader(fileStream))
                    {
                        fileStream = null;
                        json       = streamReader.ReadToEnd();
                    }
                }
                finally
                {
                    if (fileStream != null)
                    {
                        fileStream.Dispose();
                    }
                }

                dynamic jsonSummary = SimpleJson.DeserializeObject(json);

                if (jsonSummary != null)
                {
                    bool gpmdpPlaying = Convert.ToBoolean(jsonSummary.playing);

                    if (!gpmdpPlaying)
                    {
                        this.ResetSnipSinceGPMDPIsNotPlaying();
                    }
                    else
                    {
                        string trackTitle    = jsonSummary.song.title.ToString();
                        string trackArtist   = jsonSummary.song.artist.ToString();
                        string trackAlbum    = jsonSummary.song.album.ToString();
                        string trackAlbumArt = jsonSummary.song.albumArt.ToString();

                        string lastTrack = trackTitle + trackArtist;

                        if (lastTrack != this.LastTitle || Globals.RewriteUpdatedOutputFormat)
                        {
                            Globals.RewriteUpdatedOutputFormat = false;

                            if (!string.IsNullOrEmpty(trackTitle) && !string.IsNullOrEmpty(trackArtist) && !string.IsNullOrEmpty(trackAlbum))
                            {
                                TextHandler.UpdateText(
                                    trackTitle,
                                    trackArtist,
                                    trackAlbum);
                            }
                            else if (!string.IsNullOrEmpty(trackTitle) && !string.IsNullOrEmpty(trackArtist))
                            {
                                TextHandler.UpdateText(
                                    trackTitle,
                                    trackArtist);
                            }
                            else
                            {
                                // We shouldn't be here...
                            }

                            if (Globals.SaveAlbumArtwork && !string.IsNullOrEmpty(trackAlbumArt))
                            {
                                this.DownloadGPMDPAlbumArtwork(trackAlbumArt);
                            }

                            this.LastTitle = lastTrack;

                            this.gpmdpReset = false;
                        }
                    }
                }
            }
        }