view app/lib/legit_client.rb @ 238:a04b4830eef2 legit-client

Filter out non-own tweets included for replies
author nanaya <me@nanaya.net>
date Sat, 15 Jul 2023 01:53:05 +0900
parents 961d362e42c7
children c454ea4f7b34
line wrap: on
line source

module LegitClient
  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
    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
    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
    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.normalize_entity_media(json)
    ret = {}

    json.each do |entity_media|
      val = {}

      if entity_media['type'] == 'photo'
        val[:image_url] = entity_media['media_url_https']
      elsif entity_media['type'] == 'video'
        val[:variants] = entity_media['video_info']['variants']
          .filter { |variant| variant['bitrate'].present? }
          .map do |variant|
            {
              bitrate: variant['bitrate'],
              url: variant['url'],
            }
          end
      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['display_url']].nil?
        entity_media['display_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.find { |instruction| instruction['type'] == 'TimelineAddEntries' }['entries']
      .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 { |rawTweet| normalize_tweet(rawTweet['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['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']),
      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
end