Baseball Scores

May 19, 2015

We’ve done this exercise before, writing a program that gets a weather forecast, and we steal the code to fetch a url from that exercise. We also use string-split and string-find from the Standard Prelude, and we write a perfectly awful string-replace (it has quadratic performance, but a proper implementation would be linear — even so, it is dead simple and sufficient for the task at hand) that you can see at ideone. Given all that, we need only two functions to get Cardinals’ baseball scores: parse-mlb-lines fetches the scores from ESPN’s BottomLine API into a list, and cardinals extracts the Cardinals’ score from that report:

(define (parse-mlb-lines)
  (let ((lines #f))
    (with-input-from-url
      "http://sports.espn.go.com/mlb/bottomline/scores"
      (lambda ()
        (let loop ((xs (list)))
          (if (not (eof-object? (peek-char)))
              (loop (cons (read-char) xs))
              (set! lines (list->string (reverse xs)))))))
    (map (lambda (xs) (list (car xs) (string-replace (cadr xs) "%20" " ")))
      (map (lambda (s) (string-split #\= s))
         (map (lambda (s) (substring s 6 (string-length s)))
              (cdr (string-split #\& lines)))))))

(define (cardinals)
  (let loop ((lines (parse-mlb-lines)))
    (when (pair? lines)
      (if (not (string-find "St. Louis" (cadar lines)))
          (loop (cdr lines))
          (let ((num (substring (caar lines) 4
                       (string-length (caar lines)))))
            (let loop ((lines lines))
              (when (not (string=? (caar lines)
                      (string-append "right" num "_count")))
                (display (cadar lines)) (newline)
                (loop (cdr lines)))))))))

Here’s a sample:

> (cardinals)
St. Louis 0 NY Mets 0 (TOP 2ND)

You can see all the code assembled at http://ideone.com/ErWtao.

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: