Imports System.Net
Imports System.IO
Public Class frmDownload
Public WithEvents TCP As New WebClient
Dim total As Integer = 0
Dim downloades As Integer = 0
Dim TDownload As New Threading.Thread(AddressOf TCoda)
Private Shared Function SetAllowUnsafeHeaderParsing20() As Boolean
Dim sectionAssembly As Reflection.Assembly = Reflection.Assembly.GetAssembly(GetType(Net.Configuration.SettingsSection))
If sectionAssembly IsNot Nothing Then
Dim settingsType As Type = sectionAssembly.[GetType]("System.Net.Configuration. SettingsSectionInternal")
If settingsType IsNot Nothing Then
Dim sectionInstance As Object = settingsType.InvokeMember("Section", Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.[Static], Nothing, Nothing, New Object(-1) {})
If sectionInstance IsNot Nothing Then
Dim useUnsafeHeaderParsing As Reflection.FieldInfo = settingsType.GetField("useUnsafeHeaderParsing", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
If useUnsafeHeaderParsing IsNot Nothing Then
useUnsafeHeaderParsing.SetValue(sectionInstance, True)
Return True
End If
End If
End If
End If
Return False
End Function
Private Function Convert(ByVal url As String) As String
url = url.Replace("www.youtube.com", "youtube.com")
If url.IndexOf("http://youtube.com/v/") >= 0 Then
url.Replace("http://youtube.com/v/", "http://youtube.com/watch?v=")
End If
If url.IndexOf("http://youtube.com/watch?v=") < 0 Then
url = ""
End If
Return (url)
End Function
Private Function GetContent(ByVal url As String) As String
Dim buffer As String
'Try
Dim outputBuffer As String = "where=46038"
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
req.Method = "POST"
req.ContentLength = outputBuffer.Length
req.ContentType = "application/x-www-form-urlencoded"
Dim swOut As New StreamWriter(req.GetRequestStream())
swOut.Write(outputBuffer)
'swOut.Close()
Dim resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
Dim sr As New StreamReader(resp.GetResponseStream())
buffer = sr.ReadToEnd()
sr.Close()
'Catch exp As Exception
' buffer = "Errore: " & exp.Message.ToString()
'End Try
swOut.Close()
Return (buffer)
End Function
Private Sub Download_Video(ByVal url As String)
Dim buffer As String = GetContent(url)
If buffer.IndexOf("Errore:") < 0 Then
Dim start As Integer = 0, [End] As Integer = 0
Dim startTag As String = "/watch_fullscreen?"
Dim endTag As String = ";"
start = buffer.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase)
[End] = buffer.IndexOf(endTag, start, StringComparison.CurrentCultureIgnoreCase)
Dim str As String = buffer.Substring(start + startTag.Length, [End] - (start + startTag.Length))
Dim vid As String = str.Substring(str.IndexOf("video_id"), str.IndexOf("&", str.IndexOf("video_id")) - str.IndexOf("video_id"))
Dim l As String = str.Substring(str.IndexOf("&l"), str.IndexOf("&", str.IndexOf("&l") + 1) - str.IndexOf("&l"))
Dim t As String = str.Substring(str.IndexOf("&t"), str.IndexOf("&", str.IndexOf("&t") + 1) - str.IndexOf("&t"))
Dim title As String = str.Substring(str.IndexOf("&title=") + 7)
title = title.Substring(0, title.Length - 1)
lblvideo.Text = title
TCP.DownloadFile(New Uri("http://youtube.com/get_video?" & vid & l & t), ("C:\video.flv"))
End If
End Sub
Private Sub DownloadComplete(ByVal sender As Object, ByVal e As System.Net.DownloadDataCompletedEventArgs) Handles TCP.DownloadDataCompleted
If e.Cancelled = True Then
MsgBox("I/Il download sono/è stati/o annullati/o", MsgBoxStyle.Exclamation)
Else
MsgBox("I/Il download sono/è stati/o completati/o", MsgBoxStyle.Information)
End If
End Sub
Private Sub PrgChngd(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles TCP.DownloadProgressChanged
pbdownloadvideo.Value = e.ProgressPercentage()
pbtotaldownload.Value = (CInt((total - downloades) / total) * 100)
End Sub
Private Sub frmDownload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetAllowUnsafeHeaderParsing20()
TDownload.Start()
Control.CheckForIllegalCrossThreadCalls = False
total = lstVideo.Items.Count
End Sub
Private Sub TCoda() 'Thread di estrazione dalla coda
Dim url As String = ""
While True 'Ciclo infinito
If lstVideo.Items.Count > 0 Then
url = lstVideo.Items(0).ToString() 'Ottengo l'url
lstVideo.Items.RemoveAt(0) 'Rimuovo l'elemento dalla ListBox dei download
downloades = total - lstVideo.Items.Count
Download_Video(url) 'Mi raccomando, il download deve essere sincrono, non asincrono
End If
Threading.Thread.Sleep(500) 'Diamo un attimo di respiro al pc
End While
End Sub
Private Sub cmdexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdexit.Click
TCP.CancelAsync()
Me.Close()
End Sub
End Class