VB Code:
  1. Option Compare Text
  2. Private Mysql_Connection As New ADODB.Connection
  3. Private rs As New ADODB.Recordset
  4. Private Sub cmdStart_Click()
  5. Dim scoringSummary As String, teamHome As String, teamAway As String, situationType As String, homeTeamScored As Boolean, plusMinus As String, retrievedHTML As String, playerName As String
  6. Dim goalData() As String, cellData() As String, playersInvolved_Team() As String, playersInvolved_Individual() As String
  7.  
  8. Mysql_Connection.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;UID=root;PWD=REMOVED;DATABASE=nhl"   'open our connection to the database
  9.  
  10. For gameCounter = 1 To 78
  11.     retrievedHTML = Inet.OpenURL("http://www.nhl.com/scores/htmlreports/20072008/GS020" & Format(gameCounter, "000") & ".HTM")  'get the html for the game summary
  12.  
  13.     scoringSummary = FindBetween2(retrievedHTML, ez("<td align=`center` class=`sectionheading`>SCORING SUMMARY</td>"), ez("<table id=`VPenaltySummary`"))(0)    'filter out all garbage except the scoring summary
  14.     goalData = FindBetween2(scoringSummary, "<tr class=" & Chr(34), "</tr>")    'make an array containing each goal
  15.    
  16.     For x = 0 To UBound(goalData) - 1
  17.         cellData = FindBetween2(goalData(x), ez("<td align=`center`>"), "</td>")    'cellData(3) = PP/EV/SH, 4 = Team that scored
  18.         teamAway = Replace(FindBetween2(scoringSummary, ez("<td class=`heading + bborder` align=`left`>"), "</td>")(3), " On Ice", "")  'the away team
  19.         teamHome = Replace(FindBetween2(scoringSummary, ez("<td class=`heading + bborder` align=`left`>"), "</td>")(4), " On Ice", "")  'the home team
  20.         homeTeamScored = IIf(cellData(4) = teamHome, True, False)   'did the home team score?
  21.         playersInvolved_Team = FindBetween2(goalData(x), "<td>", "</td>")   'get the cell containing the players on the ice for the goal
  22.        
  23.         For y = 0 To 1  '0 = away team, 1 = home team
  24.             If (y = 0 And homeTeamScored = True) Or (y = 1 And homeTeamScored = False) Then plusMinus = " - 1" Else plusMinus = " + 1"  'determine if the players in that cell on the ice for the goal either deserve a + or a -
  25.            
  26.             playersInvolved_Individual = FindBetween2(playersInvolved_Team(y), ez("<font style=`cursor:hand;` title=`"), "</font>") 'get an array containing each player in the cell of players on the ice for a particular team
  27.             For z = 0 To UBound(playersInvolved_Individual) - 1
  28.                 playerName = Replace(CStr(FindBetween2(playersInvolved_Individual(z), " - ", Chr(34))(0)), "'", "") 'the player name, filtering out the ' character to prevent database ****ups ie brind'amour
  29.                 If InStr(cellData(3), "PP") Then
  30.                     situationType = IIf(plusMinus = " + 1", "PP", "SH")   'one mans powerplay is another teams shorthanded
  31.                 ElseIf InStr(cellData(3), "SH") Then
  32.                     situationType = IIf(plusMinus = " + 1", "SH", "PP")
  33.                 ElseIf InStr(cellData(3), "EV") Then
  34.                     situationType = "EV"
  35.                 End If
  36.  
  37.                 'Debug.Print "INSERT INTO tblPlusMinus (playerName, " & situationType & ") VALUES ('" & playerName & "','" & Replace(plusMinus, " ", "") & "') ON DUPLICATE KEY UPDATE " & situationType & " = " & situationType & plusMinus  ' Uncomment this to see the queries as they are made
  38.                 rs.Open "INSERT INTO tblPlusMinus (playerName, " & situationType & ") VALUES ('" & playerName & "','" & Replace(plusMinus, " ", "") & "') ON DUPLICATE KEY UPDATE " & situationType & " = " & situationType & plusMinus, Mysql_Connection   'and finally insert/update
  39.             Next z
  40.         Next y
  41.     Next x
  42. Next gameCounter
  43. End Sub
  44. Private Function ez(m As String) As String  'quick function to replace the ` character with the " character
  45.     ez = Replace(m, "`", Chr(34))
  46. End Function
  47.  
  48. Private Function FindBetween2(s As String, mStart As String, mEnd As String) As String()
  49.     On Error Resume Next
  50.     'find the content between the start and the end returned in an array with all matches
  51.    
  52.     Dim Ar() As String, tmp() As String, i As Integer
  53.     ReDim tmp(0)
  54.     Ar = Split(s, mStart)
  55.  
  56.     ReDim Preserve tmp(0) As String
  57.     For i = 1 To UBound(Ar)
  58.         If InStr(Ar(i), mEnd) Then tmp(UBound(tmp)) = Left$(Ar(i), InStr(1, Ar(i), mEnd) - 1): ReDim Preserve tmp(UBound(tmp) + 1) As String
  59.     Next
  60.  
  61.     FindBetween2 = tmp
  62.     Erase tmp
  63. End Function