fudmario Posted November 21, 2015 Share Posted November 21, 2015 (edited) [HIDE-THANKS][LENGUAJE=vbnet] ' *********************************************************************** ' Assembly : Youtube Downloader ' Author : fudmario ' Created : 15-06-2015 ' ' Last Modified By : fudmario ' Last Modified On : 11-20-2015 ' *********************************************************************** ' ' ' Copyright © 2015 ' ' ' ' ************************************************************************* Option Explicit On Option Strict On Option Infer On Imports System.Collections.Specialized Imports System.Net Imports System.Text Imports System.Text.RegularExpressions Imports System.Web ''' ''' Class YoutubeHelper. Esta Clase no puede ser Heredada. ''' Public NotInheritable Class YoutubeHelper ''' ''' Url para Obtener la Información del Video ''' Private Const InfoUrl As String = "http://www.youtube.com/get_video_info?video_id=" ''' ''' Expresión Regular para obtener el Link de la imagen del Video. ''' ''' The imagen. Private Shared ReadOnly Property Imagen As Regex Get Return New Regex("") End Get End Property ''' ''' Expresion Regular para Obtener el Título del video. ''' ''' The title. Private Shared ReadOnly Property Title As Regex Get Return New Regex("") End Get End Property ''' ''' Expresion Regular para obtener la Descripción del Video. ''' ''' Descripción del Video. Private Shared ReadOnly Property Description As Regex Get Return New Regex("") End Get End Property ''' ''' Expresion Regular para obtener la Duracion del Video (en segundos). ''' ''' Duracion del Video (en segundos). Private Shared ReadOnly Property Duration As Regex Get Return New Regex("length_seconds"":""(.*?)"",") End Get End Property ''' ''' Obtiene Informacion sobre la Url de Video de Youtube. ''' ''' Url del Video de YouTube. ''' Si se establece como true aplicamos el bypass. ''' YouTubeVideoInformation. ''' La no puede estar vacia;url ''' Formato de Invalido;Url Public Shared Function GetYouTubeVideoInfo(url As String, Optional ByVal byPass As Boolean = False) As YouTubeVideoInformation If String.IsNullOrEmpty(url) Then Throw New ArgumentNullException(message:="La Url no puede estar vacia", paramName:="URL") If Not Regex.IsMatch(url, "(http|https):..(www\.)?youtube.com.watch\?v=") Then Throw New FormatException(message:="Formato de Url Invalido") Dim videoInfoFull As New YouTubeVideoInformation Dim infoVideo As New List(Of YoutubeVideoInfo) Dim adInfoVideo As YoutubeVideoInfo Dim mSource As String Dim arrayParams As NameValueCollection Using c As New WebClient c.Encoding = Encoding.GetEncoding(1252) mSource = c.DownloadString(url) Select Case byPass Case True arrayParams = HttpUtility.ParseQueryString(HttpUtility.HtmlDecode(c.DownloadString(String.Format("{0}{1}&el=vevo", InfoUrl, HttpUtility.ParseQueryString(New Uri(url).Query).Get("v"))))) Case Else arrayParams = HttpUtility.ParseQueryString(HttpUtility.HtmlDecode(c.DownloadString(String.Format("{0}{1}", InfoUrl, HttpUtility.ParseQueryString(New Uri(url).Query).Get("v"))))) End Select End Using Dim ytTitle As String = HttpUtility.HtmlDecode(Title.Match(mSource).Groups(1).Value) Dim ytDescription As String = HttpUtility.HtmlDecode(Description.Match(mSource).Groups(1).Value) Dim ytDuration As Double = CDbl(Duration.Match(mSource).Groups(1).Value) Dim ytImg As String = Imagen.Match(mSource).Groups(1).Value If arrayParams("status") = "ok" Then Dim urLs As String() = arrayParams("url_encoded_fmt_stream_map").Split(","c) Dim sb As New StringBuilder() For i = 0 To urLs.Length - 1 adInfoVideo = New YoutubeVideoInfo() sb.Clear() Dim mUriDec As String = HttpUtility.HtmlDecode(urLs(i)) Dim urlParams As NameValueCollection = HttpUtility.ParseQueryString(mUriDec) Dim vidFormat As String = HttpUtility.HtmlDecode(urlParams("type")) sb.Append(HttpUtility.HtmlDecode(urlParams("Url"))) sb.AppendFormat("&signature={0}", HttpUtility.HtmlDecode(urlParams("sig"))) sb.AppendFormat("&type={0}", vidFormat) sb.AppendFormat("&title={0}", HttpUtility.HtmlDecode(arrayParams("title"))) Dim format As String = vidFormat.Split(";"c)(0).Split("/"c)(1) If format.Contains("x-flv") Then format = "flv" End If adInfoVideo.Url = sb.ToString adInfoVideo.Quality = urlParams("quality") adInfoVideo.Format = format infoVideo.Add(adInfoVideo) Next videoInfoFull.ErrorCode = 0 videoInfoFull.LinksDetails = infoVideo videoInfoFull.Title = ytTitle videoInfoFull.Duration = ytDuration videoInfoFull.Description = ytDescription videoInfoFull.ImgLink = ytImg Return videoInfoFull Else videoInfoFull.ErrorCode = 1 videoInfoFull.ErrorMsg = HttpUtility.HtmlDecode(arrayParams("reason")) Return videoInfoFull End If End Function End Class ''' ''' Información Básica del Video. ''' Public Class YoutubeVideoInfo ''' ''' Obtiene o Establece la Url Directa del Video. ''' ''' Url Directa del Video. Public Property Url() As String ''' ''' Obtiene o Establece el Formato del Video. ''' ''' Formato del Video. Public Property Format() As String ''' ''' Obtiene o Establece La Calidad del Video. ''' ''' Calidad del Video. Public Property Quality() As String End Class ''' ''' Información Completa del Video de YouTube. ''' Public Class YouTubeVideoInformation ''' ''' Obtiene o Establece la lista de Link's Disponibles y Informacion Adicional. ''' ''' lista de Link's Disponibles. Public Property LinksDetails() As List(Of YoutubeVideoInfo) ''' ''' Obtiene o Establece El Titulo del Video. ''' ''' El Titulo del Video. Public Property Title() As String ''' ''' Obtiene o Establece El link de la Imagen del Video. ''' ''' El link de la Imagen del Video. Public Property ImgLink() As String ''' ''' Obtiene o Establece la Descripción del Video. ''' ''' la Descripción del Video. Public Property Description() As String ''' ''' Obtiene o Establece la Duracion del Video. ''' ''' Duracion del Video. Public Property Duration() As Double ''' ''' Obtiene o Establece el codigo de Error: 0 = No Error, 1 = Error. ''' ''' codigo de Error: 0 = No Error, 1 = Error.. Public Property ErrorCode() As Integer ''' ''' Obtiene o Establece el Mensaje de Error. ''' ''' Mensaje de Error. Public Property ErrorMsg() As String End Class [/LENGUAJE][/HIDE-THANKS] [li]Ejemplo de Uso:[/li] [LENGUAJE=vbnet]Dim response As YouTubeVideoInformation response = YoutubeHelper.GetYouTubeVideoInfo("https://www.youtube.com/watch?v=mWRsgZuwf_8", True) If response.ErrorCode = 0 Then Dim sb As New System.Text.StringBuilder sb.AppendLine(String.Format("Titulo: {0}", response.Title)) sb.AppendLine(String.Format("Descripción: {0}", response.Description)) sb.AppendLine(String.Format("Duración: {0}", TimeSpan.FromSeconds(response.Duration).ToString("hh\:mm\:ss"))) sb.AppendLine(String.Format("Url de la Imagen: {0}", response.ImgLink)) sb.AppendLine() sb.AppendLine("Urls: ") For Each urldetails As YoutubeVideoInfo In response.LinksDetails sb.AppendLine("*************************") sb.AppendLine(String.Format("Url: {0}", urldetails.Url)) sb.AppendLine(String.Format("Formato: {0}", urldetails.Format)) sb.AppendLine(String.Format("Calidad: {0}", urldetails.Quality)) Next MessageBox.Show(sb.ToString) Else MessageBox.Show(response.ErrorMsg) End If[/LENGUAJE] [li]Retorna: [/li] This is the hidden content, please Sign In or Sign Up Edited November 21, 2015 by fudmario Link to comment Share on other sites More sharing options...
Recommended Posts