/trunk/ver5/AddIn.NicoLive/NicoLiveSource.vb

# · Visual Basic · 220 lines · 173 code · 47 blank · 0 comment · 0 complexity · 4db1e3d5684aca4612c44909ffa789aa MD5 · raw file

  1. Imports System.ComponentModel.Composition
  2. Imports Pronama.Misao
  3. Imports System.Net.Sockets
  4. Imports System.IO
  5. Imports System.Threading
  6. Imports System.ComponentModel
  7. Imports System.Text.RegularExpressions
  8. Imports System.Windows.Media
  9. Imports Pronama.NicoVideo.LiveStreaming
  10. Imports Pronama.NicoVideo
  11. Imports System.Net
  12. Imports System.Threading.Tasks
  13. <AddInExport("{0A70272E-2C7E-42B1-A7EA-C3C656B337FD}", "Nico Live Program",
  14. Description:="Get comments from live.nicovideo.jp",
  15. HasSetting:=True)>
  16. <Export(GetType(ISourceAddIn))>
  17. Public Class NicoLiveSource
  18. Implements ISourceAddIn
  19. Private Activated As Boolean
  20. Private WithEvents Client As LiveProgramClient
  21. Private Id As String
  22. Private FilterNg As Boolean
  23. #Region "Implementation"
  24. Public Event Deactivated(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAddIn.Deactivated
  25. Protected Sub OnDeactivated(ByVal e As EventArgs)
  26. RaiseEvent Deactivated(Me, e)
  27. End Sub
  28. <Import(GetType(PostMessageCallback))>
  29. Property PostMessage As PostMessageCallback Implements ISourceAddIn.PostMessage
  30. <Import(GetType(WriteLogCallback))>
  31. Public Property WriteLog As WriteLogCallback Implements ISourceAddIn.WriteLog
  32. Private Settings As ISettings
  33. Public Sub Initialize(ByVal settings As Pronama.Misao.ISettings) Implements Pronama.Misao.IAddIn.Initialize
  34. Me.Settings = settings
  35. settings.TryGetValue(Of String)("Id", Id)
  36. settings.TryGetValue(Of Boolean)("FilterNg", FilterNg)
  37. End Sub
  38. Public Sub Activate() Implements IAddIn.Activate
  39. Activated = True
  40. ConnectAsync()
  41. End Sub
  42. Public Sub Deactivate() Implements IAddIn.Deactivate
  43. Activated = False
  44. Close()
  45. OnDeactivated(EventArgs.Empty)
  46. End Sub
  47. Public Sub ShowDialog(ByVal owner As System.Windows.Window) Implements IAddIn.ShowDialog
  48. Dim window = New SettingWindow
  49. window.Owner = owner
  50. window.Id = Id
  51. window.FilterNg = FilterNg
  52. If window.ShowDialog Then
  53. Id = window.Id
  54. FilterNg = window.FilterNg
  55. Settings("Id") = Id
  56. Settings("FilterNg") = FilterNg
  57. Settings.Save()
  58. End If
  59. End Sub
  60. #End Region
  61. Private Function GetLiveId() As String
  62. If Id.StartsWith("lv") Then
  63. Return Id
  64. End If
  65. Try
  66. Dim wc = New WebClient
  67. Dim body = wc.DownloadString(New Uri("http://live.nicovideo.jp/watch/" & Id))
  68. Dim match = Regex.Match(body, "http://live.nicovideo.jp/watch/(?<id>lv(\d+))")
  69. If match.Success Then
  70. Return match.Groups("id").Value
  71. End If
  72. Catch ex As Exception
  73. End Try
  74. Return Nothing
  75. End Function
  76. Private Sub Log(ex As Exception)
  77. If TypeOf ex Is AggregateException Then
  78. For Each ie In DirectCast(ex, AggregateException).InnerExceptions
  79. WriteLog.Invoke(Me, New Log With {.Text = "Error: " & ie.Message})
  80. Next
  81. Else
  82. WriteLog.Invoke(Me, New Log With {.Text = "Error: " & ex.Message})
  83. End If
  84. End Sub
  85. Private Sub ConnectAsync()
  86. If Id = "" Then
  87. WriteLog.Invoke(Me, New Log With {.Text = "Error: No live ID"})
  88. Deactivate()
  89. Exit Sub
  90. End If
  91. WriteLog.Invoke(Me, New Log With {.Text = "Connecting..."})
  92. Me.Client = New LiveProgramClient
  93. NicoVideoWeb.GetLiveProgramAsync(Me.Id) _
  94. .ContinueWith(
  95. Sub(t)
  96. If t.IsFaulted Then
  97. Log(t.Exception)
  98. Exit Sub
  99. End If
  100. LiveProgramClient.GetCommentServersAsync(t.Result.Id) _
  101. .ContinueWith(
  102. Sub(t2)
  103. If t2.IsFaulted Then
  104. Log(t2.Exception)
  105. Else
  106. Me.Client.ConnectAsync(t2.Result.First)
  107. End If
  108. End Sub)
  109. End Sub)
  110. End Sub
  111. Private Function CreateMessage(ByVal comment As LiveCommentMessage) As Message
  112. If comment.Text.StartsWith("/") Then
  113. If comment.Source = ChatSource.Broadcaster OrElse
  114. comment.Source = ChatSource.Operator OrElse
  115. comment.Source = ChatSource.Alert Then
  116. Return Nothing
  117. End If
  118. End If
  119. If Me.FilterNg AndAlso comment.Score < -999 Then
  120. Return Nothing
  121. End If
  122. Dim m = New Message
  123. m.Text = comment.Text
  124. Dim commands = comment.Mail.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
  125. If commands.Contains("shita") Then
  126. m.Motion = MessageMotion.StackBottom
  127. ElseIf commands.Contains("ue") Then
  128. m.Motion = MessageMotion.StackTop
  129. Else
  130. m.Motion = MessageMotion.FlowDown
  131. End If
  132. If commands.Contains("big") Then
  133. m.Size = MessageSize.Large
  134. ElseIf commands.Contains("small") Then
  135. m.Size = MessageSize.Small
  136. Else
  137. m.Size = MessageSize.Normal
  138. End If
  139. Dim colorCommands = New String() {"white", "red", "pink", "orange", "yellow", "green", "cyan", "blue", "purple", "black", "niconicowhite", "truered", "passionorange", "madyellow", "elementalgreen", "marineblue", "nobleviolet"}
  140. Dim colors = New Color() {Color.FromRgb(&HFF, &HFF, &HFF), Color.FromRgb(&HFF, 0, 0), Color.FromRgb(&HFF, &H80, &H80), Color.FromRgb(&HFF, &HCC, 0), Color.FromRgb(&HFF, &HFF, 0), Color.FromRgb(0, &HFF, 0), Color.FromRgb(0, &HFF, &HFF), Color.FromRgb(0, 0, &HFF), Color.FromRgb(&HC0, 0, &HFF), Color.FromRgb(0, 0, 0), _
  141. Color.FromRgb(&HCC, &HCC, &H99), Color.FromRgb(&HCC, 0, &H33), Color.FromRgb(&HFF, &H66, 0), Color.FromRgb(&H99, &H99, 0), Color.FromRgb(0, &HCC, &H66), Color.FromRgb(&H33, &HFF, &HFC), Color.FromRgb(&H66, &H33, &HCC)}
  142. m.ForegroundColor = System.Windows.Media.Colors.White
  143. For i = 0 To colorCommands.Count - 1
  144. If commands.Contains(colorCommands(i)) Then
  145. m.ForegroundColor = colors(i)
  146. Exit For
  147. End If
  148. Next
  149. Return m
  150. End Function
  151. Private Sub Close()
  152. If Client IsNot Nothing Then
  153. Client.Close()
  154. Client = Nothing
  155. End If
  156. End Sub
  157. Private Sub Client_CommentReceived(sender As Object, e As NicoVideo.LiveStreaming.CommentReceivedEventArgs) Handles Client.CommentReceived
  158. If Not TypeOf e.Comment Is LiveCommentMessage Then
  159. Exit Sub
  160. End If
  161. Dim message = CreateMessage(DirectCast(e.Comment, LiveCommentMessage))
  162. If message Is Nothing Then
  163. Exit Sub
  164. End If
  165. PostMessage.Invoke(Me, message)
  166. End Sub
  167. Private Sub Client_ConnectCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles Client.ConnectCompleted
  168. WriteLog.Invoke(Me, New Log With {.Text = "Connection: " & If(Me.Client.Connected, "Connected", "Disconnected")})
  169. End Sub
  170. Private Sub Client_ConnectedChanged(sender As Object, e As System.EventArgs) Handles Client.ConnectedChanged
  171. WriteLog.Invoke(Me, New Log With {.Text = "Connection Changed: " & If(Me.Client.Connected, "Connected", "Disconnected")})
  172. End Sub
  173. End Class