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 Function Convert(ByVal url 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
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.DownloadFileAsync(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.DownloadFileCompleted
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 = (((total - downloades) / total) * 100)
End Sub
Private Sub frmDownload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
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