yf_as_dataframe 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rspec_status +41 -0
- data/CHANGELOG.rst +6 -0
- data/MINIMAL_INTEGRATION.md +28 -20
- data/README.md +28 -19
- data/lib/yf_as_dataframe/curl_impersonate_integration.rb +122 -49
- data/lib/yf_as_dataframe/version.rb +1 -1
- data/lib/yf_as_dataframe/yf_connection_minimal_patch.rb +5 -10
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 703c1d1e490e752f4a1ff1fd153c025cc455253d0e870008587050b22b1bbbc7
|
|
4
|
+
data.tar.gz: 0e15eaeac7ec1cb09662e7a7007e6d4d9b4957a3f2786e5a8fdcb5501925ab48
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 98d8941ef4d9fa1e58ae97a7398b001c1ae129e30b0f9dbdfc91d2c97d6aefb3268f8c559d8de10cab8571bd4b3dfce1b1708afd6e084bc57ef8879705a26800
|
|
7
|
+
data.tar.gz: 92c25eedb4161b04edaaacdd026480292bb467a06bf76769241104abb6123d7ca7c31dfa4d6b750e6f19d81a16cd02b9df6cb3c29c3e42b2fe4e50ae15795ac8
|
data/.rspec_status
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
example_id | status | run_time |
|
|
2
|
+
----------------------------------------------------- | ------- | --------------- |
|
|
3
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:1:1] | passed | 0.00235 seconds |
|
|
4
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:1:2] | passed | 0.0002 seconds |
|
|
5
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:1:3] | passed | 0.00012 seconds |
|
|
6
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:2:1] | passed | 0.00018 seconds |
|
|
7
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:2:2] | passed | 0.00013 seconds |
|
|
8
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:3:1] | passed | 0.00012 seconds |
|
|
9
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:3:2] | passed | 0.00038 seconds |
|
|
10
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:4:1] | passed | 0.00044 seconds |
|
|
11
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:4:2] | passed | 0.00028 seconds |
|
|
12
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:5:1] | passed | 0.0007 seconds |
|
|
13
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:5:2] | passed | 0.0001 seconds |
|
|
14
|
+
./spec/yf_as_dataframe/price_technical_spec.rb[1:6:1] | passed | 0.00015 seconds |
|
|
15
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:1:1] | passed | 0.00514 seconds |
|
|
16
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:1:2] | passed | 0.0001 seconds |
|
|
17
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:1:3] | passed | 0.00005 seconds |
|
|
18
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:2:1] | pending | 0.00001 seconds |
|
|
19
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:2:2] | pending | 0 seconds |
|
|
20
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:2:3] | pending | 0 seconds |
|
|
21
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:2:4] | pending | 0 seconds |
|
|
22
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:2:5] | pending | 0 seconds |
|
|
23
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:3:1:1] | pending | 0 seconds |
|
|
24
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:3:1:2] | pending | 0 seconds |
|
|
25
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:4:1] | passed | 0.00009 seconds |
|
|
26
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:4:2] | passed | 0.00006 seconds |
|
|
27
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:4:3] | passed | 0.00005 seconds |
|
|
28
|
+
./spec/yf_as_dataframe/ticker_spec.rb[1:4:4] | passed | 0.00005 seconds |
|
|
29
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:1:1] | passed | 0.00053 seconds |
|
|
30
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:1:2] | passed | 0.00006 seconds |
|
|
31
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:1:3] | passed | 0.00008 seconds |
|
|
32
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:1:4] | passed | 0.00005 seconds |
|
|
33
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:1:5] | passed | 0.00004 seconds |
|
|
34
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:1:6] | passed | 0.00004 seconds |
|
|
35
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:2:1] | passed | 0.02723 seconds |
|
|
36
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:2:2] | passed | 0.00173 seconds |
|
|
37
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:2:3] | passed | 0.00137 seconds |
|
|
38
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:3:1] | passed | 0.00158 seconds |
|
|
39
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:4:1] | passed | 0.00071 seconds |
|
|
40
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:5:1] | passed | 0.00006 seconds |
|
|
41
|
+
./spec/yf_as_dataframe/yf_connection_spec.rb[1:6:1] | passed | 0.00084 seconds |
|
data/CHANGELOG.rst
CHANGED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
0.5.1
|
|
2
|
+
=====
|
|
3
|
+
|
|
4
|
+
* Document the Homebrew dependency ``curl-impersonate``. ``brew install curl-impersonate`` installs the Homebrew core formula, which builds lexiforest/curl-impersonate v2.3.2.
|
|
5
|
+
* Look for that formula's wrappers in the Homebrew prefix (``brew --prefix``/bin, ``/opt/homebrew/bin``, ``/usr/local/bin``, and ``/home/linuxbrew/.linuxbrew/bin``).
|
|
6
|
+
* Use the newest desktop Chrome wrapper from that install. ``CURL_IMPERSONATE_DIR`` still selects a different directory.
|
data/MINIMAL_INTEGRATION.md
CHANGED
|
@@ -6,15 +6,23 @@ This is a minimal integration that makes curl-impersonate the **default behavior
|
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
|
-
### 1. Install curl-impersonate
|
|
9
|
+
### 1. Install curl-impersonate from Homebrew
|
|
10
|
+
|
|
11
|
+
Homebrew core packages [lexiforest/curl-impersonate](https://github.com/lexiforest/curl-impersonate) v2.3.2. The formula is [Homebrew/homebrew-core Formula/c/curl-impersonate.rb](https://github.com/Homebrew/homebrew-core/blob/master/Formula/c/curl-impersonate.rb).
|
|
10
12
|
|
|
11
13
|
```bash
|
|
12
|
-
# macOS
|
|
13
|
-
brew tap shakacode/brew
|
|
14
14
|
brew install curl-impersonate
|
|
15
15
|
|
|
16
|
-
#
|
|
17
|
-
|
|
16
|
+
# Apple Silicon wrappers are in /opt/homebrew/bin.
|
|
17
|
+
# Intel Homebrew uses /usr/local/bin.
|
|
18
|
+
# Linux Homebrew uses /home/linuxbrew/.linuxbrew/bin.
|
|
19
|
+
ls "$(brew --prefix)/bin"/curl_chrome*
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Bottles are published for Apple Silicon and Linux. An Intel Mac builds the formula from source. Lexiforest's own tap installs its v2.3.1 release archives instead of building current core:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
brew install lexiforest/tap/curl-impersonate
|
|
18
26
|
```
|
|
19
27
|
|
|
20
28
|
### 2. Custom Installation Directory (Optional)
|
|
@@ -29,7 +37,7 @@ export CURL_IMPERSONATE_DIR="/opt/curl-impersonate/bin"
|
|
|
29
37
|
CURL_IMPERSONATE_DIR="/opt/curl-impersonate/bin" ruby your_script.rb
|
|
30
38
|
```
|
|
31
39
|
|
|
32
|
-
|
|
40
|
+
When `CURL_IMPERSONATE_DIR` is unset, the gem searches `brew --prefix`/bin, `/opt/homebrew/bin`, `/usr/local/bin`, and `/home/linuxbrew/.linuxbrew/bin`. It uses the newest desktop Chrome wrapper in those directories.
|
|
33
41
|
|
|
34
42
|
### 3. Add Integration Files
|
|
35
43
|
|
|
@@ -100,10 +108,10 @@ puts "Using directory: #{YfAsDataframe::CurlImpersonateIntegration.executable_di
|
|
|
100
108
|
- No new method names to learn
|
|
101
109
|
- No changes to method signatures
|
|
102
110
|
|
|
103
|
-
### ✅ **
|
|
104
|
-
-
|
|
105
|
-
- Supports Chrome, Firefox, Edge, and Safari
|
|
106
|
-
-
|
|
111
|
+
### ✅ **Current browser wrapper**
|
|
112
|
+
- Uses the newest desktop Chrome wrapper installed by Homebrew
|
|
113
|
+
- Supports Chrome, Firefox, Edge, and Safari wrapper names from lexiforest
|
|
114
|
+
- Follows newer browser wrappers as the Homebrew formula updates
|
|
107
115
|
|
|
108
116
|
### ✅ **Robust Fallback**
|
|
109
117
|
- Falls back to HTTParty if curl-impersonate fails
|
|
@@ -117,7 +125,7 @@ puts "Using directory: #{YfAsDataframe::CurlImpersonateIntegration.executable_di
|
|
|
117
125
|
|
|
118
126
|
### ✅ **Environment Variable Support**
|
|
119
127
|
- Set `CURL_IMPERSONATE_DIR` to customize installation directory
|
|
120
|
-
-
|
|
128
|
+
- Otherwise searches the Homebrew prefix (`brew --prefix`/bin, `/opt/homebrew/bin`, `/usr/local/bin`, `/home/linuxbrew/.linuxbrew/bin`)
|
|
121
129
|
- Supports both persistent and per-command configuration
|
|
122
130
|
|
|
123
131
|
## Example
|
|
@@ -156,21 +164,21 @@ end
|
|
|
156
164
|
|
|
157
165
|
### "No curl-impersonate executables found"
|
|
158
166
|
```bash
|
|
159
|
-
#
|
|
160
|
-
ls -la /
|
|
167
|
+
# Wrappers installed by Homebrew core
|
|
168
|
+
ls -la "$(brew --prefix)/bin"/curl_*
|
|
161
169
|
|
|
162
|
-
#
|
|
170
|
+
# Custom directory
|
|
163
171
|
ls -la $CURL_IMPERSONATE_DIR/curl_*
|
|
164
172
|
|
|
165
|
-
#
|
|
166
|
-
brew
|
|
173
|
+
# Install the Homebrew core formula
|
|
174
|
+
brew install curl-impersonate
|
|
167
175
|
```
|
|
168
176
|
|
|
169
177
|
### Permission errors
|
|
170
178
|
```bash
|
|
171
|
-
|
|
179
|
+
chmod +x "$(brew --prefix)/bin"/curl_*
|
|
172
180
|
# or
|
|
173
|
-
|
|
181
|
+
chmod +x $CURL_IMPERSONATE_DIR/curl_*
|
|
174
182
|
```
|
|
175
183
|
|
|
176
184
|
### Still getting blocked
|
|
@@ -193,7 +201,7 @@ puts "Directory: #{YfAsDataframe::CurlImpersonateIntegration.executable_director
|
|
|
193
201
|
| `curl_impersonate_enabled` | `true` | Use curl-impersonate for requests |
|
|
194
202
|
| `curl_impersonate_fallback` | `true` | Fall back to HTTParty if curl-impersonate fails |
|
|
195
203
|
| `curl_impersonate_timeout` | `30` | Timeout in seconds for curl-impersonate requests |
|
|
196
|
-
| `CURL_IMPERSONATE_DIR` | `/usr/local/bin` | Directory containing curl-impersonate
|
|
204
|
+
| `CURL_IMPERSONATE_DIR` | Homebrew prefix, then `/opt/homebrew/bin`, `/usr/local/bin`, `/home/linuxbrew/.linuxbrew/bin` | Directory containing curl-impersonate wrappers |
|
|
197
205
|
|
|
198
206
|
## Benefits
|
|
199
207
|
|
|
@@ -224,4 +232,4 @@ puts "Directory: #{YfAsDataframe::CurlImpersonateIntegration.executable_director
|
|
|
224
232
|
4. **Add the require statements** to your code
|
|
225
233
|
5. **Test with your existing code** - it should work immediately
|
|
226
234
|
|
|
227
|
-
That's it! Your existing Yahoo Finance scraping code will now automatically use curl-impersonate to bypass TLS fingerprinting.
|
|
235
|
+
That's it! Your existing Yahoo Finance scraping code will now automatically use curl-impersonate to bypass TLS fingerprinting.
|
data/README.md
CHANGED
|
@@ -34,6 +34,23 @@ This package provides for pulling data from Yahoo!'s unofficial API, and providi
|
|
|
34
34
|
|
|
35
35
|
- **Ruby**: >= 3.3
|
|
36
36
|
- **polars-df**: 0.27.1
|
|
37
|
+
- **curl-impersonate**, from Homebrew:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
brew install curl-impersonate
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
That is the [Homebrew core formula](https://github.com/Homebrew/homebrew-core/blob/master/Formula/c/curl-impersonate.rb). It builds [lexiforest/curl-impersonate](https://github.com/lexiforest/curl-impersonate) v2.3.2 (curl 8.23.0, with current Chrome, Firefox, Safari, and Edge wrappers). Bottles are published for Apple Silicon and Linux. An Intel Mac builds the formula from source.
|
|
44
|
+
|
|
45
|
+
Homebrew puts `curl-impersonate` and the `curl_chrome*`, `curl_firefox*`, `curl_safari*`, and `curl_edge*` wrappers on the prefix `bin` directory:
|
|
46
|
+
|
|
47
|
+
| Install | Wrapper directory |
|
|
48
|
+
| --- | --- |
|
|
49
|
+
| Apple Silicon | `/opt/homebrew/bin` |
|
|
50
|
+
| Intel Homebrew | `/usr/local/bin` |
|
|
51
|
+
| Linux Homebrew | `/home/linuxbrew/.linuxbrew/bin` |
|
|
52
|
+
|
|
53
|
+
The gem asks `brew --prefix` for that directory and also checks the three paths above. `CURL_IMPERSONATE_DIR` replaces that search when the binaries live somewhere else. Requests use the newest desktop Chrome wrapper in the directory (for example `curl_chrome152`). When no wrapper is installed, requests fall back to HTTParty.
|
|
37
54
|
|
|
38
55
|
### Quick Start: The Ticker module
|
|
39
56
|
|
|
@@ -204,33 +221,25 @@ YfAsDataframe.zlema(df, column: 'Adj Close', window: 5)
|
|
|
204
221
|
|
|
205
222
|
## TLS Fingerprinting Protection
|
|
206
223
|
|
|
207
|
-
|
|
224
|
+
Yahoo requests use the Homebrew `curl-impersonate` dependency described above. The integration is loaded with the gem. The selected wrapper supplies the browser TLS fingerprint and the `User-Agent`, `Accept`, `Accept-Language`, and `Accept-Encoding` headers. The gem adds the Yahoo cookie and any other request headers.
|
|
208
225
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
To use the TLS fingerprinting protection, you need to install curl-impersonate:
|
|
226
|
+
Confirm the install:
|
|
212
227
|
|
|
213
228
|
```bash
|
|
214
|
-
|
|
215
|
-
brew
|
|
216
|
-
brew install curl-impersonate
|
|
217
|
-
|
|
218
|
-
# Verify installation
|
|
219
|
-
ls -la /usr/local/bin/curl_*
|
|
229
|
+
brew --prefix curl-impersonate
|
|
230
|
+
ls "$(brew --prefix)/bin"/curl_chrome*
|
|
220
231
|
```
|
|
221
232
|
|
|
222
|
-
### Custom
|
|
223
|
-
|
|
224
|
-
The codebase will look for the location of the curl-impersonate binaries per the `CURL_IMPERSONATE_DIR` environment variable;
|
|
225
|
-
if it is not assigned, the default location of the binaries is `/usr/local/bin`.
|
|
226
|
-
The code will randomly select one of the binaries (expected to be named "curl_chrome*", "curl_ff*", "curl_edge*", etc.) for its communications with the servers.
|
|
233
|
+
### Custom installation directory
|
|
227
234
|
|
|
228
235
|
```bash
|
|
229
|
-
# Set custom directory
|
|
230
236
|
export CURL_IMPERSONATE_DIR="/opt/curl-impersonate/bin"
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Lexiforest also publishes release archives through its own tap, one version behind Homebrew core:
|
|
231
240
|
|
|
232
|
-
|
|
233
|
-
|
|
241
|
+
```bash
|
|
242
|
+
brew install lexiforest/tap/curl-impersonate
|
|
234
243
|
```
|
|
235
244
|
|
|
236
245
|
### Configuration (Optional)
|
|
@@ -417,4 +426,4 @@ intended for research and educational purposes. You should refer to Yahoo!'s ter
|
|
|
417
426
|
[here](https://policies.yahoo.com/us/en/yahoo/terms/index.htm)) for
|
|
418
427
|
details on your rights to use the actual data downloaded.
|
|
419
428
|
|
|
420
|
-
---
|
|
429
|
+
---
|
|
@@ -5,6 +5,26 @@ require 'timeout'
|
|
|
5
5
|
|
|
6
6
|
class YfAsDataframe
|
|
7
7
|
module CurlImpersonateIntegration
|
|
8
|
+
# Homebrew core formula: https://github.com/Homebrew/homebrew-core/blob/master/Formula/c/curl-impersonate.rb
|
|
9
|
+
# That formula builds lexiforest/curl-impersonate (v2.3.2).
|
|
10
|
+
HOMEBREW_FORMULA = "curl-impersonate"
|
|
11
|
+
HOMEBREW_UPSTREAM = "https://github.com/lexiforest/curl-impersonate"
|
|
12
|
+
|
|
13
|
+
# Prefixes used when `brew --prefix` is unavailable.
|
|
14
|
+
DEFAULT_BIN_DIRECTORIES = [
|
|
15
|
+
"/opt/homebrew/bin",
|
|
16
|
+
"/usr/local/bin",
|
|
17
|
+
"/home/linuxbrew/.linuxbrew/bin"
|
|
18
|
+
].freeze
|
|
19
|
+
|
|
20
|
+
# Headers owned by the curl_* wrapper. Passing another User-Agent would
|
|
21
|
+
# disagree with the TLS fingerprint of the selected browser.
|
|
22
|
+
FINGERPRINT_HEADERS = %w[user-agent accept accept-language accept-encoding].freeze
|
|
23
|
+
|
|
24
|
+
WRAPPER_PATTERN = /\Acurl_(chrome|firefox|ff|edge|safari)(\d+)[a-z]?(?:_([a-z0-9]+))?\z/
|
|
25
|
+
BROWSER_RANK = { chrome: 0, edge: 1, safari: 2, firefox: 3 }.freeze
|
|
26
|
+
VARIANT_RANK = { nil => 0, "macos" => 1, "ios" => 2, "android" => 3 }.freeze
|
|
27
|
+
|
|
8
28
|
# Configuration
|
|
9
29
|
@curl_impersonate_enabled = true
|
|
10
30
|
@curl_impersonate_fallback = true
|
|
@@ -15,48 +35,56 @@ class YfAsDataframe
|
|
|
15
35
|
@curl_impersonate_process_timeout = 60 # New: process timeout protection
|
|
16
36
|
|
|
17
37
|
class << self
|
|
18
|
-
attr_accessor :curl_impersonate_enabled, :curl_impersonate_fallback,
|
|
38
|
+
attr_accessor :curl_impersonate_enabled, :curl_impersonate_fallback,
|
|
19
39
|
:curl_impersonate_timeout, :curl_impersonate_connect_timeout,
|
|
20
40
|
:curl_impersonate_retries, :curl_impersonate_retry_delay,
|
|
21
41
|
:curl_impersonate_process_timeout
|
|
22
42
|
end
|
|
23
43
|
|
|
24
|
-
#
|
|
44
|
+
# Directory of the wrapper that will be used, or the first directory searched.
|
|
25
45
|
def self.executable_directory
|
|
26
|
-
|
|
46
|
+
selected = selected_executable
|
|
47
|
+
return File.dirname(selected[:path]) if selected
|
|
48
|
+
|
|
49
|
+
executable_directories.first || "/usr/local/bin"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# CURL_IMPERSONATE_DIR when set, otherwise the Homebrew prefix and the usual prefixes.
|
|
53
|
+
def self.executable_directories
|
|
54
|
+
configured = ENV["CURL_IMPERSONATE_DIR"].to_s
|
|
55
|
+
return [configured] unless configured.empty?
|
|
56
|
+
|
|
57
|
+
directories = []
|
|
58
|
+
prefix = _homebrew_prefix
|
|
59
|
+
directories << File.join(prefix, "bin") if prefix && !prefix.empty?
|
|
60
|
+
directories.concat(DEFAULT_BIN_DIRECTORIES)
|
|
61
|
+
directories.uniq
|
|
27
62
|
end
|
|
28
63
|
|
|
29
|
-
#
|
|
64
|
+
# Browser wrappers found in executable_directories.
|
|
30
65
|
def self.available_executables
|
|
31
|
-
@available_executables ||=
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
executables << { path: path, executable: executable, browser: browser_type }
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
executables
|
|
66
|
+
@available_executables ||= executable_directories.flat_map { |dir| _wrappers_in(dir) }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Newest desktop Chrome wrapper. Edge, Safari, then Firefox are used when no Chrome wrapper is installed.
|
|
70
|
+
def self.selected_executable
|
|
71
|
+
available_executables.min_by do |executable|
|
|
72
|
+
[
|
|
73
|
+
BROWSER_RANK.fetch(executable[:browser], 9),
|
|
74
|
+
-executable[:version].to_i,
|
|
75
|
+
VARIANT_RANK.fetch(executable[:variant], 4),
|
|
76
|
+
executable[:executable]
|
|
77
|
+
]
|
|
47
78
|
end
|
|
48
79
|
end
|
|
49
80
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
available = available_executables
|
|
53
|
-
return nil if available.empty?
|
|
54
|
-
available.sample
|
|
81
|
+
def self.reset_executable_cache!
|
|
82
|
+
@available_executables = nil
|
|
55
83
|
end
|
|
56
84
|
|
|
57
85
|
# Make a curl-impersonate request with improved timeout handling
|
|
58
86
|
def self.make_request(url, headers: {}, params: {}, timeout: nil, retries: nil)
|
|
59
|
-
executable_info =
|
|
87
|
+
executable_info = selected_executable
|
|
60
88
|
return nil unless executable_info
|
|
61
89
|
|
|
62
90
|
timeout ||= @curl_impersonate_timeout
|
|
@@ -73,20 +101,17 @@ class YfAsDataframe
|
|
|
73
101
|
"--silent",
|
|
74
102
|
"--show-error"
|
|
75
103
|
]
|
|
76
|
-
headers.each { |key, value| cmd.concat(["-H", "#{key}: #{value}"]) }
|
|
104
|
+
_request_headers(headers).each { |key, value| cmd.concat(["-H", "#{key}: #{value}"]) }
|
|
77
105
|
unless params.empty?
|
|
78
|
-
query_string = params.map { |k, v| "#{k}=#{v}" }.join(
|
|
79
|
-
separator = url.include?(
|
|
106
|
+
query_string = params.map { |k, v| "#{k}=#{v}" }.join("&")
|
|
107
|
+
separator = url.include?("?") ? "&" : "?"
|
|
80
108
|
url = "#{url}#{separator}#{query_string}"
|
|
81
109
|
end
|
|
82
110
|
cmd << url
|
|
83
111
|
|
|
84
|
-
# puts "DEBUG: curl-impersonate command: #{cmd.join(' ')}"
|
|
85
|
-
# puts "DEBUG: curl-impersonate timeout: #{timeout} seconds"
|
|
86
|
-
|
|
87
112
|
begin
|
|
88
|
-
stdout_str =
|
|
89
|
-
stderr_str =
|
|
113
|
+
stdout_str = ""
|
|
114
|
+
stderr_str = ""
|
|
90
115
|
status = nil
|
|
91
116
|
Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
|
|
92
117
|
stdin.close
|
|
@@ -95,10 +120,9 @@ class YfAsDataframe
|
|
|
95
120
|
monitor = Thread.new do
|
|
96
121
|
sleep(timeout + 10)
|
|
97
122
|
unless done
|
|
98
|
-
|
|
99
|
-
Process.kill('TERM', pid) rescue nil
|
|
123
|
+
Process.kill("TERM", pid) rescue nil
|
|
100
124
|
sleep(1)
|
|
101
|
-
Process.kill(
|
|
125
|
+
Process.kill("KILL", pid) rescue nil if wait_thr.alive?
|
|
102
126
|
end
|
|
103
127
|
end
|
|
104
128
|
stdout_str = stdout.read
|
|
@@ -107,33 +131,82 @@ class YfAsDataframe
|
|
|
107
131
|
done = true
|
|
108
132
|
monitor.kill
|
|
109
133
|
end
|
|
110
|
-
# puts "DEBUG: curl-impersonate stdout: #{stdout_str[0..200]}..." if stdout_str && !stdout_str.empty?
|
|
111
|
-
# puts "DEBUG: curl-impersonate stderr: #{stderr_str}" if stderr_str && !stderr_str.empty?
|
|
112
|
-
# puts "DEBUG: curl-impersonate status: #{status.exitstatus}"
|
|
113
134
|
if status.success?
|
|
114
135
|
response = OpenStruct.new
|
|
115
|
-
|
|
136
|
+
response.body = stdout_str
|
|
116
137
|
response.code = 200
|
|
117
138
|
response.define_singleton_method(:success?) { true }
|
|
118
|
-
|
|
139
|
+
response.parsed_response = _parse_json_if_possible(stdout_str)
|
|
119
140
|
response
|
|
120
141
|
else
|
|
121
|
-
# puts "DEBUG: curl-impersonate failed with error: \\#{error_message}"
|
|
122
|
-
error_message = "curl failed with code \\#{status.exitstatus}: \\#{stderr_str}"
|
|
123
142
|
nil
|
|
124
143
|
end
|
|
125
|
-
rescue
|
|
126
|
-
# puts "DEBUG: curl-impersonate exception: \\#{e.message}"
|
|
144
|
+
rescue StandardError
|
|
127
145
|
nil
|
|
128
146
|
end
|
|
129
147
|
end
|
|
130
148
|
|
|
131
|
-
|
|
149
|
+
def self._wrappers_in(directory)
|
|
150
|
+
return [] unless File.directory?(directory)
|
|
151
|
+
|
|
152
|
+
Dir.glob(File.join(directory, "curl_*")).filter_map do |path|
|
|
153
|
+
next unless File.file?(path) && File.executable?(path)
|
|
154
|
+
|
|
155
|
+
_describe_wrapper(path)
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
private_class_method :_wrappers_in
|
|
159
|
+
|
|
160
|
+
def self._describe_wrapper(path)
|
|
161
|
+
match = File.basename(path).match(WRAPPER_PATTERN)
|
|
162
|
+
return nil unless match
|
|
163
|
+
|
|
164
|
+
family = match[1] == "ff" ? "firefox" : match[1]
|
|
165
|
+
{
|
|
166
|
+
path: path,
|
|
167
|
+
executable: File.basename(path),
|
|
168
|
+
browser: family.to_sym,
|
|
169
|
+
version: match[2].to_i,
|
|
170
|
+
variant: match[3]
|
|
171
|
+
}
|
|
172
|
+
end
|
|
173
|
+
private_class_method :_describe_wrapper
|
|
174
|
+
|
|
175
|
+
def self._request_headers(headers)
|
|
176
|
+
headers.each_with_object({}) do |(key, value), kept|
|
|
177
|
+
next if FINGERPRINT_HEADERS.include?(key.to_s.downcase)
|
|
178
|
+
|
|
179
|
+
kept[key] = value
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
private_class_method :_request_headers
|
|
183
|
+
|
|
184
|
+
def self._homebrew_prefix
|
|
185
|
+
return @homebrew_prefix if defined?(@homebrew_prefix)
|
|
186
|
+
|
|
187
|
+
brew = _which("brew")
|
|
188
|
+
@homebrew_prefix =
|
|
189
|
+
if brew
|
|
190
|
+
out, status = Open3.capture2(brew, "--prefix")
|
|
191
|
+
status.success? ? out.strip : nil
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
private_class_method :_homebrew_prefix
|
|
195
|
+
|
|
196
|
+
def self._which(command)
|
|
197
|
+
ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).each do |dir|
|
|
198
|
+
candidate = File.join(dir, command)
|
|
199
|
+
return candidate if File.executable?(candidate) && !File.directory?(candidate)
|
|
200
|
+
end
|
|
201
|
+
nil
|
|
202
|
+
end
|
|
203
|
+
private_class_method :_which
|
|
132
204
|
|
|
133
|
-
def self.
|
|
205
|
+
def self._parse_json_if_possible(response_body)
|
|
134
206
|
JSON.parse(response_body)
|
|
135
207
|
rescue JSON::ParserError
|
|
136
208
|
response_body
|
|
137
209
|
end
|
|
210
|
+
private_class_method :_parse_json_if_possible
|
|
138
211
|
end
|
|
139
|
-
end
|
|
212
|
+
end
|
|
@@ -55,7 +55,7 @@ class YfAsDataframe
|
|
|
55
55
|
retries: CurlImpersonateIntegration.curl_impersonate_retries
|
|
56
56
|
)
|
|
57
57
|
|
|
58
|
-
if response
|
|
58
|
+
if response&.success?
|
|
59
59
|
# puts "DEBUG: curl-impersonate succeeded"
|
|
60
60
|
return response
|
|
61
61
|
else
|
|
@@ -69,14 +69,9 @@ class YfAsDataframe
|
|
|
69
69
|
# puts "DEBUG: curl-impersonate is disabled, skipping to fallback"
|
|
70
70
|
end
|
|
71
71
|
|
|
72
|
-
# Fallback to original HTTParty method
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return HTTParty.get(url, headers: headers).body
|
|
76
|
-
else
|
|
77
|
-
# puts "DEBUG: Fallback is disabled, but forcing fallback anyway"
|
|
78
|
-
return HTTParty.get(url, headers: headers).body
|
|
79
|
-
end
|
|
72
|
+
# Fallback to the original HTTParty method. A missing Homebrew install
|
|
73
|
+
# takes this path so requests still run.
|
|
74
|
+
get_original(url, headers, params)
|
|
80
75
|
end
|
|
81
76
|
|
|
82
77
|
# get_raw_json uses get, so it automatically gets curl-impersonate behavior
|
|
@@ -125,4 +120,4 @@ class YfAsDataframe
|
|
|
125
120
|
end
|
|
126
121
|
end
|
|
127
122
|
end
|
|
128
|
-
end
|
|
123
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: yf_as_dataframe
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.5.
|
|
4
|
+
version: 0.5.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bill McKinnon
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: tzinfo
|
|
@@ -200,6 +200,7 @@ extensions: []
|
|
|
200
200
|
extra_rdoc_files: []
|
|
201
201
|
files:
|
|
202
202
|
- ".rspec"
|
|
203
|
+
- ".rspec_status"
|
|
203
204
|
- ".rubocop.yml"
|
|
204
205
|
- ".ruby-version"
|
|
205
206
|
- CHANGELOG.rst
|
|
@@ -249,7 +250,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
249
250
|
- - ">="
|
|
250
251
|
- !ruby/object:Gem::Version
|
|
251
252
|
version: '0'
|
|
252
|
-
requirements:
|
|
253
|
+
requirements:
|
|
254
|
+
- 'curl-impersonate (Homebrew): brew install curl-impersonate'
|
|
253
255
|
rubygems_version: 3.5.22
|
|
254
256
|
signing_key:
|
|
255
257
|
specification_version: 4
|