comparison app/lib/legit_client.rb @ 243:bc2f45058c9e legit-client

Prevent caching of rate limited error and combine response handling
author nanaya <me@nanaya.net>
date Sun, 16 Jul 2023 08:53:59 +0900
parents 4bca1528675e
children 9e5f9ffa4077
comparison
equal deleted inserted replaced
242:3ac13a9e593d 243:bc2f45058c9e
1 module LegitClient 1 module LegitClient
2 def self.timeline(user_id) 2 def self.timeline(user_id)
3 resp = fetch("https://twitter.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") 3 resp = fetch("https://twitter.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")
4 4
5 begin 5 handle_response resp, :timeline, "timeline(#{user_id})", ->(json) do
6 json = JSON.parse(resp) 6 normalize_timeline json['data']['user']['result']['timeline_v2']['timeline']['instructions'], user_id
7 {
8 timeline: normalize_timeline(json['data']['user']['result']['timeline_v2']['timeline']['instructions'], user_id),
9 raw: resp,
10 }
11 rescue => e
12 return if (json || {}).dig('data').is_a? Hash
13 Rails.logger.error("timeline fail: #{user_id}: #{resp}")
14 nil
15 end 7 end
16 end 8 end
17 9
18 def self.user_by_id(user_id) 10 def self.user_by_id(user_id)
19 resp = fetch("https://twitter.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") 11 resp = fetch("https://twitter.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")
20 12
21 begin 13 handle_response resp, :user, "user_by_id(#{user_id})", ->(json) do
22 json = JSON.parse(resp) 14 normalize_user json['data']['user']['result']
23 {
24 user: normalize_user(json['data']['user']['result']),
25 raw: resp,
26 }
27 rescue
28 return if (json || {}).dig('data').is_a? Hash
29 Rails.logger.error("user_by_id fail: #{user_id}: #{resp}")
30 nil
31 end 15 end
32 end 16 end
33 17
34 def self.user_by_username(username) 18 def self.user_by_username(username)
35 resp = fetch("https://twitter.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") 19 resp = fetch("https://twitter.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")
36 20
37 begin 21 handle_response resp, :user, "user_by_username(#{username})", ->(json) do
38 json = JSON.parse(resp) 22 normalize_user json['data']['user']['result']
39 {
40 user: normalize_user(json['data']['user']['result']),
41 raw: resp,
42 }
43 rescue
44 return if (json || {}).dig('data').is_a? Hash
45 Rails.logger.error("user_by_username fail: #{username}: #{resp}")
46 nil
47 end 23 end
48 end 24 end
49 25
50 def self.escape_param(param) 26 def self.escape_param(param)
51 CGI.escape JSON.dump(param) 27 CGI.escape JSON.dump(param)
52 end 28 end
53 29
54 def self.fetch(uri) 30 def self.fetch(uri)
55 Net::HTTP.get(URI(uri), $cfg[:headers].sample) 31 Net::HTTP.get(URI(uri), $cfg[:headers].sample)
32 end
33
34 def self.handle_response(resp, key, error_key, callback)
35 json = JSON.parse(resp)
36 {
37 key => callback.call(json),
38 raw: resp,
39 }
40 rescue => e
41 if json.is_a? Hash
42 if json['errors'].is_a? Array
43 return rate_limit_check(json)
44 elsif json['data'].is_a? Hash
45 return
46 end
47 end
48 Rails.logger.error("#{error_key} fail: #{resp}")
49
50 raise e
56 end 51 end
57 52
58 def self.normalize_entity_media(json) 53 def self.normalize_entity_media(json)
59 ret = {} 54 ret = {}
60 55
146 name: json['legacy']['name'], 141 name: json['legacy']['name'],
147 protected: json['legacy']['protected'] == true, 142 protected: json['legacy']['protected'] == true,
148 username: json['legacy']['screen_name'], 143 username: json['legacy']['screen_name'],
149 } 144 }
150 end 145 end
146
147 def self.rate_limit_check(json)
148 return unless json['errors'].any? { |err| err['code'] == 88 }
149
150 raise 'Rate limited!'
151 end
151 end 152 end