Last active
January 1, 2016 08:29
-
-
Save stabbylambda/8118048 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open System.Net | |
| open System.Xml | |
| open FSharp.Data | |
| type TableProvider = XmlProvider<"""<table class="tablehead" cellpadding="3" cellspacing="1"><tr class="stathead"><td>STANDINGS</td><td align="center" colspan="3">CONFERENCE</td><td align="center" colspan="4">OVERALL</td></tr><tr class="colhead"><td>Big Ten - Legends</td><td>W-L</td><td>PF</td><td>PA</td><td>W-L</td><td>PF</td><td>PA</td><td>STRK</td></tr><tr class="oddrow team-23-158"><td><a href="http://espn.go.com/college-football/team/_/id/158/nebraska-cornhuskers">Nebraska</a></td><td>7-1</td><td>231</td><td>195</td><td>10-4</td><td>487</td><td>386</td><td>L2</td></tr><tr class="evenrow team-23-130"><td><a href="http://espn.go.com/college-football/team/_/id/130/michigan-wolverines">Michigan</a></td><td>6-2</td><td>246</td><td>133</td><td>8-5</td><td>388</td><td>258</td><td>L2</td></tr><tr class="oddrow team-23-77"><td><a href="http://espn.go.com/college-football/team/_/id/77/northwestern-wildcats">Northwestern</a></td><td>5-3</td><td>253</td><td>199</td><td>10-3</td><td>412</td><td>293</td><td>W3</td></tr><tr class="evenrow team-23-127"><td><a href="http://espn.go.com/college-football/team/_/id/127/michigan-state-spartans">Michigan State</a></td><td>3-5</td><td>159</td><td>149</td><td>7-6</td><td>260</td><td>212</td><td>W2</td></tr><tr class="oddrow team-23-2294"><td><a href="http://espn.go.com/college-football/team/_/id/2294/iowa-hawkeyes">Iowa</a></td><td>2-6</td><td>150</td><td>201</td><td>4-8</td><td>232</td><td>275</td><td>-</td></tr><tr class="evenrow team-23-135"><td><a href="http://espn.go.com/college-football/team/_/id/135/minnesota-golden-gophers">Minnesota</a></td><td>2-6</td><td>137</td><td>220</td><td>6-7</td><td>287</td><td>321</td><td>L3</td></tr><tr class="colhead"><td>Big Ten - Leaders</td><td>W-L</td><td>PF</td><td>PA</td><td>W-L</td><td>PF</td><td>PA</td><td>STRK</td></tr><tr class="oddrow team-23-194"><td><a href="http://espn.go.com/college-football/team/_/id/194/ohio-state-buckeyes">Ohio State</a></td><td>8-0</td><td>295</td><td>205</td><td>12-0</td><td>446</td><td>274</td><td>-</td></tr><tr class="evenrow team-23-213"><td><a href="http://espn.go.com/college-football/team/_/id/213/penn-state-nittany-lions">Penn State</a></td><td>6-2</td><td>261</td><td>168</td><td>8-4</td><td>349</td><td>229</td><td>-</td></tr><tr class="oddrow team-23-275"><td><a href="http://espn.go.com/college-football/team/_/id/275/wisconsin-badgers">Wisconsin</a></td><td>4-4</td><td>244</td><td>146</td><td>8-6</td><td>414</td><td>268</td><td>L1</td></tr><tr class="evenrow team-23-2509"><td><a href="http://espn.go.com/college-football/team/_/id/2509/purdue-boilermakers">Purdue</a></td><td>3-5</td><td>189</td><td>265</td><td>6-7</td><td>373</td><td>406</td><td>L1</td></tr><tr class="oddrow team-23-84"><td><a href="http://espn.go.com/college-football/team/_/id/84/indiana-hoosiers">Indiana</a></td><td>2-6</td><td>231</td><td>328</td><td>4-8</td><td>369</td><td>423</td><td>-</td></tr><tr class="evenrow team-23-356"><td><a href="http://espn.go.com/college-football/team/_/id/356/illinois-fighting-illini">Illinois</a></td><td>0-8</td><td>94</td><td>281</td><td>2-10</td><td>200</td><td>385</td><td>-</td></tr></table>"""> | |
| let getConferenceStanding year school = | |
| let url = sprintf "http://espn.go.com/college-football/conferences/standings/_/id/5/year/%s/big-ten-conference" year | |
| let request = WebRequest.Create(Uri(url)) | |
| use response = request.GetResponse() | |
| use stream = response.GetResponseStream() | |
| use reader = new IO.StreamReader(stream) | |
| let htmlString = reader.ReadToEnd() | |
| let divMarkerStartPosition = htmlString.IndexOf("my-teams-table"); | |
| let tableStartPosition = htmlString.IndexOf("<table",divMarkerStartPosition); | |
| let tableEndPosition = htmlString.IndexOf("</table",tableStartPosition); | |
| let data = htmlString.Substring(tableStartPosition, tableEndPosition- tableStartPosition+8) | |
| let document = TableProvider.Parse(data) | |
| let record = query { | |
| for tr in document.GetTrs() do | |
| for td in tr.GetTds() do | |
| where (td.StringValue = Some(school)) | |
| select td.XElement.NextNode | |
| headOrDefault | |
| } | |
| (school, record) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So the only real changes here are using a TypeProvider and the query expression. There's also an sprintf in there for doing string interpolation instead of concatenating strings together.
You basically just download the string for a single year, plug it into the type declaration to give the provider a sample (you could also save a file in your project with the table in it and then provide that filename in the type declaration), and then you call TableProvider.Parse with the downloaded and parsed html table.