YTInformation-Dings

    • VB.NET

      YTInformation-Dings

      Ich habe mich schon vor längerer Zeit von @Tibiamicus: für eine kleine YTInformation-Klasse inspirieren lassen.
      Referenz hierbei ist immer sein Thread.

      Mir hat seine Herangehensweise nicht wirklich gefallen, sodass ich mir dachte, das besser zu machen. Imho ist mir das auch gelungen.
      Ich möchte mich nicht weiter mit irgendwelchen Kleinigkeiten herumschlagen, daher "nur" der Sourcecode Austausch.
      Was im voraus schon gegeben sein muss:
      • System.Web ist importiert
      • System.Net ist importiert
      • System.Reflection ist importiert

      Jetzt die vmtl. tollste Frage: "Warum Reflection?!" - Wirst du sehen, später.

      Ich nutze eine kleine Structure um mir die Arbeit mit YT Links zu vereinfachen:

      VB.NET-Quellcode

      1. ''' <summary>
      2. ''' Eine Struktur, die die Videoinformationen hält
      3. ''' </summary>
      4. ''' <remarks></remarks>
      5. Public Structure VideoInfo
      6. Private _url As String
      7. Private _type As String
      8. ''' <summary>
      9. ''' der Fallback Host
      10. ''' </summary>
      11. Public Property FallBack_Host As String
      12. ''' <summary>
      13. ''' Irgendn Tag, der irgendwie gesetzt werden muss.
      14. ''' </summary>
      15. Public Property ITag As Integer
      16. ''' <summary>
      17. ''' Qualität .. oder so
      18. ''' </summary>
      19. Public Property Quality As String
      20. ''' <summary>
      21. ''' Signatur des Videos, afaik.
      22. ''' </summary>
      23. Public Property Sig As String
      24. ''' <summary>
      25. ''' Die URL des Videos
      26. ''' </summary>
      27. Public Property Url As String
      28. Get
      29. Return _url
      30. End Get
      31. Set(value As String)
      32. ' die URL soll bitte keine komischen HTML-Escapes beinhalten
      33. _url = HttpUtility.UrlDecode(value)
      34. End Set
      35. End Property
      36. ''' <summary>
      37. ''' der Typ des Videos.
      38. ''' </summary>
      39. Public Property Type As String
      40. Get
      41. Return _type
      42. End Get
      43. Set(value As String)
      44. ' keine HTML-Escapes
      45. _type = HttpUtility.UrlDecode(value)
      46. End Set
      47. End Property
      48. ''' <summary>
      49. ''' doller FileType. (webm, flv, mp4)
      50. ''' </summary>
      51. Public ReadOnly Property FileType As String
      52. Get
      53. ' dafür muss man wissen, wie der FileType aufgebaut ist.
      54. ' einfach mal durchsteppen und gucken
      55. Return Type.Split(";"c)(0).Split("/"c)(1).Replace("x-", "")
      56. End Get
      57. End Property
      58. ''' <summary>
      59. ''' Gibt die Download-URL zurück
      60. ''' </summary>
      61. Public Overrides Function ToString() As String
      62. ' Download-Url aufbauen
      63. Return String.Format("{0}&fallback_host={1}&signature={2}", Url, FallBack_Host, Sig)
      64. End Function
      65. End Structure


      Ich habe dazu dann noch eine Wrapper-Klasse geschrieben, sodass die Informationen zu den einzelnen Teilen des Videos erreichbar sind.

      VB.NET-Quellcode

      1. ''' <summary>
      2. ''' Ein Modul, was Methoden zur Verarbeitung von YT-Videos bereitstellt
      3. ''' </summary>
      4. Public Module YTInformation
      5. ''' <summary>
      6. ''' Gibt generisch alle Keys des Videos zurück. (url_encoded_fmt_stream_map, Author, etc.)
      7. ''' </summary>
      8. ''' <param name="videoId">die VideoID. Und nur die. Keine URL!</param>
      9. Public Function Information(videoId As String) As Dictionary(Of String, String)
      10. Return Information(videoId).ToDictionary(Function(item) item.Key, Function(item) item.Value)
      11. End Function
      12. ''' <summary>
      13. ''' Gibt alle VideoInfos zu einer URL_ENCODED_FMT_STREAM_MAP.
      14. ''' </summary>
      15. ''' <param name="data">url_encoded_fmt_stream_map und nur das.</param>
      16. Public Iterator Function UrlMap(data As String) As IEnumerable(Of VideoInfo)
      17. Dim splitted As String() = data.Split(","c)
      18. For Each item As String In splitted
      19. Yield urlInfo(item)
      20. Next
      21. End Function
      22. Private Iterator Function _information(videoId As String) As IEnumerable(Of KeyValuePair(Of String, String))
      23. Dim client As New WebClient()
      24. Dim data As String = client.DownloadString(New Uri("http://youtube.com/get_video_info?&video_id=" & videoId))
      25. Dim splitted As String() = data.Split("&"c)
      26. For Each item As String In splitted
      27. Yield New KeyValuePair(Of String, String)(item.Split("="c)(0), item.Split("="c)(1))
      28. Next
      29. End Function
      30. Private Function urlInfo(line As String) As VideoInfo
      31. Dim info As New VideoInfo()
      32. Dim infoType As Type = info.GetType()
      33. Dim properties As Dictionary(Of String, PropertyInfo) = infoType.GetProperties().ToDictionary(Function(item) item.Name.ToLower(), Function(item) item)
      34. Dim data As String() = line.Split("&"c)
      35. For Each item As String In data
      36. Dim split As String() = item.Split("="c)
      37. properties(split(0).ToLower()).SetValue(info, Convert.ChangeType(split(1), properties(split(0).ToLower()).PropertyType))
      38. Next
      39. Return info
      40. End Function
      41. End Module

      Die Dokumentation ist etwas .. kurz geraten, weil ich per se keine Dokumentation nutze. Naja.
      Jedenfalls kann mit diesem Modul ein Video auf alle Informationen geprüft werden.

      Zudem noch ein kleines Anwendungsbeispiel:

      VB.NET-Quellcode

      1. Sub Main()
      2. Console.WriteLine("Video Id")
      3. Dim id As String = Console.ReadLine()
      4. Dim test As Dictionary(Of String, String) = YTInformation.Information(id)
      5. Dim dat As String = HttpUtility.UrlDecode(test("url_encoded_fmt_stream_map"))
      6. Dim t As IEnumerable(Of VideoInfo) = YTInformation.UrlMap(dat)
      7. Dim index As Integer = 0
      8. Console.WriteLine(String.Join(Environment.NewLine, t.Select(Function(item) increment(index).ToString() & ") " & item.Type & " " & item.Quality)))
      9. Console.WriteLine("Index")
      10. If Integer.TryParse(Console.ReadLine(), index) Then
      11. Dim info As VideoInfo = t.ElementAt(index)
      12. Dim client As New WebClient()
      13. Dim url As String = info.ToString()
      14. Dim decode As String = HttpUtility.UrlDecode(test("title"))
      15. For Each c As Char In Path.GetInvalidFileNameChars()
      16. decode = decode.Replace(c, "-"c)
      17. Next
      18. Dim fileType As String = info.FileType
      19. client.DownloadFile(url, decode & "." & fileType)
      20. End If
      21. End Sub
      22. Private Function increment(ByRef num As Integer) As Integer
      23. num += 1
      24. Return num
      25. End Function


      Ich werde zu diesem Modul keinen Support geben, da ihr euch sicherlich selbst erschließen könnt, wie was funktionieren soll. Wenn nicht, hier noch ein paar Links:
      System.Collections.Generic.IEnumerable(Of T)
      System.Type.GetProperties()
      Yield
      Iterator
      System.Convert.ChangeType
      Im Anhang findet ihr eine Solution (VS2013RC) mit einem C#- und einem VB-Projekt. Beides .NET 4.5.
      Dateien
      • YTDown.zip

        (13,82 kB, 143 mal heruntergeladen, zuletzt: )