Imports System.Data
Imports System.Data.SqlClient
Namespace Copyright.YouTube
Public Class Controller
Public Shared Function GetScoredVideos() As Integer
' Set the API call to return a list with the 'fairuseScore' tag
Dim apiUrl = "http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag&
dev_id=myDeveloperID_w&tag=fairuseScore&per_page=100"
Dim newScoredVideos As Integer = 0
Try
' Call the YouTube API
Dim myRequest As System.Net.WebRequest = System.Net.WebRequest.Create(apiUrl)
Dim myResponse As System.Net.WebResponse = myRequest.GetResponse()
Dim ApiStream As System.IO.Stream = myResponse.GetResponseStream()
Dim ApiDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
ApiDoc.Load(ApiStream)
Dim apiVideo As System.Xml.XmlNodeList = ApiDoc.SelectNodes
("ut_response/video_list/video")
Dim currentVideo As System.Xml.XmlNode
' Loop throught the apiVideo list
For Each currentVideo In apiVideo
Dim Video_details As System.Xml.XmlNode
' Create a new Video object for each one in the list
Dim myVideo As New Video
' Author
Video_details = currentVideo.SelectSingleNode("author")
If Not IsNothing(Video_details) Then
myVideo.Author = Video_details.InnerText
Else
myVideo.Author = ""
End If
' ID
Video_details = currentVideo.SelectSingleNode("id")
If Not IsNothing(Video_details) Then
myVideo.ID = Video_details.InnerText
Else
myVideo.ID = ""
End If
' Title
Video_details = currentVideo.SelectSingleNode("title")
If Not IsNothing(Video_details) Then
myVideo.Title = Video_details.InnerText
Else
myVideo.Title = ""
End If
' Length
Video_details = currentVideo.SelectSingleNode("length_seconds")
If Not IsNothing(Video_details) Then
myVideo.Length_Seconds = CInt(Video_details.InnerText)
Else
myVideo.Length_Seconds = -1
End If
' Rating
Video_details = currentVideo.SelectSingleNode("rating_avg")
If Not IsNothing(Video_details) Then
myVideo.Rating_Avg = CDec(Video_details.InnerText)
Else
myVideo.Rating_Avg = -1
End If
' Rating Count
Video_details = currentVideo.SelectSingleNode("rating_count")
If Not IsNothing(Video_details) Then
myVideo.Rating_Count = CInt(Video_details.InnerText)
Else
myVideo.Rating_Count = -1
End If
' Description
Video_details = currentVideo.SelectSingleNode("description")
If Not IsNothing(Video_details) Then
myVideo.Description = Video_details.InnerText
Else
myVideo.Description = ""
End If
' View Count
Video_details = currentVideo.SelectSingleNode("view_count")
If Not IsNothing(Video_details) Then
myVideo.View_Count = CInt(Video_details.InnerText)
Else
myVideo.View_Count = -1
End If
' Upload Time
Video_details = currentVideo.SelectSingleNode("upload_time")
If Not IsNothing(Video_details) Then
myVideo.Upload_Time = Video_details.InnerText
Else
myVideo.Upload_Time = ""
End If
' Comment Count
Video_details = currentVideo.SelectSingleNode("comment_count")
If Not IsNothing(Video_details) Then
myVideo.Comment_Count = CInt(Video_details.InnerText)
Else
myVideo.Comment_Count = -1
End If
' Tags
Video_details = currentVideo.SelectSingleNode("tags")
If Not IsNothing(Video_details) Then
myVideo.Tags = Video_details.InnerText
' Parse the fairuseScore out of the Tags string
myVideo.FairUse_Score = myVideo.ParseScoreFromTag(Video_details.InnerText)
Else
myVideo.Tags = ""
End If
' URL
Video_details = currentVideo.SelectSingleNode("url")
If Not IsNothing(Video_details) Then
myVideo.URL = Video_details.InnerText
Else
myVideo.URL = ""
End If
' Thumbnail Image
Video_details = currentVideo.SelectSingleNode("thumbnail_url")
If Not IsNothing(Video_details) Then
myVideo.Thumbnail_URL = Video_details.InnerText
Else
myVideo.Thumbnail_URL = ""
End If
' Embed Status
Video_details = currentVideo.SelectSingleNode("embed_status")
If Not IsNothing(Video_details) Then
If Video_details.InnerText = "OK" Then
myVideo.Embed_Status = True
Else
myVideo.Embed_Status = False
End If
Else
myVideo.Embed_Status = False
End If
' If Video is already in DB, just Update it, otherwise Insert.
If Copyright.YouTube.Video.VideoExists(myVideo.ID) Then
myVideo.Update()
Else
myVideo.Add()
End If
newScoredVideos += 1
Next
' End loop
Catch ex As Exception
Throw ex
End Try
End Function
End Class
Public Class Video
#Region "Private Fields"
Private m_VideoID As Integer
Private m_ID As String
Private m_Author As String
Private m_Title As String
Private m_Rating_Avg As Decimal
Private m_Rating_Count As Integer
Private m_Tags As String
Private m_Description As String
Private m_Update_Time As String
Private m_View_Count As Integer
Private m_Comment_Count As Integer
Private m_Upload_Time As String
Private m_Length_Seconds As Integer
Private m_Recording_Date As String
Private m_Recording_Location As String
Private m_Recording_Country As String
Private m_URL As String
Private m_Thumbnail_URL As String
Private m_Embed_Status As Boolean
Private m_FairUse_Score As Integer
#End Region
#Region "Public Properties"
Public Property VideoID() As Integer
Get
Return m_VideoID
End Get
Set(ByVal value As Integer)
m_VideoID = value
End Set
End Property
Public Property ID() As String
Get
Return m_ID
End Get
Set(ByVal value As String)
m_ID = value
End Set
End Property
Public Property Author() As String
Get
Return m_Author
End Get
Set(ByVal value As String)
m_Author = value
End Set
End Property
Public Property Title() As String
Get
Return m_Title
End Get
Set(ByVal value As String)
m_Title = value
End Set
End Property
Public Property Rating_Avg() As Decimal
Get
Return m_Rating_Avg
End Get
Set(ByVal value As Decimal)
m_Rating_Avg = value
End Set
End Property
Public Property Rating_Count() As Integer
Get
Return m_Rating_Count
End Get
Set(ByVal value As Integer)
m_Rating_Count = value
End Set
End Property
Public Property Tags() As String
Get
Return m_Tags
End Get
Set(ByVal value As String)
m_Tags = value
End Set
End Property
Public Property Description() As String
Get
Return m_Description
End Get
Set(ByVal value As String)
m_Description = value
End Set
End Property
Public Property Update_Time() As String
Get
Return m_Update_Time
End Get
Set(ByVal value As String)
m_Update_Time = value
End Set
End Property
Public Property View_Count() As Integer
Get
Return m_View_Count
End Get
Set(ByVal value As Integer)
m_View_Count = value
End Set
End Property
Public Property Comment_Count() As Integer
Get
Return m_Comment_Count
End Get
Set(ByVal value As Integer)
m_Comment_Count = value
End Set
End Property
Public Property Upload_Time() As String
Get
Return m_Upload_Time
End Get
Set(ByVal value As String)
m_Upload_Time = value
End Set
End Property
Public Property Length_Seconds() As Integer
Get
Return m_Length_Seconds
End Get
Set(ByVal value As Integer)
m_Length_Seconds = value
End Set
End Property
Public Property Recording_Date() As String
Get
Return m_Recording_Date
End Get
Set(ByVal value As String)
m_Recording_Date = value
End Set
End Property
Public Property Recording_Location() As String
Get
Return m_Recording_Location
End Get
Set(ByVal value As String)
m_Recording_Location = value
End Set
End Property
Public Property Recording_Country() As String
Get
Return m_Recording_Country
End Get
Set(ByVal value As String)
m_Recording_Country = value
End Set
End Property
Public Property URL() As String
Get
Return m_URL
End Get
Set(ByVal value As String)
m_URL = value
End Set
End Property
Public Property Thumbnail_URL() As String
Get
Return m_Thumbnail_URL
End Get
Set(ByVal value As String)
m_Thumbnail_URL = value
End Set
End Property
Public Property Embed_Status() As Boolean
Get
Return m_Embed_Status
End Get
Set(ByVal value As Boolean)
m_Embed_Status = value
End Set
End Property
Public Property FairUse_Score() As Integer
Get
Return m_FairUse_Score
End Get
Set(ByVal value As Integer)
m_FairUse_Score = value
End Set
End Property
#End Region
#Region "Constructors"
'''
''' Instantiate an empty class.
'''
'''
Sub New()
ResetProperties()
End Sub
'''
''' Instantiate and load with an existing ID.
'''
'''
'''
Sub New(ByVal existingVideoID As Integer)
m_VideoID = existingVideoID
LoadFromID()
End Sub
'''
''' Instantiate and load with an existing object.
'''
'''
'''
Sub New(ByVal existingVideo As Video)
m_VideoID = existingVideo.VideoID
LoadFromID()
End Sub
#End Region
#Region "Public Methods"
'''
''' Retrieve and load the values for the properties from the existing ProductID.
''' Return the object's loaded ProductID.
'''
'''
'''
'''
Public Overloads Function LoadFromID(ByVal existingVideoID As Integer) As Integer
m_VideoID = existingVideoID
LoadFromID()
Return m_VideoID
End Function
'''
''' Add the Fair Use data; return the ID.
'''
'''
'''
Public Function Add() As Integer
Dim myAdapter As New YouTubeDataSetTableAdapters.YTVideoTableAdapter
m_VideoID = myAdapter.IdentityInsert( _
m_ID, _
m_Author, _
m_Title, _
m_Rating_Avg, _
m_Rating_Count, _
m_Tags, _
m_Description, _
m_Update_Time, _
m_View_Count, _
m_Comment_Count, _
m_Upload_Time, _
m_Length_Seconds, _
m_Recording_Date, _
m_Recording_Location, _
m_Recording_Country, _
m_URL, _
m_Thumbnail_URL, _
m_Embed_Status, _
m_FairUse_Score)
Return m_VideoID
End Function
'''
''' Update the values of the class.
'''
'''
Public Sub Update()
Dim myAdapter As New YouTubeDataSetTableAdapters.YTVideoTableAdapter
myAdapter.Update( _
m_ID, _
m_Author, _
m_Title, _
m_Rating_Avg, _
m_Rating_Count, _
m_Tags, _
m_Description, _
m_Update_Time, _
m_View_Count, _
m_Comment_Count, _
m_Upload_Time, _
m_Length_Seconds, _
m_Recording_Date, _
m_Recording_Location, _
m_Recording_Country, _
m_URL, _
m_Thumbnail_URL, _
m_Embed_Status, _
m_FairUse_Score, _
m_VideoID, _
m_VideoID)
End Sub
'''
''' Delete a Deposit.
'''
'''
Public Sub Delete()
Dim myAdapter As New YouTubeDataSetTableAdapters.YTVideoTableAdapter
myAdapter.Delete(m_VideoID)
ResetProperties()
End Sub
'''
'''
'''
'''
'''
'''
Public Function ParseScoreFromTag(ByVal TagString As String) As Integer
Dim myTagString As String
Dim myScore As Integer = 0
' Find tag fairuseScore
myTagString = TagString.Substring(TagString.IndexOf("fairuseScore") + 12,
TagString.IndexOf("fairuseScore") + 4)
myScore = CInt(myTagString)
Return myTagString
End Function
#End Region
#Region "Shared Methods"
'''
''' Retrieve a list of all FairUses;
''' return a datatable.
'''
'''
'''
Public Shared Function List() As DataTable
Dim myAdapter As New YouTubeDataSetTableAdapters.YTVideoTableAdapter
Return myAdapter.GetData()
End Function
'''
''' Check to see if CustomerID is in the DB.
''' Return true if it is.
'''
'''
'''
'''
Public Shared Function VideoExists(ByVal newID As String) As Boolean
Dim ta As New YouTubeDataSetTableAdapters.YTVideoTableAdapter
Dim myVideoCount As Integer
myVideoCount = ta.VideoExists(newID)
If myVideoCount > 0 Then
Return True
Else
Return False
End If
End Function
#End Region
#Region "Private Methods"
'''
''' Retrieve and load the values for the properties.
'''
'''
Private Overloads Sub LoadFromID()
' Get datarow of author information from database.
Dim dr As DataRow
Dim daVideoDetails As New YouTubeDataSetTableAdapters.YTVideoTableAdapter
dr = daVideoDetails.GetData.FindByVideoID(m_VideoID)
' Load class fields from datarow.
m_VideoID = CInt(dr("VideoID"))
m_ID = IIf(dr("ID") Is DBNull.Value, "", dr("ID").ToString())
m_Author = IIf(dr("Author") Is DBNull.Value, "", dr("Author").ToString())
m_Title = IIf(dr("Title") Is DBNull.Value, "", dr("Title").ToString())
m_Rating_Avg = CDec(dr("Rating_Avg"))
m_Rating_Count = IIf(dr("Rating_Count") Is DBNull.Value, -1,
CInt(dr("Rating_Count")))
m_Tags = IIf(dr("Tags") Is DBNull.Value, "", dr("Tags").ToString())
m_Description = IIf(dr("Description") Is DBNull.Value, "",
dr("Description").ToString())
m_Update_Time = IIf(dr("Update_Time") Is DBNull.Value, "",
dr("Update_Time").ToString())
m_View_Count = IIf(dr("View_Count") Is DBNull.Value, -1,
CInt(dr("View_Count")))
m_Comment_Count = IIf(dr("Comment_Count") Is DBNull.Value, -1,
CInt(dr("Comment_Count")))
m_Upload_Time = IIf(dr("Upload_Time") Is DBNull.Value, "",
dr("Upload_Time").ToString())
m_Length_Seconds = IIf(dr("Length_Seconds") Is DBNull.Value, -1,
CInt(dr("Length_Seconds")))
m_Recording_Date = IIf(dr("Recording_Date") Is DBNull.Value, "",
dr("Recording_Date").ToString())
m_Recording_Location = IIf(dr("Recording_Location") Is DBNull.Value, "",
dr("Recording_Location").ToString())
m_Recording_Country = IIf(dr("Recording_Country") Is DBNull.Value, "",
dr("Recording_Country").ToString())
m_URL = IIf(dr("URL") Is DBNull.Value, "", dr("URL").ToString())
m_Thumbnail_URL = IIf(dr("Thumbnail_URL") Is DBNull.Value, "",
dr("Thumbnail_URL").ToString())
m_Embed_Status = CBool(dr("Embed_Status"))
m_FairUse_Score = IIf(dr("FairUse_Score") Is DBNull.Value, -1,
CInt(dr("FairUse_Score")))
End Sub
'''
''' Clear the values for the object's fields.
'''
'''
Private Sub ResetProperties()
m_VideoID = -1
m_ID = ""
m_Author = ""
m_Title = ""
m_Rating_Avg = -1
m_Rating_Count = -1
m_Tags = ""
m_Description = ""
m_Update_Time = ""
m_View_Count = -1
m_Comment_Count = 0
m_Upload_Time = ""
m_Length_Seconds = -1
m_Recording_Date = ""
m_Recording_Location = ""
m_Recording_Country = ""
m_URL = ""
m_Thumbnail_URL = ""
m_Embed_Status = False
m_FairUse_Score = -1
End Sub
#End Region
End Class
End Namespace
Comments...