annotate app/lib/legit_client.rb @ 262:cd927bb7dc0d

Move out custom application config
author nanaya <me@nanaya.net>
date Mon, 16 Dec 2024 01:39:38 +0900
parents e2150dce4e90
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
1 require "net/http"
259
8b75d00c77ba Fix missing require
nanaya <me@nanaya.net>
parents: 257
diff changeset
2
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
3 module LegitClient
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
4 def self.timeline(user_id)
257
c6a50441a58d It's now X
nanaya <me@nanaya.net>
parents: 253
diff changeset
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")
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
6
243
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
7 handle_response resp, :timeline, "timeline(#{user_id})", ->(json) do
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
8 normalize_timeline json["data"]["user"]["result"]["timeline_v2"]["timeline"]["instructions"], user_id
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
9 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
10 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
11
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
12 def self.user_by_id(user_id)
257
c6a50441a58d It's now X
nanaya <me@nanaya.net>
parents: 253
diff changeset
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")
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
14
243
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
15 handle_response resp, :user, "user_by_id(#{user_id})", ->(json) do
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
16 normalize_user json["data"]["user"]["result"]
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
17 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
18 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
19
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
20 def self.user_by_username(username)
257
c6a50441a58d It's now X
nanaya <me@nanaya.net>
parents: 253
diff changeset
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")
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
22
243
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
23 handle_response resp, :user, "user_by_username(#{username})", ->(json) do
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
24 normalize_user json["data"]["user"]["result"]
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
25 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
26 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
27
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
28 def self.escape_param(param)
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
29 CGI.escape JSON.dump(param)
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
30 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
31
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
32 def self.fetch(uri)
236
498043313523 Support multiple headers
nanaya <me@nanaya.net>
parents: 234
diff changeset
33 Net::HTTP.get(URI(uri), $cfg[:headers].sample)
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
34 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
35
243
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
36 def self.handle_response(resp, key, error_key, callback)
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
37 json = JSON.parse(resp)
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
38 {
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
39 key => callback.call(json),
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
40 raw: resp
243
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
41 }
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
42 rescue => e
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
43 if json.is_a? Hash
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
44 if json["errors"].is_a? Array
243
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
45 return rate_limit_check(json)
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
46 elsif json["data"].is_a? Hash
243
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
47 return
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
48 end
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
49 end
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
50 Rails.logger.error("#{error_key} fail: #{resp}")
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
51
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
52 raise e
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
53 end
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
54
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
55 def self.normalize_entity_media(json)
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
56 ret = {}
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
57
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
58 json.each do |entity_media|
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
59 val = {}
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
60
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
61 case entity_media["type"]
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
62 when "animated_gif", "video"
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
63 val[:variants] = entity_media["video_info"]["variants"]
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
64 .filter { |variant| variant["bitrate"].present? }
253
d726e8b92dd1 Support animated gif (same as video)
nanaya <me@nanaya.net>
parents: 252
diff changeset
65 .map do |variant|
d726e8b92dd1 Support animated gif (same as video)
nanaya <me@nanaya.net>
parents: 252
diff changeset
66 {
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
67 bitrate: variant["bitrate"],
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
68 url: variant["url"]
253
d726e8b92dd1 Support animated gif (same as video)
nanaya <me@nanaya.net>
parents: 252
diff changeset
69 }
d726e8b92dd1 Support animated gif (same as video)
nanaya <me@nanaya.net>
parents: 252
diff changeset
70 end
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
71 when "photo"
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
72 val[:image_url] = entity_media["media_url_https"].sub(/\.([^.]+)$/, '?format=\1')
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
73 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
74
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
75 if !val.empty?
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
76 val[:url] = entity_media["expanded_url"]
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
77 val[:type] = entity_media["type"]
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
78 val[:id] = entity_media["media_key"]
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
79 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
80
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
81 key = if ret[entity_media["url"]].nil?
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
82 entity_media["url"]
237
961d362e42c7 The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents: 236
diff changeset
83 else
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
84 entity_media["media_key"]
237
961d362e42c7 The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents: 236
diff changeset
85 end
961d362e42c7 The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents: 236
diff changeset
86
961d362e42c7 The url in entity media isn't unique as they all point to the same thing
nanaya <me@nanaya.net>
parents: 236
diff changeset
87 ret[key] = val
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
88 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
89
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
90 ret
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
91 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
92
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
93 def self.normalize_entity_urls(json)
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
94 ret = {}
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
95
240
c454ea4f7b34 Add support for note tweets (with no formatting)
nanaya <me@nanaya.net>
parents: 238
diff changeset
96 (json || {}).each do |entity_url|
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
97 ret[entity_url["url"]] = entity_url["expanded_url"]
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
98 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
99
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
100 ret
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
101 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
102
238
a04b4830eef2 Filter out non-own tweets included for replies
nanaya <me@nanaya.net>
parents: 237
diff changeset
103 def self.normalize_timeline(json, user_id)
252
151bc6d97d39 Include pinned tweet
nanaya <me@nanaya.net>
parents: 249
diff changeset
104 json
151bc6d97d39 Include pinned tweet
nanaya <me@nanaya.net>
parents: 249
diff changeset
105 .reduce([]) do |acc, instruction|
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
106 case instruction["type"]
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
107 when "TimelineAddEntries" then acc += instruction["entries"]
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
108 when "TimelinePinEntry" then acc << instruction["entry"]
252
151bc6d97d39 Include pinned tweet
nanaya <me@nanaya.net>
parents: 249
diff changeset
109 end
151bc6d97d39 Include pinned tweet
nanaya <me@nanaya.net>
parents: 249
diff changeset
110
151bc6d97d39 Include pinned tweet
nanaya <me@nanaya.net>
parents: 249
diff changeset
111 acc
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
112 end.filter { |entry| entry["entryId"] =~ /\A(profile-conversation|tweet)-/ }
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
113 .reduce([]) do |acc, entry|
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
114 if entry["content"]["entryType"] == "TimelineTimelineItem"
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
115 acc.push(entry["content"])
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
116 else
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
117 entry["content"]["items"].each do |item|
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
118 acc.push(item["item"])
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
119 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
120 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
121 acc
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
122 end.map { |raw_tweet| normalize_tweet(raw_tweet["itemContent"]["tweet_results"]["result"]) }
238
a04b4830eef2 Filter out non-own tweets included for replies
nanaya <me@nanaya.net>
parents: 237
diff changeset
123 .filter { |tweet| !tweet.nil? && tweet.dig(:user, :id) == user_id }
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
124 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
125
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
126 def self.normalize_tweet(json)
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
127 return nil if json.nil?
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
128
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
129 return normalize_tweet(json["tweet"]) if json["__typename"] == "TweetWithVisibilityResults"
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
130
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
131 {
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
132 id: json["rest_id"],
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
133 created_at: Time.parse(json["legacy"]["created_at"]),
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
134 user: normalize_user(json["core"]["user_results"]["result"]),
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
135 message: json.dig("note_tweet", "note_tweet_results", "result", "text") || json["legacy"]["full_text"],
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
136 retweet: normalize_tweet(json.dig("legacy", "retweeted_status_result", "result")),
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
137 quote: normalize_tweet(json.dig("quoted_status_result", "result")),
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
138 quote_id: json["legacy"]["quoted_status_id_str"],
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
139 reply_to_id: json["legacy"]["in_reply_to_status_id_str"],
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
140 reply_to_user_id: json["legacy"]["in_reply_to_user_id_str"],
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
141 reply_to_username: json["legacy"]["in_reply_to_screen_name"],
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
142 entity_urls: { **normalize_entity_urls(json["legacy"]["entities"]["urls"]), **normalize_entity_urls(json.dig("note_tweet", "note_tweet_results", "result", "entity_set", "urls")) },
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
143 entity_media: normalize_entity_media(json.dig("legacy", "extended_entities", "media") || [])
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
144 }
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
145 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
146
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
147 def self.normalize_user(json)
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
148 {
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
149 avatar_url: json["legacy"]["profile_image_url_https"],
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
150 id: json["rest_id"],
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
151 name: json["legacy"]["name"],
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
152 protected: json["legacy"]["protected"] == true,
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
153 username: json["legacy"]["screen_name"]
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
154 }
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
155 end
243
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
156
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
157 def self.rate_limit_check(json)
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
158 return unless json["errors"].any? { |err| err["code"] == 88 }
243
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
159
260
e2150dce4e90 Rubocop (rails) time
nanaya <me@nanaya.net>
parents: 259
diff changeset
160 raise "Rate limited!"
243
bc2f45058c9e Prevent caching of rate limited error and combine response handling
nanaya <me@nanaya.net>
parents: 241
diff changeset
161 end
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
162 end