diff 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
line wrap: on
line diff
--- a/app/lib/legit_client.rb	Sat Jul 15 21:03:39 2023 +0900
+++ b/app/lib/legit_client.rb	Sun Jul 16 08:53:59 2023 +0900
@@ -2,48 +2,24 @@
   def self.timeline(user_id)
     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")
 
-    begin
-      json = JSON.parse(resp)
-      {
-        timeline: normalize_timeline(json['data']['user']['result']['timeline_v2']['timeline']['instructions'], user_id),
-        raw: resp,
-      }
-    rescue => e
-      return if (json || {}).dig('data').is_a? Hash
-      Rails.logger.error("timeline fail: #{user_id}: #{resp}")
-      nil
+    handle_response resp, :timeline, "timeline(#{user_id})", ->(json) do
+      normalize_timeline json['data']['user']['result']['timeline_v2']['timeline']['instructions'], user_id
     end
   end
 
   def self.user_by_id(user_id)
     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")
 
-    begin
-      json = JSON.parse(resp)
-      {
-        user: normalize_user(json['data']['user']['result']),
-        raw: resp,
-      }
-    rescue
-      return if (json || {}).dig('data').is_a? Hash
-      Rails.logger.error("user_by_id fail: #{user_id}: #{resp}")
-      nil
+    handle_response resp, :user, "user_by_id(#{user_id})", ->(json) do
+      normalize_user json['data']['user']['result']
     end
   end
 
   def self.user_by_username(username)
     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")
 
-    begin
-      json = JSON.parse(resp)
-      {
-        user: normalize_user(json['data']['user']['result']),
-        raw: resp,
-      }
-    rescue
-      return if (json || {}).dig('data').is_a? Hash
-      Rails.logger.error("user_by_username fail: #{username}: #{resp}")
-      nil
+    handle_response resp, :user, "user_by_username(#{username})", ->(json) do
+      normalize_user json['data']['user']['result']
     end
   end
 
@@ -55,6 +31,25 @@
     Net::HTTP.get(URI(uri), $cfg[:headers].sample)
   end
 
+  def self.handle_response(resp, key, error_key, callback)
+    json = JSON.parse(resp)
+    {
+      key => callback.call(json),
+      raw: resp,
+    }
+  rescue => e
+    if json.is_a? Hash
+      if json['errors'].is_a? Array
+        return rate_limit_check(json)
+      elsif json['data'].is_a? Hash
+        return
+      end
+    end
+    Rails.logger.error("#{error_key} fail: #{resp}")
+
+    raise e
+  end
+
   def self.normalize_entity_media(json)
     ret = {}
 
@@ -148,4 +143,10 @@
       username: json['legacy']['screen_name'],
     }
   end
+
+  def self.rate_limit_check(json)
+    return unless json['errors'].any? { |err| err['code'] == 88 }
+
+    raise 'Rate limited!'
+  end
 end