Class: XTwitterScraper::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/x_twitter_scraper/client.rb,
sig/x_twitter_scraper/client.rbs

Constant Summary collapse

DEFAULT_MAX_RETRIES =

Default max number of retries to attempt after a failed retryable request.

Returns:

  • (2)
2
DEFAULT_TIMEOUT_IN_SECONDS =

Default per-request timeout.

Returns:

  • (Float)
60.0
DEFAULT_INITIAL_RETRY_DELAY =

Default initial retry delay in seconds. Overall delay is calculated using exponential backoff + jitter.

Returns:

  • (Float)
0.5
DEFAULT_MAX_RETRY_DELAY =

Default max retry delay in seconds.

Returns:

  • (Float)
8.0

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS, Internal::Transport::BaseClient::XTwitterScraper

Instance Attribute Summary collapse

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #requester, #timeout

Instance Method Summary collapse

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #request, #send_request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(api_key: ENV["X_TWITTER_SCRAPER_API_KEY"], bearer_token: ENV["X_TWITTER_SCRAPER_BEARER_TOKEN"], base_url: ENV["X_TWITTER_SCRAPER_BASE_URL"], max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY) ⇒ Client

Creates and returns a new client for interacting with the API.

"https://api.example.com/v2/". Defaults to ENV["X_TWITTER_SCRAPER_BASE_URL"]

Parameters:

  • api_key (String, nil) (defaults to: ENV["X_TWITTER_SCRAPER_API_KEY"])

    Defaults to ENV["X_TWITTER_SCRAPER_API_KEY"]

  • bearer_token (String, nil) (defaults to: ENV["X_TWITTER_SCRAPER_BEARER_TOKEN"])

    OAuth 2.1 access token Defaults to ENV["X_TWITTER_SCRAPER_BEARER_TOKEN"]

  • base_url (String, nil) (defaults to: ENV["X_TWITTER_SCRAPER_BASE_URL"])

    Override the default base URL for the API, e.g.,

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)

    Max number of retries to attempt after a failed retryable request.

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)
  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/x_twitter_scraper/client.rb', line 132

def initialize(
  api_key: ENV["X_TWITTER_SCRAPER_API_KEY"],
  bearer_token: ENV["X_TWITTER_SCRAPER_BEARER_TOKEN"],
  base_url: ENV["X_TWITTER_SCRAPER_BASE_URL"],
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
)
  base_url ||= "https://xquik.com/api/v1"

  headers = {}
  custom_headers_env = ENV["X_TWITTER_SCRAPER_CUSTOM_HEADERS"]
  unless custom_headers_env.nil?
    parsed = {}
    custom_headers_env.split("\n").each do |line|
      colon = line.index(":")
      unless colon.nil?
        parsed[line[0...colon].strip] = line[(colon + 1)..].strip
      end
    end
    headers = parsed.merge(headers)
  end

  @api_key = api_key&.to_s
  @bearer_token = bearer_token&.to_s

  super(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay,
    headers: headers
  )

  @account = XTwitterScraper::Resources::Account.new(client: self)
  @subscribe = XTwitterScraper::Resources::Subscribe.new(client: self)
  @compose = XTwitterScraper::Resources::Compose.new(client: self)
  @drafts = XTwitterScraper::Resources::Drafts.new(client: self)
  @styles = XTwitterScraper::Resources::Styles.new(client: self)
  @radar = XTwitterScraper::Resources::Radar.new(client: self)
  @monitors = XTwitterScraper::Resources::Monitors.new(client: self)
  @events = XTwitterScraper::Resources::Events.new(client: self)
  @extractions = XTwitterScraper::Resources::Extractions.new(client: self)
  @draws = XTwitterScraper::Resources::Draws.new(client: self)
  @webhooks = XTwitterScraper::Resources::Webhooks.new(client: self)
  @x = XTwitterScraper::Resources::X.new(client: self)
  @trends = XTwitterScraper::Resources::Trends.new(client: self)
  @support = XTwitterScraper::Resources::Support.new(client: self)
  @credits = XTwitterScraper::Resources::Credits.new(client: self)
  @guest_wallets = XTwitterScraper::Resources::GuestWallets.new(client: self)
