How to get video info from youtube via JSON using vb.net and System.Web.Script.Serialization or JavaScriptSerializer


'To get info from youtube API, check out the two functions below. One calls the other and it returns a JSON object which is filtered out in the first function. The result is an arraylist with Title, Views, and Description. You can easily provide a list of Youtube video ID's, send each id to the getVideoInfo function and get an arraylist back that can be used to fill the data structure of your choice - a DataTable is used here and then bound to a repeater

The code:

---

Imports System.Data
Imports System.Net
Imports System.IO
Imports System.Collections.Generic
Imports System.Web.Script.Serialization

Partial Class myApp_TrainingVideos

Inherits Page
Dim vids As New ArrayList


Protected Sub Page_Load1(sender As Object, e As EventArgs) Handles Me.Load

Dim srcBase As String = "https://www.youtube.com/embed/"
'adding videos to the arraylist - can easily load these from database here as well
vids.Add("5VcSwejU2D0")
vids.Add("Hcbl5kSCUIA")

Dim dt As New DataTable
dt.Columns.Add("srcLink")
dt.Columns.Add("srcTitle")
dt.Columns.Add("srcDesc")
dt.Columns.Add("srcViews")
For Each vid As String In vids
Dim viddata As ArrayList = getVideoInfo(vid)
dt.Rows.Add(srcBase & vid, viddata(0), viddata(1), viddata(2))
Next
If dt.Rows.Count > 0 Then
Repeater1.DataSource = dt
Repeater1.DataBind()
End If

End Sub


Public Shared Function getVideoInfo(ByVal videoID As String) As ArrayList

Dim serializer As New JavaScriptSerializer()
Dim dict As Dictionary(Of String, Object)
Dim dictResultsList As IList
Dim dictResultsList2 As Dictionary(Of String, Object)
Dim dictAgain As Dictionary(Of String, Object)
Dim dictViewCount As Dictionary(Of String, Object)
Dim description As String = ""
Dim vidInfo As New ArrayList
Dim URL As String = "https://www.googleapis.com/youtube/v3/videos?id="
Dim video As String = videoID
Dim ourKey As String = "&key=YOURYOUTUBEKEYHERE&part=snippet,contentDetails,statistics,status"
URL = URL & video & ourKey
Dim json As String = ""
Dim request As HttpWebRequest = WebRequest.Create(URL)
Dim httpResponse As HttpWebResponse = request.GetResponse()
Dim sr As StreamReader = New StreamReader(httpResponse.GetResponseStream(), Encoding.GetEncoding(1252))
Dim text As String = sr.ReadToEnd
Dim Title As String = ""
Dim views As String = ""
Dim descriptionSubstring = ""
json = text
dict = serializer.DeserializeObject(json)
dictResultsList = dict("items")
dictResultsList2 = dictResultsList("(0)")
dictAgain = dictResultsList2("snippet")
dictViewCount = dictResultsList2("statistics")
views = dictViewCount("viewCount")
description = dictAgain("description")
description = Regex.Replace(description, "(\b(?:https?://|www\.)\S*)", " ")
Title = dictAgain("title")
vidInfo.Add(Title)
vidInfo.Add(description)
vidInfo.Add(views)
Return vidInfo

End Function


Protected Function getJSONFromGoogle(ByVal videoID As String) As String

Dim URL As String = "https://www.googleapis.com/youtube/v3/videos?id="
Dim video As String = videoID
Dim ourKey As String = "&key=YOURYOUTUBEKEYHERE&part=snippet,contentDetails,statistics,status"
URL = URL & video & ourKey
Dim json As String = ""
Dim request As HttpWebRequest = WebRequest.Create(URL)
Dim httpResponse As HttpWebResponse = request.GetResponse()
Dim sr As StreamReader = New StreamReader(httpResponse.GetResponseStream(), Encoding.GetEncoding(1252))
Dim text As String = sr.ReadToEnd
json = text
Return json

End Function

 

Tags: API, JSON, Youtube
Last update:
2015-05-07 20:54
Author:
Mark
Revision:
1.0
Average rating: 1 (2 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags