Baseball Scores

May 19, 2015

There is a lot of information available on the Internet, and a lot of APIs that make it readily accessible. Today’s exercise is intended to remind you of that, and to spur you to find out something interesting for yourself.

Your task is to write a program that fetches and displays information from the Internet; I chose to get baseball scores for my beloved Cardinals, but you are welcome to pick some other topic of interest to you. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisement

Pages: 1 2

One Response to “Baseball Scores”

  1. Ken said

    Heres How I did it, Kinda I thought this was cool so I tried it on the leader boards for a game I like to play with some friends, But I don’t think I did as well as it could be or like the example did since i’m only 19.

    class Program
        {
            static void Main(string[] args)
            {
    
                System.Net.HttpWebRequest Req = (HttpWebRequest)HttpWebRequest.Create("http://destinytracker.com/destiny/leaderboards/ps");
                System.Net.HttpWebResponse Res = (HttpWebResponse)Req.GetResponse();
                StreamReader Reader = new StreamReader(Res.GetResponseStream());
                try
                {
                    string Data = Reader.ReadLine();
    
                    // This is to skip the stuff we dont want.
                    for (int i = 0; i < 801; i++)
                        Data = Reader.ReadLine();
    
                    // Here we filter what we want from what is left.
                    for (int i = 0; i < 200; i++)
                    {
                        Data = Reader.ReadLine();
    
                        switch (Data)
                        {
                            case "<td>1</td>":
                                Console.WriteLine("1. " + Get_Name(Data, Reader) + "\n");
                                break;
                            case "<td>2</td>":
                                Console.WriteLine("2. " + Get_Name(Data, Reader) + "\n");
                                break;
                            case "<td>3</td>":
                                Console.WriteLine("3. " + Get_Name(Data, Reader) + "\n");
                                break;
                            case "<td>4</td>":
                                Console.WriteLine("4. " + Get_Name(Data, Reader) + "\n");
                                break;
                            case "<td>5</td>":
                                Console.WriteLine("5. " + Get_Name(Data, Reader) + "\n");
                                break;
                            case "<td>6</td>":
                                Console.WriteLine("6. " + Get_Name(Data, Reader) + "\n");
                                break;
                            case "<td>7</td>":
                                Console.WriteLine("7. " + Get_Name(Data, Reader) + "\n");
                                break;
                            case "<td>8</td>":
                                Console.WriteLine("8. " + Get_Name(Data, Reader) + "\n");
                                break;
                            case "<td>9</td>":
                                Console.WriteLine("9. " + Get_Name(Data, Reader) + "\n");
                                break;
                            case "<td>10</td>":
                                Console.WriteLine("10. " + Get_Name(Data, Reader) + "\n");
                                break;
                        }
                    }
                    // User Input to Terminate.
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("I see.. :/ \n");
                    Console.WriteLine(ex);
                    Console.ReadLine();
                }
            }
    
            /// <summary>
            /// Filters out the crap from what we want.
            /// </summary>
            /// <param name="_Data"></param>
            /// <param name="_Reader"></param>
            /// <returns></returns>
            public static string Get_Name(string _Data, StreamReader _Reader)
            {
                for (int i = 0; i < 3; i++)
                {
                    _Data = _Reader.ReadLine();
                }
                string Crude = _Data;
    
                //// Had to look up how to use String.Remove :/
                int foundS1 = Crude.IndexOf("<");
                int foundS2 = Crude.IndexOf(">", foundS1 + 1);
    
                if (foundS1 != foundS2 && foundS1 >= 0)
                    Crude = Crude.Remove(foundS1 + 1, foundS2 - foundS1);
                ////
    
                string[] Filter = Crude.Split('<');
    
                // This is what we want.
                string Name = Filter[1].ToString();
    
                return Name + "\n Level and Class: " + Get_LvlandClass(_Data, _Reader);
            }
    
            /// <summary>
            /// Simply moves down one line to get Level and Class
            /// </summary>
            /// <param name="_Data"></param>
            /// <param name="_Reader"></param>
            /// <returns></returns>
            public static string Get_LvlandClass(string _Data, StreamReader _Reader)
            {
                _Data = _Reader.ReadLine();
                return _Data + "\n Score: "+ Get_Score(_Data, _Reader); ;
            }
    
            /// <summary>
            /// Gets score... not sure how else to say it.
            /// </summary>
            /// <param name="_Data"></param>
            /// <param name="_Reader"></param>
            /// <returns></returns>
            public static string Get_Score(string _Data, StreamReader _Reader)
            {
                while (_Data != "<div class=\"pull-right\">")
                {
                    _Data = _Reader.ReadLine();
                }
                _Data = _Reader.ReadLine();
    
                string Crude = _Data;
    
                //// Had to look up how to use String.Remove :/
                int foundS1 = Crude.IndexOf("<");
                int foundS2 = Crude.IndexOf(">", foundS1 + 1);
    
                if (foundS1 != foundS2 && foundS1 >= 0)
                    Crude = Crude.Remove(foundS1 + 1, foundS2 - foundS1);
                ////
    
                string[] Filter = Crude.Split('<');
    
                string Score = Filter[0];
    
                return Score;
            }
        }
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: