crawlora-tiktok 0.1.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 +7 -0
- data/CHANGELOG.md +9 -0
- data/LICENSE +21 -0
- data/README.md +33 -0
- data/lib/crawlora/tiktok/client.rb +274 -0
- data/lib/crawlora/tiktok/version.rb +5 -0
- data/lib/crawlora/tiktok.rb +2 -0
- metadata +51 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b02081fec3968fe3c32ead2134c6d172ed283d7998e74c537f5637ccda59981c
|
|
4
|
+
data.tar.gz: '08a7f82f354cf45e6a6a78a5b4a457ae696431bcc13fdd30d2e7e1f5873afbb5'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 921578ffc562c1d5fccca29a0e1c3eb1354ee6f7241b1ddf8a6c1ac839a7aa35740f21ac4fcb076d41caa38691497904f726a4af6ba30b190d6bfe7eb4e95071
|
|
7
|
+
data.tar.gz: 2dba0c9daa93f05bd243a0c5be250b77691d5dfc4da0b4c026877a6eb540cf4431c92882cd9ed0fb36624976cb5858dcdb974003ed0d9c71faf3629de91f2f04
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Crawlora
|
|
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,33 @@
|
|
|
1
|
+
# Crawlora TikTok Ruby client
|
|
2
|
+
|
|
3
|
+
This gem calls the Crawlora hosted API at `https://api.crawlora.net/api/v1`. It does not call or scrape TikTok directly. Requests require your Crawlora API key and use your account's service plan.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem "crawlora-tiktok"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Create an account at [crawlora.net](https://crawlora.net/signup?utm_source=rubygems&utm_medium=referral&utm_campaign=platform-clients&utm_content=tiktok-ruby-signup), open the [Crawlora console](https://crawlora.net/app?utm_source=rubygems&utm_medium=referral&utm_campaign=platform-clients&utm_content=tiktok-ruby-console) to get an API key, and set `CRAWLORA_API_KEY` before running the client:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
require "json"
|
|
15
|
+
require "crawlora/tiktok"
|
|
16
|
+
|
|
17
|
+
client = Crawlora::Tiktok::Client.new
|
|
18
|
+
result = client.request("tiktok-search", JSON.parse("{\"keyword\": \"sample\"}"))
|
|
19
|
+
puts result
|
|
20
|
+
client.close
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Use an operation-specific method for normal calls. `request(operation_id, params = {}, response_type: :auto)` is available for every operation. `response_type: :text` returns raw response text. This gem includes 25 API operations.
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
client = Crawlora::Tiktok::Client.new(api_key: ENV.fetch("CRAWLORA_API_KEY"), timeout: 30)
|
|
27
|
+
# client.<operation_method>(<endpoint parameters>)
|
|
28
|
+
client.close
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Client options include `api_key`, `base_url`, and `timeout`. Ruby stdlib provides the HTTP and JSON support.
|
|
32
|
+
|
|
33
|
+
See [Crawlora](https://crawlora.net/?utm_source=rubygems&utm_medium=referral&utm_campaign=platform-clients&utm_content=tiktok-ruby-homepage), the [API documentation](https://crawlora.net/docs?utm_source=rubygems&utm_medium=referral&utm_campaign=platform-clients&utm_content=tiktok-ruby-api-docs), and [the package repository](https://github.com/Crawlora-org/crawlora-tiktok) for account setup, the API operation reference, and release history.
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "net/http"
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module Crawlora
|
|
6
|
+
module Tiktok
|
|
7
|
+
module Errors
|
|
8
|
+
class Error < StandardError
|
|
9
|
+
attr_reader :status, :operation_id, :body
|
|
10
|
+
|
|
11
|
+
def initialize(message, status: nil, operation_id: nil, body: nil)
|
|
12
|
+
super(message)
|
|
13
|
+
@status, @operation_id, @body = status, operation_id, body
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
class ClientError < Error; end
|
|
17
|
+
class ServerError < Error; end
|
|
18
|
+
class NetworkError < Error; end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
OPERATIONS = JSON.parse(<<~'JSON').freeze
|
|
22
|
+
{"tiktok-category": {"id": "tiktok-category", "method": "GET", "params": [], "path": "/tiktok/category", "pathParams": [], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "tiktok-challenge": {"id": "tiktok-challenge", "method": "GET", "params": [{"description": "Hashtag name (e.g., 'christmas')", "in": "path", "name": "name", "required": true, "type": "string", "x-example": "christmas"}], "path": "/tiktok/hashtag/{name}", "pathParams": ["name"], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "tiktok-challenge-list": {"id": "tiktok-challenge-list", "method": "GET", "params": [{"description": "Hashtag id returned by the hashtag detail endpoint", "in": "query", "name": "id", "required": true, "type": "string", "x-example": "3242"}, {"default": 0, "description": "Pagination cursor", "in": "query", "name": "cursor", "type": "integer"}], "path": "/tiktok/hashtags", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "id", "required": true, "type": "string"}, {"in": "query", "name": "cursor", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-creative-center-hashtags": {"id": "tiktok-creative-center-hashtags", "method": "GET", "params": [{"description": "ISO-2 country code", "in": "query", "name": "country_code", "required": true, "type": "string", "x-example": "US"}, {"default": 7, "description": "Lookback window in days", "enum": [7, 30], "in": "query", "name": "period", "type": "integer"}], "path": "/tiktok/creative-center/hashtags", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "country_code", "required": true, "type": "string"}, {"enum": ["7", "30"], "in": "query", "name": "period", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-creative-center-videos": {"id": "tiktok-creative-center-videos", "method": "GET", "params": [{"description": "ISO-2 country code", "in": "query", "name": "country_code", "required": true, "type": "string", "x-example": "US"}, {"default": 7, "description": "Lookback window in days", "enum": [7, 30], "in": "query", "name": "period", "type": "integer"}, {"default": "views", "description": "Sort order", "enum": ["views", "engagement", "six_second_views"], "in": "query", "name": "sort_by", "type": "string"}, {"description": "Content tag id to filter by", "in": "query", "name": "content_label_id", "type": "string", "x-example": "11015"}, {"default": false, "description": "Restrict to organic (non-paid) videos only", "in": "query", "name": "organic_only", "type": "boolean"}], "path": "/tiktok/creative-center/videos", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "country_code", "required": true, "type": "string"}, {"enum": ["7", "30"], "in": "query", "name": "period", "type": "integer"}, {"enum": ["views", "engagement", "six_second_views"], "in": "query", "name": "sort_by", "type": "string"}, {"in": "query", "name": "content_label_id", "type": "string"}, {"in": "query", "name": "organic_only", "type": "boolean"}], "security": ["ApiKeyAuth"]}, "tiktok-explore": {"id": "tiktok-explore", "method": "GET", "params": [{"description": "Category type id returned by the category endpoint", "in": "path", "name": "id", "required": true, "type": "integer", "x-example": 120}], "path": "/tiktok/explore/{id}", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "tiktok-popular-trend-country-industry-meta": {"id": "tiktok-popular-trend-country-industry-meta", "method": "GET", "params": [], "path": "/tiktok/popular-trend/country-industry-meta", "pathParams": [], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "tiktok-post": {"id": "tiktok-post", "method": "GET", "params": [{"description": "TikTok video id", "in": "path", "name": "id", "required": true, "type": "string", "x-example": "7444278905264983342"}], "path": "/tiktok/post/{id}", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "tiktok-profile": {"id": "tiktok-profile", "method": "GET", "params": [{"description": "TikTok handle without the leading @", "in": "path", "name": "handler", "required": true, "type": "string", "x-example": "chatgpt"}], "path": "/tiktok/profile/{handler}", "pathParams": ["handler"], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "tiktok-profile-post": {"id": "tiktok-profile-post", "method": "GET", "params": [{"description": "TikTok secUid for the profile", "in": "query", "name": "secUid", "required": true, "type": "string", "x-example": "MS4wLjABAAAAT4vq3vsh9X-Vb_WtV6tz4QWTbKjliTKCiK5DqnJNtQEA2RUveHb7UdnL7xgPK2HB"}, {"default": 0, "description": "Pagination cursor", "in": "query", "name": "cursor", "type": "integer"}, {"default": 0, "description": "Sort mode: 0 latest, 1 popular, 2 oldest", "enum": [0, 1, 2], "in": "query", "name": "sort_type", "type": "integer"}], "path": "/tiktok/posts", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "secUid", "required": true, "type": "string"}, {"in": "query", "name": "cursor", "type": "integer"}, {"enum": ["0", "1", "2"], "in": "query", "name": "sort_type", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-search": {"id": "tiktok-search", "method": "GET", "params": [{"description": "Search keyword", "in": "query", "name": "keyword", "required": true, "type": "string", "x-example": "dance"}, {"default": 0, "description": "Pagination cursor", "in": "query", "name": "cursor", "type": "integer"}, {"default": 20, "description": "Result count, clamped to 50", "in": "query", "name": "count", "type": "integer"}], "path": "/tiktok/search", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "keyword", "required": true, "type": "string"}, {"in": "query", "name": "cursor", "type": "integer"}, {"in": "query", "name": "count", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-search-hashtag": {"id": "tiktok-search-hashtag", "method": "GET", "params": [{"description": "Search keyword", "in": "query", "name": "keyword", "required": true, "type": "string", "x-example": "chatgpt"}, {"default": 0, "description": "Pagination cursor", "in": "query", "name": "cursor", "type": "integer"}, {"default": 20, "description": "Result count, clamped to 50", "in": "query", "name": "count", "type": "integer"}], "path": "/tiktok/search/hashtag", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "keyword", "required": true, "type": "string"}, {"in": "query", "name": "cursor", "type": "integer"}, {"in": "query", "name": "count", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-search-user": {"id": "tiktok-search-user", "method": "GET", "params": [{"description": "Search keyword", "in": "query", "name": "keyword", "required": true, "type": "string", "x-example": "chatgpt"}, {"default": 0, "description": "Pagination cursor", "in": "query", "name": "cursor", "type": "integer"}], "path": "/tiktok/search/user", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "keyword", "required": true, "type": "string"}, {"in": "query", "name": "cursor", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-top-ads-analysis": {"id": "tiktok-top-ads-analysis", "method": "GET", "params": [{"description": "Top Ads material id", "in": "query", "name": "material_id", "required": true, "type": "string", "x-example": "7130614705291427842"}, {"default": "retain_ctr", "description": "Interactive time analysis metric", "enum": ["retain_ctr", "retain_cvr", "click_cnt", "convert_cnt", "play_retain_cnt"], "in": "query", "name": "metric", "type": "string"}, {"default": 7, "description": "Percentile lookback period in days", "enum": [7, 30, 180], "in": "query", "name": "period_type", "type": "integer"}], "path": "/tiktok/top-ads/analysis", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "material_id", "required": true, "type": "string"}, {"enum": ["retain_ctr", "retain_cvr", "click_cnt", "convert_cnt", "play_retain_cnt"], "in": "query", "name": "metric", "type": "string"}, {"enum": ["7", "30", "180"], "in": "query", "name": "period_type", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-top-ads-detail": {"id": "tiktok-top-ads-detail", "method": "GET", "params": [{"description": "Top Ads material id", "in": "query", "name": "material_id", "required": true, "type": "string", "x-example": "7631130810943897607"}], "path": "/tiktok/top-ads/detail", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "material_id", "required": true, "type": "string"}], "security": ["ApiKeyAuth"]}, "tiktok-top-ads-filters": {"id": "tiktok-top-ads-filters", "method": "GET", "params": [], "path": "/tiktok/top-ads/filters", "pathParams": [], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "tiktok-top-ads-list": {"id": "tiktok-top-ads-list", "method": "GET", "params": [{"default": 30, "description": "Lookback period in days", "enum": [7, 30, 180], "in": "query", "name": "period", "type": "integer"}, {"default": 1, "description": "Page number", "in": "query", "minimum": 1, "name": "page", "type": "integer"}, {"default": 20, "description": "Maximum number of ads to return", "in": "query", "maximum": 100, "name": "limit", "type": "integer"}, {"default": "for_you", "description": "Sort order", "enum": ["for_you", "impression", "ctr", "play_2s_rate", "play_6s_rate", "cvr", "like"], "in": "query", "name": "order_by", "type": "string"}, {"description": "Country code or comma-separated country codes from /tiktok/top-ads/filters", "in": "query", "name": "country_code", "type": "string", "x-example": "US"}, {"description": "Brand or product keyword search", "in": "query", "name": "keyword", "type": "string", "x-example": "coffee"}, {"description": "Industry filter id or comma-separated ids from /tiktok/top-ads/filters", "in": "query", "name": "industry", "type": "string", "x-example": "23118000000"}, {"description": "Objective filter id or comma-separated ids from /tiktok/top-ads/filters", "in": "query", "name": "objective", "type": "string", "x-example": "3"}, {"description": "Ad language id or comma-separated ids from /tiktok/top-ads/filters", "in": "query", "name": "ad_language", "type": "string", "x-example": "en"}, {"description": "Pattern label id or comma-separated ids from /tiktok/top-ads/filters", "in": "query", "name": "pattern_label", "type": "string", "x-example": "10100100000"}, {"description": "Video duration bucket", "enum": ["time-2", "time-3", "time-4", "time-5", "time-6", "time-7"], "in": "query", "name": "duration", "type": "string"}, {"description": "Like percentile bucket id or comma-separated ids", "enum": ["1", "2", "3", "4", "5"], "in": "query", "name": "like", "type": "string"}, {"description": "Ad format id", "enum": ["1", "2"], "in": "query", "name": "ad_format", "type": "string"}], "path": "/tiktok/top-ads/list", "pathParams": [], "produces": ["application/json"], "queryParams": [{"enum": ["7", "30", "180"], "in": "query", "name": "period", "type": "integer"}, {"in": "query", "name": "page", "type": "integer"}, {"in": "query", "name": "limit", "type": "integer"}, {"enum": ["for_you", "impression", "ctr", "play_2s_rate", "play_6s_rate", "cvr", "like"], "in": "query", "name": "order_by", "type": "string"}, {"in": "query", "name": "country_code", "type": "string"}, {"in": "query", "name": "keyword", "type": "string"}, {"in": "query", "name": "industry", "type": "string"}, {"in": "query", "name": "objective", "type": "string"}, {"in": "query", "name": "ad_language", "type": "string"}, {"in": "query", "name": "pattern_label", "type": "string"}, {"enum": ["time-2", "time-3", "time-4", "time-5", "time-6", "time-7"], "in": "query", "name": "duration", "type": "string"}, {"enum": ["1", "2", "3", "4", "5"], "in": "query", "name": "like", "type": "string"}, {"enum": ["1", "2"], "in": "query", "name": "ad_format", "type": "string"}], "security": ["ApiKeyAuth"]}, "tiktok-top-ads-location-info": {"id": "tiktok-top-ads-location-info", "method": "GET", "params": [{"default": 1, "description": "Creative Center module id", "in": "query", "name": "module", "type": "integer"}], "path": "/tiktok/top-ads/location-info", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "module", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-top-ads-locations": {"id": "tiktok-top-ads-locations", "method": "GET", "params": [], "path": "/tiktok/top-ads/locations", "pathParams": [], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "tiktok-top-ads-recommend": {"id": "tiktok-top-ads-recommend", "method": "GET", "params": [{"description": "Top Ads material id", "in": "query", "name": "material_id", "required": true, "type": "string", "x-example": "7631130810943897607"}, {"default": 1, "description": "Page number", "in": "query", "minimum": 1, "name": "page", "type": "integer"}, {"default": 20, "description": "Maximum number of ads to return", "in": "query", "maximum": 100, "name": "limit", "type": "integer"}], "path": "/tiktok/top-ads/recommend", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "material_id", "required": true, "type": "string"}, {"in": "query", "name": "page", "type": "integer"}, {"in": "query", "name": "limit", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-top-ads-safety": {"id": "tiktok-top-ads-safety", "method": "GET", "params": [], "path": "/tiktok/top-ads/safety", "pathParams": [], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "tiktok-top-ads-spotlight": {"id": "tiktok-top-ads-spotlight", "method": "GET", "params": [{"default": 1, "description": "Page number", "in": "query", "minimum": 1, "name": "page", "type": "integer"}, {"default": 20, "description": "Maximum number of ads to return", "in": "query", "maximum": 100, "name": "limit", "type": "integer"}], "path": "/tiktok/top-ads/spotlight", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "page", "type": "integer"}, {"in": "query", "name": "limit", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-top-ads-suggestions": {"id": "tiktok-top-ads-suggestions", "method": "GET", "params": [{"default": 50, "description": "Maximum number of suggestions to return", "in": "query", "name": "count", "type": "integer"}, {"default": 1, "description": "Suggestion scenario id", "in": "query", "name": "scenario", "type": "integer"}], "path": "/tiktok/top-ads/suggestions", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "count", "type": "integer"}, {"in": "query", "name": "scenario", "type": "integer"}], "security": ["ApiKeyAuth"]}, "tiktok-trending": {"id": "tiktok-trending", "method": "GET", "params": [], "path": "/tiktok/trending", "pathParams": [], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "tiktok-video-comments": {"id": "tiktok-video-comments", "method": "GET", "params": [{"description": "TikTok video id from the video URL", "in": "query", "name": "aweme_id", "required": true, "type": "string", "x-example": "7304809083817774382"}, {"default": 0, "description": "Pagination cursor", "in": "query", "name": "cursor", "type": "integer"}], "path": "/tiktok/comments", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "aweme_id", "required": true, "type": "string"}, {"in": "query", "name": "cursor", "type": "integer"}], "security": ["ApiKeyAuth"]}}
|
|
23
|
+
JSON
|
|
24
|
+
OPERATION_IDS = JSON.parse(<<~'JSON').freeze
|
|
25
|
+
["tiktok-category", "tiktok-challenge", "tiktok-challenge-list", "tiktok-creative-center-hashtags", "tiktok-creative-center-videos", "tiktok-explore", "tiktok-popular-trend-country-industry-meta", "tiktok-post", "tiktok-profile", "tiktok-profile-post", "tiktok-search", "tiktok-search-hashtag", "tiktok-search-user", "tiktok-top-ads-analysis", "tiktok-top-ads-detail", "tiktok-top-ads-filters", "tiktok-top-ads-list", "tiktok-top-ads-location-info", "tiktok-top-ads-locations", "tiktok-top-ads-recommend", "tiktok-top-ads-safety", "tiktok-top-ads-spotlight", "tiktok-top-ads-suggestions", "tiktok-trending", "tiktok-video-comments"]
|
|
26
|
+
JSON
|
|
27
|
+
OPERATION_COUNT = OPERATION_IDS.length
|
|
28
|
+
|
|
29
|
+
class Client
|
|
30
|
+
attr_reader :base_url
|
|
31
|
+
|
|
32
|
+
def initialize(api_key: ENV["CRAWLORA_API_KEY"], base_url: "https://api.crawlora.net/api/v1", timeout: 30, user_agent: "crawlora-tiktok-ruby/0.1.1", transport: nil)
|
|
33
|
+
@api_key = api_key
|
|
34
|
+
@base_url = base_url.to_s.sub(%r{/+$}, "")
|
|
35
|
+
@timeout = Float(timeout)
|
|
36
|
+
@user_agent = user_agent
|
|
37
|
+
@transport = transport
|
|
38
|
+
@closed = false
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def request(operation_id, params = {}, response_type: :auto)
|
|
42
|
+
raise Errors::ClientError, "client is closed" if @closed
|
|
43
|
+
operation_id = operation_id.to_s
|
|
44
|
+
operation = OPERATIONS[operation_id]
|
|
45
|
+
raise Errors::ClientError.new("unknown operation: #{operation_id}", operation_id: operation_id) unless operation
|
|
46
|
+
raise Errors::ClientError.new("Crawlora API key is required", operation_id: operation_id) if @api_key.nil? || @api_key.to_s.empty?
|
|
47
|
+
normalized = params.each_with_object({}) { |(key, value), out| out[key.to_s] = value }
|
|
48
|
+
url = build_url(operation, normalized)
|
|
49
|
+
uri = URI.parse(url)
|
|
50
|
+
request = Net::HTTP::Get.new(uri)
|
|
51
|
+
request["x-api-key"] = @api_key
|
|
52
|
+
request["User-Agent"] = @user_agent
|
|
53
|
+
request["Accept"] = operation["produces"].include?("text/plain") ? "application/json, text/plain" : "application/json"
|
|
54
|
+
begin
|
|
55
|
+
if @transport
|
|
56
|
+
response = @transport.call(url, request.to_hash, @timeout)
|
|
57
|
+
status = Integer(response.fetch(:status) { response.fetch("status") })
|
|
58
|
+
body = response.fetch(:body) { response.fetch("body", "") }
|
|
59
|
+
headers = response.fetch(:headers) { response.fetch("headers", {}) }
|
|
60
|
+
content_type = headers["content-type"] || headers["Content-Type"]
|
|
61
|
+
else
|
|
62
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
63
|
+
http.use_ssl = uri.scheme == "https"
|
|
64
|
+
http.open_timeout = @timeout
|
|
65
|
+
http.read_timeout = @timeout
|
|
66
|
+
response = http.start { |connection| connection.request(request) }
|
|
67
|
+
status = response.code.to_i
|
|
68
|
+
body = response.body
|
|
69
|
+
content_type = response["content-type"]
|
|
70
|
+
end
|
|
71
|
+
rescue Timeout::Error, SocketError, SystemCallError, IOError, EOFError, Net::HTTPBadResponse, Net::ProtocolError, OpenSSL::SSL::SSLError => error
|
|
72
|
+
raise Errors::NetworkError.new("Crawlora request failed: #{error.message}", operation_id: operation_id)
|
|
73
|
+
end
|
|
74
|
+
unless status >= 200 && status < 300
|
|
75
|
+
klass = status >= 500 ? Errors::ServerError : Errors::ClientError
|
|
76
|
+
raise klass.new("Crawlora returned HTTP #{status}", status: status, operation_id: operation_id, body: body)
|
|
77
|
+
end
|
|
78
|
+
parse_response(body, content_type, operation, normalized, response_type)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def close
|
|
82
|
+
@closed = true
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def closed?
|
|
86
|
+
@closed
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def with
|
|
90
|
+
return self unless block_given?
|
|
91
|
+
yield self
|
|
92
|
+
ensure
|
|
93
|
+
close if block_given?
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def self.operation_count
|
|
97
|
+
OPERATION_COUNT
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def self.operation_ids
|
|
101
|
+
OPERATION_IDS
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def self.operations
|
|
105
|
+
OPERATIONS
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
define_method('category') do |**params|
|
|
109
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
110
|
+
request('tiktok-category', params, response_type: response_type)
|
|
111
|
+
end
|
|
112
|
+
define_method('video_comments') do |**params|
|
|
113
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
114
|
+
request('tiktok-video-comments', params, response_type: response_type)
|
|
115
|
+
end
|
|
116
|
+
define_method('creative_center_hashtags') do |**params|
|
|
117
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
118
|
+
request('tiktok-creative-center-hashtags', params, response_type: response_type)
|
|
119
|
+
end
|
|
120
|
+
define_method('creative_center_videos') do |**params|
|
|
121
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
122
|
+
request('tiktok-creative-center-videos', params, response_type: response_type)
|
|
123
|
+
end
|
|
124
|
+
define_method('explore') do |**params|
|
|
125
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
126
|
+
request('tiktok-explore', params, response_type: response_type)
|
|
127
|
+
end
|
|
128
|
+
define_method('challenge') do |**params|
|
|
129
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
130
|
+
request('tiktok-challenge', params, response_type: response_type)
|
|
131
|
+
end
|
|
132
|
+
define_method('challenge_list') do |**params|
|
|
133
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
134
|
+
request('tiktok-challenge-list', params, response_type: response_type)
|
|
135
|
+
end
|
|
136
|
+
define_method('popular_trend_country_industry_meta') do |**params|
|
|
137
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
138
|
+
request('tiktok-popular-trend-country-industry-meta', params, response_type: response_type)
|
|
139
|
+
end
|
|
140
|
+
define_method('post') do |**params|
|
|
141
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
142
|
+
request('tiktok-post', params, response_type: response_type)
|
|
143
|
+
end
|
|
144
|
+
define_method('profile_post') do |**params|
|
|
145
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
146
|
+
request('tiktok-profile-post', params, response_type: response_type)
|
|
147
|
+
end
|
|
148
|
+
define_method('profile') do |**params|
|
|
149
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
150
|
+
request('tiktok-profile', params, response_type: response_type)
|
|
151
|
+
end
|
|
152
|
+
define_method('search') do |**params|
|
|
153
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
154
|
+
request('tiktok-search', params, response_type: response_type)
|
|
155
|
+
end
|
|
156
|
+
define_method('search_hashtag') do |**params|
|
|
157
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
158
|
+
request('tiktok-search-hashtag', params, response_type: response_type)
|
|
159
|
+
end
|
|
160
|
+
define_method('search_user') do |**params|
|
|
161
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
162
|
+
request('tiktok-search-user', params, response_type: response_type)
|
|
163
|
+
end
|
|
164
|
+
define_method('top_ads_analysis') do |**params|
|
|
165
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
166
|
+
request('tiktok-top-ads-analysis', params, response_type: response_type)
|
|
167
|
+
end
|
|
168
|
+
define_method('top_ads_detail') do |**params|
|
|
169
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
170
|
+
request('tiktok-top-ads-detail', params, response_type: response_type)
|
|
171
|
+
end
|
|
172
|
+
define_method('top_ads_filters') do |**params|
|
|
173
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
174
|
+
request('tiktok-top-ads-filters', params, response_type: response_type)
|
|
175
|
+
end
|
|
176
|
+
define_method('top_ads_list') do |**params|
|
|
177
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
178
|
+
request('tiktok-top-ads-list', params, response_type: response_type)
|
|
179
|
+
end
|
|
180
|
+
define_method('top_ads_location_info') do |**params|
|
|
181
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
182
|
+
request('tiktok-top-ads-location-info', params, response_type: response_type)
|
|
183
|
+
end
|
|
184
|
+
define_method('top_ads_locations') do |**params|
|
|
185
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
186
|
+
request('tiktok-top-ads-locations', params, response_type: response_type)
|
|
187
|
+
end
|
|
188
|
+
define_method('top_ads_recommend') do |**params|
|
|
189
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
190
|
+
request('tiktok-top-ads-recommend', params, response_type: response_type)
|
|
191
|
+
end
|
|
192
|
+
define_method('top_ads_safety') do |**params|
|
|
193
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
194
|
+
request('tiktok-top-ads-safety', params, response_type: response_type)
|
|
195
|
+
end
|
|
196
|
+
define_method('top_ads_spotlight') do |**params|
|
|
197
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
198
|
+
request('tiktok-top-ads-spotlight', params, response_type: response_type)
|
|
199
|
+
end
|
|
200
|
+
define_method('top_ads_suggestions') do |**params|
|
|
201
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
202
|
+
request('tiktok-top-ads-suggestions', params, response_type: response_type)
|
|
203
|
+
end
|
|
204
|
+
define_method('trending') do |**params|
|
|
205
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
206
|
+
request('tiktok-trending', params, response_type: response_type)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
private
|
|
210
|
+
|
|
211
|
+
def build_url(operation, params)
|
|
212
|
+
known = operation["params"].map { |param| param["name"] }
|
|
213
|
+
unknown = params.keys - known
|
|
214
|
+
raise Errors::ClientError.new("unknown parameters: #{unknown.join(', ')}", operation_id: operation["id"]) unless unknown.empty?
|
|
215
|
+
path = operation["path"].dup
|
|
216
|
+
operation["params"].select { |param| param["in"] == "path" }.each do |param|
|
|
217
|
+
value = params[param["name"]]
|
|
218
|
+
raise Errors::ClientError.new("missing path parameter: #{param['name']}", operation_id: operation["id"]) if value.nil?
|
|
219
|
+
path.sub!("{" + param["name"] + "}", percent_encode(value.to_s))
|
|
220
|
+
end
|
|
221
|
+
pairs = []
|
|
222
|
+
operation["queryParams"].each do |param|
|
|
223
|
+
name = param["name"]
|
|
224
|
+
value = params.key?(name) ? params[name] : param["default"]
|
|
225
|
+
if value.nil?
|
|
226
|
+
raise Errors::ClientError.new("missing query parameter: #{name}", operation_id: operation["id"]) if param["required"]
|
|
227
|
+
next
|
|
228
|
+
end
|
|
229
|
+
enum_values = param["enum"] || (param["items"] && param["items"]["enum"])
|
|
230
|
+
if enum_values && !(value.is_a?(Array) ? value : [value]).all? { |item| enum_values.map(&:to_s).include?(item.to_s) }
|
|
231
|
+
raise Errors::ClientError.new("invalid value for #{name}", operation_id: operation["id"])
|
|
232
|
+
end
|
|
233
|
+
if value.is_a?(Array)
|
|
234
|
+
format = param["collectionFormat"] || "csv"
|
|
235
|
+
if format == "multi"
|
|
236
|
+
value.each { |item| pairs << [name, scalar(item)] }
|
|
237
|
+
else
|
|
238
|
+
separator = {"csv" => ",", "ssv" => " ", "tsv" => "\t", "pipes" => "|"}[format] || ","
|
|
239
|
+
pairs << [name, value.map { |item| scalar(item) }.join(separator)]
|
|
240
|
+
end
|
|
241
|
+
else
|
|
242
|
+
pairs << [name, scalar(value)]
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
query = pairs.map { |name, value| "#{percent_encode(name)}=#{percent_encode(value)}" }.join("&")
|
|
246
|
+
@base_url + path + (query.empty? ? "" : "?" + query)
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def scalar(value)
|
|
250
|
+
value == true ? "true" : (value == false ? "false" : value.to_s)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def percent_encode(value)
|
|
254
|
+
URI::DEFAULT_PARSER.escape(value.to_s, /[^A-Za-z0-9\-._~]/)
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def parse_response(body, content_type, operation, params, response_type)
|
|
258
|
+
type = response_type.to_s
|
|
259
|
+
raise Errors::ClientError.new("response_type must be auto, json, or text", operation_id: operation["id"]) unless %w[auto json text].include?(type)
|
|
260
|
+
format = operation["params"].find { |param| param["name"] == "format" }
|
|
261
|
+
text_formats = format && format["enum"] ? format["enum"].reject { |value| %w[json application/json].include?(value.to_s.downcase) } : []
|
|
262
|
+
raw_format = params["format"] && text_formats.include?(params["format"].to_s)
|
|
263
|
+
json_format = format && format["enum"] && format["enum"].any? { |value| %w[json application/json].include?(value.to_s.downcase) } && %w[json application/json].include?(params["format"].to_s.downcase)
|
|
264
|
+
is_json = json_format || content_type.to_s.downcase.include?("json") || operation["produces"] == ["application/json"]
|
|
265
|
+
return body if type == "text" || raw_format || (type == "auto" && !is_json)
|
|
266
|
+
JSON.parse(body)
|
|
267
|
+
rescue JSON::ParserError => error
|
|
268
|
+
raise Errors::Error.new("invalid JSON response from Crawlora: #{error.message}", operation_id: operation["id"], body: body)
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
public
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: crawlora-tiktok
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Crawlora
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-10-09 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Credential-free TikTok API access through Crawlora's hosted service.
|
|
14
|
+
email:
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- CHANGELOG.md
|
|
20
|
+
- LICENSE
|
|
21
|
+
- README.md
|
|
22
|
+
- lib/crawlora/tiktok.rb
|
|
23
|
+
- lib/crawlora/tiktok/client.rb
|
|
24
|
+
- lib/crawlora/tiktok/version.rb
|
|
25
|
+
homepage: https://crawlora.net/?utm_source=rubygems&utm_medium=referral&utm_campaign=platform-clients&utm_content=tiktok-ruby-homepage
|
|
26
|
+
licenses:
|
|
27
|
+
- MIT
|
|
28
|
+
metadata:
|
|
29
|
+
source_code_uri: https://github.com/Crawlora-org/crawlora-tiktok
|
|
30
|
+
documentation_uri: https://github.com/Crawlora-org/crawlora-tiktok/blob/main/ruby/README.md
|
|
31
|
+
rubygems_mfa_required: 'true'
|
|
32
|
+
post_install_message:
|
|
33
|
+
rdoc_options: []
|
|
34
|
+
require_paths:
|
|
35
|
+
- lib
|
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '2.6'
|
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
requirements: []
|
|
47
|
+
rubygems_version: 3.4.19
|
|
48
|
+
signing_key:
|
|
49
|
+
specification_version: 4
|
|
50
|
+
summary: TikTok client for the Crawlora hosted API
|
|
51
|
+
test_files: []
|