OSDN Git Service

SearchTimelineの取得に新しいIApiConnectionを使用する
[opentween/open-tween.git] / OpenTween / Api / GraphQL / SearchTimelineRequest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 #nullable enable
23
24 using System;
25 using System.Collections.Generic;
26 using System.Threading.Tasks;
27 using System.Xml.XPath;
28 using OpenTween.Connection;
29
30 namespace OpenTween.Api.GraphQL
31 {
32     public class SearchTimelineRequest
33     {
34         public static readonly string EndpointName = "SearchTimeline";
35
36         private static readonly Uri EndpointUri = new("https://twitter.com/i/api/graphql/lZ0GCEojmtQfiUQa5oJSEw/SearchTimeline");
37
38         public string RawQuery { get; set; }
39
40         public int Count { get; set; } = 20;
41
42         public string? Cursor { get; set; }
43
44         public SearchTimelineRequest(string rawQuery)
45             => this.RawQuery = rawQuery;
46
47         public Dictionary<string, string> CreateParameters()
48         {
49             return new()
50             {
51                 ["variables"] = "{" +
52                     $@"""rawQuery"":""{JsonUtils.EscapeJsonString(this.RawQuery)}""," +
53                     $@"""count"":{this.Count}," +
54                     $@"""product"":""Latest""" +
55                     (this.Cursor != null ? $@",""cursor"":""{JsonUtils.EscapeJsonString(this.Cursor)}""" : "") +
56                     "}",
57                 ["features"] = "{" +
58                     @"""responsive_web_graphql_exclude_directive_enabled"":true," +
59                     @"""verified_phone_label_enabled"":false," +
60                     @"""responsive_web_home_pinned_timelines_enabled"":true," +
61                     @"""creator_subscriptions_tweet_preview_api_enabled"":true," +
62                     @"""responsive_web_graphql_timeline_navigation_enabled"":true," +
63                     @"""responsive_web_graphql_skip_user_profile_image_extensions_enabled"":false," +
64                     @"""c9s_tweet_anatomy_moderator_badge_enabled"":true," +
65                     @"""tweetypie_unmention_optimization_enabled"":true," +
66                     @"""responsive_web_edit_tweet_api_enabled"":true," +
67                     @"""graphql_is_translatable_rweb_tweet_is_translatable_enabled"":true," +
68                     @"""view_counts_everywhere_api_enabled"":true," +
69                     @"""longform_notetweets_consumption_enabled"":true," +
70                     @"""responsive_web_twitter_article_tweet_consumption_enabled"":false," +
71                     @"""tweet_awards_web_tipping_enabled"":false," +
72                     @"""freedom_of_speech_not_reach_fetch_enabled"":true," +
73                     @"""standardized_nudges_misinfo"":true," +
74                     @"""tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled"":true," +
75                     @"""longform_notetweets_rich_text_read_enabled"":true," +
76                     @"""longform_notetweets_inline_media_enabled"":true," +
77                     @"""responsive_web_media_download_video_enabled"":false," +
78                     @"""responsive_web_enhance_cards_enabled"":false" +
79                     "}",
80             };
81         }
82
83         public async Task<TimelineResponse> Send(IApiConnection apiConnection)
84         {
85             var request = new GetRequest
86             {
87                 RequestUri = EndpointUri,
88                 Query = this.CreateParameters(),
89                 EndpointName = EndpointName,
90             };
91
92             using var response = await apiConnection.SendAsync(request)
93                 .ConfigureAwait(false);
94
95             var rootElm = await response.ReadAsJsonXml()
96                 .ConfigureAwait(false);
97
98             ErrorResponse.ThrowIfError(rootElm);
99
100             var tweets = TimelineTweet.ExtractTimelineTweets(rootElm);
101             var cursorTop = rootElm.XPathSelectElement("//content[__typename[text()='TimelineTimelineCursor']][cursorType[text()='Top']]/value")?.Value;
102             var cursorBottom = rootElm.XPathSelectElement("//content[__typename[text()='TimelineTimelineCursor']][cursorType[text()='Bottom']]/value")?.Value;
103
104             return new(tweets, cursorTop, cursorBottom);
105         }
106     }
107 }