[Tween-svn] [1116] ・UserStreamでのDirectMessage受信に対応

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2010年 11月 25日 (木) 21:24:47 JST


Revision: 1116
          http://sourceforge.jp/projects/tween/svn/view?view=rev&revision=1116
Author:   syo68k
Date:     2010-11-25 21:24:47 +0900 (Thu, 25 Nov 2010)

Log Message:
-----------
・UserStreamでのDirectMessage受信に対応
・DataContractJsonSerializer呼び出し部分修正

Modified Paths:
--------------
    branches/UserStream/Tween/DataModel.vb
    branches/UserStream/Tween/MyCommon.vb
    branches/UserStream/Tween/Twitter.vb


-------------- next part --------------
Modified: branches/UserStream/Tween/DataModel.vb
===================================================================
--- branches/UserStream/Tween/DataModel.vb	2010-11-25 11:31:46 UTC (rev 1115)
+++ branches/UserStream/Tween/DataModel.vb	2010-11-25 12:24:47 UTC (rev 1116)
@@ -174,6 +174,11 @@
     End Class
 
     <DataContract()> _
+    Public Class directmessageevent
+        <DataMember(Name:="direct_message")> Public direct_message As directmessage
+    End Class
+
+    <DataContract()> _
     Public Class eventdata
         <DataMember(Name:="target")> Public target As user
         <DataMember(Name:="target_object")> Public target_object As status

Modified: branches/UserStream/Tween/MyCommon.vb
===================================================================
--- branches/UserStream/Tween/MyCommon.vb	2010-11-25 11:31:46 UTC (rev 1115)
+++ branches/UserStream/Tween/MyCommon.vb	2010-11-25 12:24:47 UTC (rev 1116)
@@ -116,6 +116,7 @@
         Retweet                 'Retweetする
         PublicSearch            '公式検索
         List                    'Lists
