OSDN Git Service

Twitter.TwitterUserstreamを非同期なメソッドで書き換え
[opentween/open-tween.git] / OpenTween / Connection / HttpTwitter.cs
index 5e5d4d4..a4c0775 100644 (file)
@@ -78,6 +78,7 @@ namespace OpenTween
                 un = username;
             }
             con.Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret, accessToken, accessTokenSecret, username, userId, "screen_name", "user_id");
+            con.CacheEnabled = false;
             httpCon = con;
             connectionType = AuthMethod.OAuth;
             requestToken = "";
@@ -87,10 +88,7 @@ namespace OpenTween
         {
             get
             {
-                if (httpCon != null)
-                    return ((HttpConnectionOAuth)httpCon).AccessToken;
-                else
-                    return "";
+                return ((HttpConnectionOAuth)httpCon)?.AccessToken ?? "";
             }
         }
 
@@ -98,10 +96,7 @@ namespace OpenTween
         {
             get
             {
-                if (httpCon != null)
-                    return ((HttpConnectionOAuth)httpCon).AccessTokenSecret;
-                else
-                    return "";
+                return ((HttpConnectionOAuth)httpCon)?.AccessTokenSecret ?? "";
             }
         }
 
@@ -109,10 +104,7 @@ namespace OpenTween
         {
             get
             {
-                if (httpCon != null)
-                    return httpCon.AuthUsername;
-                else
-                    return "";
+                return httpCon?.AuthUsername ?? "";
             }
         }
 
