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;
35 using System.Text.RegularExpressions;
36 using System.Threading;
37 using System.Threading.Tasks;
39 using System.Reflection;
40 using System.Collections.Generic;
41 using System.Windows.Forms;
43 using OpenTween.Api.DataModel;
44 using OpenTween.Connection;
45 using OpenTween.Models;
46 using OpenTween.Setting;
50 public class Twitter : IDisposable
52 #region Regexp from twitter-text-js
54 // The code in this region code block incorporates works covered by
55 // the following copyright and permission notices:
57 // Copyright 2011 Twitter, Inc.
59 // Licensed under the Apache License, Version 2.0 (the "License"); you
60 // may not use this work except in compliance with the License. You
61 // may obtain a copy of the License in the LICENSE file, or at:
63 // http://www.apache.org/licenses/LICENSE-2.0
65 // Unless required by applicable law or agreed to in writing, software
66 // distributed under the License is distributed on an "AS IS" BASIS,
67 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
68 // implied. See the License for the specific language governing
69 // permissions and limitations under the License.
72 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";
73 private const string NON_LATIN_HASHTAG_CHARS = @"\u0400-\u04ff\u0500-\u0527\u1100-\u11ff\u3130-\u3185\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF";
74 //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";
75 private const string CJ_HASHTAG_CHARACTERS = @"\u30A1-\u30FA\u30FC\u3005\uFF66-\uFF9F\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\u3041-\u309A\u3400-\u4DBF\p{IsCJKUnifiedIdeographs}";
76 private const string HASHTAG_BOUNDARY = @"^|$|\s|「|」|。|\.|!";
77 private const string HASHTAG_ALPHA = "[a-z_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
78 private const string HASHTAG_ALPHANUMERIC = "[a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
79 private const string HASHTAG_TERMINATOR = "[^a-z0-9_" + LATIN_ACCENTS + NON_LATIN_HASHTAG_CHARS + CJ_HASHTAG_CHARACTERS + "]";
80 public const string HASHTAG = "(" + HASHTAG_BOUNDARY + ")(#|#)(" + HASHTAG_ALPHANUMERIC + "*" + HASHTAG_ALPHA + HASHTAG_ALPHANUMERIC + "*)(?=" + HASHTAG_TERMINATOR + "|" + HASHTAG_BOUNDARY + ")";
82 private const string url_valid_preceding_chars = @"(?:[^A-Za-z0-9@@$##\ufffe\ufeff\uffff\u202a-\u202e]|^)";
83 public const string url_invalid_without_protocol_preceding_chars = @"[-_./]$";
84 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";
85 private const string url_valid_domain_chars = @"[^" + url_invalid_domain_chars + "]";
86 private const string url_valid_subdomain = @"(?:(?:" + url_valid_domain_chars + @"(?:[_-]|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
87 private const string url_valid_domain_name = @"(?:(?:" + url_valid_domain_chars + @"(?:-|" + url_valid_domain_chars + @")*)?" + url_valid_domain_chars + @"\.)";
88 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]|$))";
89 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]|$))";
90 private const string url_valid_punycode = @"(?:xn--[0-9a-z]+)";
91 private const string url_valid_domain = @"(?<domain>" + url_valid_subdomain + "*" + url_valid_domain_name + "(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + ")|" + url_valid_punycode + ")";
92 public const string url_valid_ascii_domain = @"(?:(?:[a-z0-9" + LATIN_ACCENTS + @"]+)\.)+(?:" + url_valid_GTLD + "|" + url_valid_CCTLD + "|" + url_valid_punycode + ")";
93 public const string url_invalid_short_domain = "^" + url_valid_domain_name + url_valid_CCTLD + "$";
94 private const string url_valid_port_number = @"[0-9]+";
96 private const string url_valid_general_path_chars = @"[a-z0-9!*';:=+,.$/%#\[\]\-_~|&" + LATIN_ACCENTS + "]";
97 private const string url_balance_parens = @"(?:\(" + url_valid_general_path_chars + @"+\))";
98 private const string url_valid_path_ending_chars = @"(?:[+\-a-z0-9=_#/" + LATIN_ACCENTS + "]|" + url_balance_parens + ")";
99 private const string pth = "(?:" +
101 url_valid_general_path_chars + "*" +
102 "(?:" + url_balance_parens + url_valid_general_path_chars + "*)*" +
103 url_valid_path_ending_chars +
104 ")|(?:@" + url_valid_general_path_chars + "+/)" +
106 private const string qry = @"(?<query>\?[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|]*[a-z0-9_&=#/])?";
107 public const string rgUrl = @"(?<before>" + url_valid_preceding_chars + ")" +
108 "(?<url>(?<protocol>https?://)?" +
109 "(?<domain>" + url_valid_domain + ")" +
110 "(?::" + url_valid_port_number + ")?" +
111 "(?<path>/" + pth + "*)?" +
118 /// Twitter API のステータスページのURL
120 public const string ServiceAvailabilityStatusUrl = "https://status.io.watchmouse.com/7617";
123 /// ツイートへのパーマリンクURLを判定する正規表現
125 public static readonly Regex StatusUrlRegex = new Regex(@"https?://([^.]+\.)?twitter\.com/(#!/)?(?<ScreenName>[a-zA-Z0-9_]+)/status(es)?/(?<StatusId>[0-9]+)(/photo)?", RegexOptions.IgnoreCase);
128 /// attachment_url に指定可能な URL を判定する正規表現
130 public static readonly Regex AttachmentUrlRegex = new Regex(@"https?://(
131 twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
132 | mobile\.twitter\.com/[0-9A-Za-z_]+/status/[0-9]+
133 | twitter\.com/messages/compose\?recipient_id=[0-9]+(&.+)?
134 )$", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
137 /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
139 public static readonly Regex ThirdPartyStatusUrlRegex = new Regex(@"https?://(?:[^.]+\.)?(?:
140 favstar\.fm/users/[a-zA-Z0-9_]+/status/ # Favstar
141 | favstar\.fm/t/ # Favstar (short)
142 | aclog\.koba789\.com/i/ # aclog
143 | frtrt\.net/solo_status\.php\?status= # RtRT
144 )(?<StatusId>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
147 /// DM送信かどうかを判定する正規表現
149 public static readonly Regex DMSendTextRegex = new Regex(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
151 public TwitterApi Api { get; }
152 public TwitterConfiguration Configuration { get; private set; }
153 public TwitterTextConfiguration TextConfiguration { get; private set; }
155 public bool GetFollowersSuccess { get; private set; } = false;
156 public bool GetNoRetweetSuccess { get; private set; } = false;
158 delegate void GetIconImageDelegate(PostClass post);
159 private readonly object LockObj = new object();
160 private ISet<long> followerId = new HashSet<long>();
161 private long[] noRTId = new long[0];
164 private List<string> _hashList = new List<string>();
166 //max_idで古い発言を取得するために保持(lists分は個別タブで管理)
167 private long minDirectmessage = long.MaxValue;
168 private long minDirectmessageSent = long.MaxValue;
170 private long previousStatusId = -1L;
172 //private FavoriteQueue favQueue;
174 //private List<PostClass> _deletemessages = new List<PostClass>();
176 public Twitter() : this(new TwitterApi())
180 public Twitter(TwitterApi api)
183 this.Configuration = TwitterConfiguration.DefaultConfiguration();
184 this.TextConfiguration = TwitterTextConfiguration.DefaultConfiguration();
187 public TwitterApiAccessLevel AccessLevel
188 => MyCommon.TwitterApiInfo.AccessLevel;
190 protected void ResetApiStatus()
191 => MyCommon.TwitterApiInfo.Reset();
193 public void ClearAuthInfo()
195 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
196 this.ResetApiStatus();
200 public void VerifyCredentials()
204 this.VerifyCredentialsAsync().Wait();
206 catch (AggregateException ex) when (ex.InnerException is WebApiException)
208 throw new WebApiException(ex.InnerException.Message, ex);
212 public async Task VerifyCredentialsAsync()
214 var user = await this.Api.AccountVerifyCredentials()
215 .ConfigureAwait(false);
217 this.UpdateUserStats(user);
220 public void Initialize(string token, string tokenSecret, string username, long userId)
223 if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(tokenSecret) || string.IsNullOrEmpty(username))
225 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
227 this.ResetApiStatus();
228 this.Api.Initialize(token, tokenSecret, userId, username);
229 if (SettingManager.Common.UserstreamStartup) this.ReconnectUserStream();
232 internal static string PreProcessUrl(string orgData)
236 //var IDNConveter = new IdnMapping();
237 var href = "<a href=\"";
241 if (orgData.IndexOf(href, posl2, StringComparison.Ordinal) > -1)
245 posl1 = orgData.IndexOf(href, posl2, StringComparison.Ordinal);
246 posl1 += href.Length;
247 posl2 = orgData.IndexOf("\"", posl1, StringComparison.Ordinal);
248 urlStr = orgData.Substring(posl1, posl2 - posl1);
250 if (!urlStr.StartsWith("http://", StringComparison.Ordinal)
251 && !urlStr.StartsWith("https://", StringComparison.Ordinal)
252 && !urlStr.StartsWith("ftp://", StringComparison.Ordinal))
257 var replacedUrl = MyCommon.IDNEncode(urlStr);
258 if (replacedUrl == null) continue;
259 if (replacedUrl == urlStr) continue;
261 orgData = orgData.Replace("<a href=\"" + urlStr, "<a href=\"" + replacedUrl);
272 public async Task PostStatus(PostStatusParams param)
274 this.CheckAccountState();
276 if (Twitter.DMSendTextRegex.IsMatch(param.Text))
278 var mediaId = param.MediaIds != null && param.MediaIds.Any() ? param.MediaIds[0] : (long?)null;
280 await this.SendDirectMessage(param.Text, mediaId)
281 .ConfigureAwait(false);
285 var response = await this.Api.StatusesUpdate(param.Text, param.InReplyToStatusId, param.MediaIds,
286 param.AutoPopulateReplyMetadata, param.ExcludeReplyUserIds, param.AttachmentUrl)
287 .ConfigureAwait(false);
289 var status = await response.LoadJsonAsync()
290 .ConfigureAwait(false);
292 this.UpdateUserStats(status.User);
294 if (status.Id == this.previousStatusId)
295 throw new WebApiException("OK:Delaying?");
297 this.previousStatusId = status.Id;
300 public async Task<long> UploadMedia(IMediaItem item, string mediaCategory = null)
302 this.CheckAccountState();
306 switch (item.Extension)
309 mediaType = "image/png";
313 mediaType = "image/jpeg";
316 mediaType = "image/gif";
319 mediaType = "application/octet-stream";
323 var initResponse = await this.Api.MediaUploadInit(item.Size, mediaType, mediaCategory)
324 .ConfigureAwait(false);
326 var initMedia = await initResponse.LoadJsonAsync()
327 .ConfigureAwait(false);
329 var mediaId = initMedia.MediaId;
331 await this.Api.MediaUploadAppend(mediaId, 0, item)
332 .ConfigureAwait(false);
334 var response = await this.Api.MediaUploadFinalize(mediaId)
335 .ConfigureAwait(false);
337 var media = await response.LoadJsonAsync()
338 .ConfigureAwait(false);
340 while (media.ProcessingInfo is TwitterUploadMediaResult.MediaProcessingInfo processingInfo)
342 switch (processingInfo.State)
351 throw new WebApiException($"Err:Upload failed ({processingInfo.Error?.Name})");
353 throw new WebApiException($"Err:Invalid state ({processingInfo.State})");
356 await Task.Delay(TimeSpan.FromSeconds(processingInfo.CheckAfterSecs ?? 5))
357 .ConfigureAwait(false);
359 media = await this.Api.MediaUploadStatus(mediaId)
360 .ConfigureAwait(false);
364 return media.MediaId;
367 public async Task SendDirectMessage(string postStr, long? mediaId = null)
369 this.CheckAccountState();
370 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
372 var mc = Twitter.DMSendTextRegex.Match(postStr);
374 var body = mc.Groups["body"].Value;
375 var recipientName = mc.Groups["id"].Value;
377 var recipient = await this.Api.UsersShow(recipientName)
378 .ConfigureAwait(false);
380 await this.Api.DirectMessagesEventsNew(recipient.Id, body, mediaId)
381 .ConfigureAwait(false);
384 public async Task PostRetweet(long id, bool read)
386 this.CheckAccountState();
389 var post = TabInformations.GetInstance()[id];
391 throw new WebApiException("Err:Target isn't found.");
393 var target = post.RetweetedId ?? id; //再RTの場合は元発言をRT
395 var response = await this.Api.StatusesRetweet(target)
396 .ConfigureAwait(false);
398 var status = await response.LoadJsonAsync()
399 .ConfigureAwait(false);
404 if (TabInformations.GetInstance().ContainsKey(status.Id))
409 if (status.RetweetedStatus == null)
410 throw new WebApiException("Invalid Json!");
413 post = CreatePostsFromStatusData(status);
420 if (this.ReadOwnPost) post.IsRead = true;
423 TabInformations.GetInstance().AddPost(post);
426 public string Username
427 => this.Api.CurrentScreenName;
430 => this.Api.CurrentUserId;
432 public static MyCommon.ACCOUNT_STATE AccountState { get; set; } = MyCommon.ACCOUNT_STATE.Valid;
433 public bool RestrictFavCheck { get; set; }
434 public bool ReadOwnPost { get; set; }
436 public int FollowersCount { get; private set; }
437 public int FriendsCount { get; private set; }
438 public int StatusesCount { get; private set; }
439 public string Location { get; private set; } = "";
440 public string Bio { get; private set; } = "";
442 /// <summary>ユーザーのフォロワー数などの情報を更新します</summary>
443 private void UpdateUserStats(TwitterUser self)
445 this.FollowersCount = self.FollowersCount;
446 this.FriendsCount = self.FriendsCount;
447 this.StatusesCount = self.StatusesCount;
448 this.Location = self.Location;
449 this.Bio = self.Description;
453 /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
455 public static bool VerifyApiResultCount(MyCommon.WORKERTYPE type, int count)
456 => count >= 20 && count <= GetMaxApiResultCount(type);
459 /// 渡された取得件数が更新時の取得可能範囲に収まっているか検証する
461 public static bool VerifyMoreApiResultCount(int count)
462 => count >= 20 && count <= 200;
465 /// 渡された取得件数が起動時の取得可能範囲に収まっているか検証する
467 public static bool VerifyFirstApiResultCount(int count)
468 => count >= 20 && count <= 200;
471 /// WORKERTYPEに応じた取得可能な最大件数を取得する
473 public static int GetMaxApiResultCount(MyCommon.WORKERTYPE type)
475 // 参照: REST APIs - 各endpointのcountパラメータ
476 // https://dev.twitter.com/rest/public
479 case MyCommon.WORKERTYPE.Timeline:
480 case MyCommon.WORKERTYPE.Reply:
481 case MyCommon.WORKERTYPE.UserTimeline:
482 case MyCommon.WORKERTYPE.Favorites:
483 case MyCommon.WORKERTYPE.DirectMessegeRcv:
484 case MyCommon.WORKERTYPE.DirectMessegeSnt:
485 case MyCommon.WORKERTYPE.List: // 不明
488 case MyCommon.WORKERTYPE.PublicSearch:
492 throw new InvalidOperationException("Invalid type: " + type);
497 /// WORKERTYPEに応じた取得件数を取得する
499 public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool startup)
501 if (type == MyCommon.WORKERTYPE.DirectMessegeRcv ||
502 type == MyCommon.WORKERTYPE.DirectMessegeSnt)
507 if (SettingManager.Common.UseAdditionalCount)
511 case MyCommon.WORKERTYPE.Favorites:
512 if (SettingManager.Common.FavoritesCountApi != 0)
513 return SettingManager.Common.FavoritesCountApi;
515 case MyCommon.WORKERTYPE.List:
516 if (SettingManager.Common.ListCountApi != 0)
517 return SettingManager.Common.ListCountApi;
519 case MyCommon.WORKERTYPE.PublicSearch:
520 if (SettingManager.Common.SearchCountApi != 0)
521 return SettingManager.Common.SearchCountApi;
523 case MyCommon.WORKERTYPE.UserTimeline:
524 if (SettingManager.Common.UserTimelineCountApi != 0)
525 return SettingManager.Common.UserTimelineCountApi;
528 if (more && SettingManager.Common.MoreCountApi != 0)
530 return Math.Min(SettingManager.Common.MoreCountApi, GetMaxApiResultCount(type));
532 if (startup && SettingManager.Common.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
534 return Math.Min(SettingManager.Common.FirstCountApi, GetMaxApiResultCount(type));
539 var count = SettingManager.Common.CountApi;
541 if (type == MyCommon.WORKERTYPE.Reply)
542 count = SettingManager.Common.CountApiReply;
544 return Math.Min(count, GetMaxApiResultCount(type));
547 public async Task GetHomeTimelineApi(bool read, HomeTabModel tab, bool more, bool startup)
549 this.CheckAccountState();
551 var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, startup);
553 TwitterStatus[] statuses;
556 statuses = await this.Api.StatusesHomeTimeline(count, maxId: tab.OldestId)
557 .ConfigureAwait(false);
561 statuses = await this.Api.StatusesHomeTimeline(count)
562 .ConfigureAwait(false);
565 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Timeline, tab, read);
566 if (minimumId != null)
567 tab.OldestId = minimumId.Value;
570 public async Task GetMentionsTimelineApi(bool read, MentionsTabModel tab, bool more, bool startup)
572 this.CheckAccountState();
574 var count = GetApiResultCount(MyCommon.WORKERTYPE.Reply, more, startup);
576 TwitterStatus[] statuses;
579 statuses = await this.Api.StatusesMentionsTimeline(count, maxId: tab.OldestId)
580 .ConfigureAwait(false);
584 statuses = await this.Api.StatusesMentionsTimeline(count)
585 .ConfigureAwait(false);
588 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Reply, tab, read);
589 if (minimumId != null)
590 tab.OldestId = minimumId.Value;
593 public async Task GetUserTimelineApi(bool read, string userName, UserTimelineTabModel tab, bool more)
595 this.CheckAccountState();
597 var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
599 TwitterStatus[] statuses;
600 if (string.IsNullOrEmpty(userName))
602 var target = tab.ScreenName;
603 if (string.IsNullOrEmpty(target)) return;
605 statuses = await this.Api.StatusesUserTimeline(userName, count)
606 .ConfigureAwait(false);
612 statuses = await this.Api.StatusesUserTimeline(userName, count, maxId: tab.OldestId)
613 .ConfigureAwait(false);
617 statuses = await this.Api.StatusesUserTimeline(userName, count)
618 .ConfigureAwait(false);
622 var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
624 if (minimumId != null)
625 tab.OldestId = minimumId.Value;
628 public async Task<PostClass> GetStatusApi(bool read, long id)
630 this.CheckAccountState();
632 var status = await this.Api.StatusesShow(id)
633 .ConfigureAwait(false);
635 var item = CreatePostsFromStatusData(status);
638 if (item.IsMe && !read && this.ReadOwnPost) item.IsRead = true;
643 public async Task GetStatusApi(bool read, long id, TabModel tab)
645 var post = await this.GetStatusApi(read, id)
646 .ConfigureAwait(false);
648 //非同期アイコン取得&StatusDictionaryに追加
649 if (tab != null && tab.IsInnerStorageTabType)
650 tab.AddPostQueue(post);
652 TabInformations.GetInstance().AddPost(post);
655 private PostClass CreatePostsFromStatusData(TwitterStatus status)
656 => this.CreatePostsFromStatusData(status, false);
658 private PostClass CreatePostsFromStatusData(TwitterStatus status, bool favTweet)
660 var post = new PostClass();
661 TwitterEntities entities;
664 post.StatusId = status.Id;
665 if (status.RetweetedStatus != null)
667 var retweeted = status.RetweetedStatus;
669 post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt);
672 post.RetweetedId = retweeted.Id;
674 post.TextFromApi = retweeted.FullText;
675 entities = retweeted.MergedEntities;
676 sourceHtml = retweeted.Source;
678 post.InReplyToStatusId = retweeted.InReplyToStatusId;
679 post.InReplyToUser = retweeted.InReplyToScreenName;
680 post.InReplyToUserId = status.InReplyToUserId;
689 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
690 post.IsFav = tc.Contains(retweeted.Id);
693 if (retweeted.Coordinates != null)
694 post.PostGeo = new PostClass.StatusGeo(retweeted.Coordinates.Coordinates[0], retweeted.Coordinates.Coordinates[1]);
697 var user = retweeted.User;
700 post.UserId = user.Id;
701 post.ScreenName = user.ScreenName;
702 post.Nickname = user.Name.Trim();
703 post.ImageUrl = user.ProfileImageUrlHttps;
704 post.IsProtect = user.Protected;
709 post.ScreenName = "?????";
710 post.Nickname = "Unknown User";
714 if (status.User != null)
716 post.RetweetedBy = status.User.ScreenName;
717 post.RetweetedByUserId = status.User.Id;
718 post.IsMe = post.RetweetedByUserId == this.UserId;
722 post.RetweetedBy = "?????";
723 post.RetweetedByUserId = 0L;
728 post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
730 post.TextFromApi = status.FullText;
731 entities = status.MergedEntities;
732 sourceHtml = status.Source;
733 post.InReplyToStatusId = status.InReplyToStatusId;
734 post.InReplyToUser = status.InReplyToScreenName;
735 post.InReplyToUserId = status.InReplyToUserId;
744 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
745 post.IsFav = tc.Contains(post.StatusId) && TabInformations.GetInstance()[post.StatusId].IsFav;
748 if (status.Coordinates != null)
749 post.PostGeo = new PostClass.StatusGeo(status.Coordinates.Coordinates[0], status.Coordinates.Coordinates[1]);
752 var user = status.User;
755 post.UserId = user.Id;
756 post.ScreenName = user.ScreenName;
757 post.Nickname = user.Name.Trim();
758 post.ImageUrl = user.ProfileImageUrlHttps;
759 post.IsProtect = user.Protected;
760 post.IsMe = post.UserId == this.UserId;
765 post.ScreenName = "?????";
766 post.Nickname = "Unknown User";
770 string textFromApi = post.TextFromApi;
772 var quotedStatusLink = (status.RetweetedStatus ?? status).QuotedStatusPermalink;
774 if (quotedStatusLink != null && entities.Urls.Any(x => x.ExpandedUrl == quotedStatusLink.Expanded))
775 quotedStatusLink = null; // 移行期は entities.urls と quoted_status_permalink の両方に含まれる場合がある
777 post.Text = CreateHtmlAnchor(textFromApi, entities, quotedStatusLink);
778 post.TextFromApi = textFromApi;
779 post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities, quotedStatusLink);
780 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
781 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
782 post.AccessibleText = CreateAccessibleText(textFromApi, entities, (status.RetweetedStatus ?? status).QuotedStatus, quotedStatusLink);
783 post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
784 post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
786 this.ExtractEntities(entities, post.ReplyToList, post.Media);
788 post.QuoteStatusIds = GetQuoteTweetStatusIds(entities, quotedStatusLink)
789 .Where(x => x != post.StatusId && x != post.RetweetedId)
790 .Distinct().ToArray();
792 post.ExpandedUrls = entities.OfType<TwitterEntityUrl>()
793 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
796 // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
797 if (post.Text == post.TextFromApi)
798 post.Text = post.TextFromApi;
799 if (post.AccessibleText == post.TextFromApi)
800 post.AccessibleText = post.TextFromApi;
802 // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
803 post.ScreenName = string.Intern(post.ScreenName);
804 post.Nickname = string.Intern(post.Nickname);
805 post.ImageUrl = string.Intern(post.ImageUrl);
806 post.RetweetedBy = post.RetweetedBy != null ? string.Intern(post.RetweetedBy) : null;
809 var (sourceText, sourceUri) = ParseSource(sourceHtml);
810 post.Source = string.Intern(sourceText);
811 post.SourceUri = sourceUri;
813 post.IsReply = post.RetweetedId == null && post.ReplyToList.Any(x => x.Item1 == this.UserId);
814 post.IsExcludeReply = false;
822 if (followerId.Count > 0) post.IsOwl = !followerId.Contains(post.UserId);
830 /// ツイートに含まれる引用ツイートのURLからステータスIDを抽出
832 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<TwitterEntity> entities, TwitterQuotedStatusPermalink quotedStatusLink)
834 var urls = entities.OfType<TwitterEntityUrl>().Select(x => x.ExpandedUrl);
836 if (quotedStatusLink != null)
837 urls = urls.Concat(new[] { quotedStatusLink.Expanded });
839 return GetQuoteTweetStatusIds(urls);
842 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<string> urls)
844 foreach (var url in urls)
846 var match = Twitter.StatusUrlRegex.Match(url);
849 if (long.TryParse(match.Groups["StatusId"].Value, out var statusId))
850 yield return statusId;
855 private long? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabModel tab, bool read)
857 long? minimumId = null;
859 foreach (var status in items)
861 if (minimumId == null || minimumId.Value > status.Id)
862 minimumId = status.Id;
869 if (TabInformations.GetInstance().ContainsKey(status.Id)) continue;
873 if (tab.Contains(status.Id)) continue;
878 if (gType != MyCommon.WORKERTYPE.UserTimeline &&
879 status.RetweetedStatus != null && this.noRTId.Contains(status.User.Id)) continue;
881 var post = CreatePostsFromStatusData(status);
884 if (post.IsMe && !read && this.ReadOwnPost) post.IsRead = true;
886 if (tab != null && tab.IsInnerStorageTabType)
887 tab.AddPostQueue(post);
889 TabInformations.GetInstance().AddPost(post);
895 private long? CreatePostsFromSearchJson(TwitterSearchResult items, PublicSearchTabModel tab, bool read, bool more)
897 long? minimumId = null;
899 foreach (var status in items.Statuses)
901 if (minimumId == null || minimumId.Value > status.Id)
902 minimumId = status.Id;
904 if (!more && status.Id > tab.SinceId) tab.SinceId = status.Id;
908 if (tab.Contains(status.Id)) continue;
911 var post = CreatePostsFromStatusData(status);
914 if ((post.IsMe && !read) && this.ReadOwnPost) post.IsRead = true;
916 tab.AddPostQueue(post);
922 private long? CreateFavoritePostsFromJson(TwitterStatus[] items, bool read)
924 var favTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
925 long? minimumId = null;
927 foreach (var status in items)
929 if (minimumId == null || minimumId.Value > status.Id)
930 minimumId = status.Id;
935 if (favTab.Contains(status.Id)) continue;
938 var post = CreatePostsFromStatusData(status, true);
942 TabInformations.GetInstance().AddPost(post);
948 public async Task GetListStatus(bool read, ListTimelineTabModel tab, bool more, bool startup)
950 var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
952 TwitterStatus[] statuses;
955 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, maxId: tab.OldestId, includeRTs: SettingManager.Common.IsListsIncludeRts)
956 .ConfigureAwait(false);
960 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, includeRTs: SettingManager.Common.IsListsIncludeRts)
961 .ConfigureAwait(false);
964 var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.List, tab, read);
966 if (minimumId != null)
967 tab.OldestId = minimumId.Value;
971 /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
973 /// <returns>posts の中から検索されたリプライチェインの末端</returns>
974 internal static PostClass FindTopOfReplyChain(IDictionary<Int64, PostClass> posts, Int64 startStatusId)
976 if (!posts.ContainsKey(startStatusId))
977 throw new ArgumentException("startStatusId (" + startStatusId + ") が posts の中から見つかりませんでした。", nameof(startStatusId));
979 var nextPost = posts[startStatusId];
980 while (nextPost.InReplyToStatusId != null)
982 if (!posts.ContainsKey(nextPost.InReplyToStatusId.Value))
984 nextPost = posts[nextPost.InReplyToStatusId.Value];
990 public async Task GetRelatedResult(bool read, RelatedPostsTabModel tab)
992 var targetPost = tab.TargetPost;
993 var relPosts = new Dictionary<Int64, PostClass>();
994 if (targetPost.TextFromApi.Contains("@") && targetPost.InReplyToStatusId == null)
997 var p = TabInformations.GetInstance()[targetPost.StatusId];
998 if (p != null && p.InReplyToStatusId != null)
1004 p = await this.GetStatusApi(read, targetPost.StatusId)
1005 .ConfigureAwait(false);
1009 relPosts.Add(targetPost.StatusId, targetPost);
1011 Exception lastException = null;
1013 // in_reply_to_status_id を使用してリプライチェインを辿る
1014 var nextPost = FindTopOfReplyChain(relPosts, targetPost.StatusId);
1016 while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
1018 var inReplyToId = nextPost.InReplyToStatusId.Value;
1020 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
1021 if (inReplyToPost == null)
1025 inReplyToPost = await this.GetStatusApi(read, inReplyToId)
1026 .ConfigureAwait(false);
1028 catch (WebApiException ex)
1035 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
1037 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
1040 //MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
1041 var text = targetPost.Text;
1042 var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
1043 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
1044 foreach (var _match in ma)
1046 if (Int64.TryParse(_match.Groups["StatusId"].Value, out var _statusId))
1048 if (relPosts.ContainsKey(_statusId))
1051 var p = TabInformations.GetInstance()[_statusId];
1056 p = await this.GetStatusApi(read, _statusId)
1057 .ConfigureAwait(false);
1059 catch (WebApiException ex)
1067 relPosts.Add(p.StatusId, p);
1071 relPosts.Values.ToList().ForEach(p =>
1073 if (p.IsMe && !read && this.ReadOwnPost)
1078 tab.AddPostQueue(p);
1081 if (lastException != null)
1082 throw new WebApiException(lastException.Message, lastException);
1085 public async Task GetSearch(bool read, PublicSearchTabModel tab, bool more)
1087 var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
1090 long? sinceId = null;
1093 maxId = tab.OldestId - 1;
1097 sinceId = tab.SinceId;
1100 var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
1101 .ConfigureAwait(false);
1103 if (!TabInformations.GetInstance().ContainsTab(tab))
1106 var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, more);
1108 if (minimumId != null)
1109 tab.OldestId = minimumId.Value;
1112 private void CreateDirectMessagesFromJson(TwitterDirectMessage[] item, MyCommon.WORKERTYPE gType, bool read)
1114 foreach (var message in item)
1116 var post = new PostClass();
1119 post.StatusId = message.Id;
1120 if (gType != MyCommon.WORKERTYPE.UserStream)
1122 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1124 if (minDirectmessage > post.StatusId) minDirectmessage = post.StatusId;
1128 if (minDirectmessageSent > post.StatusId) minDirectmessageSent = post.StatusId;
1135 if (TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage).Contains(post.StatusId)) continue;
1139 post.CreatedAt = MyCommon.DateTimeParse(message.CreatedAt);
1141 var textFromApi = message.Text;
1143 post.Text = CreateHtmlAnchor(textFromApi, message.Entities, quotedStatusLink: null);
1144 post.TextFromApi = this.ReplaceTextFromApi(textFromApi, message.Entities, quotedStatusLink: null);
1145 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
1146 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
1147 post.AccessibleText = CreateAccessibleText(textFromApi, message.Entities, quotedStatus: null, quotedStatusLink: null);
1148 post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
1149 post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
1152 this.ExtractEntities(message.Entities, post.ReplyToList, post.Media);
1154 post.QuoteStatusIds = GetQuoteTweetStatusIds(message.Entities, quotedStatusLink: null)
1155 .Distinct().ToArray();
1157 post.ExpandedUrls = message.Entities.OfType<TwitterEntityUrl>()
1158 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
1163 if (gType == MyCommon.WORKERTYPE.UserStream)
1165 if (this.Api.CurrentUserId == message.Recipient.Id)
1167 user = message.Sender;
1173 user = message.Recipient;
1180 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1182 user = message.Sender;
1188 user = message.Recipient;
1194 post.UserId = user.Id;
1195 post.ScreenName = user.ScreenName;
1196 post.Nickname = user.Name.Trim();
1197 post.ImageUrl = user.ProfileImageUrlHttps;
1198 post.IsProtect = user.Protected;
1200 // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
1201 if (post.Text == post.TextFromApi)
1202 post.Text = post.TextFromApi;
1203 if (post.AccessibleText == post.TextFromApi)
1204 post.AccessibleText = post.TextFromApi;
1206 // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
1207 post.ScreenName = string.Intern(post.ScreenName);
1208 post.Nickname = string.Intern(post.Nickname);
1209 post.ImageUrl = string.Intern(post.ImageUrl);
1213 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name);
1214 MessageBox.Show("Parse Error(CreateDirectMessagesFromJson)");
1219 if (post.IsMe && !read && this.ReadOwnPost) post.IsRead = true;
1220 post.IsReply = false;
1221 post.IsExcludeReply = false;
1224 var dmTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage);
1225 dmTab.AddPostQueue(post);
1229 public async Task GetDirectMessageApi(bool read, MyCommon.WORKERTYPE gType, bool more)
1231 this.CheckAccountState();
1232 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
1234 var count = GetApiResultCount(gType, more, false);
1236 TwitterDirectMessage[] messages;
1237 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1241 messages = await this.Api.DirectMessagesRecv(count, maxId: this.minDirectmessage)
1242 .ConfigureAwait(false);
1246 messages = await this.Api.DirectMessagesRecv(count)
1247 .ConfigureAwait(false);
1254 messages = await this.Api.DirectMessagesSent(count, maxId: this.minDirectmessageSent)
1255 .ConfigureAwait(false);
1259 messages = await this.Api.DirectMessagesSent(count)
1260 .ConfigureAwait(false);
1264 CreateDirectMessagesFromJson(messages, gType, read);
1267 public async Task GetFavoritesApi(bool read, FavoritesTabModel tab, bool backward)
1269 this.CheckAccountState();
1271 var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, backward, false);
1273 TwitterStatus[] statuses;
1276 statuses = await this.Api.FavoritesList(count, maxId: tab.OldestId)
1277 .ConfigureAwait(false);
1281 statuses = await this.Api.FavoritesList(count)
1282 .ConfigureAwait(false);
1285 var minimumId = this.CreateFavoritePostsFromJson(statuses, read);
1287 if (minimumId != null)
1288 tab.OldestId = minimumId.Value;
1291 private string ReplaceTextFromApi(string text, TwitterEntities entities, TwitterQuotedStatusPermalink quotedStatusLink)
1293 if (entities != null)
1295 if (entities.Urls != null)
1297 foreach (var m in entities.Urls)
1299 if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1302 if (entities.Media != null)
1304 foreach (var m in entities.Media)
1306 if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1311 if (quotedStatusLink != null)
1312 text += " " + quotedStatusLink.Display;
1317 internal static string CreateAccessibleText(string text, TwitterEntities entities, TwitterStatus quotedStatus, TwitterQuotedStatusPermalink quotedStatusLink)
1319 if (entities == null)
1322 if (entities.Urls != null)
1324 foreach (var entity in entities.Urls)
1326 if (quotedStatus != null)
1328 var matchStatusUrl = Twitter.StatusUrlRegex.Match(entity.ExpandedUrl);
1329 if (matchStatusUrl.Success && matchStatusUrl.Groups["StatusId"].Value == quotedStatus.IdStr)
1331 var quotedText = CreateAccessibleText(quotedStatus.FullText, quotedStatus.MergedEntities, quotedStatus: null, quotedStatusLink: null);
1332 text = text.Replace(entity.Url, string.Format(Properties.Resources.QuoteStatus_AccessibleText, quotedStatus.User.ScreenName, quotedText));
1337 if (!string.IsNullOrEmpty(entity.DisplayUrl))
1338 text = text.Replace(entity.Url, entity.DisplayUrl);
1342 if (entities.Media != null)
1344 foreach (var entity in entities.Media)
1346 if (!string.IsNullOrEmpty(entity.AltText))
1348 text = text.Replace(entity.Url, string.Format(Properties.Resources.ImageAltText, entity.AltText));
1350 else if (!string.IsNullOrEmpty(entity.DisplayUrl))
1352 text = text.Replace(entity.Url, entity.DisplayUrl);
1357 if (quotedStatusLink != null)
1359 var quoteText = CreateAccessibleText(quotedStatus.FullText, quotedStatus.MergedEntities, quotedStatus: null, quotedStatusLink: null);
1360 text += " " + string.Format(Properties.Resources.QuoteStatus_AccessibleText, quotedStatus.User.ScreenName, quoteText);
1369 /// <exception cref="WebApiException"/>
1370 public async Task RefreshFollowerIds()
1372 if (MyCommon._endingFlag) return;
1375 var newFollowerIds = new HashSet<long>();
1378 var ret = await this.Api.FollowersIds(cursor)
1379 .ConfigureAwait(false);
1381 if (ret.Ids == null)
1382 throw new WebApiException("ret.ids == null");
1384 newFollowerIds.UnionWith(ret.Ids);
1385 cursor = ret.NextCursor;
1386 } while (cursor != 0);
1388 this.followerId = newFollowerIds;
1389 TabInformations.GetInstance().RefreshOwl(this.followerId);
1391 this.GetFollowersSuccess = true;
1395 /// RT 非表示ユーザーを更新します
1397 /// <exception cref="WebApiException"/>
1398 public async Task RefreshNoRetweetIds()
1400 if (MyCommon._endingFlag) return;
1402 this.noRTId = await this.Api.NoRetweetIds()
1403 .ConfigureAwait(false);
1405 this.GetNoRetweetSuccess = true;
1409 /// t.co の文字列長などの設定情報を更新します
1411 /// <exception cref="WebApiException"/>
1412 public async Task RefreshConfiguration()
1414 this.Configuration = await this.Api.Configuration()
1415 .ConfigureAwait(false);
1417 // TextConfiguration 相当の JSON を得る API が存在しないため、TransformedURLLength のみ help/configuration.json に合わせて更新する
1418 this.TextConfiguration.TransformedURLLength = this.Configuration.ShortUrlLengthHttps;
1421 public async Task GetListsApi()
1423 this.CheckAccountState();
1425 var ownedLists = await TwitterLists.GetAllItemsAsync(x =>
1426 this.Api.ListsOwnerships(this.Username, cursor: x, count: 1000))
1427 .ConfigureAwait(false);
1429 var subscribedLists = await TwitterLists.GetAllItemsAsync(x =>
1430 this.Api.ListsSubscriptions(this.Username, cursor: x, count: 1000))
1431 .ConfigureAwait(false);
1433 TabInformations.GetInstance().SubscribableLists = Enumerable.Concat(ownedLists, subscribedLists)
1434 .Select(x => new ListElement(x, this))
1438 public async Task DeleteList(long listId)
1440 await this.Api.ListsDestroy(listId)
1442 .ConfigureAwait(false);
1444 var tabinfo = TabInformations.GetInstance();
1446 tabinfo.SubscribableLists = tabinfo.SubscribableLists
1447 .Where(x => x.Id != listId)
1451 public async Task<ListElement> EditList(long listId, string new_name, bool isPrivate, string description)
1453 var response = await this.Api.ListsUpdate(listId, new_name, description, isPrivate)
1454 .ConfigureAwait(false);
1456 var list = await response.LoadJsonAsync()
1457 .ConfigureAwait(false);
1459 return new ListElement(list, this);
1462 public async Task<long> GetListMembers(long listId, List<UserInfo> lists, long cursor)
1464 this.CheckAccountState();
1466 var users = await this.Api.ListsMembers(listId, cursor)
1467 .ConfigureAwait(false);
1469 Array.ForEach(users.Users, u => lists.Add(new UserInfo(u)));
1471 return users.NextCursor;
1474 public async Task CreateListApi(string listName, bool isPrivate, string description)
1476 this.CheckAccountState();
1478 var response = await this.Api.ListsCreate(listName, description, isPrivate)
1479 .ConfigureAwait(false);
1481 var list = await response.LoadJsonAsync()
1482 .ConfigureAwait(false);
1484 TabInformations.GetInstance().SubscribableLists.Add(new ListElement(list, this));
1487 public async Task<bool> ContainsUserAtList(long listId, string user)
1489 this.CheckAccountState();
1493 await this.Api.ListsMembersShow(listId, user)
1494 .ConfigureAwait(false);
1498 catch (TwitterApiException ex)
1499 when (ex.ErrorResponse.Errors.Any(x => x.Code == TwitterErrorCode.NotFound))
1505 private void ExtractEntities(TwitterEntities entities, List<Tuple<long, string>> AtList, List<MediaInfo> media)
1507 if (entities != null)
1509 if (entities.Hashtags != null)
1513 this._hashList.AddRange(entities.Hashtags.Select(x => "#" + x.Text));
1516 if (entities.UserMentions != null)
1518 foreach (var ent in entities.UserMentions)
1520 AtList.Add(Tuple.Create(ent.Id, ent.ScreenName));
1523 if (entities.Media != null)
1527 foreach (var ent in entities.Media)
1529 if (!media.Any(x => x.Url == ent.MediaUrlHttps))
1531 if (ent.VideoInfo != null &&
1532 ent.Type == "animated_gif" || ent.Type == "video")
1534 //var videoUrl = ent.VideoInfo.Variants
1535 // .Where(v => v.ContentType == "video/mp4")
1536 // .OrderByDescending(v => v.Bitrate)
1537 // .Select(v => v.Url).FirstOrDefault();
1538 media.Add(new MediaInfo(ent.MediaUrlHttps, ent.AltText, ent.ExpandedUrl));
1541 media.Add(new MediaInfo(ent.MediaUrlHttps, ent.AltText, videoUrl: null));
1549 internal static string CreateHtmlAnchor(string text, TwitterEntities entities, TwitterQuotedStatusPermalink quotedStatusLink)
1551 // PostClass.ExpandedUrlInfo を使用して非同期に URL 展開を行うためここでは expanded_url を使用しない
1552 text = TweetFormatter.AutoLinkHtml(text, entities, keepTco: true);
1554 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>");
1555 text = PreProcessUrl(text); //IDN置換
1557 if (quotedStatusLink != null)
1559 text += string.Format(" <a href=\"{0}\" title=\"{0}\">{1}</a>",
1560 WebUtility.HtmlEncode(quotedStatusLink.Url),
1561 WebUtility.HtmlEncode(quotedStatusLink.Display));
1567 private static readonly Uri SourceUriBase = new Uri("https://twitter.com/");
1570 /// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
1572 internal static (string SourceText, Uri SourceUri) ParseSource(string sourceHtml)
1574 if (string.IsNullOrEmpty(sourceHtml))
1580 // sourceHtmlの例: <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>
1582 var match = Regex.Match(sourceHtml, "^<a href=\"(?<uri>.+?)\".*?>(?<text>.+)</a>$", RegexOptions.IgnoreCase);
1585 sourceText = WebUtility.HtmlDecode(match.Groups["text"].Value);
1588 var uriStr = WebUtility.HtmlDecode(match.Groups["uri"].Value);
1589 sourceUri = new Uri(SourceUriBase, uriStr);
1591 catch (UriFormatException)
1598 sourceText = WebUtility.HtmlDecode(sourceHtml);
1602 return (sourceText, sourceUri);
1605 public async Task<TwitterApiStatus> GetInfoApi()
1607 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
1609 if (MyCommon._endingFlag) return null;
1611 var limits = await this.Api.ApplicationRateLimitStatus()
1612 .ConfigureAwait(false);
1614 MyCommon.TwitterApiInfo.UpdateFromJson(limits);
1616 return MyCommon.TwitterApiInfo;
1620 /// ブロック中のユーザーを更新します
1622 /// <exception cref="WebApiException"/>
1623 public async Task RefreshBlockIds()
1625 if (MyCommon._endingFlag) return;
1628 var newBlockIds = new HashSet<long>();
1631 var ret = await this.Api.BlocksIds(cursor)
1632 .ConfigureAwait(false);
1634 newBlockIds.UnionWith(ret.Ids);
1635 cursor = ret.NextCursor;
1636 } while (cursor != 0);
1638 newBlockIds.Remove(this.UserId); // 元のソースにあったので一応残しておく
1640 TabInformations.GetInstance().BlockIds = newBlockIds;
1644 /// ミュート中のユーザーIDを更新します
1646 /// <exception cref="WebApiException"/>
1647 public async Task RefreshMuteUserIdsAsync()
1649 if (MyCommon._endingFlag) return;
1651 var ids = await TwitterIds.GetAllItemsAsync(x => this.Api.MutesUsersIds(x))
1652 .ConfigureAwait(false);
1654 TabInformations.GetInstance().MuteUserIds = new HashSet<long>(ids);
1657 public string[] GetHashList()
1662 hashArray = _hashList.ToArray();
1668 public string AccessToken
1669 => ((TwitterApiConnection)this.Api.Connection).AccessToken;
1671 public string AccessTokenSecret
1672 => ((TwitterApiConnection)this.Api.Connection).AccessSecret;
1674 private void CheckAccountState()
1676 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1677 throw new WebApiException("Auth error. Check your account");
1680 private void CheckAccessLevel(TwitterApiAccessLevel accessLevelFlags)
1682 if (!this.AccessLevel.HasFlag(accessLevelFlags))
1683 throw new WebApiException("Auth Err:try to re-authorization.");
1686 public int GetTextLengthRemain(string postText)
1688 var matchDm = Twitter.DMSendTextRegex.Match(postText);
1689 if (matchDm.Success)
1690 return this.GetTextLengthRemainDM(matchDm.Groups["body"].Value);
1692 return this.GetTextLengthRemainWeighted(postText);
1695 private int GetTextLengthRemainDM(string postText)
1700 while (pos < postText.Length)
1704 if (char.IsSurrogatePair(postText, pos))
1705 pos += 2; // サロゲートペアの場合は2文字分進める
1710 var urls = TweetExtractor.ExtractUrls(postText);
1711 foreach (var url in urls)
1713 var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
1714 ? this.Configuration.ShortUrlLengthHttps
1715 : this.Configuration.ShortUrlLength;
1717 textLength += shortUrlLength - url.Length;
1720 return this.Configuration.DmTextCharacterLimit - textLength;
1723 private int GetTextLengthRemainWeighted(string postText)
1725 var config = this.TextConfiguration;
1726 var totalWeight = 0;
1728 var urls = TweetExtractor.ExtractUrlEntities(postText).ToArray();
1731 while (pos < postText.Length)
1733 var urlEntity = urls.FirstOrDefault(x => x.Indices[0] == pos);
1734 if (urlEntity != null)
1736 totalWeight += config.TransformedURLLength * config.Scale;
1738 var urlLength = urlEntity.Indices[1] - urlEntity.Indices[0];
1744 var codepoint = postText.GetCodepointAtSafe(pos);
1745 var weight = config.DefaultWeight;
1747 foreach (var weightRange in config.Ranges)
1749 if (codepoint >= weightRange.Start && codepoint <= weightRange.End)
1751 weight = weightRange.Weight;
1756 totalWeight += weight;
1758 var isSurrogatePair = codepoint > 0xffff;
1759 if (isSurrogatePair)
1760 pos += 2; // サロゲートペアの場合は2文字分進める
1765 var remainWeight = config.MaxWeightedTweetLength * config.Scale - totalWeight;
1767 return remainWeight / config.Scale;
1771 #region "UserStream"
1772 public string TrackWord { get; set; } = "";
1773 public bool AllAtReply { get; set; } = false;
1775 public event EventHandler NewPostFromStream;
1776 public event EventHandler UserStreamStarted;
1777 public event EventHandler UserStreamStopped;
1778 public event EventHandler<PostDeletedEventArgs> PostDeleted;
1779 public event EventHandler<UserStreamEventReceivedEventArgs> UserStreamEventReceived;
1780 private DateTimeUtc _lastUserstreamDataReceived;
1781 private TwitterUserstream userStream;
1783 public class FormattedEvent
1785 public MyCommon.EVENTTYPE Eventtype { get; set; }
1786 public DateTimeUtc CreatedAt { get; set; }
1787 public string Event { get; set; }
1788 public string Username { get; set; }
1789 public string Target { get; set; }
1790 public Int64 Id { get; set; }
1791 public bool IsMe { get; set; }
1794 public List<FormattedEvent> StoredEvent { get; } = new List<FormattedEvent>();
1796 private readonly IReadOnlyDictionary<string, MyCommon.EVENTTYPE> eventTable = new Dictionary<string, MyCommon.EVENTTYPE>
1798 ["favorite"] = MyCommon.EVENTTYPE.Favorite,
1799 ["unfavorite"] = MyCommon.EVENTTYPE.Unfavorite,
1800 ["follow"] = MyCommon.EVENTTYPE.Follow,
1801 ["list_member_added"] = MyCommon.EVENTTYPE.ListMemberAdded,
1802 ["list_member_removed"] = MyCommon.EVENTTYPE.ListMemberRemoved,
1803 ["block"] = MyCommon.EVENTTYPE.Block,
1804 ["unblock"] = MyCommon.EVENTTYPE.Unblock,
1805 ["user_update"] = MyCommon.EVENTTYPE.UserUpdate,
1806 ["deleted"] = MyCommon.EVENTTYPE.Deleted,
1807 ["list_created"] = MyCommon.EVENTTYPE.ListCreated,
1808 ["list_destroyed"] = MyCommon.EVENTTYPE.ListDestroyed,
1809 ["list_updated"] = MyCommon.EVENTTYPE.ListUpdated,
1810 ["unfollow"] = MyCommon.EVENTTYPE.Unfollow,
1811 ["list_user_subscribed"] = MyCommon.EVENTTYPE.ListUserSubscribed,
1812 ["list_user_unsubscribed"] = MyCommon.EVENTTYPE.ListUserUnsubscribed,
1813 ["mute"] = MyCommon.EVENTTYPE.Mute,
1814 ["unmute"] = MyCommon.EVENTTYPE.Unmute,
1815 ["quoted_tweet"] = MyCommon.EVENTTYPE.QuotedTweet,
1818 public bool IsUserstreamDataReceived
1819 => (DateTimeUtc.Now - this._lastUserstreamDataReceived).TotalSeconds < 31;
1821 private void userStream_StatusArrived(ITwitterStreamMessage message)
1823 this._lastUserstreamDataReceived = DateTimeUtc.Now;
1827 case StreamMessageStatus statusMessage:
1828 var status = statusMessage.Status.Normalize();
1830 if (status.RetweetedStatus is TwitterStatus retweetedStatus)
1832 var sourceUserId = statusMessage.Status.User.Id;
1833 var targetUserId = retweetedStatus.User.Id;
1835 // 自分に関係しないリツイートの場合は無視する
1836 var selfUserId = this.UserId;
1837 if (sourceUserId == selfUserId || targetUserId == selfUserId)
1839 // 公式 RT をイベントとしても扱う
1840 var evt = this.CreateEventFromRetweet(status);
1841 this.StoredEvent.Insert(0, evt);
1842 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
1844 // 従来通り公式 RT の表示も行うため break しない
1847 this.CreatePostsFromJson(new[] { status }, MyCommon.WORKERTYPE.UserStream, null, false);
1848 this.NewPostFromStream?.Invoke(this, EventArgs.Empty);
1851 case StreamMessageDirectMessage dmMessage:
1852 this.CreateDirectMessagesFromJson(new[] { dmMessage.DirectMessage }, MyCommon.WORKERTYPE.UserStream, false);
1853 this.NewPostFromStream?.Invoke(this, EventArgs.Empty);
1856 case StreamMessageDelete deleteMessage:
1857 var deletedId = deleteMessage.Status?.Id ?? deleteMessage.DirectMessage?.Id;
1858 if (deletedId == null)
1861 this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(deletedId.Value));
1863 foreach (var index in MyCommon.CountDown(this.StoredEvent.Count - 1, 0))
1865 var evt = this.StoredEvent[index];
1866 if (evt.Id == deletedId.Value && (evt.Event == "favorite" || evt.Event == "unfavorite"))
1868 this.StoredEvent.RemoveAt(index);
1873 case StreamMessageEvent eventMessage:
1874 this.CreateEventFromJson(eventMessage);
1877 case StreamMessageScrubGeo scrubGeoMessage:
1878 TabInformations.GetInstance().ScrubGeoReserve(scrubGeoMessage.UserId, scrubGeoMessage.UpToStatusId);
1887 /// UserStreamsから受信した公式RTをイベントに変換します
1889 private FormattedEvent CreateEventFromRetweet(TwitterStatus status)
1891 return new FormattedEvent
1893 Eventtype = MyCommon.EVENTTYPE.Retweet,
1895 CreatedAt = MyCommon.DateTimeParse(status.CreatedAt),
1896 IsMe = status.User.Id == this.UserId,
1897 Username = status.User.ScreenName,
1898 Target = string.Format("@{0}:{1}", new[]
1900 status.RetweetedStatus.User.ScreenName,
1901 WebUtility.HtmlDecode(status.RetweetedStatus.FullText),
1903 Id = status.RetweetedStatus.Id,
1907 private void CreateEventFromJson(StreamMessageEvent message)
1909 var eventData = message.Event;
1911 var evt = new FormattedEvent
1913 CreatedAt = MyCommon.DateTimeParse(eventData.CreatedAt),
1914 Event = eventData.Event,
1915 Username = eventData.Source.ScreenName,
1916 IsMe = eventData.Source.Id == this.UserId,
1917 Eventtype = eventTable.TryGetValue(eventData.Event, out var eventType) ? eventType : MyCommon.EVENTTYPE.None,
1920 TwitterStreamEvent<TwitterStatusCompat> tweetEvent;
1921 TwitterStatus tweet;
1923 switch (eventData.Event)
1925 case "access_revoked":
1926 case "access_unrevoked":
1928 case "user_suspend":
1931 if (eventData.Target.Id == this.UserId)
1933 if (!this.followerId.Contains(eventData.Source.Id)) this.followerId.Add(eventData.Source.Id);
1937 return; //Block後のUndoをすると、SourceとTargetが逆転したfollowイベントが帰ってくるため。
1942 evt.Target = "@" + eventData.Target.ScreenName;
1944 case "favorited_retweet":
1945 case "retweeted_retweet":
1949 tweetEvent = message.ParseTargetObjectAs<TwitterStatusCompat>();
1950 tweet = tweetEvent.TargetObject.Normalize();
1951 evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
1954 if (SettingManager.Common.IsRemoveSameEvent)
1956 if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
1960 var tabinfo = TabInformations.GetInstance();
1962 var statusId = tweet.Id;
1963 if (!tabinfo.Posts.TryGetValue(statusId, out var post))
1966 if (eventData.Event == "favorite")
1968 var favTab = tabinfo.GetTabByType(MyCommon.TabUsageType.Favorites);
1969 favTab.AddPostQueue(post);
1971 if (tweetEvent.Source.Id == this.UserId)
1975 else if (tweetEvent.Target.Id == this.UserId)
1977 post.FavoritedCount++;
1979 if (SettingManager.Common.FavEventUnread)
1980 tabinfo.SetReadAllTab(post.StatusId, read: false);
1985 if (tweetEvent.Source.Id == this.UserId)
1989 else if (tweetEvent.Target.Id == this.UserId)
1991 post.FavoritedCount = Math.Max(0, post.FavoritedCount - 1);
1995 case "quoted_tweet":
1996 if (evt.IsMe) return;
1998 tweetEvent = message.ParseTargetObjectAs<TwitterStatusCompat>();
1999 tweet = tweetEvent.TargetObject.Normalize();
2000 evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
2003 if (SettingManager.Common.IsRemoveSameEvent)
2005 if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2009 case "list_member_added":
2010 case "list_member_removed":
2011 case "list_created":
2012 case "list_destroyed":
2013 case "list_updated":
2014 case "list_user_subscribed":
2015 case "list_user_unsubscribed":
2016 var listEvent = message.ParseTargetObjectAs<TwitterList>();
2017 evt.Target = listEvent.TargetObject.FullName;
2020 if (!TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Add(eventData.Target.Id);
2024 if (TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Remove(eventData.Target.Id);
2033 evt.Target = "@" + eventData.Target.ScreenName;
2034 if (!TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2036 TabInformations.GetInstance().MuteUserIds.Add(eventData.Target.Id);
2040 evt.Target = "@" + eventData.Target.ScreenName;
2041 if (TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2043 TabInformations.GetInstance().MuteUserIds.Remove(eventData.Target.Id);
2048 MyCommon.TraceOut("Unknown Event:" + evt.Event + Environment.NewLine + message.Json);
2051 this.StoredEvent.Insert(0, evt);
2053 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
2056 private void userStream_Started()
2057 => this.UserStreamStarted?.Invoke(this, EventArgs.Empty);
2059 private void userStream_Stopped()
2060 => this.UserStreamStopped?.Invoke(this, EventArgs.Empty);
2062 public bool UserStreamActive
2063 => this.userStream != null && this.userStream.IsStreamActive;
2065 public void StartUserStream()
2067 var newStream = new TwitterUserstream(this.Api);
2069 newStream.StatusArrived += userStream_StatusArrived;
2070 newStream.Started += userStream_Started;
2071 newStream.Stopped += userStream_Stopped;
2073 newStream.Start(this.AllAtReply, this.TrackWord);
2075 var oldStream = Interlocked.Exchange(ref this.userStream, newStream);
2076 oldStream?.Dispose();
2079 public void StopUserStream()
2081 var oldStream = Interlocked.Exchange(ref this.userStream, null);
2082 oldStream?.Dispose();
2085 public void ReconnectUserStream()
2087 if (this.userStream != null)
2089 this.StartUserStream();
2093 private class TwitterUserstream : IDisposable
2095 public bool AllAtReplies { get; private set; }
2096 public string TrackWords { get; private set; }
2098 public bool IsStreamActive { get; private set; }
2100 public event Action<ITwitterStreamMessage> StatusArrived;
2101 public event Action Stopped;
2102 public event Action Started;
2104 private TwitterApi twitterApi;
2106 private Task streamTask;
2107 private CancellationTokenSource streamCts;
2109 public TwitterUserstream(TwitterApi twitterApi)
2110 => this.twitterApi = twitterApi;
2112 public void Start(bool allAtReplies, string trackwords)
2114 this.AllAtReplies = allAtReplies;
2115 this.TrackWords = trackwords;
2117 var cts = new CancellationTokenSource();
2119 this.streamCts = cts;
2120 this.streamTask = Task.Run(async () =>
2124 await this.UserStreamLoop(cts.Token)
2125 .ConfigureAwait(false);
2127 catch (OperationCanceledException) { }
2133 this.streamCts?.Cancel();
2135 // streamTask の完了を待たずに IsStreamActive を false にセットする
2136 this.IsStreamActive = false;
2137 this.Stopped?.Invoke();
2140 private async Task UserStreamLoop(CancellationToken cancellationToken)
2142 TimeSpan sleep = TimeSpan.Zero;
2145 if (sleep != TimeSpan.Zero)
2147 await Task.Delay(sleep, cancellationToken)
2148 .ConfigureAwait(false);
2149 sleep = TimeSpan.Zero;
2152 if (!MyCommon.IsNetworkAvailable())
2154 sleep = TimeSpan.FromSeconds(30);
2158 this.IsStreamActive = true;
2159 this.Started?.Invoke();
2163 var replies = this.AllAtReplies ? "all" : null;
2165 var observable = this.twitterApi.UserStreams(replies, this.TrackWords);
2167 await observable.ForEachAsync(x => this.StatusArrived?.Invoke(x), cancellationToken);
2169 // キャンセルされていないのにストリームが終了した場合
2170 sleep = TimeSpan.FromSeconds(30);
2172 catch (TwitterApiException) { sleep = TimeSpan.FromSeconds(30); }
2173 catch (IOException) { sleep = TimeSpan.FromSeconds(30); }
2174 catch (OperationCanceledException)
2176 if (cancellationToken.IsCancellationRequested)
2179 // cancellationToken によるキャンセルではない(=タイムアウトエラー)
2180 sleep = TimeSpan.FromSeconds(30);
2182 catch (Exception ex)
2184 MyCommon.ExceptionOut(ex);
2185 sleep = TimeSpan.FromSeconds(30);
2189 this.IsStreamActive = false;
2190 this.Stopped?.Invoke();
2195 private bool disposed = false;
2197 public void Dispose()
2202 this.disposed = true;
2206 this.Started = null;
2207 this.Stopped = null;
2208 this.StatusArrived = null;
2213 #region "IDisposable Support"
2214 private bool disposedValue; // 重複する呼び出しを検出するには
2217 protected virtual void Dispose(bool disposing)
2219 if (!this.disposedValue)
2223 this.StopUserStream();
2226 this.disposedValue = true;
2229 //protected Overrides void Finalize()
2231 // // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
2233 // MyBase.Finalize()
2236 // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
2237 public void Dispose()
2239 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
2241 GC.SuppressFinalize(this);
2246 public class PostDeletedEventArgs : EventArgs
2248 public long StatusId { get; }
2250 public PostDeletedEventArgs(long statusId)
2251 => this.StatusId = statusId;
2254 public class UserStreamEventReceivedEventArgs : EventArgs
2256 public Twitter.FormattedEvent EventData { get; }
2258 public UserStreamEventReceivedEventArgs(Twitter.FormattedEvent eventData)
2259 => this.EventData = eventData;