Partial Public Class MainForm
Public WithEvents TCP As New System.Net.WebClient
Dim Path As String
Public Sub New()
Me.InitializeComponent()
End Sub
#Region "MySub"
Sub GetPreview(ByVal VideoUrl As String)
Dim youtubeVideoId As String
youtubeVideoId = VideoUrl.Substring(VideoUrl.IndexOf("v=") + 2, 11)
Dim ThumbnailUrl As String
ThumbnailUrl = "http://img.youtube.com/vi/" & youtubeVideoId & "/0.jpg"
'/default.jpg per l'immagine di default
'0.jpg per l'immagine grande (480px x 360px)
'1.jpg per l'immagine alternativa 1
'3.jpg per l'immagine alternativa 2
pictureBox1.Load(ThumbnailUrl)
End Sub
Function NormalizeString(ByVal str As String) As String
str = str.Normalize()
str = str.Trim()
If str.Contains("[") Then
str = str.Replace("[", "(")
End If
If str.Contains("]") Then
str = str.Replace("]", ")")
End If
If str.Contains(":") Then
str = str.Replace(":", " ")
End If
If str.Contains("★") Then
str = str.Replace("★", " ")
End If
If str.Contains("@") Then
str = str.Replace("@", " ")
End If
If str.Contains("#") Then
str = str.Replace("#", " ")
End If
If str.Contains("§") Then
str = str.Replace("§", " ")
End If
Return str
End Function
Sub ConvertVideo(ByVal videoUrl As String)
Try
lblState.Text = ""
wb.Navigate("http://www.youtube-mp3.org/#c&v=" + videoUrl.Substring(videoUrl.IndexOf("v=") + 2, 11))
tmrFold.Start()
Catch ex As Exception
lblState.Text = "Caricamento in corso pagina di download.Riprova finchè la casella di testo diventa verde."
End Try
End Sub
#End Region
Sub BtnGenerateClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnGenerate.Click
ConvertVideo(txtUrl.Text)
End Sub
Sub TimerFoldTick(ByVal sender As Object, ByVal e As EventArgs) Handles tmrFold.Tick
If wb.ReadyState = WebBrowserReadyState.Complete Then
Try
Dim url As String = Nothing
Dim ele As HtmlElement = wb.Document.GetElementById("title")
If ele IsNot Nothing And ele.InnerText IsNot Nothing Then
lblTitle.Text = NormalizeString(ele.InnerText)
txtPath.Text += "\" + lblTitle.Text.Remove(0, 8) + ".mp3"
Path = txtPath.Text
For Each ele In wb.Document.Links
If ele.InnerText = "Scarica" Then
url = ele.GetAttribute("href")
GetPreview(txtUrl.Text)
txtUrl.BackColor = Color.LightGreen
Me.Cursor = Cursors.WaitCursor
End If
Next
TCP.DownloadFileAsync(New Uri(url), txtPath.Text)
tmrFold.Stop()
End If
Catch ex As Exception
MessageBox.Show(ex.ToString())
tmrFold.Stop()
End Try
End If
End Sub
Sub TxtUrlMouseEnter(ByVal sender As Object, ByVal e As EventArgs) Handles txtUrl.MouseEnter
If Clipboard.GetText.Contains("www") Then
txtUrl.Text = Clipboard.GetText()
End If
End Sub
Sub DownloadFileAsyncProgressChanged(ByVal sender As Object, ByVal e As Net.DownloadProgressChangedEventArgs) Handles TCP.DownloadProgressChanged
progressBar1.Value = e.ProgressPercentage
lblState.Text = "Download in corso..."
End Sub
Sub DownloadFileAsyncCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles TCP.DownloadFileCompleted
Me.Cursor = Cursors.Default
If e.Cancelled Then
lblState.Text = "Download non riuscito"
Else
lblState.Text = "Download completato in " & Path
End If
wb.Navigate("http://www.youtube-mp3.org/it")
txtUrl.BackColor = Color.White
progressBar1.Value = 0
pictureBox1.Image = Nothing
lblTitle.Text = ""
If Path = "" Then
txtPath.Text = folderBrowserDialog1.SelectedPath
Else
txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
End If
End Sub
Sub MainFormLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
End Sub
Sub BtnFoldClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnFold.Click
If folderBrowserDialog1.ShowDialog() = DialogResult.OK Then
txtPath.Text = folderBrowserDialog1.SelectedPath
Path = folderBrowserDialog1.SelectedPath
End If
End Sub
Private Sub lblHelp_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblHelp.DoubleClick
MessageBox.Show("Non riesci a scaricare un video? Controlla queste cose : " + vbNewLine +
" 1: Controlla se il video è ufficiale,alcuni ""official video"" non sono scaricabili;" + vbNewLine +
" 2: Il video contiene strani caratteri nel titolo?Prova a cambiare video,magari scegline uno col titolo più semplice;" + vbNewLine +
" 3: Non riesci proprio,neanche scegliendo un video ""unofficial"",senza strani titoli?" + vbNewLine +
" Peccato,il video non è supportato da questo programma.", "Aiuto")
End Sub
End Class