Exemplo n.º 1
0
 public Main()
 {
     InitializeComponent();
     socket.Open();
     socket.On("pause", (data) =>
     {
         WebHelperResult whr = WebHelperHook.GetStatus();
         if (whr.isPlaying)
         {
             whr.isPlaying = false;
             BeginInvoke(new Action(this.Resume));
         }
     });
     socket.On("play", (data) =>
     {
         WebHelperResult whr = WebHelperHook.GetStatus();
         if (!whr.isPlaying)
         {
             whr.isPlaying = true;
             BeginInvoke(new Action(this.Resume));
         }
     });
 }
Exemplo n.º 2
0
        /**
         * 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("\"album_resource\":"))
                    {
                        while ((line = reader.ReadLine()) != null) // Read until we find the "name" field
                        {
                            if (line.Contains("\"name\":"))
                            {
                                whr.albumName = (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.songName = (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();
                    }
                }
            }

            return whr;
        }
Exemplo n.º 3
0
        /**
         * Contains the logic for when to mute Spotify
         **/
        private void MainTimer_Tick(object sender, EventArgs e)
        {
            try {
                if (Process.GetProcessesByName("spotify").Length < 1)
                {
                    File.AppendAllText(logPath, "Spotify process not found\r\n");
                    Notify("Exiting EZBlocker.");
                    Application.Exit();
                }

                WebHelperResult whr = WebHelperHook.GetStatus();

                if (whr.isAd) // Track is ad
                {
                    if (whr.isPlaying)
                    {
                        Debug.WriteLine("Ad is playing");
                        if (lastArtistName != whr.artistName)
                        {
                            if (!muted)
                            {
                                Mute(1);
                            }
                            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*";
                        lastArtistName   = whr.artistName;
                        MessageBox.Show("Please disable 'Private Session' on Spotify for EZBlocker to function properly.", "EZBlocker", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000);
                    }
                }
                else if (!whr.isRunning)
                {
                    StatusLabel.Text = "Spotify is not running";
                    //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 EZBlocker after starting Spotify.", "EZBlocker", 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";
                    lastArtistName   = "";
                }
                else // Song is playing
                {
                    if (muted)
                    {
                        Mute(0);
                    }
                    if (MainTimer.Interval > 1000)
                    {
                        MainTimer.Interval = 1000;
                    }
                    if (lastArtistName != whr.artistName)
                    {
                        StatusLabel.Text = "Playing: " + ShortenName(whr.artistName);
                        lastArtistName   = whr.artistName;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                File.AppendAllText(logPath, ex.Message);
            }
        }
Exemplo n.º 4
0
        /**
         * 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("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 = "";
            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");
            }

            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("\"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();
                    }
                }
            }

            return whr;
        }
Exemplo n.º 6
0
        /**
         * Contains the logic for when to mute Spotify
         **/
        private void TimerMain_Tick(object sender, EventArgs e)
        {
            try
            {
                if (Process.GetProcessesByName("spotify").Length < 1)
                {
                    NotifyBalloon("Exiting EZBlocker...", 10000);
                    noErrors = false;
                    Application.Exit();
                }

                WebHelperResult whr = WebHelperHook.GetStatus();

                if (whr.isAd)
                {
                    if (whr.isPlaying)
                    {
                        if (lastSongName != whr.songName)
                        {
                            if (!muted)
                            {
                                Mute(1);
                            }
                            lastSongName = "";
                            LabelMessage("Playing: Muted ad!", "");
                        }
                    }
                    else
                    {
                        lastSongName = "";
                        LabelMessage("Ad is paused!? Why?", "Resume it now please");
                    }
                }
                else if (whr.isPrivateSession)
                {
                    if (lastSongName != whr.songName)
                    {
                        lastSongName = whr.songName;
                        LabelMessage("Playing: *Private Session*", "Please disable 'Private Session' on Spotify for EZBlocker to function properly.");
                    }
                }
                else if (!whr.isRunning)
                {
                    lastSongName = "";
                    LabelMessage("Spotify is not playing", "");
                }
                else if (!whr.isPlaying)
                {
                    lastSongName = "";
                    LabelMessage("Spotify is paused", "");
                }
                else // Song is playing
                {
                    if (muted)
                    {
                        Mute(0);
                    }
                    if (lastSongName != whr.songName)
                    {
                        lastSongName = whr.songName;
                        LabelMessage("Playing: " + ShortenName(whr.songName), "Artist: " + ShortenName(whr.artistName));
                    }
                }
            }
            catch
            {
                LabelMessage("Is Internet availble?", "");
            }
        }
Exemplo n.º 7
0
        /**
         * Contains the logic for when to mute Spotify
         **/
        private void MainTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                WebHelperResult whr = WebHelperHook.GetStatus();
                //Console.WriteLine(whr.isAd);

                if (whr.isAd) // Track is ad
                {
                    if (whr.isPlaying)
                    {
                        if (lastArtistName != whr.artistName)
                        {
                            if (!muted)
                            {
                                Mute(1);
                            }
                            StatusLabel.Text = "Muting ad: " + ShortenName(whr.artistName);
                            lastArtistName   = whr.artistName;
                            LogAction("/mute/" + whr.artistName);
                            Console.WriteLine("Blocked " + whr.artistName);
                        }
                    }
                    else // Ad is paused
                    {
                        Resume();
                        StatusLabel.Text = "Muting: " + ShortenName(whr.artistName);
                    }
                }

                /*else if (whr.length - whr.position < 1.5) // Song is within last 1.5 seconds
                 * {
                 *  if (skipAds)
                 *      NextTrack();
                 * }*/
                else if (!whr.isRunning)
                {
                    StatusLabel.Text = "Spotify is not running";
                    lastArtistName   = "N/A";
                }
                else if (!whr.isPlaying)
                {
                    StatusLabel.Text = "Spotify is paused";
                    lastArtistName   = "N/A";
                }
                else // Song is playing
                {
                    if (muted)
                    {
                        Mute(0);
                    }
                    if (lastArtistName != whr.artistName)
                    {
                        StatusLabel.Text = "Playing: " + ShortenName(whr.artistName);
                        lastArtistName   = whr.artistName;
                    }
                }
            }
            catch (Exception except)
            {
                StatusLabel.Text = "Connection Error";
                WebHelperHook.CheckWebHelper();
                Console.WriteLine(except);
                File.WriteAllText(logPath, except.ToString());
            }
        }
Exemplo n.º 8
0
        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));
            }
            catch
            {
                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("\"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("\"length\":"))
                    {
                        whr.length = Convert.ToInt32(line.Split(new char[] { ':', ',' })[1]);
                    }
                    else if (line.Contains("\"track_resource\":"))
                    {
                        while ((line = reader.ReadLine()) != null) // Read until we find the "name" field
                        {
                            if (line.Contains("\"name\":"))
                            {
                                whr.songName = (line.Replace("\"name\":", "").Split('"')[1]);
                                break;
                            }
                        }
                    }
                    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"))
                    {
                        SetCSRF();
                    }
                    else if (line.Contains("Invalid OAuth token"))
                    {
                        SetOAuth();
                    }
                    else if (line.Contains("Expired OAuth token"))
                    {
                        SetOAuth();
                    }
                }
            }

            return(whr);
        }