作るのに参考にしたサイト
とある技術の備忘録 – C#でニコ生からコメント取得
http://d.hatena.ne.jp/r7kamura/20091123/1258948977
NicoPITA Blog.
http://nicopita.info/NicoPITA/
今回は、VB.NET で作成していきます。
Imports System.Net.Sockets Imports System.Text Module Module1 Sub Main() 'http://watch.live.nicovideo.jp/api/getplayerstatus?v=lv何とか 'msの値 '取ってくるのはまた別に書くよ! Dim thread As Integer = 1062986843 Dim addr As String = "msg102.live.nicovideo.jp" Dim port As Integer = 2813 Dim more As String = "" 'res_from で過去ログ取得 -10にすると、-10コメント前を取ってくる Dim req = String.Format("<thread thread=""{0}"" version=""20061206"" res_from=""-10"" /> ", thread) Dim tcp As New TcpClient(addr, Integer.Parse(port.ToString)) Dim ns As NetworkStream = tcp.GetStream() Dim sendBytes As Byte() = Encoding.UTF8.GetBytes(req) sendBytes(sendBytes.Length - 1) = 0 ns.Write(sendBytes, 0, sendBytes.Length) Dim resSize As Integer Do Dim resBytes As Byte() = New Byte(2048) {} resSize = ns.Read(resBytes, 0, resBytes.Length) If resSize = 0 Then Exit Do End If Dim message As String = Encoding.UTF8.GetString(resBytes) 'Console.WriteLine(message) message = more & message more = "" Dim elements As String() = message.Split(New String() {vbNullChar}, StringSplitOptions.RemoveEmptyEntries) '<chat thread="1062986843" no="1" vpos="6300" date="1293718792" mail="184" user_id="mwR5e8FptFf6-O3gZtQ3ceHAgPU" premium="3" anonymity="1">テスト!</chat> For Each receiveData As String In elements '帰ってきたXMLに"<chat"から"</chat>"まで全部あったらで分ける '文字が多いいと、分割して帰ってくるから、 If receiveData.StartsWith("<chat") AndAlso receiveData.EndsWith("</chat>") Then OnReceiveChat(receiveData) Else 'チャットが分割で来たときの処理。 If receiveData.StartsWith("<thread resultcode=") = False Then If receiveData.StartsWith("<view_counter") = True Then 'ここは、ニコニコ実況ようだから、スルーでおk ElseIf receiveData.Contains("</chat>") = False Then more = receiveData End If End If End If Next Loop While ns.CanRead End Sub Public Sub OnReceiveChat(ByVal receiveData As String) Dim commentNo As String Dim commentVpos As String Dim commentDate As String Dim commentMaill As String Dim commentUserID As String Dim commentPremium As String Dim commentAnonymity As String Dim commentText As String Dim commentThread As Integer Dim commentLoom As String = "" Dim userName As String = Nothing Dim comment = XElement.Parse(receiveData) commentNo = comment.@no commentDate = comment.@date commentVpos = comment.@vpos commentMaill = comment.@mail commentUserID = comment.@user_id commentPremium = comment.@premium '時々帰って来ない時がある。 If commentPremium = Nothing Then commentPremium = "0" End If commentAnonymity = comment.@anonymity commentThread = CInt(comment.@thread) commentText = comment.Value 'ユーザーレベル種分け If commentPremium = "0" Then commentPremium = "一般" ElseIf commentPremium = "1" Then commentPremium = "プレミアム" ElseIf commentPremium = "3" Then commentPremium = "放送主" ElseIf commentPremium = "7" Then commentPremium = "BSP" End If '0=一般 '1=プレミアム '2=放送終了後に送られてくる、/disconnect のレベル '3=放送主 '7=BSP '公式生見ると25とかある時がある。何故かはわからん Console.WriteLine("{0} : {1}", commentNo, commentText) 'Console.Write(vbCrLf & CommentNo & " " & CommentVpos & " " & CommentDate & " " & CommentMaill & " " & CommentUserID & " " & CommentPremium & " " & CommentAnonymity & " " & CommentText) End Sub End Module
お疲れ様でした 一応取得部分は完成です。
次は、getplayerstatusから値を取るコードを書きたいと思います。