crawlfox 0.1.0
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 +7 -0
- data/LICENSE +21 -0
- data/README.md +39 -0
- data/lib/crawlfox.rb +122 -0
- metadata +49 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: bee7b5227e2463cfd1bd2f1bc30eebe985d9dc40672af60446174027a80c2e61
|
|
4
|
+
data.tar.gz: 64cbd128cf1ed931049d053f9ffd2e6baa93a40fa15f2621dda7fa99edcb441d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 957829da07e3df0c495235ad1df257be32a699db15893681034a7e466278c20bba86b24047737e22a238bd9796a42ef533f314eb19bffc67adb20a8e6cd91f46
|
|
7
|
+
data.tar.gz: 422d16cc631d9bd98c83e0f1606d8b43b5f6dbdac321f93bc00800fe1d4799058530e1f31960e700bedfe3fdf14231c587965faae5ca81e7370073df277fca1a
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Automote LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# crawlfox (Ruby)
|
|
2
|
+
|
|
3
|
+
Official Ruby SDK for the [CrawlFox](https://crawlfox.io) API: scrape, search (Google, Bing, or DuckDuckGo), batch scrape, and logs.
|
|
4
|
+
|
|
5
|
+
Get a key from the [dashboard](https://crawlfox.io). Set `CRAWLFOX_API_KEY`.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
gem install crawlfox
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Until RubyGems lists it:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem "crawlfox", github: "Automote-LLC/crawlfox-integrations", glob: "packages/ruby/*.gemspec"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
require "crawlfox"
|
|
19
|
+
|
|
20
|
+
app = Crawlfox::Client.new
|
|
21
|
+
|
|
22
|
+
doc = app.scrape("https://example.com", formats: ["markdown", "links"])
|
|
23
|
+
puts doc["markdown"]
|
|
24
|
+
|
|
25
|
+
hits = app.search("rust async tutorial", engine: "google", num: 10)
|
|
26
|
+
|
|
27
|
+
batch = app.batch(
|
|
28
|
+
["https://example.com/", "https://example.org/"],
|
|
29
|
+
formats: ["markdown"]
|
|
30
|
+
)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Formats: `markdown`, `html`, `rawHtml`, `json`, `links`, `images`, `emails`. Use `json_options` / CSS selectors for `json`. Credits: 1 per page, 1 per 10 requested search results.
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
ruby -Ilib:test test/client_test.rb
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Docs: https://docs.crawlfox.io
|
data/lib/crawlfox.rb
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Crawlfox
|
|
8
|
+
VERSION = "0.1.0"
|
|
9
|
+
DEFAULT_API_URL = "https://api.crawlfox.io"
|
|
10
|
+
|
|
11
|
+
class Error < StandardError
|
|
12
|
+
attr_reader :status, :code, :retryable, :body
|
|
13
|
+
|
|
14
|
+
def initialize(message, status:, code: nil, retryable: nil, body: {})
|
|
15
|
+
super(message)
|
|
16
|
+
@status = status
|
|
17
|
+
@code = code
|
|
18
|
+
@retryable = retryable
|
|
19
|
+
@body = body
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def retryable_effective?
|
|
23
|
+
return false if retryable == false
|
|
24
|
+
return true if retryable == true
|
|
25
|
+
|
|
26
|
+
[502, 503, 504].include?(status)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class Client
|
|
31
|
+
def initialize(api_key: nil, api_url: nil, timeout: 120, max_retries: 2, retry_backoff_ms: 200, transport: nil)
|
|
32
|
+
@api_key = api_key || ENV["CRAWLFOX_API_KEY"]
|
|
33
|
+
raise ArgumentError, "CrawlFox API key required. Pass api_key or set CRAWLFOX_API_KEY." if @api_key.to_s.empty?
|
|
34
|
+
|
|
35
|
+
@api_url = (api_url || ENV["CRAWLFOX_API_URL"] || DEFAULT_API_URL).to_s.sub(%r{/$}, "")
|
|
36
|
+
@timeout = timeout
|
|
37
|
+
@max_retries = max_retries
|
|
38
|
+
@retry_backoff_ms = retry_backoff_ms
|
|
39
|
+
@transport = transport || method(:net_http_transport)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def scrape(url, **options)
|
|
43
|
+
document(request("POST", "/v1/scrape", { url: url }.merge(options)))
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def scrape_get(url)
|
|
47
|
+
document(request("GET", "/v1/scrape/#{URI.encode_www_form_component(url)}"))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def batch(urls, **options)
|
|
51
|
+
env = request("POST", "/v1/batch", { urls: urls }.merge(options))
|
|
52
|
+
{
|
|
53
|
+
"success" => env.fetch("success", true),
|
|
54
|
+
"count" => env["count"] || Array(env["results"]).length,
|
|
55
|
+
"data" => Array(env["results"]).map { |item| document(item) }
|
|
56
|
+
}
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def search(q, **options)
|
|
60
|
+
env = request("POST", "/v1/search", { q: q }.merge(options))
|
|
61
|
+
{
|
|
62
|
+
"success" => env.fetch("success", true),
|
|
63
|
+
"web" => env.dig("data", "web") || [],
|
|
64
|
+
"creditsUsed" => env["creditsUsed"],
|
|
65
|
+
"id" => env["id"]
|
|
66
|
+
}
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def get_log(id)
|
|
70
|
+
request("GET", "/v1/logs/#{URI.encode_www_form_component(id)}")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def get_log_result(id)
|
|
74
|
+
request("GET", "/v1/logs/#{URI.encode_www_form_component(id)}/result")
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
def document(envelope)
|
|
80
|
+
data = envelope["data"].is_a?(Hash) ? envelope["data"].dup : envelope.dup
|
|
81
|
+
data["success"] = envelope.fetch("success", true)
|
|
82
|
+
data
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def request(method, path, body = nil)
|
|
86
|
+
payload = body && JSON.generate(body)
|
|
87
|
+
headers = {
|
|
88
|
+
"Authorization" => "Bearer #{@api_key}",
|
|
89
|
+
"Content-Type" => "application/json",
|
|
90
|
+
"User-Agent" => "crawlfox-ruby/#{VERSION}"
|
|
91
|
+
}
|
|
92
|
+
url = @api_url + path
|
|
93
|
+
attempts = @max_retries + 1
|
|
94
|
+
last = nil
|
|
95
|
+
attempts.times do |i|
|
|
96
|
+
status, raw = @transport.call(method, url, headers, payload)
|
|
97
|
+
json = JSON.parse(raw.to_s.empty? ? "{}" : raw)
|
|
98
|
+
return json if status >= 200 && status < 300
|
|
99
|
+
|
|
100
|
+
err = Error.new(json["message"] || json["title"] || "CrawlFox request failed (#{status})",
|
|
101
|
+
status: status, code: json["code"], retryable: json["retryable"], body: json)
|
|
102
|
+
raise err unless i < attempts - 1 && err.retryable_effective?
|
|
103
|
+
|
|
104
|
+
last = err
|
|
105
|
+
sleep(@retry_backoff_ms / 1000.0 * (2**i))
|
|
106
|
+
end
|
|
107
|
+
raise last || Error.new("Request failed", status: 0)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def net_http_transport(method, url, headers, body)
|
|
111
|
+
uri = URI.parse(url)
|
|
112
|
+
req = Net::HTTPGenericRequest.new(method, !body.nil?, true, uri)
|
|
113
|
+
headers.each { |k, v| req[k] = v }
|
|
114
|
+
req.body = body if body
|
|
115
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
116
|
+
http.use_ssl = uri.scheme == "https"
|
|
117
|
+
http.read_timeout = @timeout
|
|
118
|
+
res = http.request(req)
|
|
119
|
+
[res.code.to_i, res.body.to_s]
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: crawlfox
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Automote LLC
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-09-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
- pratik@automote.io
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- LICENSE
|
|
21
|
+
- README.md
|
|
22
|
+
- lib/crawlfox.rb
|
|
23
|
+
homepage: https://crawlfox.io
|
|
24
|
+
licenses:
|
|
25
|
+
- MIT
|
|
26
|
+
metadata:
|
|
27
|
+
homepage_uri: https://crawlfox.io
|
|
28
|
+
source_code_uri: https://github.com/Automote-LLC/crawlfox-integrations
|
|
29
|
+
documentation_uri: https://docs.crawlfox.io
|
|
30
|
+
post_install_message:
|
|
31
|
+
rdoc_options: []
|
|
32
|
+
require_paths:
|
|
33
|
+
- lib
|
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '3.1'
|
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
requirements: []
|
|
45
|
+
rubygems_version: 3.4.20
|
|
46
|
+
signing_key:
|
|
47
|
+
specification_version: 4
|
|
48
|
+
summary: Official Ruby SDK for the CrawlFox scrape and search API
|
|
49
|
+
test_files: []
|