11-09-2010, 05:42 PM
I posted this on HF. Thought I'd share over here.
This is my take on a Youtube Downloader. Instead of downloading the file automatically I have it generate the links in a webbrowser so the user can right-click save-target-as.
Credits go to:
xSilent - helped me learn mulithreading/use of delegates. Thanks.
TraptCode - original post similar to mine. used .Replace commands(commented in source). Thanks
Mr. Predictable - idea to use Camtasia Studio to produce gif came from him.
now the code.
You will need the following
Here is the code.
I didnt use Try-catch because I meant for this to be more informative for downloading youtube vids rather than releasing it. I plan on making a better GUI and adding error handling. But for the purpose of this thread, i left that out. Also, the code is probably a bit sloppy and im sure there are better ways to do some of things i did in this code. But this is what i know. So if you have a better way to code something, reply and let us know.
Enjoy!
Download Code here
*Message to noobs/skids*
I left this code copy/pasteable... learn from it. Go through the code as you use it and understand the flow of it. Like, when you click the button it starts a thread, which starts another sub, then learn what that sub is doing. I didnt comment this code because it's late and I am tired. If you have a question about something in the code, reply/pm.
This is my take on a Youtube Downloader. Instead of downloading the file automatically I have it generate the links in a webbrowser so the user can right-click save-target-as.
Credits go to:
xSilent - helped me learn mulithreading/use of delegates. Thanks.
TraptCode - original post similar to mine. used .Replace commands(commented in source). Thanks
Mr. Predictable - idea to use Camtasia Studio to produce gif came from him.
now the code.
You will need the following
- Textbox - txtUrl
- Button - btnGenerate
- webbrowser - wb
- listbox - lstIndex
- listbox - lstLinks
Here is the code.
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
Stream = New IO.StreamReader(HttpResponse.GetResponseStream)
scode = Stream.ReadToEnd
'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
I didnt use Try-catch because I meant for this to be more informative for downloading youtube vids rather than releasing it. I plan on making a better GUI and adding error handling. But for the purpose of this thread, i left that out. Also, the code is probably a bit sloppy and im sure there are better ways to do some of things i did in this code. But this is what i know. So if you have a better way to code something, reply and let us know.
Enjoy!
Download Code here
*Message to noobs/skids*
I left this code copy/pasteable... learn from it. Go through the code as you use it and understand the flow of it. Like, when you click the button it starts a thread, which starts another sub, then learn what that sub is doing. I didnt comment this code because it's late and I am tired. If you have a question about something in the code, reply/pm.
I code at http://tech.reboot.pro