Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
TubeToMp3 - MainForm.vb

MainForm.vb

Caricato da: Lollo 97
Scarica il programma completo

  1. 
  2. Partial Public Class MainForm
  3.  
  4.     Public WithEvents TCP As New System.Net.WebClient
  5.  
  6.     Dim Path As String
  7.  
  8.     Public Sub New()
  9.         Me.InitializeComponent()
  10.     End Sub
  11.  
  12. #Region "MySub"
  13.  
  14.     Sub GetPreview(ByVal VideoUrl As String)
  15.         Dim youtubeVideoId As String
  16.         youtubeVideoId = VideoUrl.Substring(VideoUrl.IndexOf("v=") + 2, 11)
  17.         Dim ThumbnailUrl As String
  18.         ThumbnailUrl = "http://img.youtube.com/vi/" & youtubeVideoId & "/0.jpg"
  19.         '/default.jpg per l'immagine di default
  20.         '0.jpg per l'immagine grande (480px x 360px)
  21.         '1.jpg per l'immagine alternativa 1
  22.         '3.jpg per l'immagine alternativa 2
  23.         pictureBox1.Load(ThumbnailUrl)
  24.     End Sub
  25.  
  26.     Function NormalizeString(ByVal str As String) As String
  27.         str = str.Normalize()
  28.         str = str.Trim()
  29.         If str.Contains("[") Then
  30.             str = str.Replace("[", "(")
  31.         End If
  32.         If str.Contains("]") Then
  33.             str = str.Replace("]", ")")
  34.         End If
  35.         If str.Contains(":") Then
  36.             str = str.Replace(":", " ")
  37.         End If
  38.         If str.Contains("★") Then
  39.             str = str.Replace("★", " ")
  40.         End If
  41.         If str.Contains("@") Then
  42.             str = str.Replace("@", " ")
  43.         End If
  44.         If str.Contains("#") Then
  45.             str = str.Replace("#", " ")
  46.         End If
  47.         If str.Contains("§") Then
  48.             str = str.Replace("§", " ")
  49.         End If
  50.         Return str
  51.     End Function
  52.  
  53.     Sub ConvertVideo(ByVal videoUrl As String)
  54.         Try
  55.             lblState.Text = ""
  56.             wb.Navigate("http://www.youtube-mp3.org/#c&v=" + videoUrl.Substring(videoUrl.IndexOf("v=") + 2, 11))
  57.             tmrFold.Start()
  58.         Catch ex As Exception
  59.             lblState.Text = "Caricamento in corso pagina di download.Riprova finchè la casella di testo diventa verde."
  60.         End Try
  61.     End Sub
  62.  
  63. #End Region
  64.  
  65.     Sub BtnGenerateClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnGenerate.Click
  66.         ConvertVideo(txtUrl.Text)
  67.     End Sub
  68.  
  69.     Sub TimerFoldTick(ByVal sender As Object, ByVal e As EventArgs) Handles tmrFold.Tick
  70.         If wb.ReadyState = WebBrowserReadyState.Complete Then
  71.             Try
  72.                 Dim url As String = Nothing
  73.                 Dim ele As HtmlElement = wb.Document.GetElementById("title")
  74.                 If ele IsNot Nothing And ele.InnerText IsNot Nothing Then
  75.                     lblTitle.Text = NormalizeString(ele.InnerText)
  76.                     txtPath.Text += "\" + lblTitle.Text.Remove(0, 8) + ".mp3"
  77.                     Path = txtPath.Text
  78.                     For Each ele In wb.Document.Links
  79.                         If ele.InnerText = "Scarica" Then
  80.                             url = ele.GetAttribute("href")
  81.                             GetPreview(txtUrl.Text)
  82.                             txtUrl.BackColor = Color.LightGreen
  83.                             Me.Cursor = Cursors.WaitCursor
  84.                         End If
  85.                     Next
  86.                     TCP.DownloadFileAsync(New Uri(url), txtPath.Text)
  87.                     tmrFold.Stop()
  88.                 End If
  89.             Catch ex As Exception
  90.                 MessageBox.Show(ex.ToString())
  91.                 tmrFold.Stop()
  92.             End Try
  93.         End If
  94.     End Sub
  95.  
  96.     Sub TxtUrlMouseEnter(ByVal sender As Object, ByVal e As EventArgs) Handles txtUrl.MouseEnter
  97.         If Clipboard.GetText.Contains("www") Then
  98.             txtUrl.Text = Clipboard.GetText()
  99.         End If
  100.     End Sub
  101.  
  102.     Sub DownloadFileAsyncProgressChanged(ByVal sender As Object, ByVal e As Net.DownloadProgressChangedEventArgs) Handles TCP.DownloadProgressChanged
  103.         progressBar1.Value = e.ProgressPercentage
  104.         lblState.Text = "Download in corso..."
  105.     End Sub
  106.  
  107.     Sub DownloadFileAsyncCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles TCP.DownloadFileCompleted
  108.         Me.Cursor = Cursors.Default
  109.         If e.Cancelled Then
  110.             lblState.Text = "Download non riuscito"
  111.         Else
  112.             lblState.Text = "Download completato in " & Path
  113.         End If
  114.         wb.Navigate("http://www.youtube-mp3.org/it")
  115.         txtUrl.BackColor = Color.White
  116.         progressBar1.Value = 0
  117.         pictureBox1.Image = Nothing
  118.         lblTitle.Text = ""
  119.         If Path = "" Then
  120.             txtPath.Text = folderBrowserDialog1.SelectedPath
  121.         Else
  122.             txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
  123.         End If
  124.     End Sub
  125.  
  126.     Sub MainFormLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
  127.         txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
  128.     End Sub
  129.  
  130.     Sub BtnFoldClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnFold.Click
  131.         If folderBrowserDialog1.ShowDialog() = DialogResult.OK Then
  132.             txtPath.Text = folderBrowserDialog1.SelectedPath
  133.             Path = folderBrowserDialog1.SelectedPath
  134.         End If
  135.     End Sub
  136.  
  137.     Private Sub lblHelp_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblHelp.DoubleClick
  138.         MessageBox.Show("Non riesci a scaricare un video? Controlla queste cose : " + vbNewLine +
  139.                       " 1: Controlla se il video è ufficiale,alcuni ""official video"" non sono scaricabili;" + vbNewLine +
  140.                       " 2: Il video contiene strani caratteri nel titolo?Prova a cambiare video,magari scegline uno col titolo più semplice;" + vbNewLine +
  141.                       " 3: Non riesci proprio,neanche scegliendo un video ""unofficial"",senza strani titoli?" + vbNewLine +
  142.                       " Peccato,il video non è supportato da questo programma.", "Aiuto")
  143.     End Sub
  144. End Class