OSDN Git Service

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