/** * Grabs the status of Spotify and returns a WebHelperResult object. **/ public static WebHelperResult GetStatus() { if (oauthToken == null || oauthToken == "null") { SetOAuth(); } if (csrfToken == null || csrfToken == "null") { SetCSRF(); } string result = ""; WebHelperResult whr = new WebHelperResult(); try { result = GetPage(GetURL("/remote/status.json" + "?oauth=" + oauthToken + "&csrf=" + csrfToken)); Debug.WriteLine(result); } catch (WebException ex) { Debug.WriteLine(ex); File.AppendAllText(Main.logPath, "WebHelperHook: " + ex.Message + "\r\n"); whr.isRunning = false; return(whr); } // Process data using (StringReader reader = new StringReader(result)) { string line; while ((line = reader.ReadLine()) != null) { if (line.Contains("\"running\":")) { whr.isRunning = line.Contains("true"); } // else if (line.Contains("\"track_type\":")) else if (line.Contains("\"next_enabled\":")) { whr.isAd = line.Contains("false"); } else if (line.Contains("\"private_session\":")) { whr.isPrivateSession = line.Contains("true"); } else if (line.Contains("\"playing\":")) { whr.isPlaying = line.Contains("true"); } /*else if (line.Contains("\"playing_position\":")) * { * if (!line.Contains("0,")) // Song isn't at 0 position * whr.position = Convert.ToSingle(line.Split(new char[] { ':', ',' })[1]); * }*/ else if (line.Contains("\"length\":")) { whr.length = Convert.ToInt32(line.Split(new char[] { ':', ',' })[1]); } else if (line.Contains("\"artist_resource\":")) { while ((line = reader.ReadLine()) != null) // Read until we find the "name" field { if (line.Contains("\"name\":")) { whr.artistName = (line.Replace("\"name\":", "").Split('"')[1]); break; } } } else if (line.Contains("Invalid Csrf token")) { Debug.WriteLine("Invalid CSRF token"); SetCSRF(); } else if (line.Contains("Invalid OAuth token")) { Debug.WriteLine("Invalid OAuth token"); SetOAuth(); } else if (line.Contains("Expired OAuth token")) { Debug.WriteLine("Expired OAuth token"); SetOAuth(); } } } return(whr); }
/** * Contains the logic for when to mute Spotify **/ private void MainTimer_Tick(object sender, EventArgs e) { try { if (Process.GetProcessesByName("spotify").Length < 1) { if (exitTolerance > 20) { File.AppendAllText(logPath, "Spotify process not found\r\n"); Notify("Spotify not found, please restart SpotBlocker."); if (exitTolerance > 22) { Notify("Exiting SpotBlocker."); Application.Exit(); } } exitTolerance += 1; } else { exitTolerance = 0; } WebHelperResult whr = WebHelperHook.GetStatus(); if (whr.isAd) // Track is ad { MainTimer.Interval = 1000; if (whr.isPlaying) { Debug.WriteLine("Ad is playing"); if (lastArtistName != whr.artistName) { if (!muted) { Mute(1); } artistTooltip.SetToolTip(StatusLabel, StatusLabel.Text = "Muting ad"); lastArtistName = whr.artistName; LogAction("/mute/" + whr.artistName); Debug.WriteLine("Blocked " + whr.artistName); } } else // Ad is paused { Debug.WriteLine("Ad is paused"); Resume(); } } else if (whr.isPrivateSession) { if (lastArtistName != whr.artistName) { StatusLabel.Text = "Playing: *Private Session*"; artistTooltip.SetToolTip(StatusLabel, ""); lastArtistName = whr.artistName; MessageBox.Show("Please disable 'Private Session' on Spotify for SpotBlocker to function properly.", "SpotBlocker", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); } } else if (!whr.isRunning) { StatusLabel.Text = "Spotify is not running"; artistTooltip.SetToolTip(StatusLabel, ""); //Notify("Error connecting to Spotify. Retrying..."); File.AppendAllText(logPath, "Not running.\r\n"); MainTimer.Interval = 5000; /* * MainTimer.Enabled = false; * MessageBox.Show("Spotify is not running. Please restart SpotBlocker after starting Spotify.", "SpotBlocker", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); * StatusLabel.Text = "Spotify is not running"; * Application.Exit(); */ } else if (!whr.isPlaying) { StatusLabel.Text = "Spotify is paused"; artistTooltip.SetToolTip(StatusLabel, lastArtistName = ""); } else // Song is playing { if (muted) { Mute(0); } if (MainTimer.Interval > 600) { MainTimer.Interval = 600; } if (lastArtistName != whr.artistName) { StatusLabel.Text = "Playing: " + ShortenName(whr.artistName); artistTooltip.SetToolTip(StatusLabel, lastArtistName = whr.artistName); } } } catch (Exception ex) { Debug.WriteLine(ex); File.AppendAllText(logPath, ex.Message); } }