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 /// FavstarやaclogなどTwitter関連サービスのパーマリンクURLからステータスIDを抽出する正規表現
138 public static readonly Regex ThirdPartyStatusUrlRegex = new Regex(@"https?://(?:[^.]+\.)?(?:
139 favstar\.fm/users/[a-zA-Z0-9_]+/status/ # Favstar
140 | favstar\.fm/t/ # Favstar (short)
141 | aclog\.koba789\.com/i/ # aclog
142 | frtrt\.net/solo_status\.php\?status= # RtRT
143 )(?<StatusId>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
146 /// DM送信かどうかを判定する正規表現
148 public static readonly Regex DMSendTextRegex = new Regex(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
150 public TwitterApi Api { get; }
151 public TwitterConfiguration Configuration { get; private set; }
153 delegate void GetIconImageDelegate(PostClass post);
154 private readonly object LockObj = new object();
155 private ISet<long> followerId = new HashSet<long>();
156 private bool _GetFollowerResult = false;
157 private long[] noRTId = new long[0];
158 private bool _GetNoRetweetResult = false;
161 private string _uname;
163 private bool _readOwnPost;
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();
186 public TwitterApiAccessLevel AccessLevel
190 return MyCommon.TwitterApiInfo.AccessLevel;
194 protected void ResetApiStatus()
196 MyCommon.TwitterApiInfo.Reset();
199 public void ClearAuthInfo()
201 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
202 this.ResetApiStatus();
206 public void VerifyCredentials()
210 this.VerifyCredentialsAsync().Wait();
212 catch (AggregateException ex) when (ex.InnerException is WebApiException)
214 throw new WebApiException(ex.InnerException.Message, ex);
218 public async Task VerifyCredentialsAsync()
220 var user = await this.Api.AccountVerifyCredentials()
221 .ConfigureAwait(false);
223 this.UpdateUserStats(user);
226 public void Initialize(string token, string tokenSecret, string username, long userId)
229 if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(tokenSecret) || string.IsNullOrEmpty(username))
231 Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
233 this.ResetApiStatus();
234 this.Api.Initialize(token, tokenSecret, userId, username);
235 _uname = username.ToLowerInvariant();
236 if (SettingManager.Common.UserstreamStartup) this.ReconnectUserStream();
239 public string PreProcessUrl(string orgData)
243 //var IDNConveter = new IdnMapping();
244 var href = "<a href=\"";
248 if (orgData.IndexOf(href, posl2, StringComparison.Ordinal) > -1)
252 posl1 = orgData.IndexOf(href, posl2, StringComparison.Ordinal);
253 posl1 += href.Length;
254 posl2 = orgData.IndexOf("\"", posl1, StringComparison.Ordinal);
255 urlStr = orgData.Substring(posl1, posl2 - posl1);
257 if (!urlStr.StartsWith("http://", StringComparison.Ordinal)
258 && !urlStr.StartsWith("https://", StringComparison.Ordinal)
259 && !urlStr.StartsWith("ftp://", StringComparison.Ordinal))
264 var replacedUrl = MyCommon.IDNEncode(urlStr);
265 if (replacedUrl == null) continue;
266 if (replacedUrl == urlStr) continue;
268 orgData = orgData.Replace("<a href=\"" + urlStr, "<a href=\"" + replacedUrl);
279 public async Task PostStatus(string postStr, long? reply_to, IReadOnlyList<long> mediaIds = null)
281 this.CheckAccountState();
283 if (mediaIds == null &&
284 Twitter.DMSendTextRegex.IsMatch(postStr))
286 await this.SendDirectMessage(postStr)
287 .ConfigureAwait(false);
291 var response = await this.Api.StatusesUpdate(postStr, reply_to, mediaIds)
292 .ConfigureAwait(false);
294 var status = await response.LoadJsonAsync()
295 .ConfigureAwait(false);
297 this.UpdateUserStats(status.User);
299 if (status.Id == this.previousStatusId)
300 throw new WebApiException("OK:Delaying?");
302 this.previousStatusId = status.Id;
305 public Task<long> UploadMedia(IMediaItem item)
306 => this.UploadMedia(item, SettingManager.Common.AlphaPNGWorkaround);
308 public async Task<long> UploadMedia(IMediaItem item, bool alphaPNGWorkaround)
310 this.CheckAccountState();
312 LazyJson<TwitterUploadMediaResult> response;
314 using (var origImage = item.CreateImage())
316 MemoryImage newImage;
317 if (alphaPNGWorkaround && this.AddAlphaChannelIfNeeded(origImage.Image, out 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 (_readOwnPost) post.IsRead = true;
434 TabInformations.GetInstance().AddPost(post);
437 public string Username
438 => this.Api.CurrentScreenName;
441 => this.Api.CurrentUserId;
443 private static MyCommon.ACCOUNT_STATE _accountState = MyCommon.ACCOUNT_STATE.Valid;
444 public static MyCommon.ACCOUNT_STATE AccountState
448 return _accountState;
452 _accountState = value;
456 public bool RestrictFavCheck { get; set; }
458 public bool ReadOwnPost
466 _readOwnPost = value;
470 public int FollowersCount { get; private set; }
471 public int FriendsCount { get; private set; }
472 public int StatusesCount { get; private set; }
473 public string Location { get; private set; } = "";
474 public string Bio { get; private set; } = "";
476 /// <summary>ユーザーのフォロワー数などの情報を更新します</summary>
477 private void UpdateUserStats(TwitterUser self)
479 this.FollowersCount = self.FollowersCount;
480 this.FriendsCount = self.FriendsCount;
481 this.StatusesCount = self.StatusesCount;
482 this.Location = self.Location;
483 this.Bio = self.Description;
487 /// 渡された取得件数がWORKERTYPEに応じた取得可能範囲に収まっているか検証する
489 public static bool VerifyApiResultCount(MyCommon.WORKERTYPE type, int count)
491 return count >= 20 && count <= GetMaxApiResultCount(type);
495 /// 渡された取得件数が更新時の取得可能範囲に収まっているか検証する
497 public static bool VerifyMoreApiResultCount(int count)
499 return count >= 20 && count <= 200;
503 /// 渡された取得件数が起動時の取得可能範囲に収まっているか検証する
505 public static bool VerifyFirstApiResultCount(int count)
507 return count >= 20 && count <= 200;
511 /// WORKERTYPEに応じた取得可能な最大件数を取得する
513 public static int GetMaxApiResultCount(MyCommon.WORKERTYPE type)
515 // 参照: REST APIs - 各endpointのcountパラメータ
516 // https://dev.twitter.com/rest/public
519 case MyCommon.WORKERTYPE.Timeline:
520 case MyCommon.WORKERTYPE.Reply:
521 case MyCommon.WORKERTYPE.UserTimeline:
522 case MyCommon.WORKERTYPE.Favorites:
523 case MyCommon.WORKERTYPE.DirectMessegeRcv:
524 case MyCommon.WORKERTYPE.DirectMessegeSnt:
525 case MyCommon.WORKERTYPE.List: // 不明
528 case MyCommon.WORKERTYPE.PublicSearch:
532 throw new InvalidOperationException("Invalid type: " + type);
537 /// WORKERTYPEに応じた取得件数を取得する
539 public static int GetApiResultCount(MyCommon.WORKERTYPE type, bool more, bool startup)
541 if (type == MyCommon.WORKERTYPE.DirectMessegeRcv ||
542 type == MyCommon.WORKERTYPE.DirectMessegeSnt)
547 if (SettingManager.Common.UseAdditionalCount)
551 case MyCommon.WORKERTYPE.Favorites:
552 if (SettingManager.Common.FavoritesCountApi != 0)
553 return SettingManager.Common.FavoritesCountApi;
555 case MyCommon.WORKERTYPE.List:
556 if (SettingManager.Common.ListCountApi != 0)
557 return SettingManager.Common.ListCountApi;
559 case MyCommon.WORKERTYPE.PublicSearch:
560 if (SettingManager.Common.SearchCountApi != 0)
561 return SettingManager.Common.SearchCountApi;
563 case MyCommon.WORKERTYPE.UserTimeline:
564 if (SettingManager.Common.UserTimelineCountApi != 0)
565 return SettingManager.Common.UserTimelineCountApi;
568 if (more && SettingManager.Common.MoreCountApi != 0)
570 return Math.Min(SettingManager.Common.MoreCountApi, GetMaxApiResultCount(type));
572 if (startup && SettingManager.Common.FirstCountApi != 0 && type != MyCommon.WORKERTYPE.Reply)
574 return Math.Min(SettingManager.Common.FirstCountApi, GetMaxApiResultCount(type));
579 var count = SettingManager.Common.CountApi;
581 if (type == MyCommon.WORKERTYPE.Reply)
582 count = SettingManager.Common.CountApiReply;
584 return Math.Min(count, GetMaxApiResultCount(type));
587 public async Task GetHomeTimelineApi(bool read, HomeTabModel tab, bool more, bool startup)
589 this.CheckAccountState();
591 var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, startup);
593 TwitterStatus[] statuses;
596 statuses = await this.Api.StatusesHomeTimeline(count, maxId: tab.OldestId)
597 .ConfigureAwait(false);
601 statuses = await this.Api.StatusesHomeTimeline(count)
602 .ConfigureAwait(false);
605 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Timeline, tab, read);
607 if (minimumId != null && minimumId.Value < tab.OldestId)
608 tab.OldestId = minimumId.Value;
611 public async Task GetMentionsTimelineApi(bool read, MentionsTabModel tab, bool more, bool startup)
613 this.CheckAccountState();
615 var count = GetApiResultCount(MyCommon.WORKERTYPE.Reply, more, startup);
617 TwitterStatus[] statuses;
620 statuses = await this.Api.StatusesMentionsTimeline(count, maxId: tab.OldestId)
621 .ConfigureAwait(false);
625 statuses = await this.Api.StatusesMentionsTimeline(count)
626 .ConfigureAwait(false);
629 var minimumId = this.CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.Reply, tab, read);
631 if (minimumId != null && minimumId.Value < tab.OldestId)
632 tab.OldestId = minimumId.Value;
635 public async Task GetUserTimelineApi(bool read, string userName, UserTimelineTabModel tab, bool more)
637 this.CheckAccountState();
639 var count = GetApiResultCount(MyCommon.WORKERTYPE.UserTimeline, more, false);
641 TwitterStatus[] statuses;
642 if (string.IsNullOrEmpty(userName))
644 var target = tab.ScreenName;
645 if (string.IsNullOrEmpty(target)) return;
647 statuses = await this.Api.StatusesUserTimeline(userName, count)
648 .ConfigureAwait(false);
654 statuses = await this.Api.StatusesUserTimeline(userName, count, maxId: tab.OldestId)
655 .ConfigureAwait(false);
659 statuses = await this.Api.StatusesUserTimeline(userName, count)
660 .ConfigureAwait(false);
664 var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.UserTimeline, tab, read);
666 if (minimumId != null && minimumId.Value < tab.OldestId)
667 tab.OldestId = minimumId.Value;
670 public async Task<PostClass> GetStatusApi(bool read, long id)
672 this.CheckAccountState();
674 var status = await this.Api.StatusesShow(id)
675 .ConfigureAwait(false);
677 var item = CreatePostsFromStatusData(status);
680 if (item.IsMe && !read && _readOwnPost) item.IsRead = true;
685 public async Task GetStatusApi(bool read, long id, TabModel tab)
687 var post = await this.GetStatusApi(read, id)
688 .ConfigureAwait(false);
690 //非同期アイコン取得&StatusDictionaryに追加
691 if (tab != null && tab.IsInnerStorageTabType)
692 tab.AddPostQueue(post);
694 TabInformations.GetInstance().AddPost(post);
697 private PostClass CreatePostsFromStatusData(TwitterStatus status)
699 return CreatePostsFromStatusData(status, false);
702 private PostClass CreatePostsFromStatusData(TwitterStatus status, bool favTweet)
704 var post = new PostClass();
705 TwitterEntities entities;
708 post.StatusId = status.Id;
709 if (status.RetweetedStatus != null)
711 var retweeted = status.RetweetedStatus;
713 post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt);
716 post.RetweetedId = retweeted.Id;
718 post.TextFromApi = retweeted.FullText;
719 entities = retweeted.MergedEntities;
720 sourceHtml = retweeted.Source;
722 post.InReplyToStatusId = retweeted.InReplyToStatusId;
723 post.InReplyToUser = retweeted.InReplyToScreenName;
724 post.InReplyToUserId = status.InReplyToUserId;
733 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
734 post.IsFav = tc.Contains(retweeted.Id);
737 if (retweeted.Coordinates != null)
738 post.PostGeo = new PostClass.StatusGeo(retweeted.Coordinates.Coordinates[0], retweeted.Coordinates.Coordinates[1]);
741 var user = retweeted.User;
744 post.UserId = user.Id;
745 post.ScreenName = user.ScreenName;
746 post.Nickname = user.Name.Trim();
747 post.ImageUrl = user.ProfileImageUrlHttps;
748 post.IsProtect = user.Protected;
753 post.ScreenName = "?????";
754 post.Nickname = "Unknown User";
758 if (status.User != null)
760 post.RetweetedBy = status.User.ScreenName;
761 post.RetweetedByUserId = status.User.Id;
762 post.IsMe = post.RetweetedBy.ToLowerInvariant().Equals(_uname);
766 post.RetweetedBy = "?????";
767 post.RetweetedByUserId = 0L;
772 post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt);
774 post.TextFromApi = status.FullText;
775 entities = status.MergedEntities;
776 sourceHtml = status.Source;
777 post.InReplyToStatusId = status.InReplyToStatusId;
778 post.InReplyToUser = status.InReplyToScreenName;
779 post.InReplyToUserId = status.InReplyToUserId;
788 var tc = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
789 post.IsFav = tc.Contains(post.StatusId) && TabInformations.GetInstance()[post.StatusId].IsFav;
792 if (status.Coordinates != null)
793 post.PostGeo = new PostClass.StatusGeo(status.Coordinates.Coordinates[0], status.Coordinates.Coordinates[1]);
796 var user = status.User;
799 post.UserId = user.Id;
800 post.ScreenName = user.ScreenName;
801 post.Nickname = user.Name.Trim();
802 post.ImageUrl = user.ProfileImageUrlHttps;
803 post.IsProtect = user.Protected;
804 post.IsMe = post.ScreenName.ToLowerInvariant().Equals(_uname);
809 post.ScreenName = "?????";
810 post.Nickname = "Unknown User";
814 string textFromApi = post.TextFromApi;
815 post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, entities, post.Media);
816 post.TextFromApi = textFromApi;
817 post.TextFromApi = this.ReplaceTextFromApi(post.TextFromApi, entities);
818 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
819 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
820 post.AccessibleText = this.CreateAccessibleText(textFromApi, entities, (status.RetweetedStatus ?? status).QuotedStatus);
821 post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
822 post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
824 post.QuoteStatusIds = GetQuoteTweetStatusIds(entities)
825 .Where(x => x != post.StatusId && x != post.RetweetedId)
826 .Distinct().ToArray();
828 post.ExpandedUrls = entities.OfType<TwitterEntityUrl>()
829 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
832 // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
833 if (post.Text == post.TextFromApi)
834 post.Text = post.TextFromApi;
835 if (post.AccessibleText == post.TextFromApi)
836 post.AccessibleText = post.TextFromApi;
838 // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
839 post.ScreenName = string.Intern(post.ScreenName);
840 post.Nickname = string.Intern(post.Nickname);
841 post.ImageUrl = string.Intern(post.ImageUrl);
842 post.RetweetedBy = post.RetweetedBy != null ? string.Intern(post.RetweetedBy) : null;
845 var source = ParseSource(sourceHtml);
846 post.Source = string.Intern(source.Item1);
847 post.SourceUri = source.Item2;
849 post.IsReply = post.ReplyToList.Contains(_uname);
850 post.IsExcludeReply = false;
858 if (followerId.Count > 0) post.IsOwl = !followerId.Contains(post.UserId);
866 /// ツイートに含まれる引用ツイートのURLからステータスIDを抽出
868 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<TwitterEntity> entities)
870 var urls = entities.OfType<TwitterEntityUrl>().Select(x => x.ExpandedUrl);
872 return GetQuoteTweetStatusIds(urls);
875 public static IEnumerable<long> GetQuoteTweetStatusIds(IEnumerable<string> urls)
877 foreach (var url in urls)
879 var match = Twitter.StatusUrlRegex.Match(url);
883 if (long.TryParse(match.Groups["StatusId"].Value, out statusId))
884 yield return statusId;
889 private long? CreatePostsFromJson(TwitterStatus[] items, MyCommon.WORKERTYPE gType, TabModel tab, bool read)
891 long? minimumId = null;
893 foreach (var status in items)
895 if (minimumId == null || minimumId.Value > status.Id)
896 minimumId = status.Id;
903 if (TabInformations.GetInstance().ContainsKey(status.Id)) continue;
907 if (tab.Contains(status.Id)) continue;
912 if (gType != MyCommon.WORKERTYPE.UserTimeline &&
913 status.RetweetedStatus != null && this.noRTId.Contains(status.User.Id)) continue;
915 var post = CreatePostsFromStatusData(status);
918 if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
920 if (tab != null && tab.IsInnerStorageTabType)
921 tab.AddPostQueue(post);
923 TabInformations.GetInstance().AddPost(post);
929 private long? CreatePostsFromSearchJson(TwitterSearchResult items, PublicSearchTabModel tab, bool read, bool more)
931 long? minimumId = null;
933 foreach (var status in items.Statuses)
935 if (minimumId == null || minimumId.Value > status.Id)
936 minimumId = status.Id;
938 if (!more && status.Id > tab.SinceId) tab.SinceId = status.Id;
942 if (tab.Contains(status.Id)) continue;
945 var post = CreatePostsFromStatusData(status);
948 if ((post.IsMe && !read) && this._readOwnPost) post.IsRead = true;
950 tab.AddPostQueue(post);
956 private long? CreateFavoritePostsFromJson(TwitterStatus[] items, bool read)
958 var favTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.Favorites);
959 long? minimumId = null;
961 foreach (var status in items)
963 if (minimumId == null || minimumId.Value > status.Id)
964 minimumId = status.Id;
969 if (favTab.Contains(status.Id)) continue;
972 var post = CreatePostsFromStatusData(status, true);
976 TabInformations.GetInstance().AddPost(post);
982 public async Task GetListStatus(bool read, ListTimelineTabModel tab, bool more, bool startup)
984 var count = GetApiResultCount(MyCommon.WORKERTYPE.List, more, startup);
986 TwitterStatus[] statuses;
989 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, maxId: tab.OldestId, includeRTs: SettingManager.Common.IsListsIncludeRts)
990 .ConfigureAwait(false);
994 statuses = await this.Api.ListsStatuses(tab.ListInfo.Id, count, includeRTs: SettingManager.Common.IsListsIncludeRts)
995 .ConfigureAwait(false);
998 var minimumId = CreatePostsFromJson(statuses, MyCommon.WORKERTYPE.List, tab, read);
1000 if (minimumId != null && minimumId.Value < tab.OldestId)
1001 tab.OldestId = minimumId.Value;
1005 /// startStatusId からリプライ先の発言を辿る。発言は posts 以外からは検索しない。
1007 /// <returns>posts の中から検索されたリプライチェインの末端</returns>
1008 internal static PostClass FindTopOfReplyChain(IDictionary<Int64, PostClass> posts, Int64 startStatusId)
1010 if (!posts.ContainsKey(startStatusId))
1011 throw new ArgumentException("startStatusId (" + startStatusId + ") が posts の中から見つかりませんでした。", nameof(startStatusId));
1013 var nextPost = posts[startStatusId];
1014 while (nextPost.InReplyToStatusId != null)
1016 if (!posts.ContainsKey(nextPost.InReplyToStatusId.Value))
1018 nextPost = posts[nextPost.InReplyToStatusId.Value];
1024 public async Task GetRelatedResult(bool read, RelatedPostsTabModel tab)
1026 var targetPost = tab.TargetPost;
1027 var relPosts = new Dictionary<Int64, PostClass>();
1028 if (targetPost.TextFromApi.Contains("@") && targetPost.InReplyToStatusId == null)
1031 var p = TabInformations.GetInstance()[targetPost.StatusId];
1032 if (p != null && p.InReplyToStatusId != null)
1038 p = await this.GetStatusApi(read, targetPost.StatusId)
1039 .ConfigureAwait(false);
1043 relPosts.Add(targetPost.StatusId, targetPost);
1045 Exception lastException = null;
1047 // in_reply_to_status_id を使用してリプライチェインを辿る
1048 var nextPost = FindTopOfReplyChain(relPosts, targetPost.StatusId);
1050 while (nextPost.InReplyToStatusId != null && loopCount++ <= 20)
1052 var inReplyToId = nextPost.InReplyToStatusId.Value;
1054 var inReplyToPost = TabInformations.GetInstance()[inReplyToId];
1055 if (inReplyToPost == null)
1059 inReplyToPost = await this.GetStatusApi(read, inReplyToId)
1060 .ConfigureAwait(false);
1062 catch (WebApiException ex)
1069 relPosts.Add(inReplyToPost.StatusId, inReplyToPost);
1071 nextPost = FindTopOfReplyChain(relPosts, nextPost.StatusId);
1074 //MRTとかに対応のためツイート内にあるツイートを指すURLを取り込む
1075 var text = targetPost.Text;
1076 var ma = Twitter.StatusUrlRegex.Matches(text).Cast<Match>()
1077 .Concat(Twitter.ThirdPartyStatusUrlRegex.Matches(text).Cast<Match>());
1078 foreach (var _match in ma)
1081 if (Int64.TryParse(_match.Groups["StatusId"].Value, out _statusId))
1083 if (relPosts.ContainsKey(_statusId))
1086 var p = TabInformations.GetInstance()[_statusId];
1091 p = await this.GetStatusApi(read, _statusId)
1092 .ConfigureAwait(false);
1094 catch (WebApiException ex)
1102 relPosts.Add(p.StatusId, p);
1106 relPosts.Values.ToList().ForEach(p =>
1108 if (p.IsMe && !read && this._readOwnPost)
1113 tab.AddPostQueue(p);
1116 if (lastException != null)
1117 throw new WebApiException(lastException.Message, lastException);
1120 public async Task GetSearch(bool read, PublicSearchTabModel tab, bool more)
1122 var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
1125 long? sinceId = null;
1128 maxId = tab.OldestId - 1;
1132 sinceId = tab.SinceId;
1135 var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
1136 .ConfigureAwait(false);
1138 if (!TabInformations.GetInstance().ContainsTab(tab))
1141 var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, more);
1143 if (minimumId != null && minimumId.Value < tab.OldestId)
1144 tab.OldestId = minimumId.Value;
1147 private void CreateDirectMessagesFromJson(TwitterDirectMessage[] item, MyCommon.WORKERTYPE gType, bool read)
1149 foreach (var message in item)
1151 var post = new PostClass();
1154 post.StatusId = message.Id;
1155 if (gType != MyCommon.WORKERTYPE.UserStream)
1157 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1159 if (minDirectmessage > post.StatusId) minDirectmessage = post.StatusId;
1163 if (minDirectmessageSent > post.StatusId) minDirectmessageSent = post.StatusId;
1170 if (TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage).Contains(post.StatusId)) continue;
1174 post.CreatedAt = MyCommon.DateTimeParse(message.CreatedAt);
1176 var textFromApi = message.Text;
1178 post.Text = CreateHtmlAnchor(textFromApi, post.ReplyToList, message.Entities, post.Media);
1179 post.TextFromApi = this.ReplaceTextFromApi(textFromApi, message.Entities);
1180 post.TextFromApi = WebUtility.HtmlDecode(post.TextFromApi);
1181 post.TextFromApi = post.TextFromApi.Replace("<3", "\u2661");
1182 post.AccessibleText = this.CreateAccessibleText(textFromApi, message.Entities, quoteStatus: null);
1183 post.AccessibleText = WebUtility.HtmlDecode(post.AccessibleText);
1184 post.AccessibleText = post.AccessibleText.Replace("<3", "\u2661");
1187 post.QuoteStatusIds = GetQuoteTweetStatusIds(message.Entities).Distinct().ToArray();
1189 post.ExpandedUrls = message.Entities.OfType<TwitterEntityUrl>()
1190 .Select(x => new PostClass.ExpandedUrlInfo(x.Url, x.ExpandedUrl))
1195 if (gType == MyCommon.WORKERTYPE.UserStream)
1197 if (this.Api.CurrentUserId == message.Recipient.Id)
1199 user = message.Sender;
1205 user = message.Recipient;
1212 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1214 user = message.Sender;
1220 user = message.Recipient;
1226 post.UserId = user.Id;
1227 post.ScreenName = user.ScreenName;
1228 post.Nickname = user.Name.Trim();
1229 post.ImageUrl = user.ProfileImageUrlHttps;
1230 post.IsProtect = user.Protected;
1232 // メモリ使用量削減 (同一のテキストであれば同一の string インスタンスを参照させる)
1233 if (post.Text == post.TextFromApi)
1234 post.Text = post.TextFromApi;
1235 if (post.AccessibleText == post.TextFromApi)
1236 post.AccessibleText = post.TextFromApi;
1238 // 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
1239 post.ScreenName = string.Intern(post.ScreenName);
1240 post.Nickname = string.Intern(post.Nickname);
1241 post.ImageUrl = string.Intern(post.ImageUrl);
1245 MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name);
1246 MessageBox.Show("Parse Error(CreateDirectMessagesFromJson)");
1251 if (post.IsMe && !read && _readOwnPost) post.IsRead = true;
1252 post.IsReply = false;
1253 post.IsExcludeReply = false;
1256 var dmTab = TabInformations.GetInstance().GetTabByType(MyCommon.TabUsageType.DirectMessage);
1257 dmTab.AddPostQueue(post);
1261 public async Task GetDirectMessageApi(bool read, MyCommon.WORKERTYPE gType, bool more)
1263 this.CheckAccountState();
1264 this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
1266 var count = GetApiResultCount(gType, more, false);
1268 TwitterDirectMessage[] messages;
1269 if (gType == MyCommon.WORKERTYPE.DirectMessegeRcv)
1273 messages = await this.Api.DirectMessagesRecv(count, maxId: this.minDirectmessage)
1274 .ConfigureAwait(false);
1278 messages = await this.Api.DirectMessagesRecv(count)
1279 .ConfigureAwait(false);
1286 messages = await this.Api.DirectMessagesSent(count, maxId: this.minDirectmessageSent)
1287 .ConfigureAwait(false);
1291 messages = await this.Api.DirectMessagesSent(count)
1292 .ConfigureAwait(false);
1296 CreateDirectMessagesFromJson(messages, gType, read);
1299 public async Task GetFavoritesApi(bool read, FavoritesTabModel tab, bool backward)
1301 this.CheckAccountState();
1303 var count = GetApiResultCount(MyCommon.WORKERTYPE.Favorites, backward, false);
1305 TwitterStatus[] statuses;
1308 statuses = await this.Api.FavoritesList(count, maxId: tab.OldestId)
1309 .ConfigureAwait(false);
1313 statuses = await this.Api.FavoritesList(count)
1314 .ConfigureAwait(false);
1317 var minimumId = this.CreateFavoritePostsFromJson(statuses, read);
1319 if (minimumId != null && minimumId.Value < tab.OldestId)
1320 tab.OldestId = minimumId.Value;
1323 private string ReplaceTextFromApi(string text, TwitterEntities entities)
1325 if (entities != null)
1327 if (entities.Urls != null)
1329 foreach (var m in entities.Urls)
1331 if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1334 if (entities.Media != null)
1336 foreach (var m in entities.Media)
1338 if (!string.IsNullOrEmpty(m.DisplayUrl)) text = text.Replace(m.Url, m.DisplayUrl);
1345 private string CreateAccessibleText(string text, TwitterEntities entities, TwitterStatus quoteStatus)
1347 if (entities == null)
1350 if (entities.Urls != null)
1352 foreach (var entity in entities.Urls)
1354 if (quoteStatus != null)
1356 var matchStatusUrl = Twitter.StatusUrlRegex.Match(entity.ExpandedUrl);
1357 if (matchStatusUrl.Success && matchStatusUrl.Groups["StatusId"].Value == quoteStatus.IdStr)
1359 var quoteText = this.CreateAccessibleText(quoteStatus.FullText, quoteStatus.MergedEntities, quoteStatus: null);
1360 text = text.Replace(entity.Url, string.Format(Properties.Resources.QuoteStatus_AccessibleText, quoteStatus.User.ScreenName, quoteText));
1363 else if (!string.IsNullOrEmpty(entity.DisplayUrl))
1365 text = text.Replace(entity.Url, entity.DisplayUrl);
1370 if (entities.Media != null)
1372 foreach (var entity in entities.Media)
1374 if (!string.IsNullOrEmpty(entity.AltText))
1376 text = text.Replace(entity.Url, string.Format(Properties.Resources.ImageAltText, entity.AltText));
1378 else if (!string.IsNullOrEmpty(entity.DisplayUrl))
1380 text = text.Replace(entity.Url, entity.DisplayUrl);
1391 /// <exception cref="WebApiException"/>
1392 public async Task RefreshFollowerIds()
1394 if (MyCommon._endingFlag) return;
1397 var newFollowerIds = new HashSet<long>();
1400 var ret = await this.Api.FollowersIds(cursor)
1401 .ConfigureAwait(false);
1403 if (ret.Ids == null)
1404 throw new WebApiException("ret.ids == null");
1406 newFollowerIds.UnionWith(ret.Ids);
1407 cursor = ret.NextCursor;
1408 } while (cursor != 0);
1410 this.followerId = newFollowerIds;
1411 TabInformations.GetInstance().RefreshOwl(this.followerId);
1413 this._GetFollowerResult = true;
1416 public bool GetFollowersSuccess
1420 return _GetFollowerResult;
1425 /// RT 非表示ユーザーを更新します
1427 /// <exception cref="WebApiException"/>
1428 public async Task RefreshNoRetweetIds()
1430 if (MyCommon._endingFlag) return;
1432 this.noRTId = await this.Api.NoRetweetIds()
1433 .ConfigureAwait(false);
1435 this._GetNoRetweetResult = true;
1438 public bool GetNoRetweetSuccess
1442 return _GetNoRetweetResult;
1447 /// t.co の文字列長などの設定情報を更新します
1449 /// <exception cref="WebApiException"/>
1450 public async Task RefreshConfiguration()
1452 this.Configuration = await this.Api.Configuration()
1453 .ConfigureAwait(false);
1456 public async Task GetListsApi()
1458 this.CheckAccountState();
1460 var ownedLists = await TwitterLists.GetAllItemsAsync(x =>
1461 this.Api.ListsOwnerships(this.Username, cursor: x, count: 1000))
1462 .ConfigureAwait(false);
1464 var subscribedLists = await TwitterLists.GetAllItemsAsync(x =>
1465 this.Api.ListsSubscriptions(this.Username, cursor: x, count: 1000))
1466 .ConfigureAwait(false);
1468 TabInformations.GetInstance().SubscribableLists = Enumerable.Concat(ownedLists, subscribedLists)
1469 .Select(x => new ListElement(x, this))
1473 public async Task DeleteList(long listId)
1475 await this.Api.ListsDestroy(listId)
1477 .ConfigureAwait(false);
1479 var tabinfo = TabInformations.GetInstance();
1481 tabinfo.SubscribableLists = tabinfo.SubscribableLists
1482 .Where(x => x.Id != listId)
1486 public async Task<ListElement> EditList(long listId, string new_name, bool isPrivate, string description)
1488 var response = await this.Api.ListsUpdate(listId, new_name, description, isPrivate)
1489 .ConfigureAwait(false);
1491 var list = await response.LoadJsonAsync()
1492 .ConfigureAwait(false);
1494 return new ListElement(list, this);
1497 public async Task<long> GetListMembers(long listId, List<UserInfo> lists, long cursor)
1499 this.CheckAccountState();
1501 var users = await this.Api.ListsMembers(listId, cursor)
1502 .ConfigureAwait(false);
1504 Array.ForEach(users.Users, u => lists.Add(new UserInfo(u)));
1506 return users.NextCursor;
1509 public async Task CreateListApi(string listName, bool isPrivate, string description)
1511 this.CheckAccountState();
1513 var response = await this.Api.ListsCreate(listName, description, isPrivate)
1514 .ConfigureAwait(false);
1516 var list = await response.LoadJsonAsync()
1517 .ConfigureAwait(false);
1519 TabInformations.GetInstance().SubscribableLists.Add(new ListElement(list, this));
1522 public async Task<bool> ContainsUserAtList(long listId, string user)
1524 this.CheckAccountState();
1528 await this.Api.ListsMembersShow(listId, user)
1529 .ConfigureAwait(false);
1533 catch (TwitterApiException ex)
1534 when (ex.ErrorResponse.Errors.Any(x => x.Code == TwitterErrorCode.NotFound))
1540 public string CreateHtmlAnchor(string text, List<string> AtList, TwitterEntities entities, List<MediaInfo> media)
1542 if (entities != null)
1544 if (entities.Hashtags != null)
1548 this._hashList.AddRange(entities.Hashtags.Select(x => "#" + x.Text));
1551 if (entities.UserMentions != null)
1553 foreach (var ent in entities.UserMentions)
1555 var screenName = ent.ScreenName.ToLowerInvariant();
1556 if (!AtList.Contains(screenName))
1557 AtList.Add(screenName);
1560 if (entities.Media != null)
1564 foreach (var ent in entities.Media)
1566 if (!media.Any(x => x.Url == ent.MediaUrl))
1568 if (ent.VideoInfo != null &&
1569 ent.Type == "animated_gif" || ent.Type == "video")
1571 //var videoUrl = ent.VideoInfo.Variants
1572 // .Where(v => v.ContentType == "video/mp4")
1573 // .OrderByDescending(v => v.Bitrate)
1574 // .Select(v => v.Url).FirstOrDefault();
1575 media.Add(new MediaInfo(ent.MediaUrl, ent.AltText, ent.ExpandedUrl));
1578 media.Add(new MediaInfo(ent.MediaUrl, ent.AltText, videoUrl: null));
1585 // PostClass.ExpandedUrlInfo を使用して非同期に URL 展開を行うためここでは expanded_url を使用しない
1586 text = TweetFormatter.AutoLinkHtml(text, entities, keepTco: true);
1588 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>");
1589 text = PreProcessUrl(text); //IDN置換
1594 private static readonly Uri SourceUriBase = new Uri("https://twitter.com/");
1597 /// Twitter APIから得たHTML形式のsource文字列を分析し、source名とURLに分離します
1599 public static Tuple<string, Uri> ParseSource(string sourceHtml)
1601 if (string.IsNullOrEmpty(sourceHtml))
1602 return Tuple.Create<string, Uri>("", null);
1607 // sourceHtmlの例: <a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>
1609 var match = Regex.Match(sourceHtml, "^<a href=\"(?<uri>.+?)\".*?>(?<text>.+)</a>$", RegexOptions.IgnoreCase);
1612 sourceText = WebUtility.HtmlDecode(match.Groups["text"].Value);
1615 var uriStr = WebUtility.HtmlDecode(match.Groups["uri"].Value);
1616 sourceUri = new Uri(SourceUriBase, uriStr);
1618 catch (UriFormatException)
1625 sourceText = WebUtility.HtmlDecode(sourceHtml);
1629 return Tuple.Create(sourceText, sourceUri);
1632 public async Task<TwitterApiStatus> GetInfoApi()
1634 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return null;
1636 if (MyCommon._endingFlag) return null;
1638 var limits = await this.Api.ApplicationRateLimitStatus()
1639 .ConfigureAwait(false);
1641 MyCommon.TwitterApiInfo.UpdateFromJson(limits);
1643 return MyCommon.TwitterApiInfo;
1647 /// ブロック中のユーザーを更新します
1649 /// <exception cref="WebApiException"/>
1650 public async Task RefreshBlockIds()
1652 if (MyCommon._endingFlag) return;
1655 var newBlockIds = new HashSet<long>();
1658 var ret = await this.Api.BlocksIds(cursor)
1659 .ConfigureAwait(false);
1661 newBlockIds.UnionWith(ret.Ids);
1662 cursor = ret.NextCursor;
1663 } while (cursor != 0);
1665 newBlockIds.Remove(this.UserId); // 元のソースにあったので一応残しておく
1667 TabInformations.GetInstance().BlockIds = newBlockIds;
1671 /// ミュート中のユーザーIDを更新します
1673 /// <exception cref="WebApiException"/>
1674 public async Task RefreshMuteUserIdsAsync()
1676 if (MyCommon._endingFlag) return;
1678 var ids = await TwitterIds.GetAllItemsAsync(x => this.Api.MutesUsersIds(x))
1679 .ConfigureAwait(false);
1681 TabInformations.GetInstance().MuteUserIds = new HashSet<long>(ids);
1684 public string[] GetHashList()
1689 hashArray = _hashList.ToArray();
1695 public string AccessToken
1696 => ((TwitterApiConnection)this.Api.Connection).AccessToken;
1698 public string AccessTokenSecret
1699 => ((TwitterApiConnection)this.Api.Connection).AccessSecret;
1701 private void CheckAccountState()
1703 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
1704 throw new WebApiException("Auth error. Check your account");
1707 private void CheckAccessLevel(TwitterApiAccessLevel accessLevelFlags)
1709 if (!this.AccessLevel.HasFlag(accessLevelFlags))
1710 throw new WebApiException("Auth Err:try to re-authorization.");
1713 public int GetTextLengthRemain(string postText)
1715 var matchDm = Twitter.DMSendTextRegex.Match(postText);
1716 if (matchDm.Success)
1717 return this.GetTextLengthRemainInternal(matchDm.Groups["body"].Value, isDm: true);
1719 return this.GetTextLengthRemainInternal(postText, isDm: false);
1722 private int GetTextLengthRemainInternal(string postText, bool isDm)
1727 while (pos < postText.Length)
1731 if (char.IsSurrogatePair(postText, pos))
1732 pos += 2; // サロゲートペアの場合は2文字分進める
1737 var urls = TweetExtractor.ExtractUrls(postText);
1738 foreach (var url in urls)
1740 var shortUrlLength = url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
1741 ? this.Configuration.ShortUrlLengthHttps
1742 : this.Configuration.ShortUrlLength;
1744 textLength += shortUrlLength - url.Length;
1748 return this.Configuration.DmTextCharacterLimit - textLength;
1750 return 140 - textLength;
1754 #region "UserStream"
1755 private string trackWord_ = "";
1756 public string TrackWord
1767 private bool allAtReply_ = false;
1768 public bool AllAtReply
1776 allAtReply_ = value;
1780 public event EventHandler NewPostFromStream;
1781 public event EventHandler UserStreamStarted;
1782 public event EventHandler UserStreamStopped;
1783 public event EventHandler<PostDeletedEventArgs> PostDeleted;
1784 public event EventHandler<UserStreamEventReceivedEventArgs> UserStreamEventReceived;
1785 private DateTime _lastUserstreamDataReceived;
1786 private TwitterUserstream userStream;
1788 public class FormattedEvent
1790 public MyCommon.EVENTTYPE Eventtype { get; set; }
1791 public DateTime CreatedAt { get; set; }
1792 public string Event { get; set; }
1793 public string Username { get; set; }
1794 public string Target { get; set; }
1795 public Int64 Id { get; set; }
1796 public bool IsMe { get; set; }
1799 public List<FormattedEvent> storedEvent_ = new List<FormattedEvent>();
1800 public List<FormattedEvent> StoredEvent
1804 return storedEvent_;
1808 storedEvent_ = value;
1812 private readonly IReadOnlyDictionary<string, MyCommon.EVENTTYPE> eventTable = new Dictionary<string, MyCommon.EVENTTYPE>
1814 ["favorite"] = MyCommon.EVENTTYPE.Favorite,
1815 ["unfavorite"] = MyCommon.EVENTTYPE.Unfavorite,
1816 ["follow"] = MyCommon.EVENTTYPE.Follow,
1817 ["list_member_added"] = MyCommon.EVENTTYPE.ListMemberAdded,
1818 ["list_member_removed"] = MyCommon.EVENTTYPE.ListMemberRemoved,
1819 ["block"] = MyCommon.EVENTTYPE.Block,
1820 ["unblock"] = MyCommon.EVENTTYPE.Unblock,
1821 ["user_update"] = MyCommon.EVENTTYPE.UserUpdate,
1822 ["deleted"] = MyCommon.EVENTTYPE.Deleted,
1823 ["list_created"] = MyCommon.EVENTTYPE.ListCreated,
1824 ["list_destroyed"] = MyCommon.EVENTTYPE.ListDestroyed,
1825 ["list_updated"] = MyCommon.EVENTTYPE.ListUpdated,
1826 ["unfollow"] = MyCommon.EVENTTYPE.Unfollow,
1827 ["list_user_subscribed"] = MyCommon.EVENTTYPE.ListUserSubscribed,
1828 ["list_user_unsubscribed"] = MyCommon.EVENTTYPE.ListUserUnsubscribed,
1829 ["mute"] = MyCommon.EVENTTYPE.Mute,
1830 ["unmute"] = MyCommon.EVENTTYPE.Unmute,
1831 ["quoted_tweet"] = MyCommon.EVENTTYPE.QuotedTweet,
1834 public bool IsUserstreamDataReceived
1838 return DateTime.Now.Subtract(this._lastUserstreamDataReceived).TotalSeconds < 31;
1842 private void userStream_StatusArrived(string line)
1844 this._lastUserstreamDataReceived = DateTime.Now;
1845 if (string.IsNullOrEmpty(line)) return;
1847 if (line.First() != '{' || line.Last() != '}')
1849 MyCommon.TraceOut("Invalid JSON (StatusArrived):" + Environment.NewLine + line);
1857 using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(line), XmlDictionaryReaderQuotas.Max))
1859 var xElm = XElement.Load(jsonReader);
1860 if (xElm.Element("friends") != null)
1862 Debug.WriteLine("friends");
1865 else if (xElm.Element("delete") != null)
1867 Debug.WriteLine("delete");
1870 if ((idElm = xElm.Element("delete").Element("direct_message")?.Element("id")) != null)
1873 long.TryParse(idElm.Value, out id);
1875 this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
1877 else if ((idElm = xElm.Element("delete").Element("status")?.Element("id")) != null)
1880 long.TryParse(idElm.Value, out id);
1882 this.PostDeleted?.Invoke(this, new PostDeletedEventArgs(id));
1886 MyCommon.TraceOut("delete:" + line);
1889 for (int i = this.StoredEvent.Count - 1; i >= 0; i--)
1891 var sEvt = this.StoredEvent[i];
1892 if (sEvt.Id == id && (sEvt.Event == "favorite" || sEvt.Event == "unfavorite"))
1894 this.StoredEvent.RemoveAt(i);
1899 else if (xElm.Element("limit") != null)
1901 Debug.WriteLine(line);
1904 else if (xElm.Element("event") != null)
1906 Debug.WriteLine("event: " + xElm.Element("event").Value);
1907 CreateEventFromJson(line);
1910 else if (xElm.Element("direct_message") != null)
1912 Debug.WriteLine("direct_message");
1915 else if (xElm.Element("retweeted_status") != null)
1917 var sourceUserId = xElm.XPathSelectElement("/user/id_str").Value;
1918 var targetUserId = xElm.XPathSelectElement("/retweeted_status/user/id_str").Value;
1920 // 自分に関係しないリツイートの場合は無視する
1921 var selfUserId = this.UserId.ToString();
1922 if (sourceUserId == selfUserId || targetUserId == selfUserId)
1924 // 公式 RT をイベントとしても扱う
1925 var evt = CreateEventFromRetweet(xElm);
1928 this.StoredEvent.Insert(0, evt);
1930 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
1934 // 従来通り公式 RT の表示も行うため return しない
1936 else if (xElm.Element("scrub_geo") != null)
1940 TabInformations.GetInstance().ScrubGeoReserve(long.Parse(xElm.Element("scrub_geo").Element("user_id").Value),
1941 long.Parse(xElm.Element("scrub_geo").Element("up_to_status_id").Value));
1945 MyCommon.TraceOut("scrub_geo:" + line);
1955 var message = TwitterStreamEventDirectMessage.ParseJson(line).DirectMessage;
1956 this.CreateDirectMessagesFromJson(new[] { message }, MyCommon.WORKERTYPE.UserStream, false);
1958 catch (SerializationException ex)
1960 throw TwitterApiException.CreateFromException(ex, line);
1967 var status = TwitterStatusCompat.ParseJson(line);
1968 this.CreatePostsFromJson(new[] { status.Normalize() }, MyCommon.WORKERTYPE.UserStream, null, false);
1970 catch (SerializationException ex)
1972 throw TwitterApiException.CreateFromException(ex, line);
1976 catch (WebApiException ex)
1978 MyCommon.TraceOut(ex);
1981 catch(NullReferenceException)
1983 MyCommon.TraceOut("NullRef StatusArrived: " + line);
1986 this.NewPostFromStream?.Invoke(this, EventArgs.Empty);
1990 /// UserStreamsから受信した公式RTをイベントに変換します
1992 private FormattedEvent CreateEventFromRetweet(XElement xElm)
1994 return new FormattedEvent
1996 Eventtype = MyCommon.EVENTTYPE.Retweet,
1998 CreatedAt = MyCommon.DateTimeParse(xElm.XPathSelectElement("/created_at").Value),
1999 IsMe = xElm.XPathSelectElement("/user/id_str").Value == this.UserId.ToString(),
2000 Username = xElm.XPathSelectElement("/user/screen_name").Value,
2001 Target = string.Format("@{0}:{1}", new[]
2003 xElm.XPathSelectElement("/retweeted_status/user/screen_name").Value,
2004 WebUtility.HtmlDecode(xElm.XPathSelectElement("/retweeted_status/text").Value),
2006 Id = long.Parse(xElm.XPathSelectElement("/retweeted_status/id_str").Value),
2010 private void CreateEventFromJson(string content)
2012 TwitterStreamEvent eventData = null;
2015 eventData = TwitterStreamEvent.ParseJson(content);
2017 catch(SerializationException ex)
2019 MyCommon.TraceOut(ex, "Event Serialize Exception!" + Environment.NewLine + content);
2023 MyCommon.TraceOut(ex, "Event Exception!" + Environment.NewLine + content);
2026 var evt = new FormattedEvent();
2027 evt.CreatedAt = MyCommon.DateTimeParse(eventData.CreatedAt);
2028 evt.Event = eventData.Event;
2029 evt.Username = eventData.Source.ScreenName;
2030 evt.IsMe = evt.Username.ToLowerInvariant().Equals(this.Username.ToLowerInvariant());
2032 MyCommon.EVENTTYPE eventType;
2033 eventTable.TryGetValue(eventData.Event, out eventType);
2034 evt.Eventtype = eventType;
2036 TwitterStreamEvent<TwitterStatusCompat> tweetEvent;
2037 TwitterStatus tweet;
2039 switch (eventData.Event)
2041 case "access_revoked":
2042 case "access_unrevoked":
2044 case "user_suspend":
2047 if (eventData.Target.ScreenName.ToLowerInvariant().Equals(_uname))
2049 if (!this.followerId.Contains(eventData.Source.Id)) this.followerId.Add(eventData.Source.Id);
2053 return; //Block後のUndoをすると、SourceとTargetが逆転したfollowイベントが帰ってくるため。
2058 evt.Target = "@" + eventData.Target.ScreenName;
2060 case "favorited_retweet":
2061 case "retweeted_retweet":
2065 tweetEvent = TwitterStreamEvent<TwitterStatusCompat>.ParseJson(content);
2066 tweet = tweetEvent.TargetObject.Normalize();
2067 evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
2070 if (SettingManager.Common.IsRemoveSameEvent)
2072 if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2076 var tabinfo = TabInformations.GetInstance();
2079 var statusId = tweet.Id;
2080 if (!tabinfo.Posts.TryGetValue(statusId, out post))
2083 if (eventData.Event == "favorite")
2085 var favTab = tabinfo.GetTabByType(MyCommon.TabUsageType.Favorites);
2086 favTab.AddPostQueue(post);
2088 if (tweetEvent.Source.Id == this.UserId)
2092 else if (tweetEvent.Target.Id == this.UserId)
2094 post.FavoritedCount++;
2096 if (SettingManager.Common.FavEventUnread)
2097 tabinfo.SetReadAllTab(post.StatusId, read: false);
2102 if (tweetEvent.Source.Id == this.UserId)
2106 else if (tweetEvent.Target.Id == this.UserId)
2108 post.FavoritedCount = Math.Max(0, post.FavoritedCount - 1);
2112 case "quoted_tweet":
2113 if (evt.IsMe) return;
2115 tweetEvent = TwitterStreamEvent<TwitterStatusCompat>.ParseJson(content);
2116 tweet = tweetEvent.TargetObject.Normalize();
2117 evt.Target = "@" + tweet.User.ScreenName + ":" + WebUtility.HtmlDecode(tweet.FullText);
2120 if (SettingManager.Common.IsRemoveSameEvent)
2122 if (this.StoredEvent.Any(ev => ev.Username == evt.Username && ev.Eventtype == evt.Eventtype && ev.Target == evt.Target))
2126 case "list_member_added":
2127 case "list_member_removed":
2128 case "list_created":
2129 case "list_destroyed":
2130 case "list_updated":
2131 case "list_user_subscribed":
2132 case "list_user_unsubscribed":
2133 var listEvent = TwitterStreamEvent<TwitterList>.ParseJson(content);
2134 evt.Target = listEvent.TargetObject.FullName;
2137 if (!TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Add(eventData.Target.Id);
2141 if (TabInformations.GetInstance().BlockIds.Contains(eventData.Target.Id)) TabInformations.GetInstance().BlockIds.Remove(eventData.Target.Id);
2150 evt.Target = "@" + eventData.Target.ScreenName;
2151 if (!TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2153 TabInformations.GetInstance().MuteUserIds.Add(eventData.Target.Id);
2157 evt.Target = "@" + eventData.Target.ScreenName;
2158 if (TabInformations.GetInstance().MuteUserIds.Contains(eventData.Target.Id))
2160 TabInformations.GetInstance().MuteUserIds.Remove(eventData.Target.Id);
2165 MyCommon.TraceOut("Unknown Event:" + evt.Event + Environment.NewLine + content);
2168 this.StoredEvent.Insert(0, evt);
2170 this.UserStreamEventReceived?.Invoke(this, new UserStreamEventReceivedEventArgs(evt));
2173 private void userStream_Started()
2175 this.UserStreamStarted?.Invoke(this, EventArgs.Empty);
2178 private void userStream_Stopped()
2180 this.UserStreamStopped?.Invoke(this, EventArgs.Empty);
2183 public bool UserStreamActive
2184 => this.userStream != null && this.userStream.IsStreamActive;
2186 public void StartUserStream()
2188 var newStream = new TwitterUserstream(this.Api);
2190 newStream.StatusArrived += userStream_StatusArrived;
2191 newStream.Started += userStream_Started;
2192 newStream.Stopped += userStream_Stopped;
2194 newStream.Start(this.AllAtReply, this.TrackWord);
2196 var oldStream = Interlocked.Exchange(ref this.userStream, newStream);
2197 oldStream?.Dispose();
2200 public void StopUserStream()
2202 var oldStream = Interlocked.Exchange(ref this.userStream, null);
2203 oldStream?.Dispose();
2206 public void ReconnectUserStream()
2208 if (this.userStream != null)
2210 this.StartUserStream();
2214 private class TwitterUserstream : IDisposable
2216 public bool AllAtReplies { get; private set; }
2217 public string TrackWords { get; private set; }
2219 public bool IsStreamActive { get; private set; }
2221 public event Action<string> StatusArrived;
2222 public event Action Stopped;
2223 public event Action Started;
2225 private TwitterApi twitterApi;
2227 private Task streamTask;
2228 private CancellationTokenSource streamCts;
2230 public TwitterUserstream(TwitterApi twitterApi)
2232 this.twitterApi = twitterApi;
2235 public void Start(bool allAtReplies, string trackwords)
2237 this.AllAtReplies = allAtReplies;
2238 this.TrackWords = trackwords;
2240 var cts = new CancellationTokenSource();
2242 this.streamCts = cts;
2243 this.streamTask = Task.Run(async () =>
2247 await this.UserStreamLoop(cts.Token)
2248 .ConfigureAwait(false);
2250 catch (OperationCanceledException) { }
2256 this.streamCts?.Cancel();
2258 // streamTask の完了を待たずに IsStreamActive を false にセットする
2259 this.IsStreamActive = false;
2260 this.Stopped?.Invoke();
2263 private async Task UserStreamLoop(CancellationToken cancellationToken)
2265 TimeSpan sleep = TimeSpan.Zero;
2268 if (sleep != TimeSpan.Zero)
2270 await Task.Delay(sleep, cancellationToken)
2271 .ConfigureAwait(false);
2272 sleep = TimeSpan.Zero;
2275 if (!MyCommon.IsNetworkAvailable())
2277 sleep = TimeSpan.FromSeconds(30);
2281 this.IsStreamActive = true;
2282 this.Started?.Invoke();
2286 var replies = this.AllAtReplies ? "all" : null;
2288 using (var stream = await this.twitterApi.UserStreams(replies, this.TrackWords)
2289 .ConfigureAwait(false))
2290 using (var reader = new StreamReader(stream))
2292 while (!reader.EndOfStream)
2294 var line = await reader.ReadLineAsync()
2295 .ConfigureAwait(false);
2297 cancellationToken.ThrowIfCancellationRequested();
2299 this.StatusArrived?.Invoke(line);
2303 // キャンセルされていないのにストリームが終了した場合
2304 sleep = TimeSpan.FromSeconds(30);
2306 catch (TwitterApiException) { sleep = TimeSpan.FromSeconds(30); }
2307 catch (IOException) { sleep = TimeSpan.FromSeconds(30); }
2308 catch (OperationCanceledException)
2310 if (cancellationToken.IsCancellationRequested)
2313 // cancellationToken によるキャンセルではない(=タイムアウトエラー)
2314 sleep = TimeSpan.FromSeconds(30);
2316 catch (Exception ex)
2318 MyCommon.ExceptionOut(ex);
2319 sleep = TimeSpan.FromSeconds(30);
2323 this.IsStreamActive = false;
2324 this.Stopped?.Invoke();
2329 private bool disposed = false;
2331 public void Dispose()
2336 this.disposed = true;
2340 this.Started = null;
2341 this.Stopped = null;
2342 this.StatusArrived = null;
2347 #region "IDisposable Support"
2348 private bool disposedValue; // 重複する呼び出しを検出するには
2351 protected virtual void Dispose(bool disposing)
2353 if (!this.disposedValue)
2357 this.StopUserStream();
2360 this.disposedValue = true;
2363 //protected Overrides void Finalize()
2365 // // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
2367 // MyBase.Finalize()
2370 // このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
2371 public void Dispose()
2373 // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
2375 GC.SuppressFinalize(this);
2380 public class PostDeletedEventArgs : EventArgs
2382 public long StatusId { get; }
2384 public PostDeletedEventArgs(long statusId)
2386 this.StatusId = statusId;
2390 public class UserStreamEventReceivedEventArgs : EventArgs
2392 public Twitter.FormattedEvent EventData { get; }
2394 public UserStreamEventReceivedEventArgs(Twitter.FormattedEvent eventData)
2396 this.EventData = eventData;