Generate Youtube Video URL
Posted: Sun Mar 18, 2012 11:06 am
try this guys
Code: Select all
#Region "Generate You-Tube URL"
Public Sub MakeDownloadURL(ByVal url As String, ByRef output As String, Optional ByRef type As String = "", Optional ByRef size As Integer = 0)
'PERFORMS CLEANUP
downloadurl = ""
If request IsNot Nothing Then
Try
request.Abort()
Catch
End Try
End If
If response IsNot Nothing Then
Try
response.Close()
Catch
End Try
End If
Try
source = ""
Catch
End Try
request = DirectCast(HttpWebRequest.Create(url.ToString), HttpWebRequest)
Try
response = DirectCast(request.GetResponse, HttpWebResponse)
Catch
type = ""
Exit Sub
End Try
source = New StreamReader(response.GetResponseStream, System.Text.Encoding.Default).ReadToEnd
If source.IndexOf("video_id") > -1 Then
'This grabs the .flv location URL ready for doownloading.
Dim signature As String = source
If signature.Contains("&fmt_url_map=") = True Then
Try
signature = System.Text.RegularExpressions.Regex.Split(signature, "&fmt_url_map=")(1)
Catch
End Try
ElseIf signature.Contains(Chr(34) + "fmt_url_map" + Chr(34) + ": " + Chr(34)) = True Then
Try
signature = System.Text.RegularExpressions.Regex.Split(signature, Chr(34) + "fmt_url_map" + Chr(34) + ": " + Chr(34))(1)
Catch
End Try
Else
Debug.WriteLine("Critical Error: Download URL could not be produced.")
type = ""
Exit Sub
End If
'This changes the URL to conform to internet standards eg. %20 = " "
signature = signature.Remove(0, 5)
signature = signature.Replace("%252C", ",")
signature = signature.Replace("%2F", "/")
signature = signature.Replace("%3D", "=")
signature = signature.Replace("%3F", "?")
signature = signature.Replace("%3A", ":")
signature = signature.Replace("%26", "&")
Dim sig2 As String = signature
sig2 = sig2.IndexOf("http", 30)
signature = signature.Remove(sig2, signature.Length - sig2)
signature = signature.Remove(signature.IndexOf("%"), 7)
'Removes any remaining % characters from the string.
signature = signature.Replace("%", "")
'Checks for and fixes any malformed http requests in the string.
If signature.StartsWith("ttp://") Then
signature = signature.Replace("ttp://", "http://")
End If
'Checks for and Removes any remaining C characters from the string.
If signature.Chars(signature.Length - 1) = "C" Then
signature = signature.Remove(signature.Length - 1, 1)
End If
'Checks for videos such as http://www.youtube.com/watch?v=LvLCvdeWcsw that would not download due to a new layout of download data.
If signature.Contains("rv.2.rating") Then
Dim n As Integer = signature.IndexOf(ChrW(34) + ",")
signature = signature.Remove(n, signature.Length - n)
End If
'PERFORMS CLEANUP
If request2 IsNot Nothing Then
Try
request2.Abort()
Catch
End Try
End If
If response2 IsNot Nothing Then
Try
response2.Close()
Catch
End Try
End If
Try
request2 = DirectCast(HttpWebRequest.Create(signature), HttpWebRequest)
Catch
type = ""
Exit Sub
End Try
response2 = DirectCast(request2.GetResponse, HttpWebResponse)
If response2.ContentType = "video/x-flv" Then
type = ".flv"
output = signature
size = response2.ContentLength
ElseIf response2.ContentType = "video/mp4" Then
type = ".mp4"
output = signature
size = response2.ContentLength
Else
type = ""
output = ""
Exit Sub
End If
End If
End Sub
#End Region