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 Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 // (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
11 // This file is part of OpenTween.
13 // This program is free software; you can redistribute it and/or modify it
14 // under the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 3 of the License, or (at your option)
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
25 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 // Boston, MA 02110-1301, USA.
28 using System.Diagnostics;
32 using System.Runtime.CompilerServices;
33 using System.Runtime.Serialization;
34 using System.Runtime.Serialization.Json;
36 using System.Text.RegularExpressions;
37 using System.Threading;
38 using System.Threading.Tasks;
41 using System.Xml.Linq;
42 using System.Xml.XPath;
44 using System.Reflection;
45 using System.Collections.Generic;
47 using System.Windows.Forms;
49 using OpenTween.Connection;
53 public class Twitter : IDisposable
55 #region Regexp from twitter-text-js
57 // The code in this region code block incorporates works covered by
58 // the following copyright and permission notices:
60 // Copyright 2011 Twitter, Inc.
62 // Licensed under the Apache License, Version 2.0 (the "License"); you
63 // may not use this work except in compliance with the License. You
64 // may obtain a copy of the License in the LICENSE file, or at:
66 // http://www.apache.org/licenses/LICENSE-2.0
68 // Unless required by applicable law or agreed to in writing, software
69 // distributed under the License is distributed on an "AS IS" BASIS,
70 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
71 // implied. See the License for the specific language governing
72 // permissions and limitations under the License.
75 private const string LATIN_ACCENTS = @"\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0100-\u024f\u0253\u0254\u0256\u0257\u0259\u025b\u0263\u0268\u026f\u0272\u0289\u028b\u02bb\u1e00-\u1eff";
76 private const string NON_LATIN_HASHTAG_CHARS = @"\u0400-\u04ff\u0500-\u0527\u1100-\u11ff\u3130-\u3185\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF";
77 //private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u3096\u3400-\u4DBF\u4E00-\u9FFF\u20000-\u2A6DF\u2A700-\u2B73F\u2B740-\u2B81F\u2F800-\u2FA1F";
78 private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\u30FC\u3005\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u309A\u3400-\u4DBF\p{IsCJKUnifiedIdeographs}";
79 private const string HASHTAG_BOUNDARY = @"^|$|\s|「|」|。|\.|!";
80 private const string HASHTAG_ALPHA = "[a-z_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
81 private const string HASHTAG_ALPHANUMERIC = "[a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
82 private const string HASHTAG_TERMINATOR = "[^a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
83 public const string HASHTAG = "(" + HASHTAG_BOUNDARY + ")(#|#)(" + HASHTAG_ALPHANUMERIC + "*" + HASHTAG_ALPHA + HASHTAG_ALPHANUMERIC + "*)(?=" + HASHTAG_TERMINATOR + "|" + HASHTAG_BOUNDARY + ")";
85 private const string url_valid_preceding_chars = @"(?:[^A-Za-z0-9@@$##\ufffe\ufeff\uffff\u202a-\u202e]|^)";
86 public const string url_invalid_without_protocol_preceding_chars = @"[-_./]$";
87 private const string url_invalid_domain_chars = @"\!'#%&'\(\)*\+,\\\-\.\/:;<=>\?@\[\]\^_{|}~\$\u2000-\u200a\u0009-\u000d\u0020\u0085\u00a0\u1680\u180e\u2028\u2029\u202f\u205f\u3000\ufffe\ufeff\uffff\u202a-\u202e";
88 private const string url_valid_domain_chars = @"[^" + url_invalid_domain_chars + "]";
89 private const string url_valid_subdomain = @"(?:(?:" + url_valid_domain_chars + @"(?:[_-]|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
90 private const string url_valid_domain_name = @"(?:(?:" + url_valid_domain_chars + @"(?:-|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
91 private const string url_valid_GTLD = @"(?:(?:aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|xxx)(?=[^0-9a-zA-Z]|$))";
92 private const string url_valid_CCTLD = @"(?:(?:ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|ss|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw)(?=[^0-9a-zA-Z]|$))";
93 private const string url_valid_punycode = @"(?:xn--[0-9a-z]+)";
94 private const string url_valid_domain = @"(?<domain>" + url_valid_subdomain + "*" + url_valid_domain_name + "(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + ")|" + url_valid_punycode + ")";
95 public const string url_valid_ascii_domain = @"(?:(?:[a-z0-9" + LATIN_ACCENTS + @"]+)\.)+(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + "|" + url_valid_punycode + ")";
96 public const string url_invalid_short_domain = "^" + url_valid_domain_name + url_valid_CCTLD + "$";
97 private const string url_valid_port_number = @"[0-9]+";
99 private const string url_valid_general_path_chars = @"[a-z0-9!*';:=+,.$/%#\[\]\-_~|&" + LATIN_ACCENTS + "]";
100 private const string url_balance_parens = @"(?:\(" + url_valid_general_path_chars + @"+\))";
101 private const string url_valid_path_ending_chars = @"(?:[+\-a-z0-9=_#/" + LATIN_ACCENTS + "]|" + url_balance_parens + ")";
102 private const string pth = "(?:" +
104 url_valid_general_path_chars + "*" +
105 "(?:" + url_balance_parens + url_valid_general_path_chars + "*)*" +
106 url_valid_path_ending_chars +
107 ")|(?:@" + url_valid_general_path_chars + "+/)" +
109 private const string qry = @"(?<query>\?[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|]*[a-z0-9_&=#/])?";
110 public const string rgUrl = @"(?<before>" + url_valid_preceding_chars + ")" +
111 "(?<url>(?<protocol>https?://)?" +
112 "(?<domain>" + url_valid_domain + ")" +
113 "(?::" + url_valid_port_number + ")?" +
114 "(?<path>/" + pth + "*)?" +
121 /// Twitter API のステータスページのURL
123 public const string ServiceAvailabilityStatusUrl = "https://status.io.watchmouse.com/7617";
126 /// ツイートへのパーマリンクURLを判定する正規表現
128 public static readonly Regex StatusUrlRegex = new Regex(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
131 /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
133 public static readonly Regex ThirdPartyStatusUrlRegex = new Regex(@"https?://(?:[^.]+\.)?(?:
134 favstar\.fm/users/[a-zA-Z0-9_]+/status/ # Favstar
135 | favstar\.fm/t/ # Favstar (short)
136 | aclog\.koba789\.com/i/ # aclog
137 | frtrt\.net/solo_status\.php\?status= # RtRT
138 )(?<StatusId>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
141 /// DM送信かどうかを判定する正規表現
143 public static readonly Regex DMSendTextRegex = new Regex(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
145 public TwitterConfiguration Configuration { get; private set; }
147 delegate void GetIconImageDelegate(PostClass post);
148 private readonly object LockObj = new object();
149 private ISet<long> followerId = new HashSet<long>();
150 private bool _GetFollowerResult = false;
151 private long[] noRTId = new long[0];
152 private bool _GetNoRetweetResult = false;
154 private int _followersCount = 0;
155 private int _friendsCount = 0;
156 private int _statusesCount = 0;
157 private string _location = "";
158 private string _bio = "";
161 private string _uname;
163 private bool _restrictFavCheck;
165 private bool _readOwnPost;
166 private List<string> _hashList = new List<string>();
168 //max_idで古い発言を取得するために保持(lists分は個別タブで管理)
169 private long minHomeTimeline = long.MaxValue;
170 private long minMentions = long.MaxValue;
171 private long minDirectmessage = long.MaxValue;
172 private long minDirectmessageSent = long.MaxValue;
174 //private FavoriteQueue favQueue;
176 private HttpTwitter twCon = new HttpTwitter();
178 //private List<PostClass> _deletemessages = new List<PostClass>();
182 this.Configuration = TwitterConfiguration.DefaultConfiguration();
185 public TwitterApiAccessLevel AccessLevel
189 return MyCommon.TwitterApiInfo.AccessLevel;
193 protected void ResetApiStatus()
195 MyCommon.TwitterApiInfo.Reset();
198 public void Authenticate(string username, string password)
200 this.ResetApiStatus();
206 res = twCon.AuthUserAndPass(username, password, ref content);
210 throw new WebApiException("Err:" + ex.Message, ex);
213 this.CheckStatusCode(res, content);
215 _uname = username.ToLower();
216 if (SettingCommon.Instance.UserstreamStartup) this.ReconnectUserStream();
219 public string StartAuthentication()
222 this.ResetApiStatus();
225 string pinPageUrl = null;
226 var res = twCon.AuthGetRequestToken(ref pinPageUrl);
228 throw new WebApiException("Err:Failed to access auth server.");
234 throw new WebApiException("Err:Failed to access auth server.", ex);
238 public void Authenticate(string pinCode)
240 this.ResetApiStatus();
245 res = twCon.AuthGetAccessToken(pinCode);
249 throw new WebApiException("Err:Failed to access auth acc server.", ex);
252 this.CheckStatusCode(res, null);
254 _uname = Username.ToLower();
255 if (SettingCommon.Instance.UserstreamStartup) this.ReconnectUserStream();
258 public void ClearAuthInfo()
260 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
261 this.ResetApiStatus();
262 twCon.ClearAuthInfo();
265 public void VerifyCredentials()
271 res = twCon.VerifyCredentials(ref content);
278 if (res == HttpStatusCode.OK)
280 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
284 user = TwitterUser.ParseJson(content);
286 catch(SerializationException)
290 twCon.AuthenticatedUserId = user.Id;
294 public void Initialize(string token, string tokenSecret, string username, long userId)
297 if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(tokenSecret) || string.IsNullOrEmpty(username))
299 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
301 this.ResetApiStatus();
302 twCon.Initialize(token, tokenSecret, username, userId);
303 _uname = username.ToLower();
304 if (SettingCommon.Instance.UserstreamStartup) this.ReconnectUserStream();
307 public string PreProcessUrl(string orgData)
311 //var IDNConveter = new IdnMapping();
312 var href = "<a href=\"";
316 if (orgData.IndexOf(href, posl2, StringComparison.Ordinal) > -1)
320 posl1 = orgData.IndexOf(href, posl2, StringComparison.Ordinal);
321 posl1 += href.Length;
322 posl2 = orgData.IndexOf("\"", posl1, StringComparison.Ordinal);
323 urlStr = orgData.Substring(posl1, posl2 - posl1);
325 if (!urlStr.StartsWith("http://") && !urlStr.StartsWith("https://") && !urlStr.StartsWith("ftp://"))
330 var replacedUrl = MyCommon.IDNEncode(urlStr);
331 if (replacedUrl == null) continue;
332 if (replacedUrl == urlStr) continue;
334 orgData = orgData.Replace("<a href=\"" + urlStr, "<a href=\"" + replacedUrl);
345 private string GetPlainText(string orgData)
347 return WebUtility.HtmlDecode(Regex.Replace(orgData, "(?<tagStart><a [^>]+>)(?<text>[^<]+)(?<tagEnd></a>)", "${text}"));
350 // htmlの簡易サニタイズ(詳細表示に不要なタグの除去)
352 private string SanitizeHtml(string orgdata)
354 var retdata = orgdata;
356 retdata = Regex.Replace(retdata, "<(script|object|applet|image|frameset|fieldset|legend|style).*" +
357 "</(script|object|applet|image|frameset|fieldset|legend|style)>", "", RegexOptions.IgnoreCase);
359 retdata = Regex.Replace(retdata, "<(frame|link|iframe|img)>", "", RegexOptions.IgnoreCase);
364 private string AdjustHtml(string orgData)
366 var retStr = orgData;
367 //var m = Regex.Match(retStr, "<a [^>]+>[#|#](?<1>[a-zA-Z0-9_]+)</a>");
372 // _hashList.Add("#" + m.Groups(1).Value);
376 retStr = Regex.Replace(retStr, "<a [^>]*href=\"/", "<a href=\"https://twitter.com/");
377 retStr = retStr.Replace("<a href=", "<a target=\"_self\" href=");
378 retStr = Regex.Replace(retStr, @"(\r\n?|\n)", "<br>"); // CRLF, CR, LF は全て <br> に置換する
380 //半角スペースを置換(Thanks @anis774)
384 ret = EscapeSpace(ref retStr);
387 return SanitizeHtml(retStr);
390 private bool EscapeSpace(ref string html)
392 //半角スペースを置換(Thanks @anis774)
394 for (int i = 0; i < html.Length; i++)
405 if ((!isTag) && (html[i] == ' '))
407 html = html.Remove(i, 1);
408 html = html.Insert(i, " ");
415 private struct PostInfo
417 public string CreatedAt;
420 public string UserId;
421 public PostInfo(string Created, string IdStr, string txt, string uid)
428 public bool Equals(PostInfo dst)
430 if (this.CreatedAt == dst.CreatedAt && this.Id == dst.Id && this.Text == dst.Text && this.UserId == dst.UserId)
441 static private PostInfo _prev = new PostInfo("", "", "", "");
442 private bool IsPostRestricted(TwitterStatus status)
444 var _current = new PostInfo("", "", "", "");
446 _current.CreatedAt = status.CreatedAt;
447 _current.Id = status.IdStr;
448 if (status.Text == null)
454 _current.Text = status.Text;
456 _current.UserId = status.User.IdStr;
458 if (_current.Equals(_prev))
462 _prev.CreatedAt = _current.CreatedAt;
463 _prev.Id = _current.Id;
464 _prev.Text = _current.Text;
465 _prev.UserId = _current.UserId;
470 public void PostStatus(string postStr, long? reply_to, List<long> mediaIds = null)
472 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
473 throw new WebApiException("Auth error. Check your account");
475 if (mediaIds == null &&
476 Twitter.DMSendTextRegex.IsMatch(postStr))
478 SendDirectMessage(postStr);
486 res = twCon.UpdateStatus(postStr, reply_to, mediaIds, ref content);
490 throw new WebApiException("Err:" + ex.Message, ex);
493 // 投稿に成功していても404が返ることがあるらしい: https://dev.twitter.com/discussions/1213
494 if (res == HttpStatusCode.NotFound)
497 this.CheckStatusCode(res, content);
499 TwitterStatus status;
502 status = TwitterStatus.ParseJson(content);
504 catch(SerializationException ex)
506 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
507 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
511 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
512 throw new WebApiException("Err:Invalid Json!", content, ex);
514 _followersCount = status.User.FollowersCount;
515 _friendsCount = status.User.FriendsCount;
516 _statusesCount = status.User.StatusesCount;
517 _location = status.User.Location;
518 _bio = status.User.Description;
520 if (IsPostRestricted(status))
522 throw new WebApiException("OK:Delaying?");
526 public void PostStatusWithMedia(string postStr, long? reply_to, IMediaItem item)
528 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
529 throw new WebApiException("Auth error. Check your account");
535 res = twCon.UpdateStatusWithMedia(postStr, reply_to, item, ref content);
539 throw new WebApiException("Err:" + ex.Message, ex);
542 // 投稿に成功していても404が返ることがあるらしい: https://dev.twitter.com/discussions/1213
543 if (res == HttpStatusCode.NotFound)
546 this.CheckStatusCode(res, content);
548 TwitterStatus status;
551 status = TwitterStatus.ParseJson(content);
553 catch(SerializationException ex)
555 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
556 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
560 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
561 throw new WebApiException("Err:Invalid Json!", content, ex);
563 _followersCount = status.User.FollowersCount;
564 _friendsCount = status.User.FriendsCount;
565 _statusesCount = status.User.StatusesCount;
566 _location = status.User.Location;
567 _bio = status.User.Description;
569 if (IsPostRestricted(status))
571 throw new WebApiException("OK:Delaying?");
575 public void PostStatusWithMultipleMedia(string postStr, long? reply_to, IMediaItem[] mediaItems)
577 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
578 throw new WebApiException("Auth error. Check your account");
580 if (Twitter.DMSendTextRegex.IsMatch(postStr))
582 SendDirectMessage(postStr);
586 var mediaIds = new List<long>();
588 foreach (var item in mediaItems)
590 var mediaId = UploadMedia(item);
591 mediaIds.Add(mediaId);
594 if (mediaIds.Count == 0)
595 throw new WebApiException("Err:Invalid Files!");
597 PostStatus(postStr, reply_to, mediaIds);
600 public long UploadMedia(IMediaItem item)
602 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
603 throw new WebApiException("Auth error. Check your account");
609 res = twCon.UploadMedia(item, ref content);
613 throw new WebApiException("Err:" + ex.Message, ex);
616 this.CheckStatusCode(res, content);
618 TwitterUploadMediaResult status;
621 status = TwitterUploadMediaResult.ParseJson(content);
623 catch (SerializationException ex)
625 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
626 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
630 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
631 throw new WebApiException("Err:Invalid Json!", content, ex);
634 return status.MediaId;
637 public void SendDirectMessage(string postStr)
639 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
640 throw new WebApiException("Auth error. Check your account");
642 if (this.AccessLevel == TwitterApiAccessLevel.Read || this.AccessLevel == TwitterApiAccessLevel.ReadWrite)
643 throw new WebApiException("Auth Err:try to re-authorization.");
645 var mc = Twitter.DMSendTextRegex.Match(postStr);
651 res = twCon.SendDirectMessage(mc.Groups["body"].Value, mc.Groups["id"].Value, ref content);
655 throw new WebApiException("Err:" + ex.Message, ex);
658 this.CheckStatusCode(res, content);
660 TwitterDirectMessage status;
663 status = TwitterDirectMessage.ParseJson(content);
665 catch(SerializationException ex)
667 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
668 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
672 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
673 throw new WebApiException("Err:Invalid Json!", content, ex);
675 _followersCount = status.Sender.FollowersCount;
676 _friendsCount = status.Sender.FriendsCount;
677 _statusesCount = status.Sender.StatusesCount;
678 _location = status.Sender.Location;
679 _bio = status.Sender.Description;
682 public void RemoveStatus(long id)
684 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
685 throw new WebApiException("Auth error. Check your account");
690 res = twCon.DestroyStatus(id);
694 throw new WebApiException("Err:" + ex.Message, ex);
697 this.CheckStatusCode(res, null);
700 public void PostRetweet(long id, bool read)
702 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
703 throw new WebApiException("Auth error. Check your account");
707 var post = TabInformations.GetInstance()[id];
710 throw new WebApiException("Err:Target isn't found.");
712 if (TabInformations.GetInstance()[id].RetweetedId != null)
714 target = TabInformations.GetInstance()[id].RetweetedId.Value; //再RTの場合は元発言をRT
721 res = twCon.RetweetStatus(target, ref content);
725 throw new WebApiException("Err:" + ex.Message, ex);
728 this.CheckStatusCode(res, content);
730 TwitterStatus status;
733 status = TwitterStatus.ParseJson(content);
735 catch(SerializationException ex)
737 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
738 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
742 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
743 throw new WebApiException("Err:Invalid Json!", content, ex);
747 post = CreatePostsFromStatusData(status);
749 throw new WebApiException("Invalid Json!", content);
754 if (TabInformations.GetInstance().ContainsKey(post.StatusId))
758 if (post.RetweetedId == null)
759 throw new WebApiException("Invalid Json!", content);
765 if (_readOwnPost) post.IsRead = true;
768 TabInformations.GetInstance().AddPost(post);
771 public void RemoveDirectMessage(long id, PostClass post)
773 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
774 throw new WebApiException("Auth error. Check your account");
776 if (this.AccessLevel == TwitterApiAccessLevel.Read || this.AccessLevel == TwitterApiAccessLevel.ReadWrite)
777 throw new WebApiException("Auth Err:try to re-authorization.");
780 // _deletemessages.Add(post)
786 res = twCon.DestroyDirectMessage(id);
790 throw new WebApiException("Err:" + ex.Message, ex);
793 this.CheckStatusCode(res, null);
796 public void PostFollowCommand(string screenName)
798 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
799 throw new WebApiException("Auth error. Check your account");
805 res = twCon.CreateFriendships(screenName, ref content);
809 throw new WebApiException("Err:" + ex.Message, ex);
812 this.CheckStatusCode(res, content);
815 public void PostRemoveCommand(string screenName)
817 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
818 throw new WebApiException("Auth error. Check your account");
824 res = twCon.DestroyFriendships(screenName, ref content);
828 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
831 this.CheckStatusCode(res, content);
834 public void PostCreateBlock(string screenName)
836 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
837 throw new WebApiException("Auth error. Check your account");
843 res = twCon.CreateBlock(screenName, ref content);
847 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
850 this.CheckStatusCode(res, content);
853 public void PostDestroyBlock(string screenName)
855 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
856 throw new WebApiException("Auth error. Check your account");
862 res = twCon.DestroyBlock(screenName, ref content);
866 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
869 this.CheckStatusCode(res, content);
872 public void PostReportSpam(string screenName)
874 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
875 throw new WebApiException("Auth error. Check your account");
881 res = twCon.ReportSpam(screenName, ref content);
885 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
888 this.CheckStatusCode(res, content);
891 public TwitterFriendship GetFriendshipInfo(string screenName)
893 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
894 throw new WebApiException("Auth error. Check your account");
900 res = twCon.ShowFriendships(_uname, screenName, ref content);
904 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
907 this.CheckStatusCode(res, content);
911 return TwitterFriendship.ParseJson(content);
913 catch(SerializationException ex)
915 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
916 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
920 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
921 throw new WebApiException("Err:Invalid Json!", content, ex);
925 public TwitterUser GetUserInfo(string screenName)
927 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
928 throw new WebApiException("Auth error. Check your account");
934 res = twCon.ShowUserInfo(screenName, ref content);
938 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
941 this.CheckStatusCode(res, content);
945 return TwitterUser.ParseJson(content);
947 catch (SerializationException ex)
949 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
950 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
954 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
955 throw new WebApiException("Err:Invalid Json!", content, ex);
959 public int GetStatus_Retweeted_Count(long StatusId)
961 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
962 throw new WebApiException("Auth error. Check your account");
968 res = twCon.ShowStatuses(StatusId, ref content);
972 throw new WebApiException("Err:" + ex.Message, ex);
975 this.CheckStatusCode(res, content);
979 var status = TwitterStatus.ParseJson(content);
980 return status.RetweetCount;
982 catch (SerializationException ex)
984 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
985 throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
989 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
990 throw new WebApiException("Invalid Json!", content, ex);
994 public void PostFavAdd(long id)
996 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
997 throw new WebApiException("Auth error. Check your account");
999 //if (this.favQueue == null) this.favQueue = new FavoriteQueue(this)
1001 //if (this.favQueue.Contains(id)) this.favQueue.Remove(id)
1007 res = twCon.CreateFavorites(id, ref content);
1011 //this.favQueue.Add(id)
1012 //return "Err:->FavoriteQueue:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1013 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1016 this.CheckStatusCode(res, content);
1018 if (!_restrictFavCheck)
1021 //http://twitter.com/statuses/show/id.xml APIを発行して本文を取得
1025 res = twCon.ShowStatuses(id, ref content);
1029 throw new WebApiException("Err:" + ex.Message, ex);
1032 this.CheckStatusCode(res, content);
1034 TwitterStatus status;
1037 status = TwitterStatus.ParseJson(content);
1039 catch (SerializationException ex)
1041 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1042 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
1044 catch (Exception ex)
1046 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1047 throw new WebApiException("Err:Invalid Json!", content, ex);
1049 if (status.Favorited != true)
1050 throw new WebApiException("NG(Restricted?)");
1053 public void PostFavRemove(long id)
1055 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1056 throw new WebApiException("Auth error. Check your account");
1058 //if (this.favQueue == null) this.favQueue = new FavoriteQueue(this)
1060 //if (this.favQueue.Contains(id))
1061 // this.favQueue.Remove(id)
1069 res = twCon.DestroyFavorites(id, ref content);
1073 throw new WebApiException("Err:" + ex.Message, ex);
1076 this.CheckStatusCode(res, content);
1079 public TwitterUser PostUpdateProfile(string name, string url, string location, string description)
1081 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1082 throw new WebApiException("AccountState invalid");
1088 res = twCon.UpdateProfile(name, url, location, description, ref content);
1092 throw new WebApiException("Err:" + ex.Message, content, ex);
1095 this.CheckStatusCode(res, content);
1099 return TwitterUser.ParseJson(content);
1101 catch (SerializationException e)
1103 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
1104 MyCommon.TraceOut(ex);
1109 var ex = new WebApiException("Err:Invalid Json!", content, e);
1110 MyCommon.TraceOut(ex);
1115 public void PostUpdateProfileImage(string filename)
1117 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1118 throw new WebApiException("Auth error. Check your account");
1124 res = twCon.UpdateProfileImage(new FileInfo(filename), ref content);
1128 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
1131 this.CheckStatusCode(res, content);
1134 public string Username
1138 return twCon.AuthenticatedUsername;
1146 return twCon.AuthenticatedUserId;
1150 public string Password
1154 return twCon.Password;
1158 private static MyCommon.ACCOUNT_STATE _accountState = MyCommon.ACCOUNT_STATE.Valid;
1159 public static MyCommon.ACCOUNT_STATE AccountState
1163 return _accountState;
1167 _accountState = value;
1171 public bool RestrictFavCheck
1175 _restrictFavCheck = value;
1180 public void GetTweenBinary(string strVer)
1185 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/Tween" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1186 Path.Combine(MyCommon.settingPath, "TweenNew.exe")))
1188 throw new WebApiException("Err:Download failed");
1191 if (!Directory.Exists(Path.Combine(MyCommon.settingPath, "en")))
1193 Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, "en"));
1195 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenResEn" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1196 Path.Combine(Path.Combine(MyCommon.settingPath, "en"), "Tween.resourcesNew.dll")))
1198 throw new WebApiException("Err:Download failed");
1200 //その他言語圏のリソース。取得失敗しても継続
1203 if (!Thread.CurrentThread.CurrentUICulture.IsNeutralCulture)
1205 var idx = Thread.CurrentThread.CurrentUICulture.Name.LastIndexOf('-');
1208 curCul = Thread.CurrentThread.CurrentUICulture.Name.Substring(0, idx);
1212 curCul = Thread.CurrentThread.CurrentUICulture.Name;
1217 curCul = Thread.CurrentThread.CurrentUICulture.Name;
1219 if (!string.IsNullOrEmpty(curCul) && curCul != "en" && curCul != "ja")
1221 if (!Directory.Exists(Path.Combine(MyCommon.settingPath, curCul)))
1223 Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, curCul));
1225 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenRes" + curCul + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1226 Path.Combine(Path.Combine(MyCommon.settingPath, curCul), "Tween.resourcesNew.dll")))
1228 //return "Err:Download failed";
1233 if (!Thread.CurrentThread.CurrentCulture.IsNeutralCulture)
1235 var idx = Thread.CurrentThread.CurrentCulture.Name.LastIndexOf('-');
1238 curCul2 = Thread.CurrentThread.CurrentCulture.Name.Substring(0, idx);
1242 curCul2 = Thread.CurrentThread.CurrentCulture.Name;
1247 curCul2 = Thread.CurrentThread.CurrentCulture.Name;
1249 if (!string.IsNullOrEmpty(curCul2) && curCul2 != "en" && curCul2 != curCul)
1251 if (!Directory.Exists(Path.Combine(MyCommon.settingPath, curCul2)))
1253 Directory.CreateDirectory(Path.Combine(MyCommon.settingPath, curCul2));
1255 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenRes" + curCul2 + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1256 Path.Combine(Path.Combine(MyCommon.settingPath, curCul2), "Tween.resourcesNew.dll")))
1258 //return "Err:Download failed";
1263 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenUp3.gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1264 Path.Combine(MyCommon.settingPath, "TweenUp3.exe")))
1266 throw new WebApiException("Err:Download failed");
1269 if (!(new HttpVarious()).GetDataToFile("http://tween.sourceforge.jp/TweenDll" + strVer + ".gz?" + DateTime.Now.ToString("yyMMddHHmmss") + Environment.TickCount.ToString(),
1270 Path.Combine(MyCommon.settingPath, "TweenNew.XmlSerializers.dll")))
1272 throw new WebApiException("Err:Download failed");
1275 catch (Exception ex)
1277 throw new WebApiException("Err:Download failed", ex);
1282 public bool ReadOwnPost
1286 return _readOwnPost;
1290 _readOwnPost = value;
1294 public int FollowersCount
1298 return _followersCount;
1302 public int FriendsCount
1306 return _friendsCount;
1310 public int StatusesCount
1314 return _statusesCount;
1318 public string Location
1335 /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
1337 public static bool VerifyApiResultCount(MyCommon.WORKERTYPE type, int count)
1339 return count >= 20 && count <= GetMaxApiResultCount(type);
1343 /// 渡された取得件数が更新時の取得可能範囲に収まっているか検証する
1345 public static bool VerifyMoreApiResultCount(int count)
1347 return count >= 20 && count <= 200;
1351 /// 渡された取得件数が起動時の取得可能範囲に収まっているか検証する
1353 public static bool VerifyFirstApiResultCount(int count)
1355 return count >= 20 && count <= 200;
1359 /// WORKERTYPEに応じた取得可能な最大件数を取得する
1361 public static int GetMaxApiResultCount(MyCommon.WORKERTYPE type)
1363 // 参照: REST APIs - 各endpointのcountパラメータ
1364 // https://dev.twitter.com/rest/public
1367 case MyCommon.WORKERTYPE.Timeline:
1368 case MyCommon.WORKERTYPE.Reply:
1369 case MyCommon.WORKERTYPE.UserTimeline:
1370 case MyCommon.WORKERTYPE.Favorites:
1371 case MyCommon.WORKERTYPE.DirectMessegeRcv:
1372 case MyCommon.WORKERTYPE.DirectMessegeSnt:
1373 case MyCommon.WORKERTYPE.List: // 不明
1376 case MyCommon.WORKERTYPE.PublicSearch:
1380 throw new InvalidOperationException("Invalid type: " + type);
1385 /// WORKERTYPEに応じた取得件数を取得する
1387 public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool startup)
1389 if (type == MyCommon.WORKERTYPE.DirectMessegeRcv ||
1390 type == MyCommon.WORKERTYPE.DirectMessegeSnt)
1395 if (SettingCommon.Instance.UseAdditionalCount)
1399 case MyCommon.WORKERTYPE.Favorites:
1400 if (SettingCommon.Instance.FavoritesCountApi != 0)
1401 return SettingCommon.Instance.FavoritesCountApi;
1403 case MyCommon.WORKERTYPE.List:
1404 if (SettingCommon.Instance.ListCountApi != 0)
1405 return SettingCommon.Instance.ListCountApi;
1407 case MyCommon.WORKERTYPE.PublicSearch:
1408 if (SettingCommon.Instance.SearchCountApi != 0)
1409 return SettingCommon.Instance.SearchCountApi;
1411 case MyCommon.WORKERTYPE.UserTimeline:
1412 if (SettingCommon.Instance.UserTimelineCountApi != 0)
1413 return SettingCommon.Instance.UserTimelineCountApi;
1416 if (more && SettingCommon.Instance.MoreCountApi != 0)
1418 return Math.Min(SettingCommon.Instance.MoreCountApi, GetMaxApiResultCount(type));
1420 if (startup && SettingCommon.Instance.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
1422 return Math.Min(SettingCommon.Instance.FirstCountApi, GetMaxApiResultCount(type));
1426 // 上記に当てはまらない場合の共通処理
1427 var count = SettingCommon.Instance.CountApi;
1429 if (type == MyCommon.WORKERTYPE.Reply)
1430 count = SettingCommon.Instance.CountApiReply;
1432 return Math.Min(count, GetMaxApiResultCount(type));
1435 public void GetTimelineApi(bool read,
1436 MyCommon.WORKERTYPE gType,
1440 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1441 throw new WebApiException("Auth error. Check your account");
1445 var count = GetApiResultCount(gType, more, startup);
1449 if (gType == MyCommon.WORKERTYPE.Timeline)
1453 res = twCon.HomeTimeline(count, this.minHomeTimeline, null, ref content);
1457 res = twCon.HomeTimeline(count, null, null, ref content);
1464 res = twCon.Mentions(count, this.minMentions, null, ref content);
1468 res = twCon.Mentions(count, null, null, ref content);
1474 throw new WebApiException("Err:" + ex.Message, ex);
1477 this.CheckStatusCode(res, content);
1479 var minimumId = CreatePostsFromJson(content, gType, null, read);
1481 if (minimumId != null)
1483 if (gType == MyCommon.WORKERTYPE.Timeline)
1484 this.minHomeTimeline = minimumId.Value;
1486 this.minMentions = minimumId.Value;
1490 public void GetUserTimelineApi(bool read,
1495 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1496 throw new WebApiException("Auth error. Check your account");
1500 var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
1504 if (string.IsNullOrEmpty(userName))
1506 var target = tab.User;
1507 if (string.IsNullOrEmpty(target)) return;
1509 res = twCon.UserTimeline(null, target, count, null, null, ref content);
1515 res = twCon.UserTimeline(null, userName, count, tab.OldestId, null, ref content);
1519 res = twCon.UserTimeline(null, userName, count, null, null, ref content);
1525 throw new WebApiException("Err:" + ex.Message, ex);
1528 if (res == HttpStatusCode.Unauthorized)
1529 throw new WebApiException("Err:@" + userName + "'s Tweets are protected.");
1531 this.CheckStatusCode(res, content);
1533 var minimumId = CreatePostsFromJson(content, MyCommon.WORKERTYPE.UserTimeline, tab, read);
1535 if (minimumId != null)
1536 tab.OldestId = minimumId.Value;
1539 public PostClass GetStatusApi(bool read, long id)
1541 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1542 throw new WebApiException("Auth error. Check your account");
1548 res = twCon.ShowStatuses(id, ref content);
1552 throw new WebApiException("Err:" + ex.Message, ex);
1555 if (res == HttpStatusCode.Forbidden)
1556 throw new WebApiException("Err:protected user's tweet", content);
1558 this.CheckStatusCode(res, content);
1560 TwitterStatus status;
1563 status = TwitterStatus.ParseJson(content);
1565 catch(SerializationException ex)
1567 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1568 throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
1572 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1573 throw new WebApiException("Invalid Json!", content, ex);
1576 var item = CreatePostsFromStatusData(status);
1578 throw new WebApiException("Err:Can't create post", content);
1581 if (item.IsMe && !read && _readOwnPost) item.IsRead = true;
1586 public void GetStatusApi(bool read, long id, TabClass tab)
1588 var post = this.GetStatusApi(read, id);
1590 if (tab != null) post.RelTabName = tab.TabName;
1591 //非同期アイコン取得&StatusDictionaryに追加
1592 TabInformations.GetInstance().AddPost(post);
1595 private PostClass CreatePostsFromStatusData(TwitterStatus status)
1597 return CreatePostsFromStatusData(status, false);
1600 private PostClass CreatePostsFromStatusData(TwitterStatus status, bool favTweet)
1602 var post = new PostClass();
1603 TwitterEntities entities;
1606 post.StatusId = status.Id;
1607 if (status.RetweetedStatus != null)
1609 var retweeted = status.RetweetedStatus;
1611 post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt);
1614 post.RetweetedId = retweeted.Id;
1616 post.TextFromApi = retweeted.Text;
1617 entities = retweeted.MergedEntities;
1618 sourceHtml = retweeted.Source;
1620 post.InReplyToStatusId = retweeted.InReplyToStatusId;
1621 post.InReplyToUser = retweeted.InReplyToScreenName;
1622 post.InReplyToUserId = status.InReplyToUserId;
1631 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
1632 post.IsFav = tc.Contains(retweeted.Id);
1635 if (retweeted.Coordinates != null) post.PostGeo = new PostClass.StatusGeo { Lng = retweeted.Coordinates.Coordinates[0], Lat = retweeted.Coordinates.Coordinates[1] };
1638 var user = retweeted.User;
1640 if (user == null || user.ScreenName == null || status.User.ScreenName == null) return null;
1642 post.UserId = user.Id;
1643 post.ScreenName = user.ScreenName;
1644 post.Nickname = user.Name.Trim();
1645 post.ImageUrl = user.ProfileImageUrlHttps;
1646 post.IsProtect = user.Protected;
1649 post.RetweetedBy = status.User.ScreenName;
1650 post.RetweetedByUserId = status.User.Id;
1651 post.IsMe = post.RetweetedBy.ToLower().Equals(_uname);
1655 post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
1657 post.TextFromApi = status.Text;
1658 entities = status.MergedEntities;
1659 sourceHtml = status.Source;
1660 post.InReplyToStatusId = status.InReplyToStatusId;
1661 post.InReplyToUser = status.InReplyToScreenName;
1662 post.InReplyToUserId = status.InReplyToUserId;
1671 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
1672 post.IsFav = tc.Contains(post.StatusId) && TabInformations.GetInstance()[post.StatusId].IsFav;
1675 if (status.Coordinates != null) post.PostGeo = new PostClass.StatusGeo { Lng = status.Coordinates.Coordinates[0], Lat = status.Coordinates.Coordinates[1] };
1678 var user = status.User;
1680 if (user == null || user.ScreenName == null) return null;
1682 post.UserId = user.Id;
1683 post.ScreenName = user.ScreenName;
1684 post.Nickname = user.Name.Trim();
1685 post.ImageUrl = user.ProfileImageUrlHttps;
1686 post.IsProtect = user.Protected;
1687 post.IsMe = post.ScreenName.ToLower().Equals(_uname);
1690 string textFromApi = post.TextFromApi;
1691 post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, entities, post.Media);
1692 post.TextFromApi = textFromApi;
1693 post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities);
1694 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
1695 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
1697 post.QuoteStatusIds = GetQuoteTweetStatusIds(entities)
1698 .Where(x => x != post.StatusId && x != post.RetweetedId)
1699 .Distinct().ToArray();
1702 var source = ParseSource(sourceHtml);
1703 post.Source = source.Item1;
1704 post.SourceUri = source.Item2;
1706 post.IsReply = post.ReplyToList.Contains(_uname);
1707 post.IsExcludeReply = false;
1715 if (followerId.Count > 0) post.IsOwl = !followerId.Contains(post.UserId);
1723 /// ツイートに含まれる引用ツイートのURLからステータスIDを抽出
1725 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<TwitterEntity> entities)
1727 foreach (var entity in entities)
1729 var entityUrl = entity as TwitterEntityUrl;
1730 if (entityUrl == null)
1733 var match = Twitter.StatusUrlRegex.Match(entityUrl.ExpandedUrl);
1736 yield return long.Parse(match.Groups["StatusId"].Value);
1741 private long? CreatePostsFromJson(string content, MyCommon.WORKERTYPE gType, TabClass tab, bool read)
1743 TwitterStatus[] items;
1746 items = TwitterStatus.ParseJsonArray(content);
1748 catch(SerializationException ex)
1750 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1751 throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
1755 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1756 throw new WebApiException("Invalid Json!", content, ex);
1759 long? minimumId = null;
1761 foreach (var status in items)
1763 PostClass post = null;
1764 post = CreatePostsFromStatusData(status);
1765 if (post == null) continue;
1767 if (minimumId == null || minimumId.Value > post.StatusId)
1768 minimumId = post.StatusId;
1775 if (TabInformations.GetInstance().ContainsKey(post.StatusId)) continue;
1779 if (tab.Contains(post.StatusId)) continue;
1784 if (gType != MyCommon.WORKERTYPE.UserTimeline &&
1785 post.RetweetedByUserId != null && this.noRTId.Contains(post.RetweetedByUserId.Value)) continue;
1788 if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
1790 if (tab != null) post.RelTabName = tab.TabName;
1791 //非同期アイコン取得&StatusDictionaryに追加
1792 TabInformations.GetInstance().AddPost(post);
1798 private long? CreatePostsFromSearchJson(string content, TabClass tab, bool read, int count, bool more)
1800 TwitterSearchResult items;
1803 items = TwitterSearchResult.ParseJson(content);
1805 catch (SerializationException ex)
1807 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1808 throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
1810 catch (Exception ex)
1812 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1813 throw new WebApiException("Invalid Json!", content, ex);
1816 long? minimumId = null;
1818 foreach (var result in items.Statuses)
1820 PostClass post = null;
1821 post = CreatePostsFromStatusData(result);
1825 // Search API は相変わらずぶっ壊れたデータを返すことがあるため、必要なデータが欠如しているものは取得し直す
1828 post = this.GetStatusApi(read, result.Id);
1830 catch (WebApiException)
1836 if (minimumId == null || minimumId.Value > post.StatusId)
1837 minimumId = post.StatusId;
1839 if (!more && post.StatusId > tab.SinceId) tab.SinceId = post.StatusId;
1845 if (TabInformations.GetInstance().ContainsKey(post.StatusId)) continue;
1849 if (tab.Contains(post.StatusId)) continue;
1854 if ((post.IsMe && !read) && this._readOwnPost) post.IsRead = true;
1856 if (tab != null) post.RelTabName = tab.TabName;
1857 //非同期アイコン取得&StatusDictionaryに追加
1858 TabInformations.GetInstance().AddPost(post);
1864 private void CreateFavoritePostsFromJson(string content, bool read)
1866 TwitterStatus[] item;
1869 item = TwitterStatus.ParseJsonArray(content);
1871 catch (SerializationException ex)
1873 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1874 throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
1876 catch (Exception ex)
1878 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1879 throw new WebApiException("Invalid Json!", content, ex);
1882 var favTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
1884 foreach (var status in item)
1889 if (favTab.Contains(status.Id)) continue;
1892 var post = CreatePostsFromStatusData(status, true);
1893 if (post == null) continue;
1897 TabInformations.GetInstance().AddPost(post);
1901 public void GetListStatus(bool read,
1908 var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
1914 res = twCon.GetListsStatuses(tab.ListInfo.UserId, tab.ListInfo.Id, count, tab.OldestId, null, SettingCommon.Instance.IsListsIncludeRts, ref content);
1918 res = twCon.GetListsStatuses(tab.ListInfo.UserId, tab.ListInfo.Id, count, null, null, SettingCommon.Instance.IsListsIncludeRts, ref content);
1923 throw new WebApiException("Err:" + ex.Message, ex);
1926 this.CheckStatusCode(res, content);
1928 var minimumId = CreatePostsFromJson(content, MyCommon.WORKERTYPE.List, tab, read);
1930 if (minimumId != null)
1931 tab.OldestId = minimumId.Value;
1935 /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
1937 /// <returns>posts の中から検索されたリプライチェインの末端</returns>
1938 internal static PostClass FindTopOfReplyChain(IDictionary<Int64, PostClass> posts, Int64 startStatusId)
1940 if (!posts.ContainsKey(startStatusId))
1941 throw new ArgumentException("startStatusId (" + startStatusId + ") が posts の中から見つかりませんでした。", nameof(startStatusId));
1943 var nextPost = posts[startStatusId];
1944 while (nextPost.InReplyToStatusId != null)
1946 if (!posts.ContainsKey(nextPost.InReplyToStatusId.Value))
1948 nextPost = posts[nextPost.InReplyToStatusId.Value];
1954 public void GetRelatedResult(bool read, TabClass tab)
1956 var relPosts = new Dictionary<Int64, PostClass>();
1957 if (tab.RelationTargetPost.TextFromApi.Contains("@") && tab.RelationTargetPost.InReplyToStatusId == null)
1960 var p = TabInformations.GetInstance()[tab.RelationTargetPost.StatusId];
1961 if (p != null && p.InReplyToStatusId != null)
1963 tab.RelationTargetPost = p;
1967 p = this.GetStatusApi(read, tab.RelationTargetPost.StatusId);
1968 tab.RelationTargetPost = p;
1971 relPosts.Add(tab.RelationTargetPost.StatusId, tab.RelationTargetPost.Clone());
1973 Exception lastException = null;
1975 // in_reply_to_status_id を使用してリプライチェインを辿る
1976 var nextPost = FindTopOfReplyChain(relPosts, tab.RelationTargetPost.StatusId);
1978 while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
1980 var inReplyToId = nextPost.InReplyToStatusId.Value;
1982 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
1983 if (inReplyToPost != null)
1985 inReplyToPost = inReplyToPost.Clone();
1991 inReplyToPost = this.GetStatusApi(read, inReplyToId);
1993 catch (WebApiException ex)
2000 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
2002 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
2005 //MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
2006 var text = tab.RelationTargetPost.Text;
2007 var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
2008 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
2009 foreach (var _match in ma)
2012 if (Int64.TryParse(_match.Groups["StatusId"].Value, out _statusId))
2014 if (relPosts.ContainsKey(_statusId))
2018 var _post = TabInformations.GetInstance()[_statusId];
2023 p = this.GetStatusApi(read, _statusId);
2025 catch (WebApiException ex)
2037 relPosts.Add(p.StatusId, p);
2041 relPosts.Values.ToList().ForEach(p =>
2043 if (p.IsMe && !read && this._readOwnPost)
2048 p.RelTabName = tab.TabName;
2049 TabInformations.GetInstance().AddPost(p);
2052 if (lastException != null)
2053 throw new WebApiException(lastException.Message, lastException);
2056 public void GetSearch(bool read,
2062 var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
2064 long? sinceId = null;
2067 maxId = tab.OldestId - 1;
2071 sinceId = tab.SinceId;
2076 // TODO:一時的に40>100件に 件数変更UI作成の必要あり
2077 res = twCon.Search(tab.SearchWords, tab.SearchLang, count, maxId, sinceId, ref content);
2081 throw new WebApiException("Err:" + ex.Message, ex);
2085 case HttpStatusCode.BadRequest:
2086 throw new WebApiException("Invalid query", content);
2087 case HttpStatusCode.NotFound:
2088 throw new WebApiException("Invalid query", content);
2089 case HttpStatusCode.PaymentRequired: //API Documentには420と書いてあるが、該当コードがないので402にしてある
2090 throw new WebApiException("Search API Limit?", content);
2091 case HttpStatusCode.OK:
2094 throw new WebApiException("Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")", content);
2097 if (!TabInformations.GetInstance().ContainsTab(tab))
2100 var minimumId = this.CreatePostsFromSearchJson(content, tab, read, count, more);
2102 if (minimumId != null)
2103 tab.OldestId = minimumId.Value;
2106 private void CreateDirectMessagesFromJson(string content, MyCommon.WORKERTYPE gType, bool read)
2108 TwitterDirectMessage[] item;
2111 if (gType == MyCommon.WORKERTYPE.UserStream)
2113 item = new[] { TwitterStreamEventDirectMessage.ParseJson(content).DirectMessage };
2117 item = TwitterDirectMessage.ParseJsonArray(content);
2120 catch(SerializationException ex)
2122 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2123 throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
2127 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2128 throw new WebApiException("Invalid Json!", content, ex);
2131 foreach (var message in item)
2133 var post = new PostClass();
2136 post.StatusId = message.Id;
2137 if (gType != MyCommon.WORKERTYPE.UserStream)
2139 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
2141 if (minDirectmessage > post.StatusId) minDirectmessage = post.StatusId;
2145 if (minDirectmessageSent > post.StatusId) minDirectmessageSent = post.StatusId;
2152 if (TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage).Contains(post.StatusId)) continue;
2156 post.CreatedAt = MyCommon.DateTimeParse(message.CreatedAt);
2158 var textFromApi = message.Text;
2160 post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, message.Entities, post.Media);
2161 post.TextFromApi = this.ReplaceTextFromApi(textFromApi, message.Entities);
2162 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
2163 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
2166 post.QuoteStatusIds = GetQuoteTweetStatusIds(message.Entities).Distinct().ToArray();
2170 if (gType == MyCommon.WORKERTYPE.UserStream)
2172 if (twCon.AuthenticatedUsername.Equals(message.Recipient.ScreenName, StringComparison.CurrentCultureIgnoreCase))
2174 user = message.Sender;
2180 user = message.Recipient;
2187 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
2189 user = message.Sender;
2195 user = message.Recipient;
2201 post.UserId = user.Id;
2202 post.ScreenName = user.ScreenName;
2203 post.Nickname = user.Name.Trim();
2204 post.ImageUrl = user.ProfileImageUrlHttps;
2205 post.IsProtect = user.Protected;
2209 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2210 MessageBox.Show("Parse Error(CreateDirectMessagesFromJson)");
2215 if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
2216 post.IsReply = false;
2217 post.IsExcludeReply = false;
2220 TabInformations.GetInstance().AddPost(post);
2224 public void GetDirectMessageApi(bool read,
2225 MyCommon.WORKERTYPE gType,
2228 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2229 throw new WebApiException("Auth error. Check your account");
2231 if (this.AccessLevel == TwitterApiAccessLevel.Read || this.AccessLevel == TwitterApiAccessLevel.ReadWrite)
2232 throw new WebApiException("Auth Err:try to re-authorization.");
2236 var count = GetApiResultCount(gType, more, false);
2240 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
2244 res = twCon.DirectMessages(count, minDirectmessage, null, ref content);
2248 res = twCon.DirectMessages(count, null, null, ref content);
2255 res = twCon.DirectMessagesSent(count, minDirectmessageSent, null, ref content);
2259 res = twCon.DirectMessagesSent(count, null, null, ref content);
2265 throw new WebApiException("Err:" + ex.Message, ex);
2268 this.CheckStatusCode(res, content);
2270 CreateDirectMessagesFromJson(content, gType, read);
2273 public void GetFavoritesApi(bool read,
2276 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2277 throw new WebApiException("Auth error. Check your account");
2281 var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, more, false);
2285 res = twCon.Favorites(count, ref content);
2289 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2292 this.CheckStatusCode(res, content);
2294 CreateFavoritePostsFromJson(content, read);
2297 private string ReplaceTextFromApi(string text, TwitterEntities entities)
2299 if (entities != null)
2301 if (entities.Urls != null)
2303 foreach (var m in entities.Urls)
2305 if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
2308 if (entities.Media != null)
2310 foreach (var m in entities.Media)
2312 if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
2322 /// <exception cref="WebApiException"/>
2323 public void RefreshFollowerIds()
2325 if (MyCommon._endingFlag) return;
2328 var newFollowerIds = new HashSet<long>();
2331 var ret = this.GetFollowerIdsApi(ref cursor);
2332 newFollowerIds.UnionWith(ret.Ids);
2333 cursor = ret.NextCursor;
2334 } while (cursor != 0);
2336 this.followerId = newFollowerIds;
2337 TabInformations.GetInstance().RefreshOwl(this.followerId);
2339 this._GetFollowerResult = true;
2342 public bool GetFollowersSuccess
2346 return _GetFollowerResult;
2350 private TwitterIds GetFollowerIdsApi(ref long cursor)
2352 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2353 throw new WebApiException("AccountState invalid");
2359 res = twCon.FollowerIds(cursor, ref content);
2363 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
2366 this.CheckStatusCode(res, content);
2370 var ret = TwitterIds.ParseJson(content);
2372 if (ret.Ids == null)
2374 var ex = new WebApiException("Err: ret.id == null (GetFollowerIdsApi)", content);
2375 MyCommon.ExceptionOut(ex);
2381 catch(SerializationException e)
2383 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
2384 MyCommon.TraceOut(ex);
2389 var ex = new WebApiException("Err:Invalid Json!", content, e);
2390 MyCommon.TraceOut(ex);
2396 /// RT 非表示ユーザーを更新します
2398 /// <exception cref="WebApiException"/>
2399 public void RefreshNoRetweetIds()
2401 if (MyCommon._endingFlag) return;
2403 this.noRTId = this.NoRetweetIdsApi();
2405 this._GetNoRetweetResult = true;
2408 private long[] NoRetweetIdsApi()
2410 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2411 throw new WebApiException("AccountState invalid");
2417 res = twCon.NoRetweetIds(ref content);
2421 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
2424 this.CheckStatusCode(res, content);
2428 return MyCommon.CreateDataFromJson<long[]>(content);
2430 catch(SerializationException e)
2432 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
2433 MyCommon.TraceOut(ex);
2438 var ex = new WebApiException("Err:Invalid Json!", content, e);
2439 MyCommon.TraceOut(ex);
2444 public bool GetNoRetweetSuccess
2448 return _GetNoRetweetResult;
2453 /// t.co の文字列長などの設定情報を更新します
2455 /// <exception cref="WebApiException"/>
2456 public void RefreshConfiguration()
2458 this.Configuration = this.ConfigurationApi();
2461 private TwitterConfiguration ConfigurationApi()
2467 res = twCon.GetConfiguration(ref content);
2471 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
2474 this.CheckStatusCode(res, content);
2478 return TwitterConfiguration.ParseJson(content);
2480 catch(SerializationException e)
2482 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
2483 MyCommon.TraceOut(ex);
2488 var ex = new WebApiException("Err:Invalid Json!", content, e);
2489 MyCommon.TraceOut(ex);
2494 public void GetListsApi()
2496 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2497 throw new WebApiException("AccountState invalid");
2500 IEnumerable<ListElement> lists;
2505 res = twCon.GetLists(this.Username, ref content);
2507 catch (Exception ex)
2509 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2512 this.CheckStatusCode(res, content);
2516 lists = TwitterList.ParseJsonArray(content)
2517 .Select(x => new ListElement(x, this));
2519 catch (SerializationException ex)
2521 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2522 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
2524 catch (Exception ex)
2526 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2527 throw new WebApiException("Err:Invalid Json!", content, ex);
2532 res = twCon.GetListsSubscriptions(this.Username, ref content);
2534 catch (Exception ex)
2536 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2539 this.CheckStatusCode(res, content);
2543 lists = lists.Concat(TwitterList.ParseJsonArray(content)
2544 .Select(x => new ListElement(x, this)));
2546 catch (SerializationException ex)
2548 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2549 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
2551 catch (Exception ex)
2553 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2554 throw new WebApiException("Err:Invalid Json!", content, ex);
2557 TabInformations.GetInstance().SubscribableLists = lists.ToList();
2560 public void DeleteList(string list_id)
2567 res = twCon.DeleteListID(this.Username, list_id, ref content);
2571 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2574 this.CheckStatusCode(res, content);
2577 public ListElement EditList(string list_id, string new_name, bool isPrivate, string description)
2584 res = twCon.UpdateListID(this.Username, list_id, new_name, isPrivate, description, ref content);
2588 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2591 this.CheckStatusCode(res, content);
2595 var le = TwitterList.ParseJson(content);
2596 return new ListElement(le, this);
2598 catch(SerializationException ex)
2600 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2601 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
2605 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2606 throw new WebApiException("Err:Invalid Json!", content, ex);
2610 public long GetListMembers(string list_id, List<UserInfo> lists, long cursor)
2612 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2613 throw new WebApiException("Auth error. Check your account");
2619 res = twCon.GetListMembers(this.Username, list_id, cursor, ref content);
2623 throw new WebApiException("Err:" + ex.Message);
2626 this.CheckStatusCode(res, content);
2630 var users = TwitterUsers.ParseJson(content);
2631 Array.ForEach<TwitterUser>(
2633 u => lists.Add(new UserInfo(u)));
2635 return users.NextCursor;
2637 catch(SerializationException ex)
2639 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2640 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
2644 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2645 throw new WebApiException("Err:Invalid Json!", content, ex);
2649 public void CreateListApi(string listName, bool isPrivate, string description)
2651 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2652 throw new WebApiException("Auth error. Check your account");
2658 res = twCon.CreateLists(listName, isPrivate, description, ref content);
2662 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2665 this.CheckStatusCode(res, content);
2669 var le = TwitterList.ParseJson(content);
2670 TabInformations.GetInstance().SubscribableLists.Add(new ListElement(le, this));
2672 catch(SerializationException ex)
2674 MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
2675 throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
2679 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
2680 throw new WebApiException("Err:Invalid Json!", content, ex);
2684 public bool ContainsUserAtList(string listId, string user)
2686 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2687 throw new WebApiException("Auth error. Check your account");
2694 res = this.twCon.ShowListMember(listId, user, ref content);
2698 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2701 if (res == HttpStatusCode.NotFound)
2706 this.CheckStatusCode(res, content);
2710 TwitterUser.ParseJson(content);
2719 public void AddUserToList(string listId, string user)
2726 res = twCon.CreateListMembers(listId, user, ref content);
2730 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2733 this.CheckStatusCode(res, content);
2736 public void RemoveUserToList(string listId, string user)
2743 res = twCon.DeleteListMembers(listId, user, ref content);
2747 throw new WebApiException("Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", ex);
2750 this.CheckStatusCode(res, content);
2755 public int fromIndex { get; set; }
2756 public int toIndex { get; set; }
2757 public range(int fromIndex, int toIndex)
2759 this.fromIndex = fromIndex;
2760 this.toIndex = toIndex;
2763 public async Task<string> CreateHtmlAnchorAsync(string Text, List<string> AtList, Dictionary<string, string> media)
2765 if (Text == null) return null;
2766 var retStr = Text.Replace(">", "<<<<<tweenだいなり>>>>>").Replace("<", "<<<<<tweenしょうなり>>>>>");
2768 //const string url_valid_domain = "(?<domain>(?:[^\p{P}\s][\.\-_](?=[^\p{P}\s])|[^\p{P}\s]){1,}\.[a-z]{2,}(?::[0-9]+)?)"
2769 //const string url_valid_general_path_chars = "[a-z0-9!*';:=+$/%#\[\]\-_,~]"
2770 //const string url_balance_parens = "(?:\(" + url_valid_general_path_chars + "+\))"
2771 //const string url_valid_url_path_ending_chars = "(?:[a-z0-9=_#/\-\+]+|" + url_balance_parens + ")"
2772 //const string pth = "(?:" + url_balance_parens +
2773 // "|@" + url_valid_general_path_chars + "+/" +
2774 // "|[.,]?" + url_valid_general_path_chars + "+" +
2776 //const string pth2 = "(/(?:" +
2777 // pth + "+" + url_valid_url_path_ending_chars + "|" +
2778 // pth + "+" + url_valid_url_path_ending_chars + "?|" +
2779 // url_valid_url_path_ending_chars +
2781 //const string qry = "(?<query>\?[a-z0-9!*'();:&=+$/%#\[\]\-_.,~]*[a-z0-9_&=#])?"
2782 //const string rgUrl = "(?<before>(?:[^\""':!=#]|^|\:/))" +
2783 // "(?<url>(?<protocol>https?://)" +
2784 // url_valid_domain +
2788 //const string rgUrl = "(?<before>(?:[^\""':!=#]|^|\:/))" +
2789 // "(?<url>(?<protocol>https?://|www\.)" +
2790 // url_valid_domain +
2795 retStr = await new Regex(rgUrl, RegexOptions.IgnoreCase).ReplaceAsync(retStr, async mu =>
2797 var sb = new StringBuilder(mu.Result("${before}<a href=\""));
2798 //if (mu.Result("${protocol}").StartsWith("w", StringComparison.OrdinalIgnoreCase))
2799 // sb.Append("http://");
2801 var url = mu.Result("${url}");
2802 var title = await ShortUrl.Instance.ExpandUrlAsync(url);
2803 sb.Append(url + "\" title=\"" + MyCommon.ConvertToReadableUrl(title) + "\">").Append(url).Append("</a>");
2804 if (media != null && !media.ContainsKey(url)) media.Add(url, title);
2805 return sb.ToString();
2809 retStr = Regex.Replace(retStr,
2810 @"(^|[^a-zA-Z0-9_/])([@@]+)([a-zA-Z0-9_]{1,20}/[a-zA-Z][a-zA-Z0-9\p{IsLatin-1Supplement}\-]{0,79})",
2811 "$1$2<a href=\"/$3\">$3</a>");
2813 var m = Regex.Match(retStr, "(^|[^a-zA-Z0-9_])[@@]([a-zA-Z0-9_]{1,20})");
2816 if (!AtList.Contains(m.Result("$2").ToLower())) AtList.Add(m.Result("$2").ToLower());
2820 retStr = Regex.Replace(retStr,
2821 "(^|[^a-zA-Z0-9_/])([@@])([a-zA-Z0-9_]{1,20})",
2822 "$1$2<a href=\"/$3\">$3</a>");
2825 var anchorRange = new List<range>();
2826 for (int i = 0; i < retStr.Length; i++)
2828 var index = retStr.IndexOf("<a ", i);
2829 if (index > -1 && index < retStr.Length)
2832 var toIndex = retStr.IndexOf("</a>", index);
2835 anchorRange.Add(new range(index, toIndex + 3));
2840 //retStr = Regex.Replace(retStr,
2841 // "(^|[^a-zA-Z0-9/&])([##])([0-9a-zA-Z_]*[a-zA-Z_]+[a-zA-Z0-9_\xc0-\xd6\xd8-\xf6\xf8-\xff]*)",
2842 // new MatchEvaluator(Function(mh As Match)
2843 // foreach (var rng in anchorRange)
2845 // if (mh.Index >= rng.fromIndex &&
2846 // mh.Index <= rng.toIndex) return mh.Result("$0");
2848 // if (IsNumeric(mh.Result("$3"))) return mh.Result("$0");
2851 // _hashList.Add("#" + mh.Result("$3"))
2853 // return mh.Result("$1") + "<a href=\"" + _protocol + "twitter.com/search?q=%23" + mh.Result("$3") + "\">" + mh.Result("$2$3") + "</a>";
2855 // RegexOptions.IgnoreCase)
2856 retStr = Regex.Replace(retStr,
2858 new MatchEvaluator(mh =>
2860 foreach (var rng in anchorRange)
2862 if (mh.Index >= rng.fromIndex &&
2863 mh.Index <= rng.toIndex) return mh.Result("$0");
2867 _hashList.Add("#" + mh.Result("$3"));
2869 return mh.Result("$1") + "<a href=\"https://twitter.com/search?q=%23" + mh.Result("$3") + "\">" + mh.Result("$2$3") + "</a>";
2871 RegexOptions.IgnoreCase);
2874 retStr = Regex.Replace(retStr, "(^|[^a-zA-Z0-9_/&##@@>=.~])(sm|nm)([0-9]{1,10})", "$1<a href=\"http://www.nicovideo.jp/watch/$2$3\">$2$3</a>");
2876 retStr = retStr.Replace("<<<<<tweenだいなり>>>>>", ">").Replace("<<<<<tweenしょうなり>>>>>", "<");
2878 //retStr = AdjustHtml(ShortUrl.Resolve(PreProcessUrl(retStr), true)) //IDN置換、短縮Uri解決、@リンクを相対→絶対にしてtarget属性付与
2879 retStr = AdjustHtml(PreProcessUrl(retStr)); //IDN置換、短縮Uri解決、@リンクを相対→絶対にしてtarget属性付与
2883 public async Task<string> CreateHtmlAnchorAsync(string text, List<string> AtList, TwitterEntities entities, List<MediaInfo> media)
2885 if (entities != null)
2887 if (entities.Urls != null)
2889 foreach (var ent in entities.Urls)
2891 ent.ExpandedUrl = await ShortUrl.Instance.ExpandUrlAsync(ent.ExpandedUrl)
2892 .ConfigureAwait(false);
2894 if (media != null && !media.Any(info => info.Url == ent.ExpandedUrl))
2895 media.Add(new MediaInfo(ent.ExpandedUrl));
2898 if (entities.Hashtags != null)
2902 this._hashList.AddRange(entities.Hashtags.Select(x => "#" + x.Text));
2905 if (entities.UserMentions != null)
2907 foreach (var ent in entities.UserMentions)
2909 var screenName = ent.ScreenName.ToLower();
2910 if (!AtList.Contains(screenName))
2911 AtList.Add(screenName);
2914 if (entities.Media != null)
2918 foreach (var ent in entities.Media)
2920 if (!media.Any(x => x.Url == ent.MediaUrl))
2922 if (ent.VideoInfo != null &&
2923 ent.Type == "animated_gif" || ent.Type == "video")
2925 //var videoUrl = ent.VideoInfo.Variants
2926 // .Where(v => v.ContentType == "video/mp4")
2927 // .OrderByDescending(v => v.Bitrate)
2928 // .Select(v => v.Url).FirstOrDefault();
2929 media.Add(new MediaInfo(ent.MediaUrl, ent.ExpandedUrl));
2932 media.Add(new MediaInfo(ent.MediaUrl));
2939 text = TweetFormatter.AutoLinkHtml(text, entities);
2941 text = Regex.Replace(text, "(^|[^a-zA-Z0-9_/&##@@>=.~])(sm|nm)([0-9]{1,10})", "$1<a href=\"http://www.nicovideo.jp/watch/$2$3\">$2$3</a>");
2942 text = PreProcessUrl(text); //IDN置換
2948 public string CreateHtmlAnchor(string text, List<string> AtList, TwitterEntities entities, List<MediaInfo> media)
2950 return this.CreateHtmlAnchorAsync(text, AtList, entities, media).Result;
2954 /// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
2956 public static Tuple<string, Uri> ParseSource(string sourceHtml)
2958 if (string.IsNullOrEmpty(sourceHtml))
2959 return Tuple.Create<string, Uri>("", null);
2964 // sourceHtmlの例: <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>
2966 var match = Regex.Match(sourceHtml, "^<a href=\"(?<uri>.+?)\".*?>(?<text>.+)</a>$", RegexOptions.IgnoreCase);
2969 sourceText = WebUtility.HtmlDecode(match.Groups["text"].Value);
2972 var uriStr = WebUtility.HtmlDecode(match.Groups["uri"].Value);
2973 sourceUri = new Uri(new Uri("https://twitter.com/"), uriStr);
2975 catch (UriFormatException)
2982 sourceText = WebUtility.HtmlDecode(sourceHtml);
2986 return Tuple.Create(sourceText, sourceUri);
2989 public TwitterApiStatus GetInfoApi()
2991 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
2993 if (MyCommon._endingFlag) return null;
2999 res = twCon.RateLimitStatus(ref content);
3003 this.ResetApiStatus();
3007 this.CheckStatusCode(res, content);
3011 MyCommon.TwitterApiInfo.UpdateFromJson(content);
3012 return MyCommon.TwitterApiInfo;
3014 catch (Exception ex)
3016 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
3017 MyCommon.TwitterApiInfo.Reset();
3023 /// ブロック中のユーザーを更新します
3025 /// <exception cref="WebApiException"/>
3026 public void RefreshBlockIds()
3028 if (MyCommon._endingFlag) return;
3031 var newBlockIds = new HashSet<long>();
3034 var ret = this.GetBlockIdsApi(cursor);
3035 newBlockIds.UnionWith(ret.Ids);
3036 cursor = ret.NextCursor;
3037 } while (cursor != 0);
3039 newBlockIds.Remove(this.UserId); // 元のソースにあったので一応残しておく
3041 TabInformations.GetInstance().BlockIds = newBlockIds;
3044 public TwitterIds GetBlockIdsApi(long cursor)
3046 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
3047 throw new WebApiException("AccountState invalid");
3053 res = twCon.GetBlockUserIds(ref content, cursor);
3057 throw new WebApiException("Err:" + e.Message + "(" + MethodBase.GetCurrentMethod().Name + ")", e);
3060 this.CheckStatusCode(res, content);
3064 return TwitterIds.ParseJson(content);
3066 catch(SerializationException e)
3068 var ex = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, e);
3069 MyCommon.TraceOut(ex);
3074 var ex = new WebApiException("Err:Invalid Json!", content, e);
3075 MyCommon.TraceOut(ex);
3081 /// ミュート中のユーザーIDを更新します
3083 /// <exception cref="WebApiException"/>
3084 public async Task RefreshMuteUserIdsAsync()
3086 if (MyCommon._endingFlag) return;
3088 var ids = await TwitterIds.GetAllItemsAsync(this.GetMuteUserIdsApiAsync)
3089 .ConfigureAwait(false);
3091 TabInformations.GetInstance().MuteUserIds = new HashSet<long>(ids);
3094 public async Task<TwitterIds> GetMuteUserIdsApiAsync(long cursor)
3100 var res = await Task.Run(() => twCon.GetMuteUserIds(ref content, cursor))
3101 .ConfigureAwait(false);
3103 this.CheckStatusCode(res, content);
3105 return TwitterIds.ParseJson(content);
3107 catch (WebApiException ex)
3109 MyCommon.TraceOut(ex);
3112 catch (SerializationException ex)
3114 var ex2 = new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
3115 MyCommon.TraceOut(ex2);
3120 public string[] GetHashList()
3125 hashArray = _hashList.ToArray();
3131 public string AccessToken
3135 return twCon.AccessToken;
3139 public string AccessTokenSecret
3143 return twCon.AccessTokenSecret;
3147 private void CheckStatusCode(HttpStatusCode httpStatus, string responseText,
3148 [CallerMemberName] string callerMethodName = "")
3150 if (httpStatus == HttpStatusCode.OK)
3152 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3156 if (string.IsNullOrWhiteSpace(responseText))
3158 if (httpStatus == HttpStatusCode.Unauthorized)
3159 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3161 throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")");
3166 var errors = TwitterError.ParseJson(responseText).Errors;
3167 if (errors == null || !errors.Any())
3169 throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")", responseText);
3172 foreach (var error in errors)
3174 if (error.Code == TwitterErrorCode.InvalidToken ||
3175 error.Code == TwitterErrorCode.SuspendedAccount)
3177 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3181 throw new WebApiException("Err:" + string.Join(",", errors.Select(x => x.ToString())) + "(" + callerMethodName + ")", responseText);
3183 catch (SerializationException) { }
3185 throw new WebApiException("Err:" + httpStatus + "(" + callerMethodName + ")", responseText);
3188 #region "UserStream"
3189 private string trackWord_ = "";
3190 public string TrackWord
3201 private bool allAtReply_ = false;
3202 public bool AllAtReply
3210 allAtReply_ = value;
3214 public event EventHandler NewPostFromStream;
3215 public event EventHandler UserStreamStarted;
3216 public event EventHandler UserStreamStopped;
3217 public event EventHandler<PostDeletedEventArgs> PostDeleted;
3218 public event EventHandler<UserStreamEventReceivedEventArgs> UserStreamEventReceived;
3219 private DateTime _lastUserstreamDataReceived;
3220 private TwitterUserstream userStream;
3222 public class FormattedEvent
3224 public MyCommon.EVENTTYPE Eventtype { get; set; }
3225 public DateTime CreatedAt { get; set; }
3226 public string Event { get; set; }
3227 public string Username { get; set; }
3228 public string Target { get; set; }
3229 public Int64 Id { get; set; }
3230 public bool IsMe { get; set; }
3233 public List<FormattedEvent> storedEvent_ = new List<FormattedEvent>();
3234 public List<FormattedEvent> StoredEvent
3238 return storedEvent_;
3242 storedEvent_ = value;
3246 private readonly IReadOnlyDictionary<string, MyCommon.EVENTTYPE> eventTable = new Dictionary<string, MyCommon.EVENTTYPE>
3248 ["favorite"] = MyCommon.EVENTTYPE.Favorite,
3249 ["unfavorite"] = MyCommon.EVENTTYPE.Unfavorite,
3250 ["follow"] = MyCommon.EVENTTYPE.Follow,
3251 ["list_member_added"] = MyCommon.EVENTTYPE.ListMemberAdded,
3252 ["list_member_removed"] = MyCommon.EVENTTYPE.ListMemberRemoved,
3253 ["block"] = MyCommon.EVENTTYPE.Block,
3254 ["unblock"] = MyCommon.EVENTTYPE.Unblock,
3255 ["user_update"] = MyCommon.EVENTTYPE.UserUpdate,
3256 ["deleted"] = MyCommon.EVENTTYPE.Deleted,
3257 ["list_created"] = MyCommon.EVENTTYPE.ListCreated,
3258 ["list_destroyed"] = MyCommon.EVENTTYPE.ListDestroyed,
3259 ["list_updated"] = MyCommon.EVENTTYPE.ListUpdated,
3260 ["unfollow"] = MyCommon.EVENTTYPE.Unfollow,
3261 ["list_user_subscribed"] = MyCommon.EVENTTYPE.ListUserSubscribed,
3262 ["list_user_unsubscribed"] = MyCommon.EVENTTYPE.ListUserUnsubscribed,
3263 ["mute"] = MyCommon.EVENTTYPE.Mute,
3264 ["unmute"] = MyCommon.EVENTTYPE.Unmute,
3265 ["quoted_tweet"] = MyCommon.EVENTTYPE.QuotedTweet,
3268 public bool IsUserstreamDataReceived
3272 return DateTime.Now.Subtract(this._lastUserstreamDataReceived).TotalSeconds < 31;
3276 private void userStream_StatusArrived(string line)
3278 this._lastUserstreamDataReceived = DateTime.Now;
3279 if (string.IsNullOrEmpty(line)) return;
3281 if (line.First() != '{' || line.Last() != '}')
3283 MyCommon.TraceOut("Invalid JSON (StatusArrived):" + Environment.NewLine + line);
3291 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(line), XmlDictionaryReaderQuotas.Max))
3293 var xElm = XElement.Load(jsonReader);
3294 if (xElm.Element("friends") != null)
3296 Debug.WriteLine("friends");
3299 else if (xElm.Element("delete") != null)
3301 Debug.WriteLine("delete");
3303 if (xElm.Element("delete").Element("direct_message") != null &&
3304 xElm.Element("delete").Element("direct_message").Element("id") != null)
3307 long.TryParse(xElm.Element("delete").Element("direct_message").Element("id").Value, out id);
3309 if (this.PostDeleted != null)
3310 this.PostDeleted(this, new PostDeletedEventArgs(id));
3312 else if (xElm.Element("delete").Element("status") != null &&
3313 xElm.Element("delete").Element("status").Element("id") != null)
3316 long.TryParse(xElm.Element("delete").Element("status").Element("id").Value, out id);
3318 if (this.PostDeleted != null)
3319 this.PostDeleted(this, new PostDeletedEventArgs(id));
3323 MyCommon.TraceOut("delete:" + line);
3326 for (int i = this.StoredEvent.Count - 1; i >= 0; i--)
3328 var sEvt = this.StoredEvent[i];
3329 if (sEvt.Id == id && (sEvt.Event == "favorite" || sEvt.Event == "unfavorite"))
3331 this.StoredEvent.RemoveAt(i);
3336 else if (xElm.Element("limit") != null)
3338 Debug.WriteLine(line);
3341 else if (xElm.Element("event") != null)
3343 Debug.WriteLine("event: " + xElm.Element("event").Value);
3344 CreateEventFromJson(line);
3347 else if (xElm.Element("direct_message") != null)
3349 Debug.WriteLine("direct_message");
3352 else if (xElm.Element("retweeted_status") != null)
3354 var sourceUserId = xElm.XPathSelectElement("/user/id_str").Value;
3355 var targetUserId = xElm.XPathSelectElement("/retweeted_status/user/id_str").Value;
3357 // 自分に関係しないリツイートの場合は無視する
3358 var selfUserId = this.UserId.ToString();
3359 if (sourceUserId == selfUserId || targetUserId == selfUserId)
3361 // 公式 RT をイベントとしても扱う
3362 var evt = CreateEventFromRetweet(xElm);
3365 this.StoredEvent.Insert(0, evt);
3367 if (this.UserStreamEventReceived != null)
3368 this.UserStreamEventReceived(this, new UserStreamEventReceivedEventArgs(evt));
3372 // 従来通り公式 RT の表示も行うため return しない
3374 else if (xElm.Element("scrub_geo") != null)
3378 TabInformations.GetInstance().ScrubGeoReserve(long.Parse(xElm.Element("scrub_geo").Element("user_id").Value),
3379 long.Parse(xElm.Element("scrub_geo").Element("up_to_status_id").Value));
3383 MyCommon.TraceOut("scrub_geo:" + line);
3391 CreateDirectMessagesFromJson(line, MyCommon.WORKERTYPE.UserStream, false);
3395 CreatePostsFromJson("[" + line + "]", MyCommon.WORKERTYPE.Timeline, null, false);
3398 catch(NullReferenceException)
3400 MyCommon.TraceOut("NullRef StatusArrived: " + line);
3403 if (this.NewPostFromStream != null)
3404 this.NewPostFromStream(this, EventArgs.Empty);
3408 /// UserStreamsから受信した公式RTをイベントに変換します
3410 private FormattedEvent CreateEventFromRetweet(XElement xElm)
3412 return new FormattedEvent
3414 Eventtype = MyCommon.EVENTTYPE.Retweet,
3416 CreatedAt = MyCommon.DateTimeParse(xElm.XPathSelectElement("/created_at").Value),
3417 IsMe = xElm.XPathSelectElement("/user/id_str").Value == this.UserId.ToString(),
3418 Username = xElm.XPathSelectElement("/user/screen_name").Value,
3419 Target = string.Format("@{0}: {1}", new[]
3421 xElm.XPathSelectElement("/retweeted_status/user/screen_name").Value,
3422 xElm.XPathSelectElement("/retweeted_status/text").Value,
3424 Id = long.Parse(xElm.XPathSelectElement("/retweeted_status/id_str").Value),
3428 private void CreateEventFromJson(string content)
3430 TwitterStreamEvent eventData = null;
3433 eventData = TwitterStreamEvent.ParseJson(content);
3435 catch(SerializationException ex)
3437 MyCommon.TraceOut(ex, "Event Serialize Exception!" + Environment.NewLine + content);
3441 MyCommon.TraceOut(ex, "Event Exception!" + Environment.NewLine + content);
3444 var evt = new FormattedEvent();
3445 evt.CreatedAt = MyCommon.DateTimeParse(eventData.CreatedAt);
3446 evt.Event = eventData.Event;
3447 evt.Username = eventData.Source.ScreenName;
3448 evt.IsMe = evt.Username.ToLower().Equals(this.Username.ToLower());
3450 MyCommon.EVENTTYPE eventType;
3451 eventTable.TryGetValue(eventData.Event, out eventType);
3452 evt.Eventtype = eventType;
3454 TwitterStreamEvent<TwitterStatus> tweetEvent;
3456 switch (eventData.Event)
3458 case "access_revoked":
3459 case "access_unrevoked":
3461 case "user_suspend":
3464 if (eventData.Target.ScreenName.ToLower().Equals(_uname))
3466 if (!this.followerId.Contains(eventData.Source.Id)) this.followerId.Add(eventData.Source.Id);
3470 return; //Block後のUndoをすると、SourceとTargetが逆転したfollowイベントが帰ってくるため。
3475 evt.Target = "@" + eventData.Target.ScreenName;
3477 case "favorited_retweet":
3478 case "retweeted_retweet":
3482 tweetEvent = TwitterStreamEvent<TwitterStatus>.ParseJson(content);
3483 evt.Target = "@" + tweetEvent.TargetObject.User.ScreenName + ":" + WebUtility.HtmlDecode(tweetEvent.TargetObject.Text);
3484 evt.Id = tweetEvent.TargetObject.Id;
3486 if (SettingCommon.Instance.IsRemoveSameEvent)
3488 if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
3492 var tabinfo = TabInformations.GetInstance();
3495 var statusId = tweetEvent.TargetObject.Id;
3496 if (!tabinfo.Posts.TryGetValue(statusId, out post))
3499 if (eventData.Event == "favorite")
3501 var favTab = tabinfo.GetTabByType(MyCommon.TabUsageType.Favorites);
3502 if (!favTab.Contains(post.StatusId))
3503 favTab.Add(post.StatusId, post.IsRead, false);
3505 if (tweetEvent.Source.Id == this.UserId)
3509 else if (tweetEvent.Target.Id == this.UserId)
3511 post.FavoritedCount++;
3513 if (SettingCommon.Instance.FavEventUnread)
3514 tabinfo.SetReadAllTab(post.StatusId, read: false);
3519 if (tweetEvent.Source.Id == this.UserId)
3523 else if (tweetEvent.Target.Id == this.UserId)
3525 post.FavoritedCount = Math.Max(0, post.FavoritedCount - 1);
3529 case "quoted_tweet":
3530 if (evt.IsMe) return;
3532 tweetEvent = TwitterStreamEvent<TwitterStatus>.ParseJson(content);
3533 evt.Target = "@" + tweetEvent.TargetObject.User.ScreenName + ":" + WebUtility.HtmlDecode(tweetEvent.TargetObject.Text);
3534 evt.Id = tweetEvent.TargetObject.Id;
3536 if (SettingCommon.Instance.IsRemoveSameEvent)
3538 if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
3542 case "list_member_added":
3543 case "list_member_removed":
3544 case "list_created":
3545 case "list_destroyed":
3546 case "list_updated":
3547 case "list_user_subscribed":
3548 case "list_user_unsubscribed":
3549 var listEvent = TwitterStreamEvent<TwitterList>.ParseJson(content);
3550 evt.Target = listEvent.TargetObject.FullName;
3553 if (!TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Add(eventData.Target.Id);
3557 if (TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Remove(eventData.Target.Id);
3566 evt.Target = "@" + eventData.Target.ScreenName;
3567 if (!TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
3569 TabInformations.GetInstance().MuteUserIds.Add(eventData.Target.Id);
3573 evt.Target = "@" + eventData.Target.ScreenName;
3574 if (TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
3576 TabInformations.GetInstance().MuteUserIds.Remove(eventData.Target.Id);
3581 MyCommon.TraceOut("Unknown Event:" + evt.Event + Environment.NewLine + content);
3584 this.StoredEvent.Insert(0, evt);
3586 if (this.UserStreamEventReceived != null)
3587 this.UserStreamEventReceived(this, new UserStreamEventReceivedEventArgs(evt));
3590 private void userStream_Started()
3592 if (this.UserStreamStarted != null)
3593 this.UserStreamStarted(this, EventArgs.Empty);
3596 private void userStream_Stopped()
3598 if (this.UserStreamStopped != null)
3599 this.UserStreamStopped(this, EventArgs.Empty);
3602 public bool UserStreamEnabled
3606 return userStream == null ? false : userStream.Enabled;
3610 public void StartUserStream()
3612 if (userStream != null)
3616 userStream = new TwitterUserstream(twCon);
3617 userStream.StatusArrived += userStream_StatusArrived;
3618 userStream.Started += userStream_Started;
3619 userStream.Stopped += userStream_Stopped;
3620 userStream.Start(this.AllAtReply, this.TrackWord);
3623 public void StopUserStream()
3625 if (userStream != null) userStream.Dispose();
3627 if (!MyCommon._endingFlag)
3629 if (this.UserStreamStopped != null)
3630 this.UserStreamStopped(this, EventArgs.Empty);
3634 public void ReconnectUserStream()
3636 if (userStream != null)
3638 this.StartUserStream();
3642 private class TwitterUserstream : IDisposable
3644 public event Action<string> StatusArrived;
3645 public event Action Stopped;
3646 public event Action Started;
3647 private HttpTwitter twCon;
3649 private Thread _streamThread;
3650 private bool _streamActive;
3652 private bool _allAtreplies = false;
3653 private string _trackwords = "";
3655 public TwitterUserstream(HttpTwitter twitterConnection)
3657 twCon = (HttpTwitter)twitterConnection.Clone();
3660 public void Start(bool allAtReplies, string trackwords)
3662 this.AllAtReplies = allAtReplies;
3663 this.TrackWords = trackwords;
3664 _streamActive = true;
3665 if (_streamThread != null && _streamThread.IsAlive) return;
3666 _streamThread = new Thread(UserStreamLoop);
3667 _streamThread.Name = "UserStreamReceiver";
3668 _streamThread.IsBackground = true;
3669 _streamThread.Start();
3676 return _streamActive;
3680 public bool AllAtReplies
3684 return _allAtreplies;
3688 _allAtreplies = value;
3692 public string TrackWords
3700 _trackwords = value;
3704 private void UserStreamLoop()
3710 StreamReader sr = null;
3713 if (!MyCommon.IsNetworkAvailable())
3719 if (Started != null)
3723 var res = twCon.UserStream(ref st, _allAtreplies, _trackwords, Networking.GetUserAgentString());
3727 case HttpStatusCode.OK:
3728 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3730 case HttpStatusCode.Unauthorized:
3731 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3739 //MyCommon.TraceOut("Stop:stream is null")
3743 sr = new StreamReader(st);
3745 while (_streamActive && !sr.EndOfStream && Twitter.AccountState == MyCommon.ACCOUNT_STATE.Valid)
3747 if (StatusArrived != null)
3749 StatusArrived(sr.ReadLine());
3751 //this.LastTime = Now;
3754 if (sr.EndOfStream || Twitter.AccountState == MyCommon.ACCOUNT_STATE.Invalid)
3757 //MyCommon.TraceOut("Stop:EndOfStream")
3762 catch(WebException ex)
3764 if (ex.Status == WebExceptionStatus.Timeout)
3766 sleepSec = 30; //MyCommon.TraceOut("Stop:Timeout")
3768 else if (ex.Response != null && (int)((HttpWebResponse)ex.Response).StatusCode == 420)
3770 //MyCommon.TraceOut("Stop:Connection Limit")
3776 //MyCommon.TraceOut("Stop:WebException " + ex.Status.ToString())
3779 catch(ThreadAbortException)
3786 //MyCommon.TraceOut("Stop:IOException with Active." + Environment.NewLine + ex.Message)
3788 catch(ArgumentException ex)
3790 //System.ArgumentException: ストリームを読み取れませんでした。
3791 //サーバー側もしくは通信経路上で切断された場合?タイムアウト頻発後発生
3793 MyCommon.TraceOut(ex, "Stop:ArgumentException");
3797 MyCommon.TraceOut("Stop:Exception." + Environment.NewLine + ex.Message);
3798 MyCommon.ExceptionOut(ex);
3805 if (Stopped != null)
3810 twCon.RequestAbort();
3811 if (sr != null) sr.Close();
3815 while (_streamActive && ms < sleepSec * 1000)
3823 } while (this._streamActive);
3827 if (Stopped != null)
3832 MyCommon.TraceOut("Stop:EndLoop");
3835 #region "IDisposable Support"
3836 private bool disposedValue; // 重複する呼び出しを検出するには
3839 protected virtual void Dispose(bool disposing)
3841 if (!this.disposedValue)
3845 _streamActive = false;
3846 if (_streamThread != null && _streamThread.IsAlive)
3848 _streamThread.Abort();
3852 this.disposedValue = true;
3855 //protected Overrides void Finalize()
3857 // // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3859 // MyBase.Finalize()
3862 // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
3863 public void Dispose()
3865 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3867 GC.SuppressFinalize(this);
3874 #region "IDisposable Support"
3875 private bool disposedValue; // 重複する呼び出しを検出するには
3878 protected virtual void Dispose(bool disposing)
3880 if (!this.disposedValue)
3884 this.StopUserStream();
3887 this.disposedValue = true;
3890 //protected Overrides void Finalize()
3892 // // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3894 // MyBase.Finalize()
3897 // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
3898 public void Dispose()
3900 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
3902 GC.SuppressFinalize(this);
3907 public class PostDeletedEventArgs : EventArgs
3909 public long StatusId { get; }
3911 public PostDeletedEventArgs(long statusId)
3913 this.StatusId = statusId;
3917 public class UserStreamEventReceivedEventArgs : EventArgs
3919 public Twitter.FormattedEvent EventData { get; }
3921 public UserStreamEventReceivedEventArgs(Twitter.FormattedEvent eventData)
3923 this.EventData = eventData;