OSDN Git Service

Merge remote-tracking branch 'sfjp/master'
[opentween/open-tween.git] / OpenTween / Connection / HttpTwitter.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 // 
10 // This file is part of OpenTween.
11 // 
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 // 
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
20 // for more details. 
21 // 
22 // You should have received a copy of the GNU General public License along
23 // with this program. if (not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 using System;
28 using System.Collections.Generic;
29 using System.Linq;
30 using System.Text;
31 using System.Net;
32 using System.IO;
33
34 namespace OpenTween
35 {
36     public class HttpTwitter : ICloneable
37     {
38         //OAuth関連
39         ///<summary>
40         ///OAuthのアクセストークン取得先URI
41         ///</summary>
42         private const string AccessTokenUrlXAuth = "https://api.twitter.com/oauth/access_token";
43         private const string RequestTokenUrl = "https://api.twitter.com/oauth/request_token";
44         private const string AuthorizeUrl = "https://api.twitter.com/oauth/authorize";
45         private const string AccessTokenUrl = "https://api.twitter.com/oauth/access_token";
46
47         private static string _protocol = "http://";
48
49         private const string PostMethod = "POST";
50         private const string GetMethod = "GET";
51
52         private IHttpConnection httpCon; //HttpConnectionApi or HttpConnectionOAuth
53         private HttpVarious httpConVar = new HttpVarious();
54
55         private enum AuthMethod
56         {
57             OAuth,
58             Basic,
59         }
60         private AuthMethod connectionType = AuthMethod.Basic;
61
62         private string requestToken;
63
64         private static string tk = "";
65         private static string tks = "";
66         private static string un = "";
67
68         private Dictionary<string, string> apiStatusHeaders = new Dictionary<string, string>
69         {
70             {"X-Access-Level", ""},
71             {"X-RateLimit-Limit", ""},
72             {"X-RateLimit-Remaining", ""},
73             {"X-RateLimit-Reset", ""},
74             {"X-MediaRateLimit-Limit", ""},
75             {"X-MediaRateLimit-Remaining", ""},
76             {"X-MediaRateLimit-Reset", ""},
77         };
78
79         public void Initialize(string accessToken,
80                                         string accessTokenSecret,
81                                         string username,
82                                         long userId)
83         {
84             //for OAuth
85             HttpOAuthApiProxy con = new HttpOAuthApiProxy();
86             if (tk != accessToken || tks != accessTokenSecret ||
87                     un != username || connectionType != AuthMethod.OAuth)
88             {
89                 // 以前の認証状態よりひとつでも変化があったらhttpヘッダより読み取ったカウントは初期化
90                 tk = accessToken;
91                 tks = accessTokenSecret;
92                 un = username;
93             }
94             con.Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret, accessToken, accessTokenSecret, username, userId, "screen_name", "user_id");
95             httpCon = con;
96             connectionType = AuthMethod.OAuth;
97             requestToken = "";
98         }
99
100         public string AccessToken
101         {
102             get
103             {
104                 if (httpCon != null)
105                     return ((HttpConnectionOAuth)httpCon).AccessToken;
106                 else
107                     return "";
108             }
109         }
110
111         public string AccessTokenSecret
112         {
113             get
114             {
115                 if (httpCon != null)
116                     return ((HttpConnectionOAuth)httpCon).AccessTokenSecret;
117                 else
118                     return "";
119             }
120         }
121
122         public string AuthenticatedUsername
123         {
124             get
125             {
126                 if (httpCon != null)
127                     return httpCon.AuthUsername;
128                 else
129                     return "";
130             }
131         }
132
133         public long AuthenticatedUserId
134         {
135             get
136             {
137                 if (httpCon != null)
138                     return httpCon.AuthUserId;
139                 else
140                     return 0;
141             }
142             set
143             {
144                 if (httpCon != null)
145                     httpCon.AuthUserId = value;
146             }
147         }
148
149         public string Password
150         {
151             get
152             {
153                 return "";
154             }
155         }
156
157         public bool AuthGetRequestToken(ref string content)
158         {
159             Uri authUri = null;
160             bool result = ((HttpOAuthApiProxy)httpCon).AuthenticatePinFlowRequest(RequestTokenUrl, AuthorizeUrl, ref requestToken, ref authUri);
161             content = authUri.ToString();
162             return result;
163         }
164
165         public HttpStatusCode AuthGetAccessToken(string pin)
166         {
167             return ((HttpOAuthApiProxy)httpCon).AuthenticatePinFlow(AccessTokenUrl, requestToken, pin);
168         }
169
170         public HttpStatusCode AuthUserAndPass(string username, string password, ref string content)
171         {
172             return httpCon.Authenticate(new Uri(AccessTokenUrlXAuth), username, password, ref content);
173         }
174
175         public void ClearAuthInfo()
176         {
177             this.Initialize("", "", "", 0);
178         }
179
180         public static bool UseSsl
181         {
182             set
183             {
184                 if (value)
185                     _protocol = "https://";
186                 else
187                     _protocol = "http://";
188             }
189         }
190
191         public HttpStatusCode UpdateStatus(string status, long replyToId, ref string content)
192         {
193             Dictionary<string, string> param = new Dictionary<string, string>();
194             param.Add("status", status);
195             if (replyToId > 0) param.Add("in_reply_to_status_id", replyToId.ToString());
196             param.Add("include_entities", "true");
197             //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
198
199             return httpCon.GetContent(PostMethod,
200                                       CreateTwitterUri("/1/statuses/update.json"),
201                                       param,
202                                       ref content,
203                                       null,
204                                       null);
205         }
206
207         public HttpStatusCode UpdateStatusWithMedia(string status, long replyToId, FileInfo mediaFile, ref string content)
208         {
209             //画像投稿用エンドポイント
210             Dictionary<string, string> param = new Dictionary<string, string>();
211             param.Add("status", status);
212             if (replyToId > 0) param.Add("in_reply_to_status_id", replyToId.ToString());
213             param.Add("include_entities", "true");
214             //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
215
216             List<KeyValuePair<string, FileInfo>> binary = new List<KeyValuePair<string, FileInfo>>();
217             binary.Add(new KeyValuePair<string, FileInfo>("media[]", mediaFile));
218
219             return httpCon.GetContent(PostMethod,
220                                       new Uri("https://upload.twitter.com/1/statuses/update_with_media.json"),
221                                       param,
222                                       binary,
223                                       ref content,
224                                       this.apiStatusHeaders,
225                                       GetApiCallback);
226         }
227
228         public HttpStatusCode DestroyStatus(long id)
229         {
230             string content = null;
231
232             return httpCon.GetContent(PostMethod,
233                                       CreateTwitterUri("/1/statuses/destroy/" + id.ToString()+ ".json"),
234                                       null,
235                                       ref content,
236                                       null,
237                                       null);
238         }
239
240         public HttpStatusCode SendDirectMessage(string status, string sendto, ref string content)
241         {
242             Dictionary<string, string> param = new Dictionary<string, string>();
243             param.Add("text", status);
244             param.Add("screen_name", sendto);
245             //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
246
247             return httpCon.GetContent(PostMethod,
248                                       CreateTwitterUri("/1/direct_messages/new.json"),
249                                       param,
250                                       ref content,
251                                       null,
252                                       null);
253         }
254
255         public HttpStatusCode DestroyDirectMessage(long id)
256         {
257             string content = null;
258
259             return httpCon.GetContent(PostMethod,
260                                       CreateTwitterUri("/1/direct_messages/destroy/" + id.ToString()+ ".json"),
261                                       null,
262                                       ref content,
263                                       null,
264                                       null);
265         }
266
267         public HttpStatusCode RetweetStatus(long id, ref string content)
268         {
269             Dictionary<string, string> param = new Dictionary<string, string>();
270             param.Add("include_entities", "true");
271
272             return httpCon.GetContent(PostMethod,
273                                       CreateTwitterUri("/1/statuses/retweet/" + id.ToString() + ".json"),
274                                       param,
275                                       ref content,
276                                       null,
277                                       null);
278         }
279
280         public HttpStatusCode ShowUserInfo(string screenName, ref string content)
281         {
282             Dictionary<string, string> param = new Dictionary<string, string>();
283             param.Add("screen_name", screenName);
284             param.Add("include_entities", "true");
285             return httpCon.GetContent(GetMethod,
286                                       CreateTwitterUri("/1/users/show.json"),
287                                       param,
288                                       ref content,
289                                       this.apiStatusHeaders,
290                                       GetApiCallback);
291         }
292
293         public HttpStatusCode CreateFriendships(string screenName, ref string content)
294         {
295             Dictionary<string, string> param = new Dictionary<string, string>();
296             param.Add("screen_name", screenName);
297
298             return httpCon.GetContent(PostMethod,
299                                       CreateTwitterUri("/1/friendships/create.json"),
300                                       param,
301                                       ref content,
302                                       null,
303                                       null);
304         }
305
306         public HttpStatusCode DestroyFriendships(string screenName, ref string content)
307         {
308             Dictionary<string, string> param = new Dictionary<string, string>();
309             param.Add("screen_name", screenName);
310
311             return httpCon.GetContent(PostMethod,
312                                       CreateTwitterUri("/1/friendships/destroy.json"),
313                                       param,
314                                       ref content,
315                                       null,
316                                       null);
317         }
318
319         public HttpStatusCode CreateBlock(string screenName, ref string content)
320         {
321             Dictionary<string, string> param = new Dictionary<string, string>();
322             param.Add("screen_name", screenName);
323
324             return httpCon.GetContent(PostMethod,
325                                       CreateTwitterUri("/1/blocks/create.json"),
326                                       param,
327                                       ref content,
328                                       null,
329                                       null);
330         }
331
332         public HttpStatusCode DestroyBlock(string screenName, ref string content)
333         {
334             Dictionary<string, string> param = new Dictionary<string, string>();
335             param.Add("screen_name", screenName);
336
337             return httpCon.GetContent(PostMethod,
338                                       CreateTwitterUri("/1/blocks/destroy.json"),
339                                       param,
340                                       ref content,
341                                       null,
342                                       null);
343         }
344
345         public HttpStatusCode ReportSpam(string screenName, ref string content)
346         {
347             Dictionary<string, string> param = new Dictionary<string, string>();
348             param.Add("screen_name", screenName);
349
350             return httpCon.GetContent(PostMethod,
351                                       CreateTwitterUri("/1/report_spam.json"),
352                                       param,
353                                       ref content,
354                                       null,
355                                       null);
356         }
357
358         public HttpStatusCode ShowFriendships(string souceScreenName, string targetScreenName, ref string content)
359         {
360             Dictionary<string, string> param = new Dictionary<string, string>();
361             param.Add("source_screen_name", souceScreenName);
362             param.Add("target_screen_name", targetScreenName);
363
364             return httpCon.GetContent(GetMethod,
365                                       CreateTwitterUri("/1/friendships/show.json"),
366                                       param,
367                                       ref content,
368                                       this.apiStatusHeaders,
369                                       GetApiCallback);
370         }
371
372         public HttpStatusCode ShowStatuses(long id, ref string content)
373         {
374             Dictionary<string, string> param = new Dictionary<string, string>();
375             param.Add("include_entities", "true");
376             return httpCon.GetContent(GetMethod,
377                                       CreateTwitterUri("/1/statuses/show/" + id.ToString() + ".json"),
378                                       param,
379                                       ref content,
380                                       this.apiStatusHeaders,
381                                       GetApiCallback);
382         }
383
384         public HttpStatusCode CreateFavorites(long id, ref string content)
385         {
386             return httpCon.GetContent(PostMethod,
387                                       CreateTwitterUri("/1/favorites/create/" + id.ToString() + ".json"),
388                                       null,
389                                       ref content,
390                                       null,
391                                       null);
392         }
393
394         public HttpStatusCode DestroyFavorites(long id, ref string content)
395         {
396             return httpCon.GetContent(PostMethod,
397                                       CreateTwitterUri("/1/favorites/destroy/" + id.ToString() + ".json"),
398                                       null,
399                                       ref content,
400                                       null,
401                                       null);
402         }
403
404         public HttpStatusCode HomeTimeline(int count, long max_id, long since_id, ref string content)
405         {
406             Dictionary<string, string> param = new Dictionary<string, string>();
407             if (count > 0)
408                 param.Add("count", count.ToString());
409             if (max_id > 0)
410                 param.Add("max_id", max_id.ToString());
411             if (since_id > 0)
412                 param.Add("since_id", since_id.ToString());
413
414             param.Add("include_entities", "true");
415
416             return httpCon.GetContent(GetMethod,
417                                       CreateTwitterUri("/1/statuses/home_timeline.json"),
418                                       param,
419                                       ref content,
420                                       this.apiStatusHeaders,
421                                       GetApiCallback);
422         }
423
424         public HttpStatusCode UserTimeline(long user_id, string screen_name, int count, long max_id, long since_id, ref string content)
425         {
426             Dictionary<string, string> param = new Dictionary<string, string>();
427
428             if ((user_id == 0 && string.IsNullOrEmpty(screen_name)) ||
429                 (user_id != 0 && !string.IsNullOrEmpty(screen_name))) return HttpStatusCode.BadRequest;
430
431             if (user_id > 0)
432                 param.Add("user_id", user_id.ToString());
433             if (!string.IsNullOrEmpty(screen_name))
434                 param.Add("screen_name", screen_name);
435             if (count > 0)
436                 param.Add("count", count.ToString());
437             if (max_id > 0)
438                 param.Add("max_id", max_id.ToString());
439             if (since_id > 0)
440                 param.Add("since_id", since_id.ToString());
441
442             param.Add("include_rts", "true");
443             param.Add("include_entities", "true");
444
445             return httpCon.GetContent(GetMethod,
446                                       CreateTwitterUri("/1/statuses/user_timeline.json"),
447                                       param,
448                                       ref content,
449                                       this.apiStatusHeaders,
450                                       GetApiCallback);
451         }
452
453         public HttpStatusCode PublicTimeline(int count, long max_id, long since_id, ref string content)
454         {
455             Dictionary<string, string> param = new Dictionary<string, string>();
456             if (count > 0)
457                 param.Add("count", count.ToString());
458             if (max_id > 0)
459                 param.Add("max_id", max_id.ToString());
460             if (since_id > 0)
461                 param.Add("since_id", since_id.ToString());
462
463             param.Add("include_entities", "true");
464
465             return httpCon.GetContent(GetMethod,
466                                       CreateTwitterUri("/1/statuses/public_timeline.json"),
467                                       param,
468                                       ref content,
469                                       this.apiStatusHeaders,
470                                       GetApiCallback);
471         }
472
473         public HttpStatusCode Mentions(int count, long max_id, long since_id, ref string content)
474         {
475             Dictionary<string, string> param = new Dictionary<string, string>();
476             if (count > 0)
477                 param.Add("count", count.ToString());
478             if (max_id > 0)
479                 param.Add("max_id", max_id.ToString());
480             if (since_id > 0)
481                 param.Add("since_id", since_id.ToString());
482
483             param.Add("include_entities", "true");
484
485             return httpCon.GetContent(GetMethod,
486                                       CreateTwitterUri("/1/statuses/mentions.json"),
487                                       param,
488                                       ref content,
489                                       this.apiStatusHeaders,
490                                       GetApiCallback);
491         }
492
493         public HttpStatusCode DirectMessages(int count, long max_id, long since_id, ref string content)
494         {
495             Dictionary<string, string> param = new Dictionary<string, string>();
496             if (count > 0)
497                 param.Add("count", count.ToString());
498             if (max_id > 0)
499                 param.Add("max_id", max_id.ToString());
500             if (since_id > 0)
501                 param.Add("since_id", since_id.ToString());
502             param.Add("include_entities", "true");
503
504             return httpCon.GetContent(GetMethod,
505                                       CreateTwitterUri("/1/direct_messages.json"),
506                                       param,
507                                       ref content,
508                                       this.apiStatusHeaders,
509                                       GetApiCallback);
510         }
511
512         public HttpStatusCode DirectMessagesSent(int count, long max_id, long since_id, ref string content)
513         {
514             Dictionary<string, string> param = new Dictionary<string, string>();
515             if (count > 0)
516                 param.Add("count", count.ToString());
517             if (max_id > 0)
518                 param.Add("max_id", max_id.ToString());
519             if (since_id > 0)
520                 param.Add("since_id", since_id.ToString());
521             param.Add("include_entities", "true");
522
523             return httpCon.GetContent(GetMethod,
524                                       CreateTwitterUri("/1/direct_messages/sent.json"),
525                                       param,
526                                       ref content,
527                                       this.apiStatusHeaders,
528                                       GetApiCallback);
529         }
530
531         public HttpStatusCode Favorites(int count, int page, ref string content)
532         {
533             Dictionary<string, string> param = new Dictionary<string, string>();
534             if (count != 20) param.Add("count", count.ToString());
535
536             if (page > 0)
537             {
538                 param.Add("page", page.ToString());
539             }
540
541             param.Add("include_entities", "true");
542
543             return httpCon.GetContent(GetMethod,
544                                       CreateTwitterUri("/1/favorites.json"),
545                                       param,
546                                       ref content,
547                                       this.apiStatusHeaders,
548                                       GetApiCallback);
549         }
550
551         public HttpStatusCode PhoenixSearch(string querystr, ref string content)
552         {
553             Dictionary<string, string> param = new Dictionary<string, string>();
554             string[] tmp;
555             string[] paramstr;
556             if (string.IsNullOrEmpty(querystr)) return HttpStatusCode.BadRequest;
557
558             tmp = querystr.Split(new char[] {'?', '&'}, StringSplitOptions.RemoveEmptyEntries);
559             foreach (string tmp2 in tmp)
560             {
561                 paramstr = tmp2.Split(new char[] {'='});
562                 param.Add(paramstr[0], paramstr[1]);
563             }
564
565             return httpConVar.GetContent(GetMethod,
566                                          CreateTwitterUri("/phoenix_search.phoenix"),
567                                          param,
568                                          out content,
569                                          null,
570                                          MyCommon.GetAssemblyName());
571         }
572
573         public HttpStatusCode PhoenixSearch(string words, string lang, int rpp, int page, long sinceId, ref string content)
574         {
575             Dictionary<string, string> param = new Dictionary<string, string>();
576             if (!string.IsNullOrEmpty(words)) param.Add("q", words);
577             param.Add("include_entities", "1");
578             param.Add("contributor_details", "true");
579             if (!string.IsNullOrEmpty(lang)) param.Add("lang", lang);
580             if (rpp > 0) param.Add("rpp", rpp.ToString());
581             if (page > 0) param.Add("page", page.ToString());
582             if (sinceId > 0) param.Add("since_id", sinceId.ToString());
583
584             if (param.Count == 0) return HttpStatusCode.BadRequest;
585
586             return httpConVar.GetContent(GetMethod,
587                                          CreateTwitterUri("/phoenix_search.phoenix"),
588                                          param,
589                                          out content,
590                                          null,
591                                          MyCommon.GetAssemblyName());
592         }
593
594         public HttpStatusCode Search(string words, string lang, int rpp, int page, long sinceId, ref string content)
595         {
596             Dictionary<string, string> param = new Dictionary<string, string>();
597             if (!string.IsNullOrEmpty(words)) param.Add("q", words);
598             if (!string.IsNullOrEmpty(lang)) param.Add("lang", lang);
599             if (rpp > 0) param.Add("rpp", rpp.ToString());
600             if (page > 0) param.Add("page", page.ToString());
601             if (sinceId > 0) param.Add("since_id", sinceId.ToString());
602
603             if (param.Count == 0) return HttpStatusCode.BadRequest;
604
605             param.Add("result_type", "recent");
606             param.Add("include_entities", "true");
607             return httpConVar.GetContent(GetMethod,
608                                          this.CreateTwitterSearchUri("/search.json"),
609                                          param,
610                                          out content,
611                                          null,
612                                          MyCommon.GetAssemblyName());
613         }
614
615         public HttpStatusCode SavedSearches(ref string content)
616         {
617             return httpCon.GetContent(GetMethod,
618                                       CreateTwitterUri("/1/saved_searches.json"),
619                                       null,
620                                       ref content,
621                                       null,
622                                       GetApiCallback);
623         }
624
625         public HttpStatusCode FollowerIds(long cursor, ref string content)
626         {
627             Dictionary<string, string> param = new Dictionary<string, string>();
628             param.Add("cursor", cursor.ToString());
629
630             return httpCon.GetContent(GetMethod,
631                                       CreateTwitterUri("/1/followers/ids.json"),
632                                       param,
633                                       ref content,
634                                       this.apiStatusHeaders,
635                                       GetApiCallback);
636         }
637
638         public HttpStatusCode NoRetweetIds(long cursor, ref string content)
639         {
640             Dictionary<string, string> param = new Dictionary<string, string>();
641             param.Add("cursor", cursor.ToString());
642
643             return httpCon.GetContent(GetMethod,
644                                       CreateTwitterUri("/1/friendships/no_retweet_ids.json"),
645                                       param,
646                                       ref content,
647                                       this.apiStatusHeaders,
648                                       GetApiCallback);
649         }
650
651         public HttpStatusCode RateLimitStatus(ref string content)
652         {
653             return httpCon.GetContent(GetMethod,
654                                       CreateTwitterUri("/1/account/rate_limit_status.json"),
655                                       null,
656                                       ref content,
657                                       this.apiStatusHeaders,
658                                       GetApiCallback);
659         }
660
661         #region Lists
662         public HttpStatusCode GetLists(string user, long cursor, ref string content)
663         {
664             Dictionary<string, string> param = new Dictionary<string, string>();
665             param.Add("screen_name", user);
666             param.Add("cursor", cursor.ToString());
667             return httpCon.GetContent(GetMethod,
668                                       CreateTwitterUri("/1/lists.json"),
669                                       param,
670                                       ref content,
671                                       this.apiStatusHeaders,
672                                       GetApiCallback);
673         }
674
675         public HttpStatusCode UpdateListID(string user, string list_id, string name, Boolean isPrivate, string description, ref string content)
676         {
677             string mode = "public";
678             if (isPrivate)
679                 mode = "private";
680
681             Dictionary<string, string> param = new Dictionary<string, string>();
682             param.Add("screen_name", user);
683             param.Add("list_id", list_id);
684             if (name != null) param.Add("name", name);
685             if (mode != null) param.Add("mode", mode);
686             if (description != null) param.Add("description", description);
687
688             return httpCon.GetContent(PostMethod,
689                                       CreateTwitterUri("/1/lists/update.json"),
690                                       param,
691                                       ref content,
692                                       null,
693                                       null);
694         }
695
696         public HttpStatusCode DeleteListID(string user, string list_id, ref string content)
697         {
698             Dictionary<string, string> param = new Dictionary<string, string>();
699             param.Add("screen_name", user);
700             param.Add("list_id", list_id);
701
702             return httpCon.GetContent(PostMethod,
703                                       CreateTwitterUri("/1/lists/destroy.json"),
704                                       param,
705                                       ref content,
706                                       null,
707                                       null);
708         }
709
710         public HttpStatusCode GetListsSubscriptions(string user, long cursor, ref string content)
711         {
712             Dictionary<string, string> param = new Dictionary<string, string>();
713             param.Add("screen_name", user);
714             param.Add("cursor", cursor.ToString());
715             return httpCon.GetContent(GetMethod,
716                                       CreateTwitterUri("/1/lists/subscriptions.json"),
717                                       param,
718                                       ref content,
719                                       this.apiStatusHeaders,
720                                       GetApiCallback);
721         }
722
723         public HttpStatusCode GetListsStatuses(long userId, long list_id, int per_page, long max_id, long since_id, Boolean isRTinclude, ref string content)
724         {
725             //認証なくても取得できるが、protectedユーザー分が抜ける
726             Dictionary<string, string> param = new Dictionary<string, string>();
727             param.Add("user_id", userId.ToString());
728             param.Add("list_id", list_id.ToString());
729             if (isRTinclude)
730                 param.Add("include_rts", "true");
731             if (per_page > 0)
732                 param.Add("per_page", per_page.ToString());
733             if (max_id > 0)
734                 param.Add("max_id", max_id.ToString());
735             if (since_id > 0)
736                 param.Add("since_id", since_id.ToString());
737             param.Add("include_entities", "true");
738
739             return httpCon.GetContent(GetMethod,
740                                       CreateTwitterUri("/1/lists/statuses.json"),
741                                       param,
742                                       ref content,
743                                       this.apiStatusHeaders,
744                                       GetApiCallback);
745         }
746
747         public HttpStatusCode CreateLists(string listname, Boolean isPrivate, string description, ref string content)
748         {
749             string mode = "public";
750             if (isPrivate)
751                 mode = "private";
752
753             Dictionary<string, string> param = new Dictionary<string, string>();
754             param.Add("name", listname);
755             param.Add("mode", mode);
756             if (!string.IsNullOrEmpty(description))
757                 param.Add("description", description);
758
759             return httpCon.GetContent(PostMethod,
760                                       CreateTwitterUri("/1/lists/create.json"),
761                                       param,
762                                       ref content,
763                                       null,
764                                       null);
765         }
766
767         public HttpStatusCode GetListMembers(string user, string list_id, long cursor, ref string content)
768         {
769             Dictionary<string, string> param = new Dictionary<string, string>();
770             param.Add("screen_name", user);
771             param.Add("list_id", list_id);
772             param.Add("cursor", cursor.ToString());
773             return httpCon.GetContent(GetMethod,
774                                       CreateTwitterUri("/1/lists/members.json"),
775                                       param,
776                                       ref content,
777                                       this.apiStatusHeaders,
778                                       GetApiCallback);
779         }
780
781         public HttpStatusCode CreateListMembers(string list_id, string memberName, ref string content)
782         {
783             Dictionary<string, string> param = new Dictionary<string, string>();
784             param.Add("list_id", list_id);
785             param.Add("screen_name", memberName);
786             return httpCon.GetContent(PostMethod,
787                                       CreateTwitterUri("/1/lists/members/create.json"),
788                                       param,
789                                       ref content,
790                                       null,
791                                       null);
792         }
793
794         //public HttpStatusCode CreateListMembers(string user, string list_id, string memberName, ref string content)
795         //{
796         //    //正常に動かないので旧APIで様子見
797         //    //Dictionary<string, string> param = new Dictionary<string, string>();
798         //    //param.Add("screen_name", user)
799         //    //param.Add("list_id", list_id)
800         //    //param.Add("member_screen_name", memberName)
801         //    //return httpCon.GetContent(PostMethod,
802         //    //                          CreateTwitterUri("/1/lists/members/create.json"),
803         //    //                          param,
804         //    //                          ref content,
805         //    //                          null,
806         //    //                          null)
807         //    Dictionary<string, string> param = new Dictionary<string, string>();
808         //    param.Add("id", memberName)
809         //    return httpCon.GetContent(PostMethod,
810         //                              CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"),
811         //                              param,
812         //                              ref content,
813         //                              null,
814         //                              null)
815         //}
816
817         public HttpStatusCode DeleteListMembers(string list_id, string memberName, ref string content)
818         {
819             Dictionary<string, string> param = new Dictionary<string, string>();
820             param.Add("screen_name", memberName);
821             param.Add("list_id", list_id);
822             return httpCon.GetContent(PostMethod,
823                                       CreateTwitterUri("/1/lists/members/destroy.json"),
824                                       param,
825                                       ref content,
826                                       null,
827                                       null);
828         }
829
830         //public HttpStatusCode DeleteListMembers(string user, string list_id, string memberName, ref string content)
831         //{
832         //    //Dictionary<string, string> param = new Dictionary<string, string>();
833         //    //param.Add("screen_name", user)
834         //    //param.Add("list_id", list_id)
835         //    //param.Add("member_screen_name", memberName)
836         //    //return httpCon.GetContent(PostMethod,
837         //    //                          CreateTwitterUri("/1/lists/members/destroy.json"),
838         //    //                          param,
839         //    //                          ref content,
840         //    //                          null,
841         //    //                          null)
842         //    Dictionary<string, string> param = new Dictionary<string, string>();
843         //    param.Add("id", memberName)
844         //    param.Add("_method", "DELETE")
845         //    return httpCon.GetContent(PostMethod,
846         //                              CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"),
847         //                              param,
848         //                              ref content,
849         //                              null,
850         //                              null)
851         //}
852
853         public HttpStatusCode ShowListMember(string list_id, string memberName, ref string content)
854         {
855             //新APIがmember_screen_nameもmember_user_idも無視して、自分のIDを返してくる。
856             //正式にドキュメントに反映されるまで旧APIを使用する
857             Dictionary<string, string> param = new Dictionary<string, string>();
858             param.Add("screen_name", memberName);
859             param.Add("list_id", list_id);
860             return httpCon.GetContent(GetMethod,
861                                       CreateTwitterUri("/1/lists/members/show.json"),
862                                       param,
863                                       ref content,
864                                       this.apiStatusHeaders,
865                                       GetApiCallback);
866             //return httpCon.GetContent(GetMethod,
867             //                          CreateTwitterUri("/1/" + user + "/" + list_id + "/members/" + id + ".json"),
868             //                          null,
869             //                          ref content,
870             //                          MyCommon.TwitterApiInfo.HttpHeaders,
871             //                          GetApiCallback);
872         }
873         #endregion
874
875         public HttpStatusCode Statusid_retweeted_by_ids(long statusid, int count, int page, ref string content)
876         {
877             Dictionary<string, string> param = new Dictionary<string, string>();
878             if (count > 0)
879                 param.Add("count", count.ToString());
880             if (page > 0)
881                 param.Add("page", page.ToString());
882
883             return httpCon.GetContent(GetMethod,
884                                       CreateTwitterUri("/1/statuses/" + statusid.ToString()+ "/retweeted_by/ids.json"),
885                                       param,
886                                       ref content,
887                                       this.apiStatusHeaders,
888                                       GetApiCallback);
889         }
890
891         public HttpStatusCode UpdateProfile(string name, string url, string location, string description, ref string content)
892         {
893             Dictionary<string, string> param = new Dictionary<string, string>();
894
895             param.Add("name", name);
896             param.Add("url", url);
897             param.Add("location", location);
898             param.Add("description", description);
899             param.Add("include_entities", "true");
900
901             return httpCon.GetContent(PostMethod,
902                                       CreateTwitterUri("/1/account/update_profile.json"),
903                                       param,
904                                       ref content,
905                                       null,
906                                       null);
907         }
908
909         public HttpStatusCode UpdateProfileImage(FileInfo imageFile, ref string content)
910         {
911             List<KeyValuePair<string, FileInfo>> binary = new List<KeyValuePair<string, FileInfo>>();
912             binary.Add(new KeyValuePair<string, FileInfo>("image", imageFile));
913
914             return httpCon.GetContent(PostMethod,
915                                       CreateTwitterUri("/1/account/update_profile_image.json"),
916                                       null,
917                                       binary,
918                                       ref content,
919                                       null,
920                                       null);
921         }
922
923         public HttpStatusCode GetRelatedResults(long id, ref string content)
924         {
925             //認証なくても取得できるが、protectedユーザー分が抜ける
926             Dictionary<string, string> param = new Dictionary<string, string>();
927
928             param.Add("include_entities", "true");
929
930             return httpCon.GetContent(GetMethod,
931                                       CreateTwitterUri("/1/related_results/show/" + id.ToString()+ ".json"),
932                                       param,
933                                       ref content,
934                                       this.apiStatusHeaders,
935                                       GetApiCallback);
936         }
937
938         public HttpStatusCode GetBlockUserIds(ref string content)
939         {
940             return httpCon.GetContent(GetMethod,
941                                       CreateTwitterUri("/1/blocks/blocking/ids.json"),
942                                       null,
943                                       ref content,
944                                       this.apiStatusHeaders,
945                                       GetApiCallback);
946         }
947
948         public HttpStatusCode GetConfiguration(ref string content)
949         {
950             return httpCon.GetContent(GetMethod,
951                                       CreateTwitterUri("/1/help/configuration.json"),
952                                       null,
953                                       ref content,
954                                       this.apiStatusHeaders,
955                                       GetApiCallback);
956         }
957
958         public HttpStatusCode VerifyCredentials(ref string content)
959         {
960             return httpCon.GetContent(GetMethod,
961                                       CreateTwitterUri("/1/account/verify_credentials.json"),
962                                       null,
963                                       ref content,
964                                       this.apiStatusHeaders,
965                                       GetApiCallback);
966         }
967
968         #region Proxy API
969         private static string _twitterUrl = "api.twitter.com";
970         private static string _TwitterSearchUrl = "search.twitter.com";
971         private static string _twitterUserStreamUrl = "userstream.twitter.com";
972         private static string _twitterStreamUrl = "stream.twitter.com";
973
974         private Uri CreateTwitterUri(string path)
975         {
976             return new Uri(string.Format("{0}{1}{2}", _protocol, _twitterUrl, path));
977         }
978
979         private Uri CreateTwitterSearchUri(string path)
980         {
981             return new Uri(string.Format("{0}{1}{2}", _protocol, _TwitterSearchUrl, path));
982         }
983
984         private Uri CreateTwitterUserStreamUri(string path)
985         {
986             return new Uri(string.Format("{0}{1}{2}", "https://", _twitterUserStreamUrl, path));
987         }
988
989         private Uri CreateTwitterStreamUri(string path)
990         {
991             return new Uri(string.Format("{0}{1}{2}", "http://", _twitterStreamUrl, path));
992         }
993
994         public static string TwitterUrl
995         {
996             set
997             {
998                 _twitterUrl = value;
999                 HttpOAuthApiProxy.ProxyHost = value;
1000             }
1001         }
1002
1003         public static string TwitterSearchUrl
1004         {
1005             set
1006             {
1007                 _TwitterSearchUrl = value;
1008             }
1009         }
1010         #endregion
1011
1012         private void GetApiCallback(Object sender, ref HttpStatusCode code, ref string content)
1013         {
1014             if (code < HttpStatusCode.InternalServerError)
1015                 MyCommon.TwitterApiInfo.UpdateFromHeader(this.apiStatusHeaders);
1016         }
1017
1018         public HttpStatusCode UserStream(ref Stream content,
1019                                          bool allAtReplies,
1020                                          string trackwords,
1021                                          string userAgent)
1022         {
1023             Dictionary<string, string> param = new Dictionary<string, string>();
1024
1025             if (allAtReplies)
1026                 param.Add("replies", "all");
1027
1028             if (!string.IsNullOrEmpty(trackwords))
1029                 param.Add("track", trackwords);
1030
1031             return httpCon.GetContent(GetMethod,
1032                                       CreateTwitterUserStreamUri("/2/user.json"),
1033                                       param,
1034                                       ref content,
1035                                       userAgent);
1036         }
1037
1038         public HttpStatusCode FilterStream(ref Stream content,
1039                                            string trackwords,
1040                                            string userAgent)
1041         {
1042             //文中の日本語キーワードに反応せず、使えない(スペースで分かち書きしてないと反応しない)
1043             Dictionary<string, string> param = new Dictionary<string, string>();
1044
1045             if (!string.IsNullOrEmpty(trackwords))
1046                 param.Add("track", string.Join(",", trackwords.Split(" ".ToCharArray())));
1047
1048             return httpCon.GetContent(PostMethod,
1049                                       CreateTwitterStreamUri("/1/statuses/filter.json"),
1050                                       param,
1051                                       ref content,
1052                                       userAgent);
1053         }
1054
1055         public void RequestAbort()
1056         {
1057             httpCon.RequestAbort();
1058         }
1059
1060         public object Clone()
1061         {
1062             HttpTwitter myCopy = new HttpTwitter();
1063             myCopy.Initialize(this.AccessToken, this.AccessTokenSecret, this.AuthenticatedUsername, this.AuthenticatedUserId);
1064             return myCopy;
1065         }
1066     }
1067 }