diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 07b92ce10..6f6773623 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -3,6 +3,7 @@ ==== Unreleased * NEW: graphqlエンドポイントを使用した検索タイムラインの取得に対応 * NEW: graphqlエンドポイントを使用したプロフィール情報の取得に対応 + * NEW: graphqlエンドポイントを使用したユーザータイムラインの取得に対応 * CHG: タイムライン更新が停止する不具合が報告される件への暫定的な対処 - タイムライン更新に30秒以上掛かっている場合は完了を待機せず次のタイマーを開始させる - タイムライン更新の次回実行が1時間以上先になる場合は異常値としてタイマーをリセットする diff --git a/OpenTween.Tests/Api/GraphQL/UserTweetsRequestTest.cs b/OpenTween.Tests/Api/GraphQL/UserTweetsRequestTest.cs new file mode 100644 index 000000000..337828eb4 --- /dev/null +++ b/OpenTween.Tests/Api/GraphQL/UserTweetsRequestTest.cs @@ -0,0 +1,96 @@ +// OpenTween - Client of Twitter +// Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/> +// All rights reserved. +// +// This file is part of OpenTween. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. If not, see <http://www.gnu.org/licenses/>, or write to +// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, +// Boston, MA 02110-1301, USA. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Moq; +using OpenTween.Connection; +using Xunit; + +namespace OpenTween.Api.GraphQL +{ + public class UserTweetsRequestTest + { + [Fact] + public async Task Send_Test() + { + using var responseStream = File.OpenRead("Resources/Responses/UserTweets_SimpleTweet.json"); + + var mock = new Mock<IApiConnection>(); + mock.Setup(x => + x.GetStreamAsync(It.IsAny<Uri>(), It.IsAny<IDictionary<string, string>>()) + ) + .Callback<Uri, IDictionary<string, string>>((url, param) => + { + Assert.Equal(new("https://twitter.com/i/api/graphql/2GIWTr7XwadIixZDtyXd4A/UserTweets"), url); + Assert.Equal(3, param.Count); + Assert.Equal("""{"userId":"40480664","count":20,"includePromotedContent":true,"withQuickPromoteEligibilityTweetFields":true,"withVoice":true,"withV2Timeline":true}""", param["variables"]); + Assert.True(param.ContainsKey("features")); + Assert.True(param.ContainsKey("fieldToggles")); + }) + .ReturnsAsync(responseStream); + + var request = new UserTweetsRequest(userId: "40480664") + { + Count = 20, + }; + + var response = await request.Send(mock.Object).ConfigureAwait(false); + Assert.Single(response.Tweets); + Assert.Equal("DAABCgABF_tTnZu__-0KAAIWZa6KTRoAAwgAAwAAAAIAAA", response.CursorBottom); + + mock.VerifyAll(); + } + + [Fact] + public async Task Send_RequestCursor_Test() + { + using var responseStream = File.OpenRead("Resources/Responses/UserTweets_SimpleTweet.json"); + + var mock = new Mock<IApiConnection>(); + mock.Setup(x => + x.GetStreamAsync(It.IsAny<Uri>(), It.IsAny<IDictionary<string, string>>()) + ) + .Callback<Uri, IDictionary<string, string>>((url, param) => + { + Assert.Equal(new("https://twitter.com/i/api/graphql/2GIWTr7XwadIixZDtyXd4A/UserTweets"), url); + Assert.Equal(3, param.Count); + Assert.Equal("""{"userId":"40480664","count":20,"includePromotedContent":true,"withQuickPromoteEligibilityTweetFields":true,"withVoice":true,"withV2Timeline":true,"cursor":"aaa"}""", param["variables"]); + Assert.True(param.ContainsKey("features")); + Assert.True(param.ContainsKey("fieldToggles")); + }) + .ReturnsAsync(responseStream); + + var request = new UserTweetsRequest(userId: "40480664") + { + Count = 20, + Cursor = "aaa", + }; + + await request.Send(mock.Object).ConfigureAwait(false); + mock.VerifyAll(); + } + } +} diff --git a/OpenTween.Tests/OpenTween.Tests.csproj b/OpenTween.Tests/OpenTween.Tests.csproj index e2f296863..ebdd16c4f 100644 --- a/OpenTween.Tests/OpenTween.Tests.csproj +++ b/OpenTween.Tests/OpenTween.Tests.csproj @@ -100,5 +100,8 @@ <None Update="Resources\Responses\UserByScreenName.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> + <None Update="Resources\Responses\UserTweets_SimpleTweet.json"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> </ItemGroup> </Project> diff --git a/OpenTween.Tests/Resources/Responses/UserTweets_SimpleTweet.json b/OpenTween.Tests/Resources/Responses/UserTweets_SimpleTweet.json new file mode 100644 index 000000000..fc2ee0f15 --- /dev/null +++ b/OpenTween.Tests/Resources/Responses/UserTweets_SimpleTweet.json @@ -0,0 +1,185 @@ +{ + "data": { + "user": { + "result": { + "__typename": "User", + "timeline_v2": { + "timeline": { + "instructions": [ + { + "type": "TimelineClearCache" + }, + { + "type": "TimelineAddEntries", + "entries": [ + { + "entryId": "tweet-1612926719035600906", + "sortIndex": "1728066818414739426", + "content": { + "entryType": "TimelineTimelineItem", + "__typename": "TimelineTimelineItem", + "itemContent": { + "itemType": "TimelineTweet", + "__typename": "TimelineTweet", + "tweet_results": { + "result": { + "__typename": "Tweet", + "rest_id": "1612926719035600906", + "core": { + "user_results": { + "result": { + "__typename": "User", + "id": "VXNlcjo0MDQ4MDY2NA==", + "rest_id": "40480664", + "affiliates_highlighted_label": {}, + "has_graduated_access": true, + "is_blue_verified": false, + "profile_image_shape": "Circle", + "legacy": { + "following": true, + "can_dm": false, + "can_media_tag": false, + "created_at": "Sat May 16 15:20:01 +0000 2009", + "default_profile": false, + "default_profile_image": false, + "description": "OpenTween Project 言い出しっぺ", + "entities": { + "description": { + "urls": [] + }, + "url": { + "urls": [ + { + "display_url": "m.upsilo.net/@upsilon", + "expanded_url": "https://m.upsilo.net/@upsilon", + "url": "https://t.co/vNMmyHHOQD", + "indices": [ + 0, + 23 + ] + } + ] + } + }, + "fast_followers_count": 0, + "favourites_count": 215409, + "followers_count": 1288, + "friends_count": 2, + "has_custom_timelines": false, + "is_translator": false, + "listed_count": 92, + "location": "Funabashi, Chiba, Japan", + "media_count": 876, + "name": "upsilon", + "normal_followers_count": 1288, + "pinned_tweet_ids_str": [], + "possibly_sensitive": false, + "profile_banner_url": "https://pbs.twimg.com/profile_banners/40480664/1349188016", + "profile_image_url_https": "https://pbs.twimg.com/profile_images/719076434/____normal.png", + "profile_interstitial_type": "", + "screen_name": "kim_upsilon", + "statuses_count": 10081, + "translator_type": "regular", + "url": "https://t.co/vNMmyHHOQD", + "verified": false, + "want_retweets": true, + "withheld_in_countries": [] + } + } + } + }, + "unmention_data": {}, + "edit_control": { + "edit_tweet_ids": [ + "1612926719035600906" + ], + "editable_until_msecs": "1673388471000", + "is_edit_eligible": true, + "edits_remaining": "5" + }, + "is_translatable": true, + "views": { + "count": "288", + "state": "EnabledWithCount" + }, + "source": "<a href=\"https://www.opentween.org/\" rel=\"nofollow\">OpenTween (dev)</a>", + "legacy": { + "bookmark_count": 0, + "bookmarked": false, + "created_at": "Tue Jan 10 21:37:51 +0000 2023", + "conversation_id_str": "1612926719035600906", + "display_text_range": [ + 0, + 5 + ], + "entities": { + "user_mentions": [], + "urls": [], + "hashtags": [], + "symbols": [] + }, + "favorite_count": 2, + "favorited": false, + "full_text": "よろしくね", + "is_quote_status": false, + "lang": "ja", + "quote_count": 0, + "reply_count": 0, + "retweet_count": 0, + "retweeted": false, + "user_id_str": "40480664", + "id_str": "1612926719035600906" + }, + "quick_promote_eligibility": { + "eligibility": "IneligibleNotProfessional" + } + } + }, + "tweetDisplayType": "Tweet" + }, + "clientEventInfo": { + "component": "tweet", + "element": "tweet", + "details": { + "timelinesDetails": { + "injectionType": "RankedOrganicTweet", + "controllerData": "DAACDAABDAABCgABAAAAAAAAAAAKAAkAAAAAZE0+pwAAAAA=" + } + } + } + } + }, + { + "entryId": "cursor-top-1728066818414739457", + "sortIndex": "1728066818414739457", + "content": { + "entryType": "TimelineTimelineCursor", + "__typename": "TimelineTimelineCursor", + "value": "DAABCgABF_tTnZvAJxEKAAIWes8rE1oQAAgAAwAAAAEAAA", + "cursorType": "Top" + } + }, + { + "entryId": "cursor-bottom-1728066818414739439", + "sortIndex": "1728066818414739439", + "content": { + "entryType": "TimelineTimelineCursor", + "__typename": "TimelineTimelineCursor", + "value": "DAABCgABF_tTnZu__-0KAAIWZa6KTRoAAwgAAwAAAAIAAA", + "cursorType": "Bottom" + } + } + ] + } + ], + "metadata": { + "scribeConfig": { + "page": "profileBest" + } + } + } + } + } + } + } +} diff --git a/OpenTween/Api/GraphQL/UserTweetsRequest.cs b/OpenTween/Api/GraphQL/UserTweetsRequest.cs new file mode 100644 index 000000000..e9749c44e --- /dev/null +++ b/OpenTween/Api/GraphQL/UserTweetsRequest.cs @@ -0,0 +1,102 @@ +// OpenTween - Client of Twitter +// Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/> +// All rights reserved. +// +// This file is part of OpenTween. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. If not, see <http://www.gnu.org/licenses/>, or write to +// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, +// Boston, MA 02110-1301, USA. + +#nullable enable + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.Serialization.Json; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using System.Xml.Linq; +using System.Xml.XPath; +using OpenTween.Connection; + +namespace OpenTween.Api.GraphQL +{ + public class UserTweetsRequest + { + private static readonly Uri EndpointUri = new("https://twitter.com/i/api/graphql/2GIWTr7XwadIixZDtyXd4A/UserTweets"); + + public string UserId { get; set; } + + public int Count { get; set; } = 20; + + public string? Cursor { get; set; } + + public UserTweetsRequest(string userId) + => this.UserId = userId; + + public Dictionary<string, string> CreateParameters() + { + return new() + { + ["variables"] = "{" + + $@"""userId"":""{JsonUtils.EscapeJsonString(this.UserId)}""," + + $@"""count"":20," + + $@"""includePromotedContent"":true," + + $@"""withQuickPromoteEligibilityTweetFields"":true," + + $@"""withVoice"":true," + + $@"""withV2Timeline"":true" + + (this.Cursor != null ? $@",""cursor"":""{JsonUtils.EscapeJsonString(this.Cursor)}""" : "") + + "}", + ["features"] = """ + {"rweb_lists_timeline_redesign_enabled":true,"responsive_web_graphql_exclude_directive_enabled":true,"verified_phone_label_enabled":false,"creator_subscriptions_tweet_preview_api_enabled":true,"responsive_web_graphql_timeline_navigation_enabled":true,"responsive_web_graphql_skip_user_profile_image_extensions_enabled":false,"tweetypie_unmention_optimization_enabled":true,"responsive_web_edit_tweet_api_enabled":true,"graphql_is_translatable_rweb_tweet_is_translatable_enabled":true,"view_counts_everywhere_api_enabled":true,"longform_notetweets_consumption_enabled":true,"responsive_web_twitter_article_tweet_consumption_enabled":false,"tweet_awards_web_tipping_enabled":false,"freedom_of_speech_not_reach_fetch_enabled":true,"standardized_nudges_misinfo":true,"tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled":true,"longform_notetweets_rich_text_read_enabled":true,"longform_notetweets_inline_media_enabled":true,"responsive_web_media_download_video_enabled":false,"responsive_web_enhance_cards_enabled":false} + """, + ["fieldToggles"] = """ + {"withAuxiliaryUserLabels":false,"withArticleRichContentState":false} + """, + }; + } + + public async Task<TimelineResponse> Send(IApiConnection apiConnection) + { + var param = this.CreateParameters(); + + XElement rootElm; + try + { + using var stream = await apiConnection.GetStreamAsync(EndpointUri, param); + using var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max); + rootElm = XElement.Load(jsonReader); + } + catch (IOException ex) + { + throw new WebApiException("IO Error", ex); + } + catch (NotSupportedException ex) + { + // NotSupportedException: Stream does not support reading. のエラーが時々報告される + throw new WebApiException("Stream Error", ex); + } + + ErrorResponse.ThrowIfError(rootElm); + + var tweets = TimelineTweet.ExtractTimelineTweets(rootElm); + var cursorBottom = rootElm.XPathSelectElement("//content[__typename[text()='TimelineTimelineCursor']][cursorType[text()='Bottom']]/value")?.Value; + + return new(tweets, cursorBottom); + } + } +} diff --git a/OpenTween/Models/TabInformations.cs b/OpenTween/Models/TabInformations.cs index eee786929..7b338462a 100644 --- a/OpenTween/Models/TabInformations.cs +++ b/OpenTween/Models/TabInformations.cs @@ -253,7 +253,10 @@ public void LoadTabsFromSettings(SettingTabs settingTabs) MyCommon.TabUsageType.UserDefined => new FilterTabModel(tabName), MyCommon.TabUsageType.UserTimeline - => new UserTimelineTabModel(tabName, tabSetting.User!), + => new UserTimelineTabModel(tabName, tabSetting.User!) + { + UserId = tabSetting.UserId, + }, MyCommon.TabUsageType.PublicSearch => new PublicSearchTabModel(tabName) { diff --git a/OpenTween/Models/UserTimelineTabModel.cs b/OpenTween/Models/UserTimelineTabModel.cs index 1d876ec0e..51faea6eb 100644 --- a/OpenTween/Models/UserTimelineTabModel.cs +++ b/OpenTween/Models/UserTimelineTabModel.cs @@ -43,8 +43,12 @@ public override MyCommon.TabUsageType TabType public string ScreenName { get; } + public string? UserId { get; set; } + public PostId? OldestId { get; set; } + public string? CursorBottom { get; set; } + public UserTimelineTabModel(string tabName, string screenName) : base(tabName) { @@ -64,7 +68,7 @@ public override async Task RefreshAsync(Twitter tw, bool backward, bool startup, progress.Report("UserTimeline refreshing..."); - await tw.GetUserTimelineApi(read, this.ScreenName, this, backward) + await tw.GetUserTimelineApi(read, this, backward) .ConfigureAwait(false); TabInformations.GetInstance().DistributePosts(); diff --git a/OpenTween/Setting/SettingTabs.cs b/OpenTween/Setting/SettingTabs.cs index f8974b99a..7f14dd5f2 100644 --- a/OpenTween/Setting/SettingTabs.cs +++ b/OpenTween/Setting/SettingTabs.cs @@ -83,6 +83,14 @@ public class SettingTabItem /// </summary> public string? User { get; set; } + /// <summary> + /// 表示するユーザーのID (<see cref="MyCommon.TabUsageType.UserTimeline"/> で使用) + /// </summary> + /// <remarks> + /// v3.7.1 まで存在しない項目のため、空の場合は <see cref="User"/> から補う必要がある + /// </remarks> + public string? UserId { get; set; } + /// <summary> /// 検索文字列 (<see cref="MyCommon.TabUsageType.PublicSearch"/> で使用) /// </summary> diff --git a/OpenTween/Tween.cs b/OpenTween/Tween.cs index 7bf8bac8a..f879d58c9 100644 --- a/OpenTween/Tween.cs +++ b/OpenTween/Tween.cs @@ -5807,6 +5807,7 @@ private void SaveConfigsTabs() break; case UserTimelineTabModel userTab: tabSetting.User = userTab.ScreenName; + tabSetting.UserId = userTab.UserId; break; case PublicSearchTabModel searchTab: tabSetting.SearchWords = searchTab.SearchWords; diff --git a/OpenTween/Twitter.cs b/OpenTween/Twitter.cs index 1046b9fa0..1942c99ee 100644 --- a/OpenTween/Twitter.cs +++ b/OpenTween/Twitter.cs @@ -660,7 +660,7 @@ public async Task GetMentionsTimelineApi(bool read, MentionsTabModel tab, bool m tab.OldestId = minimumId; } - public async Task GetUserTimelineApi(bool read, string userName, UserTimelineTabModel tab, bool more) + public async Task GetUserTimelineApi(bool read, UserTimelineTabModel tab, bool more) { this.CheckAccountState(); @@ -671,24 +671,39 @@ public async Task GetUserTimelineApi(bool read, string userName, UserTimelineTab count = Math.Min(count, 99); TwitterStatus[] statuses; - if (MyCommon.IsNullOrEmpty(userName)) + if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie) { - var target = tab.ScreenName; - if (MyCommon.IsNullOrEmpty(target)) return; - userName = target; - statuses = await this.Api.StatusesUserTimeline(userName, count) + var userId = tab.UserId; + if (MyCommon.IsNullOrEmpty(userId)) + { + var user = await this.GetUserInfo(tab.ScreenName) + .ConfigureAwait(false); + + userId = user.IdStr; + tab.UserId = user.IdStr; + } + + var request = new UserTweetsRequest(userId) + { + Count = count, + Cursor = more ? tab.CursorBottom : null, + }; + var response = await request.Send(this.Api.Connection) .ConfigureAwait(false); + + statuses = response.Tweets.Select(x => x.ToTwitterStatus()).ToArray(); + tab.CursorBottom = response.CursorBottom; } else { if (more) { - statuses = await this.Api.StatusesUserTimeline(userName, count, maxId: tab.OldestId as TwitterStatusId) + statuses = await this.Api.StatusesUserTimeline(tab.ScreenName, count, maxId: tab.OldestId as TwitterStatusId) .ConfigureAwait(false); } else { - statuses = await this.Api.StatusesUserTimeline(userName, count) + statuses = await this.Api.StatusesUserTimeline(tab.ScreenName, count) .ConfigureAwait(false); } }