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.Net.Http;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.Serialization;
35 using System.Runtime.Serialization.Json;
37 using System.Text.RegularExpressions;
38 using System.Threading;
39 using System.Threading.Tasks;
42 using System.Xml.Linq;
43 using System.Xml.XPath;
45 using System.Reflection;
46 using System.Collections.Generic;
48 using System.Windows.Forms;
50 using OpenTween.Api.DataModel;
51 using OpenTween.Connection;
52 using OpenTween.Models;
53 using System.Drawing.Imaging;
54 using OpenTween.Setting;
58 public class Twitter : IDisposable
60 #region Regexp from twitter-text-js
62 // The code in this region code block incorporates works covered by
63 // the following copyright and permission notices:
65 // Copyright 2011 Twitter, Inc.
67 // Licensed under the Apache License, Version 2.0 (the "License"); you
68 // may not use this work except in compliance with the License. You
69 // may obtain a copy of the License in the LICENSE file, or at:
71 // http://www.apache.org/licenses/LICENSE-2.0
73 // Unless required by applicable law or agreed to in writing, software
74 // distributed under the License is distributed on an "AS IS" BASIS,
75 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
76 // implied. See the License for the specific language governing
77 // permissions and limitations under the License.
80 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";
81 private const string NON_LATIN_HASHTAG_CHARS = @"\u0400-\u04ff\u0500-\u0527\u1100-\u11ff\u3130-\u3185\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF";
82 //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";
83 private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\u30FC\u3005\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u309A\u3400-\u4DBF\p{IsCJKUnifiedIdeographs}";
84 private const string HASHTAG_BOUNDARY = @"^|$|\s|「|」|。|\.|!";
85 private const string HASHTAG_ALPHA = "[a-z_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
86 private const string HASHTAG_ALPHANUMERIC = "[a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
87 private const string HASHTAG_TERMINATOR = "[^a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
88 public const string HASHTAG = "(" + HASHTAG_BOUNDARY + ")(#|#)(" + HASHTAG_ALPHANUMERIC + "*" + HASHTAG_ALPHA + HASHTAG_ALPHANUMERIC + "*)(?=" + HASHTAG_TERMINATOR + "|" + HASHTAG_BOUNDARY + ")";
90 private const string url_valid_preceding_chars = @"(?:[^A-Za-z0-9@@$##\ufffe\ufeff\uffff\u202a-\u202e]|^)";
91 public const string url_invalid_without_protocol_preceding_chars = @"[-_./]$";
92 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";
93 private const string url_valid_domain_chars = @"[^" + url_invalid_domain_chars + "]";
94 private const string url_valid_subdomain = @"(?:(?:" + url_valid_domain_chars + @"(?:[_-]|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
95 private const string url_valid_domain_name = @"(?:(?:" + url_valid_domain_chars + @"(?:-|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
96 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]|$))";
97 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]|$))";
98 private const string url_valid_punycode = @"(?:xn--[0-9a-z]+)";
99 private const string url_valid_domain = @"(?<domain>" + url_valid_subdomain + "*" + url_valid_domain_name + "(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + ")|" + url_valid_punycode + ")";
100 public const string url_valid_ascii_domain = @"(?:(?:[a-z0-9" + LATIN_ACCENTS + @"]+)\.)+(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + "|" + url_valid_punycode + ")";
101 public const string url_invalid_short_domain = "^" + url_valid_domain_name + url_valid_CCTLD + "$";
102 private const string url_valid_port_number = @"[0-9]+";
104 private const string url_valid_general_path_chars = @"[a-z0-9!*';:=+,.$/%#\[\]\-_~|&" + LATIN_ACCENTS + "]";
105 private const string url_balance_parens = @"(?:\(" + url_valid_general_path_chars + @"+\))";
106 private const string url_valid_path_ending_chars = @"(?:[+\-a-z0-9=_#/" + LATIN_ACCENTS + "]|" + url_balance_parens + ")";
107 private const string pth = "(?:" +
109 url_valid_general_path_chars + "*" +
110 "(?:" + url_balance_parens + url_valid_general_path_chars + "*)*" +
111 url_valid_path_ending_chars +
112 ")|(?:@" + url_valid_general_path_chars + "+/)" +
114 private const string qry = @"(?<query>\?[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|]*[a-z0-9_&=#/])?";
115 public const string rgUrl = @"(?<before>" + url_valid_preceding_chars + ")" +
116 "(?<url>(?<protocol>https?://)?" +
117 "(?<domain>" + url_valid_domain + ")" +
118 "(?::" + url_valid_port_number + ")?" +
119 "(?<path>/" + pth + "*)?" +
126 /// Twitter API のステータスページのURL
128 public const string ServiceAvailabilityStatusUrl = "https://status.io.watchmouse.com/7617";
131 /// ツイートへのパーマリンクURLを判定する正規表現
133 public static readonly Regex StatusUrlRegex = new Regex(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
136 /// attachment_url に指定可能な URL を判定する正規表現
138 public static readonly Regex AttachmentUrlRegex = new Regex(@"https?://(
139 twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
140 | mobile\.twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
141 | twitter\.com/messages/compose\?recipient_id=[0-9]+(&.+)?
142 )$", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
145 /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
147 public static readonly Regex ThirdPartyStatusUrlRegex = new Regex(@"https?://(?:[^.]+\.)?(?:
148 favstar\.fm/users/[a-zA-Z0-9_]+/status/ # Favstar
149 | favstar\.fm/t/ # Favstar (short)
150 | aclog\.koba789\.com/i/ # aclog
151 | frtrt\.net/solo_status\.php\?status= # RtRT
152 )(?<StatusId>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
155 /// DM送信かどうかを判定する正規表現
157 public static readonly Regex DMSendTextRegex = new Regex(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
159 public TwitterApi Api { get; }
160 public TwitterConfiguration Configuration { get; private set; }
161 public TwitterTextConfiguration TextConfiguration { get; private set; }
163 public bool GetFollowersSuccess { get; private set; } = false;
164 public bool GetNoRetweetSuccess { get; private set; } = false;
166 delegate void GetIconImageDelegate(PostClass post);
167 private readonly object LockObj = new object();
168 private ISet<long> followerId = new HashSet<long>();
169 private long[] noRTId = new long[0];
172 private List<string> _hashList = new List<string>();
174 //max_idで古い発言を取得するために保持(lists分は個別タブで管理)
175 private long minDirectmessage = long.MaxValue;
176 private long minDirectmessageSent = long.MaxValue;
178 private long previousStatusId = -1L;
180 //private FavoriteQueue favQueue;
182 //private List<PostClass> _deletemessages = new List<PostClass>();
184 public Twitter() : this(new TwitterApi())
188 public Twitter(TwitterApi api)
191 this.Configuration = TwitterConfiguration.DefaultConfiguration();
192 this.TextConfiguration = TwitterTextConfiguration.DefaultConfiguration();
195 public TwitterApiAccessLevel AccessLevel
196 => MyCommon.TwitterApiInfo.AccessLevel;
198 protected void ResetApiStatus()
199 => MyCommon.TwitterApiInfo.Reset();
201 public void ClearAuthInfo()
203 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
204 this.ResetApiStatus();
208 public void VerifyCredentials()
212 this.VerifyCredentialsAsync().Wait();
214 catch (AggregateException ex) when (ex.InnerException is WebApiException)
216 throw new WebApiException(ex.InnerException.Message, ex);
220 public async Task VerifyCredentialsAsync()
222 var user = await this.Api.AccountVerifyCredentials()
223 .ConfigureAwait(false);
225 this.UpdateUserStats(user);
228 public void Initialize(string token, string tokenSecret, string username, long userId)
231 if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(tokenSecret) || string.IsNullOrEmpty(username))
233 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
235 this.ResetApiStatus();
236 this.Api.Initialize(token, tokenSecret, userId, username);
237 if (SettingManager.Common.UserstreamStartup) this.ReconnectUserStream();
240 internal static string PreProcessUrl(string orgData)
244 //var IDNConveter = new IdnMapping();
245 var href = "<a href=\"";
249 if (orgData.IndexOf(href, posl2, StringComparison.Ordinal) > -1)
253 posl1 = orgData.IndexOf(href, posl2, StringComparison.Ordinal);
254 posl1 += href.Length;
255 posl2 = orgData.IndexOf("\"", posl1, StringComparison.Ordinal);
256 urlStr = orgData.Substring(posl1, posl2 - posl1);
258 if (!urlStr.StartsWith("http://", StringComparison.Ordinal)
259 && !urlStr.StartsWith("https://", StringComparison.Ordinal)
260 && !urlStr.StartsWith("ftp://", StringComparison.Ordinal))
265 var replacedUrl = MyCommon.IDNEncode(urlStr);
266 if (replacedUrl == null) continue;
267 if (replacedUrl == urlStr) continue;
269 orgData = orgData.Replace("<a href=\"" + urlStr, "<a href=\"" + replacedUrl);
280 public async Task PostStatus(PostStatusParams param)
282 this.CheckAccountState();
284 if (Twitter.DMSendTextRegex.IsMatch(param.Text))
286 await this.SendDirectMessage(param.Text)
287 .ConfigureAwait(false);
291 var response = await this.Api.StatusesUpdate(param.Text, param.InReplyToStatusId, param.MediaIds,
292 param.AutoPopulateReplyMetadata, param.ExcludeReplyUserIds, param.AttachmentUrl)
293 .ConfigureAwait(false);
295 var status = await response.LoadJsonAsync()
296 .ConfigureAwait(false);
298 this.UpdateUserStats(status.User);
300 if (status.Id == this.previousStatusId)
301 throw new WebApiException("OK:Delaying?");
303 this.previousStatusId = status.Id;
306 public Task<long> UploadMedia(IMediaItem item)
307 => this.UploadMedia(item, SettingManager.Common.AlphaPNGWorkaround);
309 public async Task<long> UploadMedia(IMediaItem item, bool alphaPNGWorkaround)
311 this.CheckAccountState();
313 LazyJson<TwitterUploadMediaResult> response;
315 using (var origImage = item.CreateImage())
317 if (alphaPNGWorkaround && this.AddAlphaChannelIfNeeded(origImage.Image, out var newImage))
319 using (var newMediaItem = new MemoryImageMediaItem(newImage))
321 response = await this.Api.MediaUpload(newMediaItem)
322 .ConfigureAwait(false);
327 response = await this.Api.MediaUpload(item)
328 .ConfigureAwait(false);
332 var media = await response.LoadJsonAsync()
333 .ConfigureAwait(false);
335 return media.MediaId;
339 /// pic.twitter.com アップロード時に JPEG への変換を回避するための加工を行う
342 /// pic.twitter.com へのアップロード時に、アルファチャンネルを持たない PNG 画像が
343 /// JPEG 形式に変換され画質が低下する問題を回避します。
344 /// PNG 以外の画像や、すでにアルファチャンネルを持つ PNG 画像に対しては何もしません。
346 /// <returns>加工が行われた場合は true、そうでない場合は false</returns>
347 private bool AddAlphaChannelIfNeeded(Image origImage, out MemoryImage newImage)
351 // PNG 画像以外に対しては何もしない
352 if (origImage.RawFormat.Guid != ImageFormat.Png.Guid)
355 using (var bitmap = new Bitmap(origImage))
357 // アルファ値が 255 以外のピクセルが含まれていた場合は何もしない
358 foreach (var x in Enumerable.Range(0, bitmap.Width))
360 foreach (var y in Enumerable.Range(0, bitmap.Height))
362 if (bitmap.GetPixel(x, y).A != 255)
367 // 左上の 1px だけアルファ値を 254 にする
368 var pixel = bitmap.GetPixel(0, 0);
369 var newPixel = Color.FromArgb(pixel.A - 1, pixel.R, pixel.G, pixel.B);
370 bitmap.SetPixel(0, 0, newPixel);
372 // MemoryImage 作成時に画像はコピーされるため、この後 bitmap は破棄しても問題ない
373 newImage = MemoryImage.CopyFromImage(bitmap);
379 public async Task SendDirectMessage(string postStr)
381 this.CheckAccountState();
382 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
384 var mc = Twitter.DMSendTextRegex.Match(postStr);
386 var response = await this.Api.DirectMessagesNew(mc.Groups["body"].Value, mc.Groups["id"].Value)
387 .ConfigureAwait(false);
389 var dm = await response.LoadJsonAsync()
390 .ConfigureAwait(false);
392 this.UpdateUserStats(dm.Sender);
395 public async Task PostRetweet(long id, bool read)
397 this.CheckAccountState();
400 var post = TabInformations.GetInstance()[id];
402 throw new WebApiException("Err:Target isn't found.");
404 var target = post.RetweetedId ?? id; //再RTの場合は元発言をRT
406 var response = await this.Api.StatusesRetweet(target)
407 .ConfigureAwait(false);
409 var status = await response.LoadJsonAsync()
410 .ConfigureAwait(false);
415 if (TabInformations.GetInstance().ContainsKey(status.Id))
420 if (status.RetweetedStatus == null)
421 throw new WebApiException("Invalid Json!");
424 post = CreatePostsFromStatusData(status);
431 if (this.ReadOwnPost) post.IsRead = true;
434 TabInformations.GetInstance().AddPost(post);
437 public string Username
438 => this.Api.CurrentScreenName;
441 => this.Api.CurrentUserId;
443 public static MyCommon.ACCOUNT_STATE AccountState { get; set; } = MyCommon.ACCOUNT_STATE.Valid;
444 public bool RestrictFavCheck { get; set; }
445 public bool ReadOwnPost { get; set; }
447 public int FollowersCount { get; private set; }
448 public int FriendsCount { get; private set; }
449 public int StatusesCount { get; private set; }
450 public string Location { get; private set; } = "";
451 public string Bio { get; private set; } = "";
453 /// <summary>ユーザーのフォロワー数などの情報を更新します</summary>
454 private void UpdateUserStats(TwitterUser self)
456 this.FollowersCount = self.FollowersCount;
457 this.FriendsCount = self.FriendsCount;
458 this.StatusesCount = self.StatusesCount;
459 this.Location = self.Location;
460 this.Bio = self.Description;
464 /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
466 public static bool VerifyApiResultCount(MyCommon.WORKERTYPE type, int count)
467 => count >= 20 && count <= GetMaxApiResultCount(type);
470 /// 渡された取得件数が更新時の取得可能範囲に収まっているか検証する
472 public static bool VerifyMoreApiResultCount(int count)
473 => count >= 20 && count <= 200;
476 /// 渡された取得件数が起動時の取得可能範囲に収まっているか検証する
478 public static bool VerifyFirstApiResultCount(int count)
479 => count >= 20 && count <= 200;
482 /// WORKERTYPEに応じた取得可能な最大件数を取得する
484 public static int GetMaxApiResultCount(MyCommon.WORKERTYPE type)
486 // 参照: REST APIs - 各endpointのcountパラメータ
487 // https://dev.twitter.com/rest/public
490 case MyCommon.WORKERTYPE.Timeline:
491 case MyCommon.WORKERTYPE.Reply:
492 case MyCommon.WORKERTYPE.UserTimeline:
493 case MyCommon.WORKERTYPE.Favorites:
494 case MyCommon.WORKERTYPE.DirectMessegeRcv:
495 case MyCommon.WORKERTYPE.DirectMessegeSnt:
496 case MyCommon.WORKERTYPE.List: // 不明
499 case MyCommon.WORKERTYPE.PublicSearch:
503 throw new InvalidOperationException("Invalid type: " + type);
508 /// WORKERTYPEに応じた取得件数を取得する
510 public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool startup)
512 if (type == MyCommon.WORKERTYPE.DirectMessegeRcv ||
513 type == MyCommon.WORKERTYPE.DirectMessegeSnt)
518 if (SettingManager.Common.UseAdditionalCount)
522 case MyCommon.WORKERTYPE.Favorites:
523 if (SettingManager.Common.FavoritesCountApi != 0)
524 return SettingManager.Common.FavoritesCountApi;
526 case MyCommon.WORKERTYPE.List:
527 if (SettingManager.Common.ListCountApi != 0)
528 return SettingManager.Common.ListCountApi;
530 case MyCommon.WORKERTYPE.PublicSearch:
531 if (SettingManager.Common.SearchCountApi != 0)
532 return SettingManager.Common.SearchCountApi;
534 case MyCommon.WORKERTYPE.UserTimeline:
535 if (SettingManager.Common.UserTimelineCountApi != 0)
536 return SettingManager.Common.UserTimelineCountApi;
539 if (more && SettingManager.Common.MoreCountApi != 0)
541 return Math.Min(SettingManager.Common.MoreCountApi, GetMaxApiResultCount(type));
543 if (startup && SettingManager.Common.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
545 return Math.Min(SettingManager.Common.FirstCountApi, GetMaxApiResultCount(type));
550 var count = SettingManager.Common.CountApi;
552 if (type == MyCommon.WORKERTYPE.Reply)
553 count = SettingManager.Common.CountApiReply;
555 return Math.Min(count, GetMaxApiResultCount(type));
558 public async Task GetHomeTimelineApi(bool read, HomeTabModel tab, bool more, bool startup)
560 this.CheckAccountState();
562 var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, startup);
564 TwitterStatus[] statuses;
567 statuses = await this.Api.StatusesHomeTimeline(count, maxId: tab.OldestId)
568 .ConfigureAwait(false);
572 statuses = await this.Api.StatusesHomeTimeline(count)
573 .ConfigureAwait(false);
576 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Timeline, tab, read);
577 if (minimumId != null)
578 tab.OldestId = minimumId.Value;
581 public async Task GetMentionsTimelineApi(bool read, MentionsTabModel tab, bool more, bool startup)
583 this.CheckAccountState();
585 var count = GetApiResultCount(MyCommon.WORKERTYPE.Reply, more, startup);
587 TwitterStatus[] statuses;
590 statuses = await this.Api.StatusesMentionsTimeline(count, maxId: tab.OldestId)
591 .ConfigureAwait(false);
595 statuses = await this.Api.StatusesMentionsTimeline(count)
596 .ConfigureAwait(false);
599 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Reply, tab, read);
600 if (minimumId != null)
601 tab.OldestId = minimumId.Value;
604 public async Task GetUserTimelineApi(bool read, string userName, UserTimelineTabModel tab, bool more)
606 this.CheckAccountState();
608 var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
610 TwitterStatus[] statuses;
611 if (string.IsNullOrEmpty(userName))
613 var target = tab.ScreenName;
614 if (string.IsNullOrEmpty(target)) return;
616 statuses = await this.Api.StatusesUserTimeline(userName, count)
617 .ConfigureAwait(false);
623 statuses = await this.Api.StatusesUserTimeline(userName, count, maxId: tab.OldestId)
624 .ConfigureAwait(false);
628 statuses = await this.Api.StatusesUserTimeline(userName, count)
629 .ConfigureAwait(false);
633 var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
635 if (minimumId != null)
636 tab.OldestId = minimumId.Value;
639 public async Task<PostClass> GetStatusApi(bool read, long id)
641 this.CheckAccountState();
643 var status = await this.Api.StatusesShow(id)
644 .ConfigureAwait(false);
646 var item = CreatePostsFromStatusData(status);
649 if (item.IsMe && !read && this.ReadOwnPost) item.IsRead = true;
654 public async Task GetStatusApi(bool read, long id, TabModel tab)
656 var post = await this.GetStatusApi(read, id)
657 .ConfigureAwait(false);
659 //非同期アイコン取得&StatusDictionaryに追加
660 if (tab != null && tab.IsInnerStorageTabType)
661 tab.AddPostQueue(post);
663 TabInformations.GetInstance().AddPost(post);
666 private PostClass CreatePostsFromStatusData(TwitterStatus status)
667 => this.CreatePostsFromStatusData(status, false);
669 private PostClass CreatePostsFromStatusData(TwitterStatus status, bool favTweet)
671 var post = new PostClass();
672 TwitterEntities entities;
675 post.StatusId = status.Id;
676 if (status.RetweetedStatus != null)
678 var retweeted = status.RetweetedStatus;
680 post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt);
683 post.RetweetedId = retweeted.Id;
685 post.TextFromApi = retweeted.FullText;
686 entities = retweeted.MergedEntities;
687 sourceHtml = retweeted.Source;
689 post.InReplyToStatusId = retweeted.InReplyToStatusId;
690 post.InReplyToUser = retweeted.InReplyToScreenName;
691 post.InReplyToUserId = status.InReplyToUserId;
700 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
701 post.IsFav = tc.Contains(retweeted.Id);
704 if (retweeted.Coordinates != null)
705 post.PostGeo = new PostClass.StatusGeo(retweeted.Coordinates.Coordinates[0], retweeted.Coordinates.Coordinates[1]);
708 var user = retweeted.User;
711 post.UserId = user.Id;
712 post.ScreenName = user.ScreenName;
713 post.Nickname = user.Name.Trim();
714 post.ImageUrl = user.ProfileImageUrlHttps;
715 post.IsProtect = user.Protected;
720 post.ScreenName = "?????";
721 post.Nickname = "Unknown User";
725 if (status.User != null)
727 post.RetweetedBy = status.User.ScreenName;
728 post.RetweetedByUserId = status.User.Id;
729 post.IsMe = post.RetweetedByUserId == this.UserId;
733 post.RetweetedBy = "?????";
734 post.RetweetedByUserId = 0L;
739 post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
741 post.TextFromApi = status.FullText;
742 entities = status.MergedEntities;
743 sourceHtml = status.Source;
744 post.InReplyToStatusId = status.InReplyToStatusId;
745 post.InReplyToUser = status.InReplyToScreenName;
746 post.InReplyToUserId = status.InReplyToUserId;
755 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
756 post.IsFav = tc.Contains(post.StatusId) && TabInformations.GetInstance()[post.StatusId].IsFav;
759 if (status.Coordinates != null)
760 post.PostGeo = new PostClass.StatusGeo(status.Coordinates.Coordinates[0], status.Coordinates.Coordinates[1]);
763 var user = status.User;
766 post.UserId = user.Id;
767 post.ScreenName = user.ScreenName;
768 post.Nickname = user.Name.Trim();
769 post.ImageUrl = user.ProfileImageUrlHttps;
770 post.IsProtect = user.Protected;
771 post.IsMe = post.UserId == this.UserId;
776 post.ScreenName = "?????";
777 post.Nickname = "Unknown User";
781 string textFromApi = post.TextFromApi;
783 var quotedStatusLink = (status.RetweetedStatus ?? status).QuotedStatusPermalink;
785 if (quotedStatusLink != null && entities.Urls.Any(x => x.ExpandedUrl == quotedStatusLink.Expanded))
786 quotedStatusLink = null; // 移行期は entities.urls と quoted_status_permalink の両方に含まれる場合がある
788 post.Text = CreateHtmlAnchor(textFromApi, entities, quotedStatusLink);
789 post.TextFromApi = textFromApi;
790 post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities, quotedStatusLink);
791 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
792 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
793 post.AccessibleText = CreateAccessibleText(textFromApi, entities, (status.RetweetedStatus ?? status).QuotedStatus, quotedStatusLink);
794 post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
795 post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
797 this.ExtractEntities(entities, post.ReplyToList, post.Media);
799 post.QuoteStatusIds = GetQuoteTweetStatusIds(entities, quotedStatusLink)
800 .Where(x => x != post.StatusId && x != post.RetweetedId)
801 .Distinct().ToArray();
803 post.ExpandedUrls = entities.OfType<TwitterEntityUrl>()
804 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
807 // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
808 if (post.Text == post.TextFromApi)
809 post.Text = post.TextFromApi;
810 if (post.AccessibleText == post.TextFromApi)
811 post.AccessibleText = post.TextFromApi;
813 // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
814 post.ScreenName = string.Intern(post.ScreenName);
815 post.Nickname = string.Intern(post.Nickname);
816 post.ImageUrl = string.Intern(post.ImageUrl);
817 post.RetweetedBy = post.RetweetedBy != null ? string.Intern(post.RetweetedBy) : null;
820 var (sourceText, sourceUri) = ParseSource(sourceHtml);
821 post.Source = string.Intern(sourceText);
822 post.SourceUri = sourceUri;
824 post.IsReply = post.RetweetedId == null && post.ReplyToList.Any(x => x.Item1 == this.UserId);
825 post.IsExcludeReply = false;
833 if (followerId.Count > 0) post.IsOwl = !followerId.Contains(post.UserId);
841 /// ツイートに含まれる引用ツイートのURLからステータスIDを抽出
843 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<TwitterEntity> entities, TwitterQuotedStatusPermalink quotedStatusLink)
845 var urls = entities.OfType<TwitterEntityUrl>().Select(x => x.ExpandedUrl);
847 if (quotedStatusLink != null)
848 urls = urls.Concat(new[] { quotedStatusLink.Expanded });
850 return GetQuoteTweetStatusIds(urls);
853 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<string> urls)
855 foreach (var url in urls)
857 var match = Twitter.StatusUrlRegex.Match(url);
860 if (long.TryParse(match.Groups["StatusId"].Value, out var statusId))
861 yield return statusId;
866 private long? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabModel tab, bool read)
868 long? minimumId = null;
870 foreach (var status in items)
872 if (minimumId == null || minimumId.Value > status.Id)
873 minimumId = status.Id;
880 if (TabInformations.GetInstance().ContainsKey(status.Id)) continue;
884 if (tab.Contains(status.Id)) continue;
889 if (gType != MyCommon.WORKERTYPE.UserTimeline &&
890 status.RetweetedStatus != null && this.noRTId.Contains(status.User.Id)) continue;
892 var post = CreatePostsFromStatusData(status);
895 if (post.IsMe && !read && this.ReadOwnPost) post.IsRead = true;
897 if (tab != null && tab.IsInnerStorageTabType)
898 tab.AddPostQueue(post);
900 TabInformations.GetInstance().AddPost(post);
906 private long? CreatePostsFromSearchJson(TwitterSearchResult items, PublicSearchTabModel tab, bool read, bool more)
908 long? minimumId = null;
910 foreach (var status in items.Statuses)
912 if (minimumId == null || minimumId.Value > status.Id)
913 minimumId = status.Id;
915 if (!more && status.Id > tab.SinceId) tab.SinceId = status.Id;
919 if (tab.Contains(status.Id)) continue;
922 var post = CreatePostsFromStatusData(status);
925 if ((post.IsMe && !read) && this.ReadOwnPost) post.IsRead = true;
927 tab.AddPostQueue(post);
933 private long? CreateFavoritePostsFromJson(TwitterStatus[] items, bool read)
935 var favTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
936 long? minimumId = null;
938 foreach (var status in items)
940 if (minimumId == null || minimumId.Value > status.Id)
941 minimumId = status.Id;
946 if (favTab.Contains(status.Id)) continue;
949 var post = CreatePostsFromStatusData(status, true);
953 TabInformations.GetInstance().AddPost(post);
959 public async Task GetListStatus(bool read, ListTimelineTabModel tab, bool more, bool startup)
961 var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
963 TwitterStatus[] statuses;
966 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, maxId: tab.OldestId, includeRTs: SettingManager.Common.IsListsIncludeRts)
967 .ConfigureAwait(false);
971 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, includeRTs: SettingManager.Common.IsListsIncludeRts)
972 .ConfigureAwait(false);
975 var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.List, tab, read);
977 if (minimumId != null)
978 tab.OldestId = minimumId.Value;
982 /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
984 /// <returns>posts の中から検索されたリプライチェインの末端</returns>
985 internal static PostClass FindTopOfReplyChain(IDictionary<Int64, PostClass> posts, Int64 startStatusId)
987 if (!posts.ContainsKey(startStatusId))
988 throw new ArgumentException("startStatusId (" + startStatusId + ") が posts の中から見つかりませんでした。", nameof(startStatusId));
990 var nextPost = posts[startStatusId];
991 while (nextPost.InReplyToStatusId != null)
993 if (!posts.ContainsKey(nextPost.InReplyToStatusId.Value))
995 nextPost = posts[nextPost.InReplyToStatusId.Value];
1001 public async Task GetRelatedResult(bool read, RelatedPostsTabModel tab)
1003 var targetPost = tab.TargetPost;
1004 var relPosts = new Dictionary<Int64, PostClass>();
1005 if (targetPost.TextFromApi.Contains("@") && targetPost.InReplyToStatusId == null)
1008 var p = TabInformations.GetInstance()[targetPost.StatusId];
1009 if (p != null && p.InReplyToStatusId != null)
1015 p = await this.GetStatusApi(read, targetPost.StatusId)
1016 .ConfigureAwait(false);
1020 relPosts.Add(targetPost.StatusId, targetPost);
1022 Exception lastException = null;
1024 // in_reply_to_status_id を使用してリプライチェインを辿る
1025 var nextPost = FindTopOfReplyChain(relPosts, targetPost.StatusId);
1027 while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
1029 var inReplyToId = nextPost.InReplyToStatusId.Value;
1031 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
1032 if (inReplyToPost == null)
1036 inReplyToPost = await this.GetStatusApi(read, inReplyToId)
1037 .ConfigureAwait(false);
1039 catch (WebApiException ex)
1046 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
1048 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
1051 //MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
1052 var text = targetPost.Text;
1053 var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
1054 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
1055 foreach (var _match in ma)
1057 if (Int64.TryParse(_match.Groups["StatusId"].Value, out var _statusId))
1059 if (relPosts.ContainsKey(_statusId))
1062 var p = TabInformations.GetInstance()[_statusId];
1067 p = await this.GetStatusApi(read, _statusId)
1068 .ConfigureAwait(false);
1070 catch (WebApiException ex)
1078 relPosts.Add(p.StatusId, p);
1082 relPosts.Values.ToList().ForEach(p =>
1084 if (p.IsMe && !read && this.ReadOwnPost)
1089 tab.AddPostQueue(p);
1092 if (lastException != null)
1093 throw new WebApiException(lastException.Message, lastException);
1096 public async Task GetSearch(bool read, PublicSearchTabModel tab, bool more)
1098 var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
1101 long? sinceId = null;
1104 maxId = tab.OldestId - 1;
1108 sinceId = tab.SinceId;
1111 var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
1112 .ConfigureAwait(false);
1114 if (!TabInformations.GetInstance().ContainsTab(tab))
1117 var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, more);
1119 if (minimumId != null)
1120 tab.OldestId = minimumId.Value;
1123 private void CreateDirectMessagesFromJson(TwitterDirectMessage[] item, MyCommon.WORKERTYPE gType, bool read)
1125 foreach (var message in item)
1127 var post = new PostClass();
1130 post.StatusId = message.Id;
1131 if (gType != MyCommon.WORKERTYPE.UserStream)
1133 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1135 if (minDirectmessage > post.StatusId) minDirectmessage = post.StatusId;
1139 if (minDirectmessageSent > post.StatusId) minDirectmessageSent = post.StatusId;
1146 if (TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage).Contains(post.StatusId)) continue;
1150 post.CreatedAt = MyCommon.DateTimeParse(message.CreatedAt);
1152 var textFromApi = message.Text;
1154 post.Text = CreateHtmlAnchor(textFromApi, message.Entities, quotedStatusLink: null);
1155 post.TextFromApi = this.ReplaceTextFromApi(textFromApi, message.Entities, quotedStatusLink: null);
1156 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
1157 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
1158 post.AccessibleText = CreateAccessibleText(textFromApi, message.Entities, quotedStatus: null, quotedStatusLink: null);
1159 post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
1160 post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
1163 this.ExtractEntities(message.Entities, post.ReplyToList, post.Media);
1165 post.QuoteStatusIds = GetQuoteTweetStatusIds(message.Entities, quotedStatusLink: null)
1166 .Distinct().ToArray();
1168 post.ExpandedUrls = message.Entities.OfType<TwitterEntityUrl>()
1169 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
1174 if (gType == MyCommon.WORKERTYPE.UserStream)
1176 if (this.Api.CurrentUserId == message.Recipient.Id)
1178 user = message.Sender;
1184 user = message.Recipient;
1191 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1193 user = message.Sender;
1199 user = message.Recipient;
1205 post.UserId = user.Id;
1206 post.ScreenName = user.ScreenName;
1207 post.Nickname = user.Name.Trim();
1208 post.ImageUrl = user.ProfileImageUrlHttps;
1209 post.IsProtect = user.Protected;
1211 // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
1212 if (post.Text == post.TextFromApi)
1213 post.Text = post.TextFromApi;
1214 if (post.AccessibleText == post.TextFromApi)
1215 post.AccessibleText = post.TextFromApi;
1217 // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
1218 post.ScreenName = string.Intern(post.ScreenName);
1219 post.Nickname = string.Intern(post.Nickname);
1220 post.ImageUrl = string.Intern(post.ImageUrl);
1224 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name);
1225 MessageBox.Show("Parse Error(CreateDirectMessagesFromJson)");
1230 if (post.IsMe && !read && this.ReadOwnPost) post.IsRead = true;
1231 post.IsReply = false;
1232 post.IsExcludeReply = false;
1235 var dmTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage);
1236 dmTab.AddPostQueue(post);
1240 public async Task GetDirectMessageApi(bool read, MyCommon.WORKERTYPE gType, bool more)
1242 this.CheckAccountState();
1243 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
1245 var count = GetApiResultCount(gType, more, false);
1247 TwitterDirectMessage[] messages;
1248 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1252 messages = await this.Api.DirectMessagesRecv(count, maxId: this.minDirectmessage)
1253 .ConfigureAwait(false);
1257 messages = await this.Api.DirectMessagesRecv(count)
1258 .ConfigureAwait(false);
1265 messages = await this.Api.DirectMessagesSent(count, maxId: this.minDirectmessageSent)
1266 .ConfigureAwait(false);
1270 messages = await this.Api.DirectMessagesSent(count)
1271 .ConfigureAwait(false);
1275 CreateDirectMessagesFromJson(messages, gType, read);
1278 public async Task GetFavoritesApi(bool read, FavoritesTabModel tab, bool backward)
1280 this.CheckAccountState();
1282 var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, backward, false);
1284 TwitterStatus[] statuses;
1287 statuses = await this.Api.FavoritesList(count, maxId: tab.OldestId)
1288 .ConfigureAwait(false);
1292 statuses = await this.Api.FavoritesList(count)
1293 .ConfigureAwait(false);
1296 var minimumId = this.CreateFavoritePostsFromJson(statuses, read);
1298 if (minimumId != null)
1299 tab.OldestId = minimumId.Value;
1302 private string ReplaceTextFromApi(string text, TwitterEntities entities, TwitterQuotedStatusPermalink quotedStatusLink)
1304 if (entities != null)
1306 if (entities.Urls != null)
1308 foreach (var m in entities.Urls)
1310 if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1313 if (entities.Media != null)
1315 foreach (var m in entities.Media)
1317 if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1322 if (quotedStatusLink != null)
1323 text += " " + quotedStatusLink.Display;
1328 internal static string CreateAccessibleText(string text, TwitterEntities entities, TwitterStatus quotedStatus, TwitterQuotedStatusPermalink quotedStatusLink)
1330 if (entities == null)
1333 if (entities.Urls != null)
1335 foreach (var entity in entities.Urls)
1337 if (quotedStatus != null)
1339 var matchStatusUrl = Twitter.StatusUrlRegex.Match(entity.ExpandedUrl);
1340 if (matchStatusUrl.Success && matchStatusUrl.Groups["StatusId"].Value == quotedStatus.IdStr)
1342 var quotedText = CreateAccessibleText(quotedStatus.FullText, quotedStatus.MergedEntities, quotedStatus: null, quotedStatusLink: null);
1343 text = text.Replace(entity.Url, string.Format(Properties.Resources.QuoteStatus_AccessibleText, quotedStatus.User.ScreenName, quotedText));
1348 if (!string.IsNullOrEmpty(entity.DisplayUrl))
1349 text = text.Replace(entity.Url, entity.DisplayUrl);
1353 if (entities.Media != null)
1355 foreach (var entity in entities.Media)
1357 if (!string.IsNullOrEmpty(entity.AltText))
1359 text = text.Replace(entity.Url, string.Format(Properties.Resources.ImageAltText, entity.AltText));
1361 else if (!string.IsNullOrEmpty(entity.DisplayUrl))
1363 text = text.Replace(entity.Url, entity.DisplayUrl);
1368 if (quotedStatusLink != null)
1370 var quoteText = CreateAccessibleText(quotedStatus.FullText, quotedStatus.MergedEntities, quotedStatus: null, quotedStatusLink: null);
1371 text += " " + string.Format(Properties.Resources.QuoteStatus_AccessibleText, quotedStatus.User.ScreenName, quoteText);
1380 /// <exception cref="WebApiException"/>
1381 public async Task RefreshFollowerIds()
1383 if (MyCommon._endingFlag) return;
1386 var newFollowerIds = new HashSet<long>();
1389 var ret = await this.Api.FollowersIds(cursor)
1390 .ConfigureAwait(false);
1392 if (ret.Ids == null)
1393 throw new WebApiException("ret.ids == null");
1395 newFollowerIds.UnionWith(ret.Ids);
1396 cursor = ret.NextCursor;
1397 } while (cursor != 0);
1399 this.followerId = newFollowerIds;
1400 TabInformations.GetInstance().RefreshOwl(this.followerId);
1402 this.GetFollowersSuccess = true;
1406 /// RT 非表示ユーザーを更新します
1408 /// <exception cref="WebApiException"/>
1409 public async Task RefreshNoRetweetIds()
1411 if (MyCommon._endingFlag) return;
1413 this.noRTId = await this.Api.NoRetweetIds()
1414 .ConfigureAwait(false);
1416 this.GetNoRetweetSuccess = true;
1420 /// t.co の文字列長などの設定情報を更新します
1422 /// <exception cref="WebApiException"/>
1423 public async Task RefreshConfiguration()
1425 this.Configuration = await this.Api.Configuration()
1426 .ConfigureAwait(false);
1428 // TextConfiguration 相当の JSON を得る API が存在しないため、TransformedURLLength のみ help/configuration.json に合わせて更新する
1429 this.TextConfiguration.TransformedURLLength = this.Configuration.ShortUrlLengthHttps;
1432 public async Task GetListsApi()
1434 this.CheckAccountState();
1436 var ownedLists = await TwitterLists.GetAllItemsAsync(x =>
1437 this.Api.ListsOwnerships(this.Username, cursor: x, count: 1000))
1438 .ConfigureAwait(false);
1440 var subscribedLists = await TwitterLists.GetAllItemsAsync(x =>
1441 this.Api.ListsSubscriptions(this.Username, cursor: x, count: 1000))
1442 .ConfigureAwait(false);
1444 TabInformations.GetInstance().SubscribableLists = Enumerable.Concat(ownedLists, subscribedLists)
1445 .Select(x => new ListElement(x, this))
1449 public async Task DeleteList(long listId)
1451 await this.Api.ListsDestroy(listId)
1453 .ConfigureAwait(false);
1455 var tabinfo = TabInformations.GetInstance();
1457 tabinfo.SubscribableLists = tabinfo.SubscribableLists
1458 .Where(x => x.Id != listId)
1462 public async Task<ListElement> EditList(long listId, string new_name, bool isPrivate, string description)
1464 var response = await this.Api.ListsUpdate(listId, new_name, description, isPrivate)
1465 .ConfigureAwait(false);
1467 var list = await response.LoadJsonAsync()
1468 .ConfigureAwait(false);
1470 return new ListElement(list, this);
1473 public async Task<long> GetListMembers(long listId, List<UserInfo> lists, long cursor)
1475 this.CheckAccountState();
1477 var users = await this.Api.ListsMembers(listId, cursor)
1478 .ConfigureAwait(false);
1480 Array.ForEach(users.Users, u => lists.Add(new UserInfo(u)));
1482 return users.NextCursor;
1485 public async Task CreateListApi(string listName, bool isPrivate, string description)
1487 this.CheckAccountState();
1489 var response = await this.Api.ListsCreate(listName, description, isPrivate)
1490 .ConfigureAwait(false);
1492 var list = await response.LoadJsonAsync()
1493 .ConfigureAwait(false);
1495 TabInformations.GetInstance().SubscribableLists.Add(new ListElement(list, this));
1498 public async Task<bool> ContainsUserAtList(long listId, string user)
1500 this.CheckAccountState();
1504 await this.Api.ListsMembersShow(listId, user)
1505 .ConfigureAwait(false);
1509 catch (TwitterApiException ex)
1510 when (ex.ErrorResponse.Errors.Any(x => x.Code == TwitterErrorCode.NotFound))
1516 private void ExtractEntities(TwitterEntities entities, List<Tuple<long, string>> AtList, List<MediaInfo> media)
1518 if (entities != null)
1520 if (entities.Hashtags != null)
1524 this._hashList.AddRange(entities.Hashtags.Select(x => "#" + x.Text));
1527 if (entities.UserMentions != null)
1529 foreach (var ent in entities.UserMentions)
1531 AtList.Add(Tuple.Create(ent.Id, ent.ScreenName));
1534 if (entities.Media != null)
1538 foreach (var ent in entities.Media)
1540 if (!media.Any(x => x.Url == ent.MediaUrlHttps))
1542 if (ent.VideoInfo != null &&
1543 ent.Type == "animated_gif" || ent.Type == "video")
1545 //var videoUrl = ent.VideoInfo.Variants
1546 // .Where(v => v.ContentType == "video/mp4")
1547 // .OrderByDescending(v => v.Bitrate)
1548 // .Select(v => v.Url).FirstOrDefault();
1549 media.Add(new MediaInfo(ent.MediaUrlHttps, ent.AltText, ent.ExpandedUrl));
1552 media.Add(new MediaInfo(ent.MediaUrlHttps, ent.AltText, videoUrl: null));
1560 internal static string CreateHtmlAnchor(string text, TwitterEntities entities, TwitterQuotedStatusPermalink quotedStatusLink)
1562 // PostClass.ExpandedUrlInfo を使用して非同期に URL 展開を行うためここでは expanded_url を使用しない
1563 text = TweetFormatter.AutoLinkHtml(text, entities, keepTco: true);
1565 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>");
1566 text = PreProcessUrl(text); //IDN置換
1568 if (quotedStatusLink != null)
1570 text += string.Format(" <a href=\"{0}\" title=\"{0}\">{1}</a>",
1571 WebUtility.HtmlEncode(quotedStatusLink.Url),
1572 WebUtility.HtmlEncode(quotedStatusLink.Display));
1578 private static readonly Uri SourceUriBase = new Uri("https://twitter.com/");
1581 /// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
1583 internal static (string SourceText, Uri SourceUri) ParseSource(string sourceHtml)
1585 if (string.IsNullOrEmpty(sourceHtml))
1591 // sourceHtmlの例: <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>
1593 var match = Regex.Match(sourceHtml, "^<a href=\"(?<uri>.+?)\".*?>(?<text>.+)</a>$", RegexOptions.IgnoreCase);
1596 sourceText = WebUtility.HtmlDecode(match.Groups["text"].Value);
1599 var uriStr = WebUtility.HtmlDecode(match.Groups["uri"].Value);
1600 sourceUri = new Uri(SourceUriBase, uriStr);
1602 catch (UriFormatException)
1609 sourceText = WebUtility.HtmlDecode(sourceHtml);
1613 return (sourceText, sourceUri);
1616 public async Task<TwitterApiStatus> GetInfoApi()
1618 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
1620 if (MyCommon._endingFlag) return null;
1622 var limits = await this.Api.ApplicationRateLimitStatus()
1623 .ConfigureAwait(false);
1625 MyCommon.TwitterApiInfo.UpdateFromJson(limits);
1627 return MyCommon.TwitterApiInfo;
1631 /// ブロック中のユーザーを更新します
1633 /// <exception cref="WebApiException"/>
1634 public async Task RefreshBlockIds()
1636 if (MyCommon._endingFlag) return;
1639 var newBlockIds = new HashSet<long>();
1642 var ret = await this.Api.BlocksIds(cursor)
1643 .ConfigureAwait(false);
1645 newBlockIds.UnionWith(ret.Ids);
1646 cursor = ret.NextCursor;
1647 } while (cursor != 0);
1649 newBlockIds.Remove(this.UserId); // 元のソースにあったので一応残しておく
1651 TabInformations.GetInstance().BlockIds = newBlockIds;
1655 /// ミュート中のユーザーIDを更新します
1657 /// <exception cref="WebApiException"/>
1658 public async Task RefreshMuteUserIdsAsync()
1660 if (MyCommon._endingFlag) return;
1662 var ids = await TwitterIds.GetAllItemsAsync(x => this.Api.MutesUsersIds(x))
1663 .ConfigureAwait(false);
1665 TabInformations.GetInstance().MuteUserIds = new HashSet<long>(ids);
1668 public string[] GetHashList()
1673 hashArray = _hashList.ToArray();
1679 public string AccessToken
1680 => ((TwitterApiConnection)this.Api.Connection).AccessToken;
1682 public string AccessTokenSecret
1683 => ((TwitterApiConnection)this.Api.Connection).AccessSecret;
1685 private void CheckAccountState()
1687 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1688 throw new WebApiException("Auth error. Check your account");
1691 private void CheckAccessLevel(TwitterApiAccessLevel accessLevelFlags)
1693 if (!this.AccessLevel.HasFlag(accessLevelFlags))
1694 throw new WebApiException("Auth Err:try to re-authorization.");
1697 public int GetTextLengthRemain(string postText)
1699 var matchDm = Twitter.DMSendTextRegex.Match(postText);
1700 if (matchDm.Success)
1701 return this.GetTextLengthRemainDM(matchDm.Groups["body"].Value);
1703 return this.GetTextLengthRemainWeighted(postText);
1706 private int GetTextLengthRemainDM(string postText)
1711 while (pos < postText.Length)
1715 if (char.IsSurrogatePair(postText, pos))
1716 pos += 2; // サロゲートペアの場合は2文字分進める
1721 var urls = TweetExtractor.ExtractUrls(postText);
1722 foreach (var url in urls)
1724 var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
1725 ? this.Configuration.ShortUrlLengthHttps
1726 : this.Configuration.ShortUrlLength;
1728 textLength += shortUrlLength - url.Length;
1731 return this.Configuration.DmTextCharacterLimit - textLength;
1734 private int GetTextLengthRemainWeighted(string postText)
1736 var config = this.TextConfiguration;
1737 var totalWeight = 0;
1739 var urls = TweetExtractor.ExtractUrlEntities(postText).ToArray();
1742 while (pos < postText.Length)
1744 var urlEntity = urls.FirstOrDefault(x => x.Indices[0] == pos);
1745 if (urlEntity != null)
1747 totalWeight += config.TransformedURLLength * config.Scale;
1749 var urlLength = urlEntity.Indices[1] - urlEntity.Indices[0];
1755 var codepoint = postText.GetCodepointAtSafe(pos);
1756 var weight = config.DefaultWeight;
1758 foreach (var weightRange in config.Ranges)
1760 if (codepoint >= weightRange.Start && codepoint <= weightRange.End)
1762 weight = weightRange.Weight;
1767 totalWeight += weight;
1769 var isSurrogatePair = codepoint > 0xffff;
1770 if (isSurrogatePair)
1771 pos += 2; // サロゲートペアの場合は2文字分進める
1776 var remainWeight = config.MaxWeightedTweetLength * config.Scale - totalWeight;
1778 return remainWeight / config.Scale;
1782 #region "UserStream"
1783 public string TrackWord { get; set; } = "";
1784 public bool AllAtReply { get; set; } = false;
1786 public event EventHandler NewPostFromStream;
1787 public event EventHandler UserStreamStarted;
1788 public event EventHandler UserStreamStopped;
1789 public event EventHandler<PostDeletedEventArgs> PostDeleted;
1790 public event EventHandler<UserStreamEventReceivedEventArgs> UserStreamEventReceived;
1791 private DateTimeUtc _lastUserstreamDataReceived;
1792 private TwitterUserstream userStream;
1794 public class FormattedEvent
1796 public MyCommon.EVENTTYPE Eventtype { get; set; }
1797 public DateTimeUtc CreatedAt { get; set; }
1798 public string Event { get; set; }
1799 public string Username { get; set; }
1800 public string Target { get; set; }
1801 public Int64 Id { get; set; }
1802 public bool IsMe { get; set; }
1805 public List<FormattedEvent> StoredEvent { get; } = new List<FormattedEvent>();
1807 private readonly IReadOnlyDictionary<string, MyCommon.EVENTTYPE> eventTable = new Dictionary<string, MyCommon.EVENTTYPE>
1809 ["favorite"] = MyCommon.EVENTTYPE.Favorite,
1810 ["unfavorite"] = MyCommon.EVENTTYPE.Unfavorite,
1811 ["follow"] = MyCommon.EVENTTYPE.Follow,
1812 ["list_member_added"] = MyCommon.EVENTTYPE.ListMemberAdded,
1813 ["list_member_removed"] = MyCommon.EVENTTYPE.ListMemberRemoved,
1814 ["block"] = MyCommon.EVENTTYPE.Block,
1815 ["unblock"] = MyCommon.EVENTTYPE.Unblock,
1816 ["user_update"] = MyCommon.EVENTTYPE.UserUpdate,
1817 ["deleted"] = MyCommon.EVENTTYPE.Deleted,
1818 ["list_created"] = MyCommon.EVENTTYPE.ListCreated,
1819 ["list_destroyed"] = MyCommon.EVENTTYPE.ListDestroyed,
1820 ["list_updated"] = MyCommon.EVENTTYPE.ListUpdated,
1821 ["unfollow"] = MyCommon.EVENTTYPE.Unfollow,
1822 ["list_user_subscribed"] = MyCommon.EVENTTYPE.ListUserSubscribed,
1823 ["list_user_unsubscribed"] = MyCommon.EVENTTYPE.ListUserUnsubscribed,
1824 ["mute"] = MyCommon.EVENTTYPE.Mute,
1825 ["unmute"] = MyCommon.EVENTTYPE.Unmute,
1826 ["quoted_tweet"] = MyCommon.EVENTTYPE.QuotedTweet,
1829 public bool IsUserstreamDataReceived
1830 => (DateTimeUtc.Now - this._lastUserstreamDataReceived).TotalSeconds < 31;
1832 private void userStream_StatusArrived(string line)
1834 this._lastUserstreamDataReceived = DateTimeUtc.Now;
1835 if (string.IsNullOrEmpty(line)) return;
1837 if (line.First() != '{' || line.Last() != '}')
1839 MyCommon.TraceOut("Invalid JSON (StatusArrived):" + Environment.NewLine + line);
1847 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(line), XmlDictionaryReaderQuotas.Max))
1849 var xElm = XElement.Load(jsonReader);
1850 if (xElm.Element("friends") != null)
1852 Debug.WriteLine("friends");
1855 else if (xElm.Element("delete") != null)
1857 Debug.WriteLine("delete");
1860 if ((idElm = xElm.Element("delete").Element("direct_message")?.Element("id")) != null)
1863 long.TryParse(idElm.Value, out id);
1865 this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
1867 else if ((idElm = xElm.Element("delete").Element("status")?.Element("id")) != null)
1870 long.TryParse(idElm.Value, out id);
1872 this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
1876 MyCommon.TraceOut("delete:" + line);
1879 for (int i = this.StoredEvent.Count - 1; i >= 0; i--)
1881 var sEvt = this.StoredEvent[i];
1882 if (sEvt.Id == id && (sEvt.Event == "favorite" || sEvt.Event == "unfavorite"))
1884 this.StoredEvent.RemoveAt(i);
1889 else if (xElm.Element("limit") != null)
1891 Debug.WriteLine(line);
1894 else if (xElm.Element("event") != null)
1896 Debug.WriteLine("event: " + xElm.Element("event").Value);
1897 CreateEventFromJson(line);
1900 else if (xElm.Element("direct_message") != null)
1902 Debug.WriteLine("direct_message");
1905 else if (xElm.Element("retweeted_status") != null)
1907 var sourceUserId = xElm.XPathSelectElement("/user/id_str").Value;
1908 var targetUserId = xElm.XPathSelectElement("/retweeted_status/user/id_str").Value;
1910 // 自分に関係しないリツイートの場合は無視する
1911 var selfUserId = this.UserId.ToString();
1912 if (sourceUserId == selfUserId || targetUserId == selfUserId)
1914 // 公式 RT をイベントとしても扱う
1915 var evt = CreateEventFromRetweet(xElm);
1918 this.StoredEvent.Insert(0, evt);
1920 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
1924 // 従来通り公式 RT の表示も行うため return しない
1926 else if (xElm.Element("scrub_geo") != null)
1930 TabInformations.GetInstance().ScrubGeoReserve(long.Parse(xElm.Element("scrub_geo").Element("user_id").Value),
1931 long.Parse(xElm.Element("scrub_geo").Element("up_to_status_id").Value));
1935 MyCommon.TraceOut("scrub_geo:" + line);
1945 var message = TwitterStreamEventDirectMessage.ParseJson(line).DirectMessage;
1946 this.CreateDirectMessagesFromJson(new[] { message }, MyCommon.WORKERTYPE.UserStream, false);
1948 catch (SerializationException ex)
1950 throw TwitterApiException.CreateFromException(ex, line);
1957 var status = TwitterStatusCompat.ParseJson(line);
1958 this.CreatePostsFromJson(new[] { status.Normalize() }, MyCommon.WORKERTYPE.UserStream, null, false);
1960 catch (SerializationException ex)
1962 throw TwitterApiException.CreateFromException(ex, line);
1966 catch (WebApiException ex)
1968 MyCommon.TraceOut(ex);
1971 catch (XmlException)
1973 MyCommon.TraceOut("XmlException (StatusArrived): " + line);
1975 catch(NullReferenceException)
1977 MyCommon.TraceOut("NullRef StatusArrived: " + line);
1980 this.NewPostFromStream?.Invoke(this, EventArgs.Empty);
1984 /// UserStreamsから受信した公式RTをイベントに変換します
1986 private FormattedEvent CreateEventFromRetweet(XElement xElm)
1988 return new FormattedEvent
1990 Eventtype = MyCommon.EVENTTYPE.Retweet,
1992 CreatedAt = MyCommon.DateTimeParse(xElm.XPathSelectElement("/created_at").Value),
1993 IsMe = xElm.XPathSelectElement("/user/id_str").Value == this.UserId.ToString(),
1994 Username = xElm.XPathSelectElement("/user/screen_name").Value,
1995 Target = string.Format("@{0}:{1}", new[]
1997 xElm.XPathSelectElement("/retweeted_status/user/screen_name").Value,
1998 WebUtility.HtmlDecode(xElm.XPathSelectElement("/retweeted_status/text").Value),
2000 Id = long.Parse(xElm.XPathSelectElement("/retweeted_status/id_str").Value),
2004 private void CreateEventFromJson(string content)
2006 TwitterStreamEvent eventData = null;
2009 eventData = TwitterStreamEvent.ParseJson(content);
2011 catch(SerializationException ex)
2013 MyCommon.TraceOut(ex, "Event Serialize Exception!" + Environment.NewLine + content);
2017 MyCommon.TraceOut(ex, "Event Exception!" + Environment.NewLine + content);
2020 var evt = new FormattedEvent
2022 CreatedAt = MyCommon.DateTimeParse(eventData.CreatedAt),
2023 Event = eventData.Event,
2024 Username = eventData.Source.ScreenName,
2025 IsMe = eventData.Source.Id == this.UserId,
2026 Eventtype = eventTable.TryGetValue(eventData.Event, out var eventType) ? eventType : MyCommon.EVENTTYPE.None,
2029 TwitterStreamEvent<TwitterStatusCompat> tweetEvent;
2030 TwitterStatus tweet;
2032 switch (eventData.Event)
2034 case "access_revoked":
2035 case "access_unrevoked":
2037 case "user_suspend":
2040 if (eventData.Target.Id == this.UserId)
2042 if (!this.followerId.Contains(eventData.Source.Id)) this.followerId.Add(eventData.Source.Id);
2046 return; //Block後のUndoをすると、SourceとTargetが逆転したfollowイベントが帰ってくるため。
2051 evt.Target = "@" + eventData.Target.ScreenName;
2053 case "favorited_retweet":
2054 case "retweeted_retweet":
2058 tweetEvent = TwitterStreamEvent<TwitterStatusCompat>.ParseJson(content);
2059 tweet = tweetEvent.TargetObject.Normalize();
2060 evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
2063 if (SettingManager.Common.IsRemoveSameEvent)
2065 if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2069 var tabinfo = TabInformations.GetInstance();
2071 var statusId = tweet.Id;
2072 if (!tabinfo.Posts.TryGetValue(statusId, out var post))
2075 if (eventData.Event == "favorite")
2077 var favTab = tabinfo.GetTabByType(MyCommon.TabUsageType.Favorites);
2078 favTab.AddPostQueue(post);
2080 if (tweetEvent.Source.Id == this.UserId)
2084 else if (tweetEvent.Target.Id == this.UserId)
2086 post.FavoritedCount++;
2088 if (SettingManager.Common.FavEventUnread)
2089 tabinfo.SetReadAllTab(post.StatusId, read: false);
2094 if (tweetEvent.Source.Id == this.UserId)
2098 else if (tweetEvent.Target.Id == this.UserId)
2100 post.FavoritedCount = Math.Max(0, post.FavoritedCount - 1);
2104 case "quoted_tweet":
2105 if (evt.IsMe) return;
2107 tweetEvent = TwitterStreamEvent<TwitterStatusCompat>.ParseJson(content);
2108 tweet = tweetEvent.TargetObject.Normalize();
2109 evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
2112 if (SettingManager.Common.IsRemoveSameEvent)
2114 if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2118 case "list_member_added":
2119 case "list_member_removed":
2120 case "list_created":
2121 case "list_destroyed":
2122 case "list_updated":
2123 case "list_user_subscribed":
2124 case "list_user_unsubscribed":
2125 var listEvent = TwitterStreamEvent<TwitterList>.ParseJson(content);
2126 evt.Target = listEvent.TargetObject.FullName;
2129 if (!TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Add(eventData.Target.Id);
2133 if (TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Remove(eventData.Target.Id);
2142 evt.Target = "@" + eventData.Target.ScreenName;
2143 if (!TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2145 TabInformations.GetInstance().MuteUserIds.Add(eventData.Target.Id);
2149 evt.Target = "@" + eventData.Target.ScreenName;
2150 if (TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2152 TabInformations.GetInstance().MuteUserIds.Remove(eventData.Target.Id);
2157 MyCommon.TraceOut("Unknown Event:" + evt.Event + Environment.NewLine + content);
2160 this.StoredEvent.Insert(0, evt);
2162 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
2165 private void userStream_Started()
2166 => this.UserStreamStarted?.Invoke(this, EventArgs.Empty);
2168 private void userStream_Stopped()
2169 => this.UserStreamStopped?.Invoke(this, EventArgs.Empty);
2171 public bool UserStreamActive
2172 => this.userStream != null && this.userStream.IsStreamActive;
2174 public void StartUserStream()
2176 var newStream = new TwitterUserstream(this.Api);
2178 newStream.StatusArrived += userStream_StatusArrived;
2179 newStream.Started += userStream_Started;
2180 newStream.Stopped += userStream_Stopped;
2182 newStream.Start(this.AllAtReply, this.TrackWord);
2184 var oldStream = Interlocked.Exchange(ref this.userStream, newStream);
2185 oldStream?.Dispose();
2188 public void StopUserStream()
2190 var oldStream = Interlocked.Exchange(ref this.userStream, null);
2191 oldStream?.Dispose();
2194 public void ReconnectUserStream()
2196 if (this.userStream != null)
2198 this.StartUserStream();
2202 private class TwitterUserstream : IDisposable
2204 public bool AllAtReplies { get; private set; }
2205 public string TrackWords { get; private set; }
2207 public bool IsStreamActive { get; private set; }
2209 public event Action<string> StatusArrived;
2210 public event Action Stopped;
2211 public event Action Started;
2213 private TwitterApi twitterApi;
2215 private Task streamTask;
2216 private CancellationTokenSource streamCts;
2218 public TwitterUserstream(TwitterApi twitterApi)
2219 => this.twitterApi = twitterApi;
2221 public void Start(bool allAtReplies, string trackwords)
2223 this.AllAtReplies = allAtReplies;
2224 this.TrackWords = trackwords;
2226 var cts = new CancellationTokenSource();
2228 this.streamCts = cts;
2229 this.streamTask = Task.Run(async () =>
2233 await this.UserStreamLoop(cts.Token)
2234 .ConfigureAwait(false);
2236 catch (OperationCanceledException) { }
2242 this.streamCts?.Cancel();
2244 // streamTask の完了を待たずに IsStreamActive を false にセットする
2245 this.IsStreamActive = false;
2246 this.Stopped?.Invoke();
2249 private async Task UserStreamLoop(CancellationToken cancellationToken)
2251 TimeSpan sleep = TimeSpan.Zero;
2254 if (sleep != TimeSpan.Zero)
2256 await Task.Delay(sleep, cancellationToken)
2257 .ConfigureAwait(false);
2258 sleep = TimeSpan.Zero;
2261 if (!MyCommon.IsNetworkAvailable())
2263 sleep = TimeSpan.FromSeconds(30);
2267 this.IsStreamActive = true;
2268 this.Started?.Invoke();
2272 var replies = this.AllAtReplies ? "all" : null;
2274 using (var stream = await this.twitterApi.UserStreams(replies, this.TrackWords)
2275 .ConfigureAwait(false))
2276 using (var reader = new StreamReader(stream))
2278 while (!reader.EndOfStream)
2280 var line = await reader.ReadLineAsync()
2281 .ConfigureAwait(false);
2283 cancellationToken.ThrowIfCancellationRequested();
2285 this.StatusArrived?.Invoke(line);
2289 // キャンセルされていないのにストリームが終了した場合
2290 sleep = TimeSpan.FromSeconds(30);
2292 catch (TwitterApiException) { sleep = TimeSpan.FromSeconds(30); }
2293 catch (IOException) { sleep = TimeSpan.FromSeconds(30); }
2294 catch (OperationCanceledException)
2296 if (cancellationToken.IsCancellationRequested)
2299 // cancellationToken によるキャンセルではない(=タイムアウトエラー)
2300 sleep = TimeSpan.FromSeconds(30);
2302 catch (Exception ex)
2304 MyCommon.ExceptionOut(ex);
2305 sleep = TimeSpan.FromSeconds(30);
2309 this.IsStreamActive = false;
2310 this.Stopped?.Invoke();
2315 private bool disposed = false;
2317 public void Dispose()
2322 this.disposed = true;
2326 this.Started = null;
2327 this.Stopped = null;
2328 this.StatusArrived = null;
2333 #region "IDisposable Support"
2334 private bool disposedValue; // 重複する呼び出しを検出するには
2337 protected virtual void Dispose(bool disposing)
2339 if (!this.disposedValue)
2343 this.StopUserStream();
2346 this.disposedValue = true;
2349 //protected Overrides void Finalize()
2351 // // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
2353 // MyBase.Finalize()
2356 // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
2357 public void Dispose()
2359 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
2361 GC.SuppressFinalize(this);
2366 public class PostDeletedEventArgs : EventArgs
2368 public long StatusId { get; }
2370 public PostDeletedEventArgs(long statusId)
2371 => this.StatusId = statusId;
2374 public class UserStreamEventReceivedEventArgs : EventArgs
2376 public Twitter.FormattedEvent EventData { get; }
2378 public UserStreamEventReceivedEventArgs(Twitter.FormattedEvent eventData)
2379 => this.EventData = eventData;