[VB.NET] What does this code do?

Discussion in 'Mixed Languages' started by Stannieman, Dec 20, 2010.

  1. Stannieman

    Stannieman MDL Guru

    Sep 4, 2009
    2,232
    1,803
    90
    #1 Stannieman, Dec 20, 2010
    Last edited by a moderator: Apr 20, 2017
    Hi,
    I want to make a program that automatically downloads youtube video's to a folder, and therefor I first need the link of the flv/mp4 files.

    The only fully working thing I've found is the code below, but I don't know what it actually does.

    Code:
    Public Class Form1
        Dim HttpRequest As Net.HttpWebRequest
        Dim HttpResponse As Net.HttpWebResponse
        Dim Stream As IO.StreamReader
        Dim scode As String = ""
        Delegate Sub _generateLinks()
        Private Sub generateLinks()
            Dim type As String = ""
            Dim page As String = ""
            Dim tstring As String = ""
            Dim link As Integer = 1
            Dim index As Integer = 0
            Dim count As Integer = 0
            Dim num1 As Integer = 0
            Dim num2 As Integer = 0
            lstIndex.Items.Clear()
            lstLinks.Items.Clear()
            Do Until index = -1
                index = scode.IndexOf("http://v", index + 1)
                If index <> -1 Then
                    lstIndex.Items.Add(index)
                End If
            Loop
            For i = 0 To lstIndex.Items.Count - 2
                num1 = lstIndex.Items(i)
                num2 = lstIndex.Items(i + 1)
                If num2 - num1 < 380 Then
                    tstring = scode.Substring(num1, num2 - num1)
                    Dim right As String = Microsoft.VisualBasic.Right(tstring, 8)
                    If right.Substring(0, 1) = "%" Then
                        tstring = tstring.Substring(0, tstring.Count - 8)
                    Else
                        tstring = tstring.Substring(0, tstring.Count - 7)
                    End If
                    If Not lstLinks.Items.Contains(tstring) Then
                        If Not Microsoft.VisualBasic.Right(tstring, 3) = "com" Then
                            lstLinks.Items.Add(tstring)
                        End If
                    End If
                    count += 1
                End If
            Next
            index = 0
            For i = 0 To lstLinks.Items.Count - 1
                HttpRequest = Net.HttpWebRequest.Create(New Uri(lstLinks.Items.Item(i)))
                HttpResponse = HttpRequest.GetResponse
                type = HttpResponse.ContentType
                page = page & "<br>" & "<a href = """ & lstLinks.Items(i) & """>" & "Link " & link & "  -  Type: " & type
                link += 1
            Next
            wb.DocumentText = page
            btnGenerate.Enabled = True
        End Sub
        Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
            Dim t As Threading.Thread
            wb.DocumentText = ""
            btnGenerate.Enabled = False
            t = New Threading.Thread(AddressOf getpage)
            t.Start()
        End Sub
        Private Sub getpage()
            HttpRequest = Net.HttpWebRequest.Create(txtUrl.Text)
            HttpResponse = HttpRequest.GetResponse
            MsgBox(HttpRequest)
            Stream = New IO.StreamReader(HttpResponse.GetResponseStream)
            scode = Stream.ReadToEnd
            MsgBox(Stream)
            'credit to TraptCode for replace codes
            scode = scode.Replace("%3A", ":")
            scode = scode.Replace("%2F", "/")
            scode = scode.Replace("%3F", "?")
            scode = scode.Replace("%3D", "=")
            scode = scode.Replace("%26", "&")
            scode = scode.Replace("%25", "%")
            Invoke(New _generateLinks(AddressOf generateLinks))
        End Sub
    End Class
    The getpage sub seems to call the server with the url to recieve 'some data'. The strings in the replace lines look like strings you see a lot in urls, so I guess it's replacing web standard code with normal strings?

    Then it call's the generate links sub and that's where I get totally lost.
    Is there anyone willing to explain this a bit to me?

    Thanks,
    Stannieman
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Daz

    Daz MDL Developer / Admin
    Staff Member

    Jul 31, 2009
    9,488
    66,573
    300
    A delegate is basically the OOP way to point to a method or sub.

    Step #1
    btnGenerate_Click //Launch

    Step #2
    getpage //Loaded in a thread

    Step #3
    generateLinks //OOP way to point to a method/sub
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Stannieman

    Stannieman MDL Guru

    Sep 4, 2009
    2,232
    1,803
    90
    Thanks, but that's nor really what I need to know.
    This code can generate links to the real flv or mp4 file from the link to the video page. First that link was just in plain text in the webpage (when you look at the page code in your webbrowser), but now it's a bit harder. So actually I just need to know how to convert the page link into the file links. And since this code does that and it's the only 100% working example I could find, it's possible to get the needed steps from the code.

    The only problem is I can't really find how it's doing that, and I'm not yet a very experienced programmer
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Daz

    Daz MDL Developer / Admin
    Staff Member

    Jul 31, 2009
    9,488
    66,573
    300
    #4 Daz, Dec 21, 2010
    Last edited by a moderator: Apr 20, 2017
    Oh, in that case:
    Code:
        Private Sub getpage()
            HttpRequest = Net.HttpWebRequest.Create(txtUrl.Text)
            HttpResponse = HttpRequest.GetResponse
            MsgBox(HttpRequest)
            Stream = New IO.StreamReader(HttpResponse.GetResponseStream)
            scode = Stream.ReadToEnd
            MsgBox(Stream)
            'credit to TraptCode for replace codes
            scode = scode.Replace("%3A", ":")
            scode = scode.Replace("%2F", "/")
            scode = scode.Replace("%3F", "?")
            scode = scode.Replace("%3D", "=")
            scode = scode.Replace("%26", "&")
            scode = scode.Replace("%25", "%") //URL characters replaced before handing off to generateLinks
            Invoke(New _generateLinks(AddressOf generateLinks))
        End Sub
    
    
    
        Private Sub generateLinks()
            Dim type As String = ""
            Dim page As String = ""
            Dim tstring As String = ""
            Dim link As Integer = 1
            Dim index As Integer = 0
            Dim count As Integer = 0
            Dim num1 As Integer = 0
            Dim num2 As Integer = 0
            lstIndex.Items.Clear()
            lstLinks.Items.Clear()
            Do Until index = -1 //Loop through scode until there are no more links starting "http://v"
                index = scode.IndexOf("http://v", index + 1) //Find links starting "http://v"
                If index <> -1 Then
                    lstIndex.Items.Add(index) //Add to array
                End If
            Loop
            For i = 0 To lstIndex.Items.Count - 2 //Loop array
                num1 = lstIndex.Items(i)
                num2 = lstIndex.Items(i + 1)
                If num2 - num1 < 380 Then //If less than 380 do this...
                    tstring = scode.Substring(num1, num2 - num1) //Crop
                    Dim right As String = Microsoft.VisualBasic.Right(tstring, 8) //Get 8 chars of the text from the right
                    If right.Substring(0, 1) = "%" Then //Is the last char %
                        tstring = tstring.Substring(0, tstring.Count - 8) //Crop based on char check
                    Else
                        tstring = tstring.Substring(0, tstring.Count - 7) //Crop based on char check
                    End If
                    If Not lstLinks.Items.Contains(tstring) Then //Does the lstLinks array contain our link already? If it doesn't proceed
                        If Not Microsoft.VisualBasic.Right(tstring, 3) = "com" Then //Does the link not end in "com"
                            lstLinks.Items.Add(tstring) //Add it if it doesn't
                        End If
                    End If
                    count += 1
                End If
            Next
            index = 0
            For i = 0 To lstLinks.Items.Count - 1 //Loop through what we added
                HttpRequest = Net.HttpWebRequest.Create(New Uri(lstLinks.Items.Item(i))) //Issue request on each link
                HttpResponse = HttpRequest.GetResponse
                type = HttpResponse.ContentType
                page = page & "<br>" & "<a href = """ & lstLinks.Items(i) & """>" & "Link " & link & "  -  Type: " & type
                link += 1
            Next
            wb.DocumentText = page //Ready to display
            btnGenerate.Enabled = True
        End Sub
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Stannieman

    Stannieman MDL Guru

    Sep 4, 2009
    2,232
    1,803
    90
    OK thanks daz, that helped me out ;)
    keep up the good work
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...