TizzyT

YouTubeRequest - TizzyT

May 9th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.74 KB | None | 0 0
  1. Imports Globals.GlobalResources
  2. Imports SHDocVw
  3. Imports System.Net
  4.  
  5. Public Class Class1
  6.     Implements Globals.Plgn
  7.  
  8.     Dim WithEvents Browser As SHDocVw.InternetExplorer ' Browser object
  9.     Dim WithEvents Poller As New Timers.Timer ' timer object
  10.     Dim playlist As New List(Of String) ' list to store playlist
  11.     Dim playing As Boolean = False ' playing status
  12.  
  13.     Public Sub Config() Implements Globals.Plgn.Config
  14.         ' Displays a message box when trying to configure plugin
  15.         MsgBox("Nothing to configure yet" & vbCrLf & _
  16.                "!ytr <youtube video ID> to add to playlist" & vbCrLf & _
  17.                "Skipping and other feature will come later." & vbCrLf & _
  18.                "This is only a test version")
  19.     End Sub
  20.  
  21.     Public ReadOnly Property Name As String Implements Globals.Plgn.Name
  22.         Get
  23.             Return "YouTubeRequest - TizzyT"
  24.         End Get
  25.     End Property
  26.  
  27.     Public Function ProcessMessage(InputMessage As Globals.GlobalResources.Message) As Boolean _
  28.         Implements Globals.Plgn.ProcessMessage
  29.         If InputMessage.Type = MessageType.PRIVMSG Then ' If message is a PRIVMSG ...
  30.             ' Check if the message is from the bot owner, if so ...
  31.             If InputMessage.Nick.Trim().ToLower() = InputMessage.Owner.Trim().ToLower() Then
  32.                 ' look to see if command !ytr was issued in message, if so ...
  33.                 If InputMessage.Message.StartsWith("!ytr", StringComparison.CurrentCultureIgnoreCase) Then
  34.                     ' get video id from command
  35.                     Dim VideoID As String = InputMessage.Message.Trim().Split(" ")(1)
  36.                     ' grab source from youtube page to get title from ...
  37.                     Dim Title As String = New WebClient().DownloadString("https://www.youtube.com/watch?v=" & VideoID)
  38.                     ' taking everything right of "<title>" and then ...
  39.                     Title = Title.Split(New String() {"<title>"}, StringSplitOptions.None)(1)
  40.                     ' taking everything left of "</title>" and remove leading/trailing white space.
  41.                     Title = Title.Split(New String() {"</title>"}, StringSplitOptions.None)(0).Trim()
  42.                     If Title = "YouTube" Then ' if tite is "YouTube" its an invalid video.
  43.                         ' Announce that its an invalid video.
  44.                         AddToSend("PRIVMSG " & InputMessage.Target & " :Invalid ID " & InputMessage.Nick)
  45.                     ElseIf Title.EndsWith(" - YouTube") Then ' if title ends with " - YouTube" its most likely valid
  46.                         Title = Title.Replace(" - YouTube", String.Empty) ' clean up title by removing " - YouTube"
  47.                         Me.playlist.Add(VideoID & " ::: " & Title) ' add id and title to playlist
  48.                         AddToSend("PRIVMSG " & InputMessage.Target + " :Added: " & Title) ' announce added video
  49.                     End If
  50.                 End If
  51.             End If
  52.         End If
  53.         Return False ' return false so that this message can be handled my the next plugin
  54.     End Function
  55.  
  56.     Protected Overrides Sub Finalize()
  57.         playing = False
  58.         Try
  59.             ' try to close the browser object
  60.             Browser.Quit()
  61.         Catch ex As Exception
  62.             ' if ie instance refuses to close, forcefully close all ie instances
  63.             While True
  64.                 Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("iexplore")
  65.                 If pProcess.Length = 0 Then
  66.                     Exit While
  67.                 Else
  68.                     For Each p As Process In pProcess
  69.                         Try
  70.                             p.Kill()
  71.                         Catch ex1 As Exception
  72.                         End Try
  73.                     Next
  74.                 End If
  75.             End While
  76.         End Try
  77.         MyBase.Finalize()
  78.     End Sub
  79.  
  80.     ' This function is triggered everytime the browser title changes
  81.     Private Sub Browser_TitleChange(Text As String) Handles Browser.TitleChange
  82.         If Text = "0" Then ' if browser title is 0 (meaning video finished playing) ...
  83.             playing = False ' set playing status to false and ...
  84.             Browser.Quit() ' Close the browser object
  85.         End If
  86.     End Sub
  87.  
  88.     ' This function is triggered every half second
  89.     Private Sub Poller_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles Poller.Elapsed
  90.         If Not playing Then ' if playing status is false ...
  91.             If playlist.Count > 0 Then ' check how many videos are in the playlist, its greater than 0 ...
  92.                 playing = True ' set playing status to true
  93.                 Dim Info(2) As String ' create string array to store video info
  94.                 Info(0) = playlist(0) ' info(0) = the first video in playlist
  95.                 playlist.RemoveAt(0) ' remove first video from playlist
  96.                 Info(1) = Info(0).Split(" ::: ")(0) ' info(1) = left side of " ::: " (videoid)
  97.                 Info(2) = Info(0).Substring(Info(1).Length + 5) ' info(2) = right side of " ::: " (title)
  98.                 Browser = CreateObject("InternetExplorer.Application") ' create a new browser object for video to play
  99.                 Browser.Visible = False ' ensures browser is not visible
  100.                 ' browser navigates to specified url trailed by videoID
  101.                 Browser.Navigate("http://tizzyt-archive.blogspot.com/p/blog-page_3.html?" & Info(1))
  102.                 ' Annouce that the video(as title) is now playing.
  103.                 AddToSend("PRIVMSG " & GetCurrentChan() & " :Now Playing: " & Info(2))
  104.             End If
  105.         End If
  106.     End Sub
  107.  
  108.     Public Sub New()
  109.         Poller.Interval = 500
  110.         playing = False
  111.         Poller.Start()
  112.     End Sub
  113. End Class
Advertisement
Add Comment
Please, Sign In to add comment