Code:
Public Class Form1
Private _baseUrl As String = ""
Private _outputFilename As String = ""
Private _webRequest As System.Net.WebRequest
Private _webResponse As System.Net.WebResponse
Private _startTime As DateTime
Private _forceStop As Boolean = False
Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
Dim startPage As Int32 = 0
Dim endPage As Int32 = 0
Dim totalUsers As Int64 = 0 '' sum total of all usersnames found
If Not Int32.TryParse(txtStartPage.Text, startPage) Then
MessageBox.Show("Invalid start page number. Must be 0 < x > 1000000")
txtStartPage.Focus()
txtStartPage.SelectAll()
Exit Sub
End If
If Not Int32.TryParse(txtStopPage.Text, endPage) Then
MessageBox.Show("Invalid end page number. Must be 0 < x > 1000000")
txtStopPage.Focus()
txtStopPage.SelectAll()
Exit Sub
End If
If endPage <= startPage Then
MessageBox.Show("Invalid end page..must be greater than start page.")
txtStopPage.Focus()
txtStopPage.SelectAll()
Exit Sub
End If
If txtOutputFilename.TextLength = 0 Then
MessageBox.Show("Invalid output file name.")
txtOutputFilename.Focus()
Exit Sub
End If
''User start/stop pages were valid
_baseUrl = txtBaseUrl.Text
_outputFilename = txtOutputFilename.Text
Dim _pageUsers() As String = Nothing
Dim _tmpSpan As TimeSpan
_startTime = Date.Now()
For xx As Int32 = startPage To endPage
If _forceStop Then
DoOutput("Force stop. Shutting down.")
DoOutput("Last page successfully read #:" & (xx - 1).ToString())
Application.DoEvents()
_forceStop = False
Exit Sub
End If
lblCurrentPage.Text = "Current Page: " + FormatNumber(xx, 0)
Try
_pageUsers = GetUsernamesByPage(xx) '' this function parses the raw page data and pulls out the usernames
Catch ex As Exception
'' server was busy? Page unreachable.
DoOutput("Server busy/unreachable! page # " & xx.ToString() & " (wait 10sec.)")
Application.DoEvents()
xx -= 1
Threading.Thread.Sleep(10000)
Continue For
End Try
System****.File.AppendAllLines(_outputFilename & ".txt", _pageUsers)
totalUsers += _pageUsers.Length()
lblTotalUsers.Text = "Total Users: " + FormatNumber(totalUsers, 0)
lblCurrentUser.Text = "Current User: " & _pageUsers(_pageUsers.Length - 1)
ProgressBar1.Value = CInt((xx / endPage) * 100)
_tmpSpan = Date.Now.Subtract(_startTime)
lblTotalTime.Text = "Total Time: " & Math.Floor(_tmpSpan.TotalMinutes).ToString & " m, " & CInt(_tmpSpan.TotalSeconds Mod 60).ToString() & " s"
Application.DoEvents() '' hmm
Next
_tmpSpan = Date.Now.Subtract(_startTime)
DoOutput("Done. Time taken: " & Math.Floor(_tmpSpan.TotalMinutes).ToString() & " minutes, " & CInt(_tmpSpan.TotalSeconds Mod 60).ToString() & " seconds.")
MsgBox("DONE. Time taken: " & Math.Floor(_tmpSpan.TotalMinutes).ToString() & " minutes, " & CInt(_tmpSpan.TotalSeconds Mod 60).ToString() & " seconds.")
End Sub
Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
_forceStop = True
End Sub
Private Function GetUsernamesByPage(ByVal pageNumber As Int32) As String()
Dim _userNames As New List(Of String)
_webRequest = System.Net.HttpWebRequest.Create(_baseUrl + pageNumber.ToString())
_webRequest.Timeout = 10000 '' timeout = 10 seconds
_webResponse = _webRequest.GetResponse()
Dim _entirePageText As String = New System****.StreamReader(_webResponse.GetResponseStream, System.Text.Encoding.ASCII).ReadToEnd
Dim _tableText As String = ""
'' Find the table that contains all users
Dim tableStartIndex As Int32 = 0
Dim tableEndIndex As Int32 = 0
tableStartIndex = _entirePageText.IndexOf("<tbody>")
If tableStartIndex = -1 Then
MessageBox.Show("Unable to find user table inside webpage. Format is out of date! This program needs to be updated!")
_userNames.Add("ERROR Page #: " & pageNumber.ToString())
Return _userNames.ToArray()
End If
tableEndIndex = _entirePageText.IndexOf("</table>", tableStartIndex)
_tableText = _entirePageText.Substring(tableStartIndex, tableEndIndex)
''Find each row
Dim _rowStartIndex As Int32 = _tableText.IndexOf("</th><th><a href=")
Dim _rowEndIndex As Int32 = 0
Do While _rowStartIndex > -1
_rowEndIndex = _tableText.IndexOf(" />", _rowStartIndex) + 3
_userNames.Add(_tableText.Substring(_rowEndIndex, _tableText.IndexOf("</a>", _rowEndIndex) - _rowEndIndex)) '' lol
_rowStartIndex = _tableText.IndexOf("</th><th><a href=", _rowEndIndex)
Loop
Return _userNames.ToArray()
End Function
Private Sub DoOutput(ByVal msg As String)
txtOutput.AppendText("[" & Date.Now.ToLongTimeString & "] " & msg & Environment.NewLine)
End Sub
End Class