@@ -120,10 +112,7 @@ namespace OpenTween
         {
             get
             {
-                if (httpCon != null)
-                    return httpCon.AuthUserId;
-                else
-                    return 0;
+                return httpCon?.AuthUserId ?? 0;
             }
             set
             {
@@ -163,364 +152,13 @@ namespace OpenTween
             this.Initialize("", "", "", 0);
         }
 
-        public HttpStatusCode UpdateStatus(string status, long? replyToId, List<long> mediaIds, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("status", status);
-            if (replyToId != null) param.Add("in_reply_to_status_id", replyToId.ToString());
-            param.Add("include_entities", "true");
-            //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
-
-            if (mediaIds != null && mediaIds.Count > 0)
-                param.Add("media_ids", string.Join(",", mediaIds));
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/statuses/update.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode UpdateStatusWithMedia(string status, long? replyToId, IMediaItem item, ref string content)
-        {
-            //画像投稿用エンドポイント
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("status", status);
-            if (replyToId != null) param.Add("in_reply_to_status_id", replyToId.ToString());
-            param.Add("include_entities", "true");
-            //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
-
-            var binary = new List<KeyValuePair<string, IMediaItem>>();
-            binary.Add(new KeyValuePair<string, IMediaItem>("media[]", item));
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/statuses/update_with_media.json"),
-                param,
-                binary,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/statuses/update_with_media"));
-        }
-
-        public HttpStatusCode UploadMedia(IMediaItem item, ref string content)
-        {
-            //画像投稿専用エンドポイント
-            var binary = new List<KeyValuePair<string, IMediaItem>>();
-            binary.Add(new KeyValuePair<string, IMediaItem>("media", item));
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUploadUri("/1.1/media/upload.json"),
-                null,
-                binary,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode DestroyStatus(long id)
-        {
-            string content = null;
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/statuses/destroy/" + id + ".json"),
-                null,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode SendDirectMessage(string status, string sendto, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("text", status);
-            param.Add("screen_name", sendto);
-            //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/direct_messages/new.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode DestroyDirectMessage(long id)
-        {
-            string content = null;
-
-            var param = new Dictionary<string, string>();
-            param.Add("id", id.ToString());
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/direct_messages/destroy.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode RetweetStatus(long id, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("include_entities", "true");
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/statuses/retweet/" + id + ".json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode ShowUserInfo(string screenName, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("screen_name", screenName);
-            param.Add("include_entities", "true");
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/users/show.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/users/show/:id"));
-        }
-
-        public HttpStatusCode CreateFriendships(string screenName, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("screen_name", screenName);
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/friendships/create.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode DestroyFriendships(string screenName, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("screen_name", screenName);
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/friendships/destroy.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode CreateBlock(string screenName, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("screen_name", screenName);
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/blocks/create.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode DestroyBlock(string screenName, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("screen_name", screenName);
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/blocks/destroy.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode ReportSpam(string screenName, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("screen_name", screenName);
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/users/report_spam.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode ShowFriendships(string souceScreenName, string targetScreenName, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("source_screen_name", souceScreenName);
-            param.Add("target_screen_name", targetScreenName);
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/friendships/show.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/friendships/show"));
-        }
-
-        public HttpStatusCode ShowStatuses(long id, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("include_entities", "true");
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/statuses/show/" + id + ".json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/statuses/show/:id"));
-        }
-
-        public HttpStatusCode CreateFavorites(long id, ref string content)
-        {
-            var param = new Dictionary<string, string>();
-            param.Add("id", id.ToString());
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/favorites/create.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode DestroyFavorites(long id, ref string content)
-        {
-            var param = new Dictionary<string, string>();
-            param.Add("id", id.ToString());
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/favorites/destroy.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode HomeTimeline(int? count, long? max_id, long? since_id, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            if (count != null)
-                param.Add("count", count.ToString());
-            if (max_id != null)
-                param.Add("max_id", max_id.ToString());
-            if (since_id != null)
-                param.Add("since_id", since_id.ToString());
-
-            param.Add("include_entities", "true");
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/statuses/home_timeline.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/statuses/home_timeline"));
-        }
-
-        public HttpStatusCode UserTimeline(long? user_id, string screen_name, int? count, long? max_id, long? since_id, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-
-            if ((user_id == null && string.IsNullOrEmpty(screen_name)) ||
-                (user_id != null && !string.IsNullOrEmpty(screen_name))) return HttpStatusCode.BadRequest;
-
-            if (user_id != null)
-                param.Add("user_id", user_id.ToString());
-            if (!string.IsNullOrEmpty(screen_name))
-                param.Add("screen_name", screen_name);
-            if (count != null)
-                param.Add("count", count.ToString());
-            if (max_id != null)
-                param.Add("max_id", max_id.ToString());
-            if (since_id != null)
-                param.Add("since_id", since_id.ToString());
-
-            param.Add("include_rts", "true");
-            param.Add("include_entities", "true");
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/statuses/user_timeline.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/statuses/user_timeline"));
-        }
-
-        public HttpStatusCode Mentions(int? count, long? max_id, long? since_id, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            if (count != null)
-                param.Add("count", count.ToString());
-            if (max_id != null)
-                param.Add("max_id", max_id.ToString());
-            if (since_id != null)
-                param.Add("since_id", since_id.ToString());
-
-            param.Add("include_entities", "true");
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/statuses/mentions_timeline.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/statuses/mentions_timeline"));
-        }
-
-        public HttpStatusCode DirectMessages(int? count, long? max_id, long? since_id, ref string content)
+        public HttpStatusCode Favorites(int? count, ref string content)
         {
             Dictionary<string, string> param = new Dictionary<string, string>();
             if (count != null)
                 param.Add("count", count.ToString());
-            if (max_id != null)
-                param.Add("max_id", max_id.ToString());
-            if (since_id != null)
-                param.Add("since_id", since_id.ToString());
-            param.Add("include_entities", "true");
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/direct_messages.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/direct_messages"));
-        }
-
-        public HttpStatusCode DirectMessagesSent(int? count, long? max_id, long? since_id, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            if (count != null)
-                param.Add("count", count.ToString());
-            if (max_id != null)
-                param.Add("max_id", max_id.ToString());
-            if (since_id != null)
-                param.Add("since_id", since_id.ToString());
-            param.Add("include_entities", "true");
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/direct_messages/sent.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/direct_messages/sent"));
-        }
-
-        public HttpStatusCode Favorites(int? count, int? page, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            if (count != null) param.Add("count", count.ToString());
-
-            if (page != null)
-            {
-                param.Add("page", page.ToString());
-            }
-
             param.Add("include_entities", "true");
+            param.Add("include_ext_alt_text", "true");
 
             return httpCon.GetContent(GetMethod,
                 this.CreateTwitterUri("/1.1/favorites/list.json"),
@@ -543,6 +181,7 @@ namespace OpenTween
 
             param.Add("result_type", "recent");
             param.Add("include_entities", "true");
+            param.Add("include_ext_alt_text", "true");
             return httpCon.GetContent(GetMethod,
                 this.CreateTwitterUri("/1.1/search/tweets.json"),
                 param,
@@ -561,29 +200,6 @@ namespace OpenTween
                 this.CreateApiCalllback("/saved_searches/list"));
         }
 
-        public HttpStatusCode FollowerIds(long cursor, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("cursor", cursor.ToString());
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/followers/ids.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/followers/ids"));
-        }
-
-        public HttpStatusCode NoRetweetIds(ref string content)
-        {
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/friendships/no_retweets/ids.json"),
-                null,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/friendships/no_retweets/ids"));
-        }
-
         public HttpStatusCode RateLimitStatus(ref string content)
         {
             return httpCon.GetContent(GetMethod,
@@ -656,29 +272,6 @@ namespace OpenTween
                 this.CreateApiCalllback("/lists/subscriptions"));
         }
 
-        public HttpStatusCode GetListsStatuses(long userId, long list_id, int? per_page, long? max_id, long? since_id, Boolean isRTinclude, ref string content)
-        {
-            //認証なくても取得できるが、protectedユーザー分が抜ける
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("user_id", userId.ToString());
-            param.Add("list_id", list_id.ToString());
-            param.Add("include_rts", isRTinclude ? "true" : "false");
-            if (per_page != null)
-                param.Add("count", per_page.ToString());
-            if (max_id != null)
-                param.Add("max_id", max_id.ToString());
-            if (since_id != null)
-                param.Add("since_id", since_id.ToString());
-            param.Add("include_entities", "true");
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/lists/statuses.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/lists/statuses"));
-        }
-
         public HttpStatusCode CreateLists(string listname, Boolean isPrivate, string description, ref string content)
         {
             string mode = "public";
@@ -771,83 +364,6 @@ namespace OpenTween
                 this.CreateApiCalllback("/statuses/retweeters/ids"));
         }
 
-        public HttpStatusCode UpdateProfile(string name, string url, string location, string description, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-
-            param.Add("name", name);
-            param.Add("url", url);
-            param.Add("location", location);
-
-            // name, location, description に含まれる < > " の文字はTwitter側で除去されるが、
-            // twitter.com の挙動では description でのみ &lt; 等の文字参照を使って表示することができる
-            var escapedDescription = description.Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;");
-            param.Add("description", escapedDescription);
-
-            param.Add("include_entities", "true");
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/account/update_profile.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode UpdateProfileImage(FileInfo imageFile, ref string content)
-        {
-            var binary = new List<KeyValuePair<string, IMediaItem>>();
-            binary.Add(new KeyValuePair<string, IMediaItem>("image", new FileMediaItem(imageFile)));
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/account/update_profile_image.json"),
-                null,
-                binary,
-                ref content,
-                null,
-                null);
-        }
-
-        public HttpStatusCode GetBlockUserIds(ref string content, long? cursor)
-        {
-            var param = new Dictionary<string, string>();
-
-            if (cursor != null)
-                param.Add("cursor", cursor.ToString());
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/blocks/ids.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/blocks/ids"));
-        }
-
-        public HttpStatusCode GetMuteUserIds(ref string content, long? cursor)
-        {
-            var param = new Dictionary<string, string>();
-
-            if (cursor != null)
-                param.Add("cursor", cursor.ToString());
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/mutes/users/ids.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/mutes/users/ids"));
-        }
-
-        public HttpStatusCode GetConfiguration(ref string content)
-        {
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/help/configuration.json"),
-                null,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/help/configuration"));
-        }
-
         public HttpStatusCode VerifyCredentials(ref string content)
         {
             return httpCon.GetContent(GetMethod,
@@ -886,6 +402,7 @@ namespace OpenTween
 
         public static string TwitterUrl
         {
+            get { return _twitterUrl; }
             set
             {
                 _twitterUrl = value;
@@ -898,16 +415,16 @@ namespace OpenTween
         {
             return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
             {
-                {"X-Access-Level", ""},
-                {"X-RateLimit-Limit", ""},
-                {"X-RateLimit-Remaining", ""},
-                {"X-RateLimit-Reset", ""},
-                {"X-Rate-Limit-Limit", ""},
-                {"X-Rate-Limit-Remaining", ""},
-                {"X-Rate-Limit-Reset", ""},
-                {"X-MediaRateLimit-Limit", ""},
-                {"X-MediaRateLimit-Remaining", ""},
-                {"X-MediaRateLimit-Reset", ""},
+                ["X-Access-Level"] = "",
+                ["X-RateLimit-Limit"] = "",
+                ["X-RateLimit-Remaining"] = "",
+                ["X-RateLimit-Reset"] = "",
+                ["X-Rate-Limit-Limit"] = "",
+                ["X-Rate-Limit-Remaining"] = "",
+                ["X-Rate-Limit-Reset"] = "",
+                ["X-MediaRateLimit-Limit"] = "",
+                ["X-MediaRateLimit-Remaining"] = "",
+                ["X-MediaRateLimit-Reset"] = "",
             };
         }
 
@@ -920,26 +437,6 @@ namespace OpenTween
             };
         }
 
-        public HttpStatusCode UserStream(ref Stream content,
-                                         bool allAtReplies,
-                                         string trackwords,
-                                         string userAgent)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-
-            if (allAtReplies)
-                param.Add("replies", "all");
-
-            if (!string.IsNullOrEmpty(trackwords))
-                param.Add("track", trackwords);
-
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUserStreamUri("/1.1/user.json"),
-                param,
-                ref content,
-                userAgent);
-        }
-
         public HttpStatusCode FilterStream(ref Stream content,
                                            string trackwords,
                                            string userAgent)