[Tween-svn] [1281] JSON対応完了

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2010年 12月 29日 (水) 23:16:33 JST


Revision: 1281
          http://sourceforge.jp/projects/tween/svn/view?view=rev&revision=1281
Author:   kiri_feather
Date:     2010-12-29 23:16:32 +0900 (Wed, 29 Dec 2010)

Log Message:
-----------
JSON対応完了

Modified Paths:
--------------
    trunk/Tween/Connection/HttpTwitter.vb
    trunk/Tween/DataModel.vb
    trunk/Tween/Twitter.vb
    trunk/Tween/UserInfo.vb


-------------- next part --------------
Modified: trunk/Tween/Connection/HttpTwitter.vb
===================================================================
--- trunk/Tween/Connection/HttpTwitter.vb	2010-12-29 07:08:33 UTC (rev 1280)
+++ trunk/Tween/Connection/HttpTwitter.vb	2010-12-29 14:16:32 UTC (rev 1281)
@@ -589,7 +589,7 @@
         Dim param As New Dictionary(Of String, String)
         param.Add("cursor", cursor.ToString())
         Return httpCon.GetContent(GetMethod, _
-                            CreateTwitterUri("/1/" + user + "/" + list_id + "/members.xml"), _
+                            CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"), _
                             param, _
                             content, _
                             TwitterApiInfo.HttpHeaders, _
@@ -600,7 +600,7 @@
         Dim param As New Dictionary(Of String, String)
         param.Add("id", id)
         Return httpCon.GetContent(PostMethod, _
-                            CreateTwitterUri("/1/" + user + "/" + list_id + "/members.xml"), _
+                            CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"), _
                             param, _
                             content, _
                             Nothing, _
@@ -612,7 +612,7 @@
         param.Add("id", id)
         param.Add("_method", "DELETE")
         Return httpCon.GetContent(PostMethod, _
-                            CreateTwitterUri("/1/" + user + "/" + list_id + "/members.xml"), _
+                            CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"), _
                             param, _
                             content, _
                             Nothing, _
@@ -621,7 +621,7 @@
 
     Public Function GetListMembersID(ByVal user As String, ByVal list_id As String, ByVal id As String, ByRef content As String) As HttpStatusCode
         Return httpCon.GetContent(GetMethod, _
-                            CreateTwitterUri("/1/" + user + "/" + list_id + "/members/" + id + ".xml"), _
+                            CreateTwitterUri("/1/" + user + "/" + list_id + "/members/" + id + ".json"), _
                             Nothing, _
                             content, _
                             TwitterApiInfo.HttpHeaders, _
@@ -638,7 +638,7 @@
         End If
 
         Return httpCon.GetContent(GetMethod, _
-                            CreateTwitterUri("/1/statuses/" + statusid.ToString + "/retweeted_by/ids.xml"), _
+                            CreateTwitterUri("/1/statuses/" + statusid.ToString + "/retweeted_by/ids.json"), _
                             param, _
                             content, _
                             TwitterApiInfo.HttpHeaders, _
@@ -654,7 +654,7 @@
         param.Add("description", description)
 
         Return httpCon.GetContent(PostMethod, _
-                    CreateTwitterUri("/1/account/update_profile.xml"), _
+                    CreateTwitterUri("/1/account/update_profile.json"), _
                     param, _
                     content, _
                     Nothing, _
@@ -666,7 +666,7 @@
         binary.Add(New KeyValuePair(Of String, FileInfo)("image", imageFile))
 
         Return httpCon.GetContent(PostMethod, _
-                            CreateTwitterUri("/1/account/update_profile_image.xml"), _
+                            CreateTwitterUri("/1/account/update_profile_image.json"), _
                             Nothing, _
                             binary, _
                             content, _

Modified: trunk/Tween/DataModel.vb
===================================================================
--- trunk/Tween/DataModel.vb	2010-12-29 07:08:33 UTC (rev 1280)
+++ trunk/Tween/DataModel.vb	2010-12-29 14:16:32 UTC (rev 1281)
@@ -307,4 +307,12 @@
         <DataMember(Name:="next_cursor")> Public NextCursor As Long
         <DataMember(Name:="previous_cursor")> Public PreviousCursor As Long
     End Class
+
+    <DataContract()> _
+    Public Class Users
+        <DataMember(Name:="users")> Public users As User()
+        <DataMember(Name:="next_cursor")> Public NextCursor As Long
+        <DataMember(Name:="previous_cursor")> Public PreviousCursor As Long
+    End Class
+
 End Class

Modified: trunk/Tween/Twitter.vb
===================================================================
--- trunk/Tween/Twitter.vb	2010-12-29 07:08:33 UTC (rev 1280)
+++ trunk/Tween/Twitter.vb	2010-12-29 14:16:32 UTC (rev 1281)
@@ -909,19 +909,18 @@
 
             Select Case res
                 Case HttpStatusCode.OK
-                    Dim xdoc As New XmlDocument
-                    Dim xnode As XmlNodeList
-                    Dim result As String = ""
-                    Twitter.AccountState = ACCOUNT_STATE.Valid
                     Try
-                        xdoc.LoadXml(content)
-                        xnode = xdoc.GetElementsByTagName("ids")
-                        retweeted_count += xnode.ItemOf(0).ChildNodes.Count
-                        If xnode.ItemOf(0).ChildNodes.Count < 100 Then Exit For
+                        Dim ids As Int64() = CreateDataFromJson(Of Int64())(content)
+                        retweeted_count += ids.Length
+                        If ids.Length < 100 Then Exit For
+                    Catch ex As SerializationException
+                        retweeted_count = -1
+                        TraceOut(ex.Message + Environment.NewLine + content)
+                        Return "Err:Json Parse Error(DataContractJsonSerializer)"
                     Catch ex As Exception
                         retweeted_count = -1
-                        result = "Err:Invalid XML."
-                        xmlBuf = Nothing
+                        TraceOut(content)
+                        Return "Err:Invalid Json!"
                     End Try
                 Case HttpStatusCode.BadRequest
                     retweeted_count = -1
@@ -1061,15 +1060,12 @@
                 Twitter.AccountState = ACCOUNT_STATE.Invalid
                 Return "Check your Username/Password."
             Case HttpStatusCode.Forbidden
-                Dim xd As XmlDocument = New XmlDocument
-                Try
-                    xd.LoadXml(content)
-                    Dim xNode As XmlNode = Nothing
-                    xNode = xd.SelectSingleNode("/hash/error")
-                    Return "Err:" + xNode.InnerText + "(" + GetCurrentMethod.Name + ")"
-                Catch ex As Exception
-                    Return "Err:Forbidden" + "(" + GetCurrentMethod.Name + ")"
-                End Try
+                Dim errMsg As String = GetErrorMessageJson(content)
+                If String.IsNullOrEmpty(errMsg) Then
+                    Return "Err:Forbidden(" + GetCurrentMethod.Name + ")"
+                Else
+                    Return "Err:" + errMsg
+                End If
             Case Else
                 Return "Err:" + res.ToString + "(" + GetCurrentMethod.Name + ")"
         End Select
@@ -1096,15 +1092,12 @@
                 Twitter.AccountState = ACCOUNT_STATE.Invalid
                 Return "Check your Username/Password."
             Case HttpStatusCode.Forbidden
-                Dim xd As XmlDocument = New XmlDocument
-                Try
-                    xd.LoadXml(content)
-                    Dim xNode As XmlNode = Nothing
-                    xNode = xd.SelectSingleNode("/hash/error")
-                    Return "Err:" + xNode.InnerText + "(" + GetCurrentMethod.Name + ")"
-                Catch ex As Exception
-                    Return "Err:Forbidden" + "(" + GetCurrentMethod.Name + ")"
-                End Try
+                Dim errMsg As String = GetErrorMessageJson(content)
+                If String.IsNullOrEmpty(errMsg) Then
+                    Return "Err:Forbidden(" + GetCurrentMethod.Name + ")"
+                Else
+                    Return "Err:" + errMsg
+                End If
             Case Else
                 Return "Err:" + res.ToString + "(" + GetCurrentMethod.Name + ")"
         End Select
@@ -2467,7 +2460,6 @@
 
         Dim res As HttpStatusCode
         Dim content As String = ""
-        'Dim cursor As Long = -1
 
         'Do
         Try
@@ -2488,25 +2480,23 @@
                 Return "Err:" + res.ToString() + "(" + GetCurrentMethod.Name + ")"
         End Select
 
-        Dim xdoc As New XmlDocument
         Try
-            xdoc.LoadXml(content)
+            Dim users = CreateDataFromJson(Of TwitterDataModel.Users)(content)
+            Array.ForEach(Of TwitterDataModel.User)(
+                users.users,
+                New Action(Of TwitterDataModel.User)(Sub(u)
+                                                         lists.Add(New UserInfo(u))
+                                                     End Sub))
+            cursor = users.NextCursor
+            Return ""
+        Catch ex As SerializationException
+            TraceOut(ex.Message + Environment.NewLine + content)
+            Return "Err:Json Parse Error(DataContractJsonSerializer)"
         Catch ex As Exception
             TraceOut(content)
-            Return "Invalid XML!"
+            Return "Err:Invalid Json!"
         End Try
 
-        Try
-            For Each xentryNode As XmlNode In xdoc.DocumentElement.SelectNodes("/users_list/users/user")
-                lists.Add(New UserInfo(xentryNode))
-            Next
-            cursor = Long.Parse(xdoc.DocumentElement.SelectSingleNode("/users_list/next_cursor").InnerText)
-        Catch ex As Exception
-            TraceOut(content)
-            Return "Invalid XML!"
-        End Try
-        'Loop While cursor <> 0
-
         Return ""
     End Function
 
@@ -2576,16 +2566,14 @@
                 Return "Err:" + res.ToString() + "(" + GetCurrentMethod.Name + ")"
         End Select
 
-        Dim xdoc As New XmlDocument
         Try
-            xdoc.LoadXml(content)
-            value = xdoc.DocumentElement.Name = "user"
+            Dim u = CreateDataFromJson(Of TwitterDataModel.User)(content)
+            value = True
+            Return ""
         Catch ex As Exception
-            TraceOut(content)
-            Return "Invalid XML!"
+            value = False
+            Return ""
         End Try
-
-        Return ""
     End Function
 
     Public Function AddUserToList(ByVal list_name As String, ByVal user As String) As String
@@ -2598,6 +2586,18 @@
             Return "Err:" + ex.Message + "(" + GetCurrentMethod.Name + ")"
         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
+
         Return ""
     End Function
 
@@ -2611,6 +2611,18 @@
             Return "Err:" + ex.Message + "(" + GetCurrentMethod.Name + ")"
         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
+
         Return ""
     End Function
 
@@ -2867,12 +2879,17 @@
                 Debug.Print("delete")
                 Dim post As PostClass = Nothing
                 Dim id As Int64
-                If xElm.Element("delete").Element("direct_message") IsNot Nothing Then
+                If xElm.Element("delete").Element("direct_message") IsNot Nothing AndAlso
+                    xElm.Element("delete").Element("direct_message").Element("id") IsNot Nothing Then
                     id = CLng(xElm.Element("delete").Element("direct_message").Element("id").Value)
                     RaiseEvent PostDeleted(id, post)
-                Else
+                ElseIf xElm.Element("delete").Element("status") IsNot Nothing AndAlso
+                    xElm.Element("delete").Element("status").Element("id") IsNot Nothing Then
                     id = CLng(xElm.Element("delete").Element("status").Element("id").Value)
                     RaiseEvent PostDeleted(id, post)
+                Else
+                    TraceOut("delete:" + line)
+                    Exit Sub
                 End If
                 CreateDeleteEvent(DateTime.Now, id, post)
                 Exit Sub
@@ -2887,7 +2904,11 @@
                 Debug.Print("direct_message")
                 isDm = True
             ElseIf xElm.Element("scrub_geo") IsNot Nothing Then
-                Debug.Print("scrub_geo: user_id=" + xElm.Element("user_id").Value.ToString + " up_to_status_id=" + xElm.Element("up_to_status_id").Value.ToString)
+                Try
+                    Debug.Print("scrub_geo: user_id=" + xElm.Element("user_id").Value.ToString + " up_to_status_id=" + xElm.Element("up_to_status_id").Value.ToString)
+                Catch ex As Exception
+                    TraceOut("scrub_geo:" + line)
+                End Try
                 Exit Sub
             End If
         End Using

Modified: trunk/Tween/UserInfo.vb
===================================================================
--- trunk/Tween/UserInfo.vb	2010-12-29 07:08:33 UTC (rev 1280)
+++ trunk/Tween/UserInfo.vb	2010-12-29 14:16:32 UTC (rev 1281)
@@ -3,25 +3,30 @@
 
     End Sub
 
-    Public Sub New(ByVal xmlNode As Xml.XmlNode)
-        Me.Id = Long.Parse(xmlNode.Item("id").InnerText)
-        Me.Name = xmlNode.Item("name").InnerText
-        Me.ScreenName = xmlNode.Item("screen_name").InnerText
-        Me.Location = xmlNode.Item("location").InnerText
-        Me.Description = xmlNode.Item("description").InnerText
-        Me.ImageUrl = New Uri(xmlNode.Item("profile_image_url").InnerText)
-        Me.Url = xmlNode.Item("url").InnerText
-        Me.Protect = Boolean.Parse(xmlNode.Item("protected").InnerText)
-        Me.FriendsCount = Integer.Parse(xmlNode.Item("friends_count").InnerText)
-        Me.FollowersCount = Integer.Parse(xmlNode.Item("followers_count").InnerText)
-        Me.CreatedAt = DateTime.ParseExact(xmlNode.Item("created_at").InnerText, "ddd MMM dd HH:mm:ss zzzz yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.None)
-        Me.StatusesCount = Integer.Parse(xmlNode.Item("statuses_count").InnerText)
-        Me.Verified = Boolean.Parse(xmlNode.Item("verified").InnerText)
-        Me.isFollowing = Boolean.Parse(xmlNode.Item("following").InnerText)
-        Dim postNode As Xml.XmlNode = xmlNode.Item("status")
-        Me.RecentPost = postNode.Item("text").InnerText
-        Me.PostCreatedAt = DateTime.ParseExact(postNode.Item("created_at").InnerText, "ddd MMM dd HH:mm:ss zzzz yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.None)
-        Me.PostSource = postNode.Item("source").InnerText
+    Public Sub New(ByVal user As TwitterDataModel.User)
+        Me.Id = user.Id
+        Me.Name = user.Name
+        Me.ScreenName = user.ScreenName
+        Me.Location = user.Location
+        Me.Description = user.Description
+        Try
+            Me.ImageUrl = New Uri(user.ProfileImageUrl)
+        Catch ex As Exception
+            Me.ImageUrl = Nothing
+        End Try
+        Me.Url = user.Url
+        Me.Protect = user.Protected
+        Me.FriendsCount = user.FriendsCount
+        Me.FollowersCount = user.FollowersCount
+        Me.CreatedAt = DateTimeParse(user.CreatedAt)
+        Me.StatusesCount = user.StatusesCount
+        Me.Verified = user.Verified
+        Me.isFollowing = Me.isFollowing
+        If user.Status IsNot Nothing Then
+            Me.RecentPost = user.Status.Text
+            Me.PostCreatedAt = DateTimeParse(user.Status.CreatedAt)
+            Me.PostSource = user.Status.Source
+        End If
     End Sub
 
     Public Id As Int64 = 0



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