comparison app/lib/legit_client.rb @ 260:e2150dce4e90

Rubocop (rails) time
author nanaya <me@nanaya.net>
date Sun, 15 Dec 2024 22:59:09 +0900
parents 8b75d00c77ba
children
comparison
equal deleted inserted replaced
259:8b75d00c77ba 260:e2150dce4e90
1 require 'net/http' 1 require "net/http"
2 2
3 module LegitClient 3 module LegitClient
4 def self.timeline(user_id) 4 def self.timeline(user_id)
5 resp = fetch("https://x.com/i/api/graphql/1-5o8Qhfc2kWlu_2rWNcug/UserTweetsAndReplies?variables=%7B%22userId%22%3A#{escape_param user_id}%2C%22count%22%3A50%2C%22includePromotedContent%22%3Atrue%2C%22withCommunity%22%3Atrue%2C%22withVoice%22%3Atrue%2C%22withV2Timeline%22%3Atrue%7D&features=%7B%22rweb_lists_timeline_redesign_enabled%22%3Atrue%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22tweetypie_unmention_optimization_enabled%22%3Atrue%2C%22responsive_web_edit_tweet_api_enabled%22%3Atrue%2C%22graphql_is_translatable_rweb_tweet_is_translatable_enabled%22%3Atrue%2C%22view_counts_everywhere_api_enabled%22%3Atrue%2C%22longform_notetweets_consumption_enabled%22%3Atrue%2C%22responsive_web_twitter_article_tweet_consumption_enabled%22%3Afalse%2C%22tweet_awards_web_tipping_enabled%22%3Afalse%2C%22freedom_of_speech_not_reach_fetch_enabled%22%3Atrue%2C%22standardized_nudges_misinfo%22%3Atrue%2C%22tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled%22%3Atrue%2C%22longform_notetweets_rich_text_read_enabled%22%3Atrue%2C%22longform_notetweets_inline_media_enabled%22%3Atrue%2C%22responsive_web_media_download_video_enabled%22%3Afalse%2C%22responsive_web_enhance_cards_enabled%22%3Afalse%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%2C%22withArticleRichContentState%22%3Afalse%7D") 5 resp = fetch("https://x.com/i/api/graphql/1-5o8Qhfc2kWlu_2rWNcug/UserTweetsAndReplies?variables=%7B%22userId%22%3A#{escape_param user_id}%2C%22count%22%3A50%2C%22includePromotedContent%22%3Atrue%2C%22withCommunity%22%3Atrue%2C%22withVoice%22%3Atrue%2C%22withV2Timeline%22%3Atrue%7D&features=%7B%22rweb_lists_timeline_redesign_enabled%22%3Atrue%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22tweetypie_unmention_optimization_enabled%22%3Atrue%2C%22responsive_web_edit_tweet_api_enabled%22%3Atrue%2C%22graphql_is_translatable_rweb_tweet_is_translatable_enabled%22%3Atrue%2C%22view_counts_everywhere_api_enabled%22%3Atrue%2C%22longform_notetweets_consumption_enabled%22%3Atrue%2C%22responsive_web_twitter_article_tweet_consumption_enabled%22%3Afalse%2C%22tweet_awards_web_tipping_enabled%22%3Afalse%2C%22freedom_of_speech_not_reach_fetch_enabled%22%3Atrue%2C%22standardized_nudges_misinfo%22%3Atrue%2C%22tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled%22%3Atrue%2C%22longform_notetweets_rich_text_read_enabled%22%3Atrue%2C%22longform_notetweets_inline_media_enabled%22%3Atrue%2C%22responsive_web_media_download_video_enabled%22%3Afalse%2C%22responsive_web_enhance_cards_enabled%22%3Afalse%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%2C%22withArticleRichContentState%22%3Afalse%7D")
6 6
7 handle_response resp, :timeline, "timeline(#{user_id})", ->(json) do 7 handle_response resp, :timeline, "timeline(#{user_id})", ->(json) do
8 normalize_timeline json['data']['user']['result']['timeline_v2']['timeline']['instructions'], user_id 8 normalize_timeline json["data"]["user"]["result"]["timeline_v2"]["timeline"]["instructions"], user_id
9 end 9 end
10 end 10 end
11 11
12 def self.user_by_id(user_id) 12 def self.user_by_id(user_id)
13 resp = fetch("https://x.com/i/api/graphql/i_0UQ54YrCyqLUvgGzXygA/UserByRestId?variables=%7B%22userId%22%3A#{escape_param user_id}%2C%22withSafetyModeUserFields%22%3Atrue%7D&features=%7B%22hidden_profile_likes_enabled%22%3Afalse%2C%22hidden_profile_subscriptions_enabled%22%3Afalse%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22highlights_tweets_tab_ui_enabled%22%3Atrue%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%7D") 13 resp = fetch("https://x.com/i/api/graphql/i_0UQ54YrCyqLUvgGzXygA/UserByRestId?variables=%7B%22userId%22%3A#{escape_param user_id}%2C%22withSafetyModeUserFields%22%3Atrue%7D&features=%7B%22hidden_profile_likes_enabled%22%3Afalse%2C%22hidden_profile_subscriptions_enabled%22%3Afalse%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22highlights_tweets_tab_ui_enabled%22%3Atrue%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%7D")
14 14
15 handle_response resp, :user, "user_by_id(#{user_id})", ->(json) do 15 handle_response resp, :user, "user_by_id(#{user_id})", ->(json) do
16 normalize_user json['data']['user']['result'] 16 normalize_user json["data"]["user"]["result"]
17 end 17 end
18 end 18 end
19 19
20 def self.user_by_username(username) 20 def self.user_by_username(username)
21 resp = fetch("https://x.com/i/api/graphql/xc8f1g7BYqr6VTzTbvNlGw/UserByScreenName?variables=%7B%22screen_name%22%3A#{escape_param username}%2C%22withSafetyModeUserFields%22%3Atrue%7D&features=%7B%22hidden_profile_likes_enabled%22%3Afalse%2C%22hidden_profile_subscriptions_enabled%22%3Afalse%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22subscriptions_verification_info_verified_since_enabled%22%3Atrue%2C%22highlights_tweets_tab_ui_enabled%22%3Atrue%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%7D") 21 resp = fetch("https://x.com/i/api/graphql/xc8f1g7BYqr6VTzTbvNlGw/UserByScreenName?variables=%7B%22screen_name%22%3A#{escape_param username}%2C%22withSafetyModeUserFields%22%3Atrue%7D&features=%7B%22hidden_profile_likes_enabled%22%3Afalse%2C%22hidden_profile_subscriptions_enabled%22%3Afalse%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22subscriptions_verification_info_verified_since_enabled%22%3Atrue%2C%22highlights_tweets_tab_ui_enabled%22%3Atrue%2C%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%7D&fieldToggles=%7B%22withAuxiliaryUserLabels%22%3Afalse%7D")
22 22
23 handle_response resp, :user, "user_by_username(#{username})", ->(json) do 23 handle_response resp, :user, "user_by_username(#{username})", ->(json) do
24 normalize_user json['data']['user']['result'] 24 normalize_user json["data"]["user"]["result"]
25 end 25 end
26 end 26 end
27 27
28 def self.escape_param(param) 28 def self.escape_param(param)
29 CGI.escape JSON.dump(param) 29 CGI.escape JSON.dump(param)
35 35
36 def self.handle_response(resp, key, error_key, callback) 36 def self.handle_response(resp, key, error_key, callback)
37 json = JSON.parse(resp) 37 json = JSON.parse(resp)
38 { 38 {
39 key => callback.call(json), 39 key => callback.call(json),
40 raw: resp, 40 raw: resp
41 } 41 }
42 rescue => e 42 rescue => e
43 if json.is_a? Hash 43 if json.is_a? Hash
44 if json['errors'].is_a? Array 44 if json["errors"].is_a? Array
45 return rate_limit_check(json) 45 return rate_limit_check(json)
46 elsif json['data'].is_a? Hash 46 elsif json["data"].is_a? Hash
47 return 47 return
48 end 48 end
49 end 49 end
50 Rails.logger.error("#{error_key} fail: #{resp}") 50 Rails.logger.error("#{error_key} fail: #{resp}")
51 51
56 ret = {} 56 ret = {}
57 57
58 json.each do |entity_media| 58 json.each do |entity_media|
59 val = {} 59 val = {}
60 60
61 case entity_media['type'] 61 case entity_media["type"]
62 when 'animated_gif', 'video' 62 when "animated_gif", "video"
63 val[:variants] = entity_media['video_info']['variants'] 63 val[:variants] = entity_media["video_info"]["variants"]
64 .filter { |variant| variant['bitrate'].present? } 64 .filter { |variant| variant["bitrate"].present? }
65 .map do |variant| 65 .map do |variant|
66 { 66 {
67 bitrate: variant['bitrate'], 67 bitrate: variant["bitrate"],
68 url: variant['url'], 68 url: variant["url"]
69 } 69 }
70 end 70 end
71 when 'photo' 71 when "photo"
72 val[:image_url] = entity_media['media_url_https'].sub(/\.([^.]+)$/, '?format=\1') 72 val[:image_url] = entity_media["media_url_https"].sub(/\.([^.]+)$/, '?format=\1')
73 end 73 end
74 74
75 if !val.empty? 75 if !val.empty?
76 val[:url] = entity_media['expanded_url'] 76 val[:url] = entity_media["expanded_url"]
77 val[:type] = entity_media['type'] 77 val[:type] = entity_media["type"]
78 val[:id] = entity_media['media_key'] 78 val[:id] = entity_media["media_key"]
79 end 79 end
80 80
81 key = if ret[entity_media['url']].nil? 81 key = if ret[entity_media["url"]].nil?
82 entity_media['url'] 82 entity_media["url"]
83 else 83 else
84 entity_media['media_key'] 84 entity_media["media_key"]
85 end 85 end
86 86
87 ret[key] = val 87 ret[key] = val
88 end 88 end
89 89
92 92
93 def self.normalize_entity_urls(json) 93 def self.normalize_entity_urls(json)
94 ret = {} 94 ret = {}
95 95
96 (json || {}).each do |entity_url| 96 (json || {}).each do |entity_url|
97 ret[entity_url['url']] = entity_url['expanded_url'] 97 ret[entity_url["url"]] = entity_url["expanded_url"]
98 end 98 end
99 99
100 ret 100 ret
101 end 101 end
102 102
103 def self.normalize_timeline(json, user_id) 103 def self.normalize_timeline(json, user_id)
104 json 104 json
105 .reduce([]) do |acc, instruction| 105 .reduce([]) do |acc, instruction|
106 case instruction['type'] 106 case instruction["type"]
107 when 'TimelineAddEntries' then acc += instruction['entries'] 107 when "TimelineAddEntries" then acc += instruction["entries"]
108 when 'TimelinePinEntry' then acc << instruction['entry'] 108 when "TimelinePinEntry" then acc << instruction["entry"]
109 end 109 end
110 110
111 acc 111 acc
112 end.filter { |entry| entry['entryId'] =~ /\A(profile-conversation|tweet)-/ } 112 end.filter { |entry| entry["entryId"] =~ /\A(profile-conversation|tweet)-/ }
113 .reduce([]) do |acc, entry| 113 .reduce([]) do |acc, entry|
114 if entry['content']['entryType'] == 'TimelineTimelineItem' 114 if entry["content"]["entryType"] == "TimelineTimelineItem"
115 acc.push(entry['content']) 115 acc.push(entry["content"])
116 else 116 else
117 entry['content']['items'].each do |item| 117 entry["content"]["items"].each do |item|
118 acc.push(item['item']) 118 acc.push(item["item"])
119 end 119 end
120 end 120 end
121 acc 121 acc
122 end.map { |raw_tweet| normalize_tweet(raw_tweet['itemContent']['tweet_results']['result']) } 122 end.map { |raw_tweet| normalize_tweet(raw_tweet["itemContent"]["tweet_results"]["result"]) }
123 .filter { |tweet| !tweet.nil? && tweet.dig(:user, :id) == user_id } 123 .filter { |tweet| !tweet.nil? && tweet.dig(:user, :id) == user_id }
124 end 124 end
125 125
126 def self.normalize_tweet(json) 126 def self.normalize_tweet(json)
127 return nil if json.nil? 127 return nil if json.nil?
128 128
129 return normalize_tweet(json['tweet']) if json['__typename'] == 'TweetWithVisibilityResults' 129 return normalize_tweet(json["tweet"]) if json["__typename"] == "TweetWithVisibilityResults"
130 130
131 { 131 {
132 id: json['rest_id'], 132 id: json["rest_id"],
133 created_at: Time.parse(json['legacy']['created_at']), 133 created_at: Time.parse(json["legacy"]["created_at"]),
134 user: normalize_user(json['core']['user_results']['result']), 134 user: normalize_user(json["core"]["user_results"]["result"]),
135 message: json.dig('note_tweet', 'note_tweet_results', 'result', 'text') || json['legacy']['full_text'], 135 message: json.dig("note_tweet", "note_tweet_results", "result", "text") || json["legacy"]["full_text"],
136 retweet: normalize_tweet(json.dig('legacy', 'retweeted_status_result', 'result')), 136 retweet: normalize_tweet(json.dig("legacy", "retweeted_status_result", "result")),
137 quote: normalize_tweet(json.dig('quoted_status_result', 'result')), 137 quote: normalize_tweet(json.dig("quoted_status_result", "result")),
138 quote_id: json['legacy']['quoted_status_id_str'], 138 quote_id: json["legacy"]["quoted_status_id_str"],
139 reply_to_id: json['legacy']['in_reply_to_status_id_str'], 139 reply_to_id: json["legacy"]["in_reply_to_status_id_str"],
140 reply_to_user_id: json['legacy']['in_reply_to_user_id_str'], 140 reply_to_user_id: json["legacy"]["in_reply_to_user_id_str"],
141 reply_to_username: json['legacy']['in_reply_to_screen_name'], 141 reply_to_username: json["legacy"]["in_reply_to_screen_name"],
142 entity_urls: { **normalize_entity_urls(json['legacy']['entities']['urls']), **normalize_entity_urls(json.dig('note_tweet', 'note_tweet_results', 'result', 'entity_set', 'urls')) }, 142 entity_urls: { **normalize_entity_urls(json["legacy"]["entities"]["urls"]), **normalize_entity_urls(json.dig("note_tweet", "note_tweet_results", "result", "entity_set", "urls")) },
143 entity_media: normalize_entity_media(json.dig('legacy', 'extended_entities', 'media') || []), 143 entity_media: normalize_entity_media(json.dig("legacy", "extended_entities", "media") || [])
144 } 144 }
145 end 145 end
146 146
147 def self.normalize_user(json) 147 def self.normalize_user(json)
148 { 148 {
149 avatar_url: json['legacy']['profile_image_url_https'], 149 avatar_url: json["legacy"]["profile_image_url_https"],
150 id: json['rest_id'], 150 id: json["rest_id"],
151 name: json['legacy']['name'], 151 name: json["legacy"]["name"],
152 protected: json['legacy']['protected'] == true, 152 protected: json["legacy"]["protected"] == true,
153 username: json['legacy']['screen_name'], 153 username: json["legacy"]["screen_name"]
154 } 154 }
155 end 155 end
156 156
157 def self.rate_limit_check(json) 157 def self.rate_limit_check(json)
158 return unless json['errors'].any? { |err| err['code'] == 88 } 158 return unless json["errors"].any? { |err| err["code"] == 88 }
159 159
160 raise 'Rate limited!' 160 raise "Rate limited!"
161 end 161 end
162 end 162 end