Today’s quick Ruby hack is a script I threw together REAL quick (in about five minutes) to help a friend. Basically, my friend needed a pipe delimited list of NFL players from sportsline.com - I think he is doing something on his local machine with his fantasy roster - but honestly, I don’t know why he really needed this. In any case, what he wanted was the sportsline.com playerid, the player’s name, the player’s position, and the NFL team they are on. As luck would have it, pages like this one have just that easily available. So it’s really a simple case of looping through each letter listing on Sportsline.com and breaking out the data we want. Again, this is a case of a one-off script I’m running local and that doesn’t really need to be all that fast. What this means is I can take a bunch of shortcuts, I can hard code against my data scenarios, and I don’t really have to worry about security… so without further ado, here’s the quick hack in it’s full:As you can see, it’s very light on comments (and logic really)…so just to break it down a little bit: 1. Sportsline.com lists players who’s last name start with each letter - so we start by looping through the alphabet. 2. We wrap our process in a begin/rescue/end loop just in case we hit a problem on a given page, our program will continue to grab the data for the other letters. 3. We use a simple Net::HTTP call to grab the data for each page. 4. We use a handful of regular expressions to get the data we want out…I could have done these calls/assignments all in one regular expression, but I found it easier to build it up in bits and so I just kept it that way. (In something I was going to spend more time on, I would have purged these down into one regex call) 5. I only write the data out to the file (and screen) if there was a player ID found…this way we ignore any junk lines or false matches. And it’s basically that simple. Here’s the first few lines of the generated file:
This post has received 42 loves.
Kevin has a day job as CTO of Veritonic and is spending nights & weekends hacking on Share Game Tape. You can also check out some of his open source code on GitHub or connect with him on Twitter @falicon or via email at kevin at falicon.com.
If you have comments, thoughts, or want to respond to something you see here I would encourage you to respond via a post on your own blog (and then let me know about the link via one of the routes mentioned above).