view 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
line wrap: on
line source

require "net/http"

module LegitClient
  def self.timeline(user_id)
    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")

    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://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")

    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://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")

    handle_response resp, :user, "user_by_username(#{username})", ->(json) do
      normalize_user json["data"]["user"]["result"]
    end
  end

  def self.escape_param(param)
    CGI.escape JSON.dump(param)
  end

  def self.fetch(uri)
    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 = {}

    json.each do |entity_media|
      val = {}

      case entity_media["type"]
      when "animated_gif", "video"
          val[:variants] = entity_media["video_info"]["variants"]
            .filter { |variant| variant["bitrate"].present? }
            .map do |variant|
              {
                bitrate: variant["bitrate"],
                url: variant["url"]
              }
            end
      when "photo"
          val[:image_url] = entity_media["media_url_https"].sub(/\.([^.]+)$/, '?format=\1')
      end

      if !val.empty?
        val[:url] = entity_media["expanded_url"]
        val[:type] = entity_media["type"]
        val[:id] = entity_media["media_key"]
      end

      key = if ret[entity_media["url"]].nil?
        entity_media["url"]
      else
        entity_media["media_key"]
      end

      ret[key] = val
    end

    ret
  end

  def self.normalize_entity_urls(json)
    ret = {}

    (json || {}).each do |entity_url|
      ret[entity_url["url"]] = entity_url["expanded_url"]
    end

    ret
  end

  def self.normalize_timeline(json, user_id)
    json
      .reduce([]) do |acc, instruction|
        case instruction["type"]
        when "TimelineAddEntries" then acc += instruction["entries"]
        when "TimelinePinEntry" then acc << instruction["entry"]
        end

        acc
      end.filter { |entry| entry["entryId"] =~ /\A(profile-conversation|tweet)-/ }
      .reduce([]) do |acc, entry|
        if entry["content"]["entryType"] == "TimelineTimelineItem"
          acc.push(entry["content"])
        else
          entry["content"]["items"].each do |item|
            acc.push(item["item"])
          end
        end
        acc
      end.map { |raw_tweet| normalize_tweet(raw_tweet["itemContent"]["tweet_results"]["result"]) }
      .filter { |tweet| !tweet.nil? && tweet.dig(:user, :id) == user_id }
  end

  def self.normalize_tweet(json)
    return nil if json.nil?

    return normalize_tweet(json["tweet"]) if json["__typename"] == "TweetWithVisibilityResults"

    {
      id: json["rest_id"],
      created_at: Time.parse(json["legacy"]["created_at"]),
      user: normalize_user(json["core"]["user_results"]["result"]),
      message: json.dig("note_tweet", "note_tweet_results", "result", "text") || json["legacy"]["full_text"],
      retweet: normalize_tweet(json.dig("legacy", "retweeted_status_result", "result")),
      quote: normalize_tweet(json.dig("quoted_status_result", "result")),
      quote_id: json["legacy"]["quoted_status_id_str"],
      reply_to_id: json["legacy"]["in_reply_to_status_id_str"],
      reply_to_user_id: json["legacy"]["in_reply_to_user_id_str"],
      reply_to_username: json["legacy"]["in_reply_to_screen_name"],
      entity_urls: { **normalize_entity_urls(json["legacy"]["entities"]["urls"]), **normalize_entity_urls(json.dig("note_tweet", "note_tweet_results", "result", "entity_set", "urls")) },
      entity_media: normalize_entity_media(json.dig("legacy", "extended_entities", "media") || [])
    }
  end

  def self.normalize_user(json)
    {
      avatar_url: json["legacy"]["profile_image_url_https"],
      id: json["rest_id"],
      name: json["legacy"]["name"],
      protected: json["legacy"]["protected"] == true,
      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