end

Instance Attribute Details

#accountXTwitterScraper::Resources::Account (readonly)

Account info and settings



31
32
33
# File 'lib/x_twitter_scraper/client.rb', line 31

def 
  @account
end

#api_keyString? (readonly)

Returns:

  • (String, nil)


23
24
25
# File 'lib/x_twitter_scraper/client.rb', line 23

def api_key
  @api_key
end

#bearer_tokenString? (readonly)

OAuth 2.1 access token

Returns:

  • (String, nil)


27
28
29
# File 'lib/x_twitter_scraper/client.rb', line 27

def bearer_token
  @bearer_token
end

#composeXTwitterScraper::Resources::Compose (readonly)

AI tweet composition, drafts, writing styles, and radar



39
40
41
# File 'lib/x_twitter_scraper/client.rb', line 39

def compose
  @compose
end

#creditsXTwitterScraper::Resources::Credits (readonly)

Subscription, billing, and credits



85
86
87
# File 'lib/x_twitter_scraper/client.rb', line 85

def credits
  @credits
end

#draftsXTwitterScraper::Resources::Drafts (readonly)

AI tweet composition, drafts, writing styles, and radar



43
44
45
# File 'lib/x_twitter_scraper/client.rb', line 43

def drafts
  @drafts
end

#drawsXTwitterScraper::Resources::Draws (readonly)

Giveaway draws from tweet replies



67
68
69
# File 'lib/x_twitter_scraper/client.rb', line 67

def draws
  @draws
end

#eventsXTwitterScraper::Resources::Events (readonly)

Activity events from monitored accounts



59
60
61
# File 'lib/x_twitter_scraper/client.rb', line 59

def events
  @events
end

#extractionsXTwitterScraper::Resources::Extractions (readonly)

Bulk data extraction (23 tool types)



63
64
65
# File 'lib/x_twitter_scraper/client.rb', line 63

def extractions
  @extractions
end

#guest_walletsXTwitterScraper::Resources::GuestWallets (readonly)

Accountless prepaid access for paid read endpoints



89
90
91
# File 'lib/x_twitter_scraper/client.rb', line 89

def guest_wallets
  @guest_wallets
end

#monitorsXTwitterScraper::Resources::Monitors (readonly)

X account monitoring with 1-second checks



55
56
57
# File 'lib/x_twitter_scraper/client.rb', line 55

def monitors
  @monitors
end

#radarXTwitterScraper::Resources::Radar (readonly)

AI tweet composition, drafts, writing styles, and radar



51
52
53
# File 'lib/x_twitter_scraper/client.rb', line 51

def radar
  @radar
end

#stylesXTwitterScraper::Resources::Styles (readonly)

AI tweet composition, drafts, writing styles, and radar



47
48
49
# File 'lib/x_twitter_scraper/client.rb', line 47

def styles
  @styles
end

#subscribeXTwitterScraper::Resources::Subscribe (readonly)

Subscription, billing, and credits



35
36
37
# File 'lib/x_twitter_scraper/client.rb', line 35

def subscribe
  @subscribe
end

#supportXTwitterScraper::Resources::Support (readonly)



81
82
83
# File 'lib/x_twitter_scraper/client.rb', line 81

def support
  @support
end

Trending topics and hashtags by region



78
79
80
# File 'lib/x_twitter_scraper/client.rb', line 78

def trends
  @trends
end

#webhooksXTwitterScraper::Resources::Webhooks (readonly)

Webhook endpoint management and delivery



71
72
73
# File 'lib/x_twitter_scraper/client.rb', line 71

def webhooks
  @webhooks
end

#xXTwitterScraper::Resources::X (readonly)



74
75
76
# File 'lib/x_twitter_scraper/client.rb', line 74

def x
  @x
end