+        UserStream              'UserStream
         '''
         ErrorState              'エラー表示のみで後処理終了(認証エラー時など)
     End Enum

Modified: branches/UserStream/Tween/Twitter.vb
===================================================================
--- branches/UserStream/Tween/Twitter.vb	2010-11-25 11:31:46 UTC (rev 1115)
+++ branches/UserStream/Tween/Twitter.vb	2010-11-25 12:24:47 UTC (rev 1116)
@@ -1441,10 +1441,7 @@
         Dim arIdx As Integer = -1
         Dim dlgt(300) As GetIconImageDelegate    'countQueryに合わせる
         Dim ar(300) As IAsyncResult              'countQueryに合わせる
-#If 0 Then
-        item = DirectCast(serializer.ReadObject(stream), List(Of DataModel.status))
-#Else
-        ' エラーチェックはまだ行わない
+
         Try
             item = DirectCast(serializer.ReadObject(content), List(Of DataModel.status))
         Catch ex As SerializationException
@@ -1454,7 +1451,6 @@
             TraceOut(Encoding.Unicode.GetString(content.GetBuffer()))
             Return "Invalid Json!"
         End Try
-#End If
 
         For Each status As DataModel.status In item
             Dim post As New PostClass
@@ -1888,75 +1884,54 @@
         Return ""
     End Function
 
-    Public Function GetDirectMessageApi(ByVal read As Boolean, _
-                            ByVal gType As WORKERTYPE, _
-                            ByVal more As Boolean) As String
-        If _endingFlag Then Return ""
+    Private Function CreateDirectMessagesFromJson(ByVal content As MemoryStream, ByVal gType As WORKERTYPE, ByVal read As Boolean) As String
 
-        If Twitter.AccountState <> ACCOUNT_STATE.Valid Then Return ""
+        'Dim serializer As New DataContractJsonSerializer(GetType(List(Of DataModel.directmessage)))
+        Dim serializer As DataContractJsonSerializer
+        Dim item As New List(Of DataModel.directmessage)
 
-        Dim res As HttpStatusCode
-        Dim content As String = ""
-
-        Try
-            If gType = WORKERTYPE.DirectMessegeRcv Then
-                If more Then
-                    res = twCon.DirectMessages(20, minDirectmessage, 0, content)
-                Else
-                    res = twCon.DirectMessages(20, 0, 0, content)
-                End If
-            Else
-                If more Then
-                    res = twCon.DirectMessagesSent(20, minDirectmessageSent, 0, content)
-                Else
-                    res = twCon.DirectMessagesSent(20, 0, 0, content)
-                End If
-            End If
-        Catch ex As Exception
-            Return "Err:" + ex.Message
-        End Try
-
-        Select Case res
-            Case HttpStatusCode.OK
-                Twitter.AccountState = ACCOUNT_STATE.Valid
-            Case HttpStatusCode.Unauthorized
-                Twitter.AccountState = ACCOUNT_STATE.Invalid
-                Return "Check your Username/Password."
-            Case HttpStatusCode.BadRequest
-                Return "Err:API Limits?"
-            Case Else
-                Return "Err:" + res.ToString() + "(" + GetCurrentMethod.Name + ")"
-        End Select
-
-        Dim stream As New MemoryStream(Encoding.Unicode.GetBytes(content))
-        Dim serializer As New DataContractJsonSerializer(GetType(List(Of DataModel.directmessage)))
-        Dim item As List(Of DataModel.directmessage)
-
         Dim arIdx As Integer = -1
         Dim dlgt(300) As GetIconImageDelegate    'countQueryに合わせる
         Dim ar(300) As IAsyncResult              'countQueryに合わせる
+        Dim typ As Type
 
+        If gType = WORKERTYPE.UserStream Then
+            typ = GetType(List(Of DataModel.directmessageevent))
+            serializer = New DataContractJsonSerializer(typ)
+        Else
+            serializer = New DataContractJsonSerializer(GetType(List(Of DataModel.directmessage)))
+        End If
+
         Try
-            item = DirectCast(serializer.ReadObject(stream), List(Of DataModel.directmessage))
+            If gType = WORKERTYPE.UserStream Then
+                Dim itm As List(Of DataModel.directmessageevent)
+                itm = DirectCast(serializer.ReadObject(content), List(Of DataModel.directmessageevent))
+                For Each dat As DataModel.directmessageevent In itm
+                    item.Add(dat.direct_message)
+                Next
+            Else
+                item = DirectCast(serializer.ReadObject(content), List(Of DataModel.directmessage))
+            End If
         Catch ex As SerializationException
-            TraceOut(ex.Message + Environment.NewLine + content)
+            TraceOut(ex.Message + Environment.NewLine + Encoding.Unicode.GetString(content.GetBuffer()))
             Return "Json Parse Error(DataContractJsonSerializer)"
         Catch ex As Exception
-            TraceOut(content)
+            TraceOut(Encoding.Unicode.GetString(content.GetBuffer()))
             Return "Invalid Json!"
-        Finally
-            stream.Close()
         End Try
 
         For Each message As DataModel.directmessage In item
             Dim post As New PostClass
             Try
                 post.Id = message.id
-                If gType = WORKERTYPE.DirectMessegeRcv Then
-                    If minDirectmessage > post.Id Then minDirectmessage = post.Id
-                Else
-                    If minDirectmessageSent > post.Id Then minDirectmessageSent = post.Id
+                If gType <> WORKERTYPE.UserStream Then
+                    If gType = WORKERTYPE.DirectMessegeRcv Then
+                        If minDirectmessage > post.Id Then minDirectmessage = post.Id
+                    Else
+                        If minDirectmessageSent > post.Id Then minDirectmessageSent = post.Id
+                    End If
                 End If
+
                 '二重取得回避
                 SyncLock LockObj
                     If TabInformations.GetInstance.GetTabByType(TabUsageType.DirectMessage).Contains(post.Id) Then Continue For
@@ -1971,28 +1946,40 @@
                 post.Data = HttpUtility.HtmlDecode(post.Data)
                 post.Data = post.Data.Replace("<3", "♡")
                 post.IsFav = False
-                '受信DMかの判定で使用
-                If gType = WORKERTYPE.DirectMessegeRcv Then
-                    post.IsOwl = True
-                Else
-                    post.IsOwl = False
-                End If
+
                 '以下、ユーザー情報
                 Dim user As DataModel.user
-                If gType = WORKERTYPE.DirectMessegeRcv Then
-                    user = message.sender
-                    post.IsMe = False
+                If gType = WORKERTYPE.UserStream Then
+                    If twCon.AuthenticatedUsername.Equals(message.recipient.screen_name, StringComparison.CurrentCultureIgnoreCase) Then
+                        user = message.sender
+                        post.IsMe = False
+                        post.IsOwl = True
+                    Else
+                        user = message.recipient
+                        post.IsMe = True
+                        post.IsOwl = False
+                    End If
                 Else
-                    user = message.recipient
-                    post.IsMe = True
+                    If gType = WORKERTYPE.DirectMessegeRcv Then
+                        user = message.sender
+                        post.IsMe = False
+                        post.IsOwl = True
+                    Else
+                        user = message.recipient
+                        post.IsMe = True
+                        post.IsOwl = False
+                    End If
                 End If
+
                 post.Uid = user.id
                 post.Name = user.screen_name
                 post.Nickname = user._name
                 post.ImageUrl = user.profile_image_url
                 post.IsProtect = user.protected
             Catch ex As Exception
-                TraceOut(content)
+                TraceOut(Encoding.Unicode.GetString(content.GetBuffer()))
+                MessageBox.Show("Parse Error(CreateDirectMessagesFromJson)")
+                Continue For
             End Try
 
             post.IsRead = read
@@ -2019,8 +2006,57 @@
         Next
 
         Return ""
+
     End Function
 
+    Public Function GetDirectMessageApi(ByVal read As Boolean, _
+                            ByVal gType As WORKERTYPE, _
+                            ByVal more As Boolean) As String
+        If _endingFlag Then Return ""
+
+        If Twitter.AccountState <> ACCOUNT_STATE.Valid Then Return ""
+
+        Dim res As HttpStatusCode
+        Dim content As String = ""
+
+        Try
+            If gType = WORKERTYPE.DirectMessegeRcv Then
+                If more Then
+                    res = twCon.DirectMessages(20, minDirectmessage, 0, content)
+                Else
+                    res = twCon.DirectMessages(20, 0, 0, content)
+                End If
+            Else
+                If more Then
+                    res = twCon.DirectMessagesSent(20, minDirectmessageSent, 0, content)
+                Else
+                    res = twCon.DirectMessagesSent(20, 0, 0, content)
+                End If
+            End If
+        Catch ex As Exception
+            Return "Err:" + ex.Message
+        End Try
+
+        Select Case res
+            Case HttpStatusCode.OK
+                Twitter.AccountState = ACCOUNT_STATE.Valid
+            Case HttpStatusCode.Unauthorized
+                Twitter.AccountState = ACCOUNT_STATE.Invalid
+                Return "Check your Username/Password."
+            Case HttpStatusCode.BadRequest
+                Return "Err:API Limits?"
+            Case Else
+                Return "Err:" + res.ToString() + "(" + GetCurrentMethod.Name + ")"
+        End Select
+
+        Using stream As New MemoryStream()
+            Dim buf As Byte() = Encoding.Unicode.GetBytes(content)
+            stream.Write(buf, 0, buf.Length)
+            stream.Seek(offset:=0, loc:=SeekOrigin.Begin)
+            Return CreateDirectMessagesFromJson(stream, gType, read)
+        End Using
+    End Function
+
     Public Function GetFavoritesApi(ByVal read As Boolean, _
                         ByVal gType As WORKERTYPE) As String
 
@@ -2053,7 +2089,6 @@
                 Return "Err:" + res.ToString() + "(" + GetCurrentMethod.Name + ")"
         End Select
 
-        Dim stream As New MemoryStream(Encoding.Unicode.GetBytes(content))
         Dim serializer As New DataContractJsonSerializer(GetType(List(Of DataModel.status)))
         Dim item As List(Of DataModel.status)
 
@@ -2062,15 +2097,18 @@
         Dim ar(300) As IAsyncResult              'countQueryに合わせる
 
         Try
-            item = DirectCast(serializer.ReadObject(stream), List(Of DataModel.status))
+            Using stream As New MemoryStream()
+                Dim buf As Byte() = Encoding.Unicode.GetBytes(content)
+                stream.Write(buf, 0, buf.Length)
+                stream.Seek(offset:=0, loc:=SeekOrigin.Begin)
+                item = DirectCast(serializer.ReadObject(stream), List(Of DataModel.status))
+            End Using
         Catch ex As SerializationException
             TraceOut(ex.Message + Environment.NewLine + content)
             Return "Json Parse Error(DataContractJsonSerializer)"
         Catch ex As Exception
             TraceOut(content)
             Return "Invalid Json!"
-        Finally
-            stream.Close()
         End Try
 
         For Each status As DataModel.status In item
@@ -2848,11 +2886,21 @@
                     res.Append("[")
                     res.Append(line)
                     res.Append("]")
+
+                    Dim isDM As Boolean = False
+                    If line.StartsWith("{""direct_message"":") Then
+                        isDM = True
+                    End If
+
                     Using stream As New MemoryStream()
                         Dim buf As Byte() = Encoding.Unicode.GetBytes(res.ToString)
                         stream.Write(buf, offset:=0, count:=buf.Length)
                         stream.Seek(offset:=0, loc:=SeekOrigin.Begin)
-                        CreatePostsFromJson(stream, WORKERTYPE.Timeline, Nothing, False, Nothing, Nothing)
+                        If isDM Then
+                            CreateDirectMessagesFromJson(stream, WORKERTYPE.UserStream, False)
+                        Else
+                            CreatePostsFromJson(stream, WORKERTYPE.Timeline, Nothing, False, Nothing, Nothing)
+                        End If
                     End Using
                     RaiseEvent NewPostFromStream()
                 Loop



Tween-svn メーリングリストの案内
Back to archive index