private void Main_Timer_Tick(object sender, EventArgs e) { WebHelperResult whr = WebHelperHook.GetStatus(); try { if (!whr.isRunning) { StatusLabel.Text = "Spotify is not running"; lastArtistName = "N/A"; lblOpenSpotify.Visible = true; } else if (!whr.isPlaying) { StatusLabel.Text = "Spotify is paused"; lastArtistName = "N/A"; } else // Song is playing { if (lastArtistName != whr.artistName) { StatusLabel.Text = "Playing: " + whr.artistName + " - " + whr.trackName; lastArtistName = whr.artistName; File.WriteAllText(Settings.Default.SaveLocation, CustomOutput(Settings.Default.OutputText)); } } } catch (Exception except) { StatusLabel.Text = "Connection Error"; WebHelperHook.CheckWebHelper(); Console.WriteLine(except); File.WriteAllText(logPath, except.ToString()); } }
public string CustomOutput(string text) { WebHelperResult whr = WebHelperHook.GetStatus(); if (text != string.Empty) { string back; back = text.Replace("{artist}", whr.artistName).Replace("{song}", whr.trackName); return(back); } else { return(null); } }
public MainForm() { if (!HasNet35()) { MessageBox.Show(".Net Framework 3.5 not found. NPS may not work properly.", "NPS"); } InitializeComponent(); if (Settings.Default.FirstTime == 1) { Settings.Default.SaveLocation = Application.StartupPath + @"\NPS.txt"; Settings.Default.FirstTime = 0; Settings.Default.Save(); } try { if (!File.Exists(jsonPath)) { File.WriteAllBytes(jsonPath, Properties.Resources.Newtonsoft_Json); } } catch (Exception ex) { Debug.WriteLine(ex); MessageBox.Show("Error loading NPS dependencies. Please run NPS as administrator or put NPS in a user folder."); } WebHelperResult whr = WebHelperHook.GetStatus(); // Front Page enabled = false; if (!whr.isRunning) { StatusLabel.Text = "Spotify is not running"; lblOpenSpotify.Visible = true; } // Settings Page tbCustomizeOutput.Text = Settings.Default.OutputText; cbUpdate.Checked = Properties.Settings.Default.UpdateSettings; if (Properties.Settings.Default.UpdateSettings == true) { CheckUpdate(); } // About Page lblVersion.Text = "v." + Settings.Default.Version; // Else txtLocation.Text = Settings.Default.SaveLocation; try { if (!File.Exists(jsonPath)) { File.WriteAllBytes(jsonPath, Properties.Resources.Newtonsoft_Json); } } catch (Exception ex) { Debug.WriteLine(ex); MessageBox.Show("Error loading NPS dependencies. Please run NPS as administrator or put NPS in a user folder."); } }
/** * 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 = GetPage(GetURL("/remote/status.json" + "?oauth=" + oauthToken + "&csrf=" + csrfToken)); Console.WriteLine(result); WebHelperResult whr = new WebHelperResult(); // 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("\"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("\"track_resource\":")) { while ((line = reader.ReadLine()) != null) // Read until we find the "name" field { if (line.Contains("\"name\":")) { whr.trackName = (line.Replace("\"name\":", "").Split('"')[1]); break; } } } else if (line.Contains("Invalid Csrf token")) { csrfToken = null; } } } return(whr); }
/** * 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 = GetPage(GetURL("/remote/status.json" + "?oauth=" + oauthToken + "&csrf=" + csrfToken)); Console.WriteLine(result); WebHelperResult whr = new WebHelperResult(); // 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("\"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("\"track_resource\":")) { while ((line = reader.ReadLine()) != null) // Read until we find the "name" field { if (line.Contains("\"name\":")) { whr.trackName = (line.Replace("\"name\":", "").Split('"')[1]); break; } } } else if (line.Contains("Invalid Csrf token")) { csrfToken = null; } } } return whr; }