OSDN Git Service

リスト機能の API v1.1 対応が不十分であったため修正
authorKimura Youichi <kim.upsilon@bucyou.net>
Wed, 5 Jun 2013 21:59:02 +0000 (06:59 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Thu, 6 Jun 2013 22:06:06 +0000 (07:06 +0900)
OpenTween/Connection/HttpTwitter.cs
OpenTween/Twitter.cs

index 25591c1..95b567e 100644 (file)
@@ -694,11 +694,14 @@ namespace OpenTween
         }
 
         #region Lists
-        public HttpStatusCode GetLists(string user, long cursor, ref string content)
+        public HttpStatusCode GetLists(string user, long? cursor, ref string content)
         {
             Dictionary<string, string> param = new Dictionary<string, string>();
             param.Add("screen_name", user);
-            param.Add("cursor", cursor.ToString());
+
+            if (cursor != null)
+                param.Add("cursor", cursor.Value.ToString()); // API v1
+
             return httpCon.GetContent(GetMethod,
                 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/list.json" : "/1/lists.json"),
                 param,
@@ -742,11 +745,14 @@ namespace OpenTween
                 null);
         }
 
-        public HttpStatusCode GetListsSubscriptions(string user, long cursor, ref string content)
+        public HttpStatusCode GetListsSubscriptions(string user, long? cursor, ref string content)
         {
             Dictionary<string, string> param = new Dictionary<string, string>();
             param.Add("screen_name", user);
-            param.Add("cursor", cursor.ToString());
+
+            if (cursor != null)
+                param.Add("cursor", cursor.Value.ToString()); // API v1
+
             return httpCon.GetContent(GetMethod,
                 CreateTwitterUri(HttpTwitter.API11Enabled ? "/1.1/lists/subscriptions.json" : "/1/lists/subscriptions.json"),
                 param,
index d871239..a63cbee 100644 (file)
@@ -3459,6 +3459,11 @@ namespace OpenTween
 
         public string GetListsApi()
         {
+            return HttpTwitter.API11Enabled ? this.GetListsApi11() : this.GetListsApi10();
+        }
+
+        private string GetListsApi10()
+        {
             if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
 
             HttpStatusCode res = HttpStatusCode.BadRequest;
@@ -3558,6 +3563,97 @@ namespace OpenTween
             return "";
         }
 
+        private string GetListsApi11()
+        {
+            if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
+
+            HttpStatusCode res = HttpStatusCode.BadRequest;
+            IEnumerable<ListElement> lists;
+            var content = "";
+
+            try
+            {
+                res = twCon.GetLists(this.Username, null, ref content);
+            }
+            catch (Exception ex)
+            {
+                return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
+            }
+
+            switch (res)
+            {
+                case HttpStatusCode.OK:
+                    Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
+                    break;
+                case HttpStatusCode.Unauthorized:
+                    Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
+                    return Properties.Resources.Unauthorized;
+                case HttpStatusCode.BadRequest:
+                    return "Err:API Limits?";
+                default:
+                    return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
+            }
+
+            try
+            {
+                lists = MyCommon.CreateDataFromJson<List<TwitterDataModel.ListElementData>>(content)
+                    .Select(x => new ListElement(x, this));
+            }
+            catch (SerializationException ex)
+            {
+                MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
+                return "Err:Json Parse Error(DataContractJsonSerializer)";
+            }
+            catch (Exception ex)
+            {
+                MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
+                return "Err:Invalid Json!";
+            }
+
+            content = "";
+            try
+            {
+                res = twCon.GetListsSubscriptions(this.Username, null, ref content);
+            }
+            catch (Exception ex)
+            {
+                return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
+            }
+
+            switch (res)
+            {
+                case HttpStatusCode.OK:
+                    Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
+                    break;
+                case HttpStatusCode.Unauthorized:
+                    Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
+                    return Properties.Resources.Unauthorized;
+                case HttpStatusCode.BadRequest:
+                    return "Err:API Limits?";
+                default:
+                    return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
+            }
+
+            try
+            {
+                lists = lists.Concat(MyCommon.CreateDataFromJson<List<TwitterDataModel.ListElementData>>(content)
+                    .Select(x => new ListElement(x, this)));
+            }
+            catch (SerializationException ex)
+            {
+                MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
+                return "Err:Json Parse Error(DataContractJsonSerializer)";
+            }
+            catch (Exception ex)
+            {
+                MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
+                return "Err:Invalid Json!";
+            }
+
+            TabInformations.GetInstance().SubscribableLists = lists.ToList();
+            return "";
+        }
+
         public string DeleteList(string list_id)
         {
             HttpStatusCode res = HttpStatusCode.BadRequest;