data-for-seo 0.2.0

Client library for the DataForSEO API v3 (SERP, Keywords Data, Labs, Backlinks, OnPage, and more)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use crate::entity::{
    ContentAnalysisApiAvailableFilters, ContentAnalysisApiCategory,
    ContentAnalysisApiCategoryTrends, ContentAnalysisApiIdList, ContentAnalysisApiLanguage,
    ContentAnalysisApiLocation, ContentAnalysisApiPhraseTrends,
    ContentAnalysisApiRatingDistribution, ContentAnalysisApiSearch,
    ContentAnalysisApiSentimentAnalysis, ContentAnalysisApiSummary,
};
use crate::{DataForSeoApiResponse, DataForSeoClient};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// Content Analysis API — citation search and sentiment analytics.
/// See <https://docs.dataforseo.com/v3/content_analysis/overview/>.
pub struct ContentAnalysisApi<'a> {
    client: &'a DataForSeoClient,
}

impl DataForSeoClient {
    /// Returns the Content Analysis API endpoints.
    pub fn content_analysis(&self) -> ContentAnalysisApi<'_> {
        ContentAnalysisApi { client: self }
    }
}

impl ContentAnalysisApi<'_> {
    /// Finds citations for the target keyword.
    /// <https://docs.dataforseo.com/v3/content_analysis/search/live/>
    pub async fn search(
        &self,
        data: Vec<ContentAnalysisApiSearchPost>,
    ) -> DataForSeoApiResponse<ContentAnalysisApiSearch> {
        self.client
            .http_post("/v3/content_analysis/search/live", &data)
            .await
    }

    /// Overview of the citation data available for the target keyword.
    /// <https://docs.dataforseo.com/v3/content_analysis/summary/live/>
    pub async fn summary(
        &self,
        data: Vec<ContentAnalysisApiSummaryPost>,
    ) -> DataForSeoApiResponse<ContentAnalysisApiSummary> {
        self.client
            .http_post("/v3/content_analysis/summary/live", &data)
            .await
    }

    /// Citation stats by sentiment polarity and connotation.
    /// <https://docs.dataforseo.com/v3/content_analysis/sentiment_analysis/live/>
    pub async fn sentiment_analysis(
        &self,
        data: Vec<ContentAnalysisApiSentimentAnalysisPost>,
    ) -> DataForSeoApiResponse<ContentAnalysisApiSentimentAnalysis> {
        self.client
            .http_post("/v3/content_analysis/sentiment_analysis/live", &data)
            .await
    }

    /// Citation stats distributed by content rating.
    /// <https://docs.dataforseo.com/v3/content_analysis/rating_distribution/live/>
    pub async fn rating_distribution(
        &self,
        data: Vec<ContentAnalysisApiRatingDistributionPost>,
    ) -> DataForSeoApiResponse<ContentAnalysisApiRatingDistribution> {
        self.client
            .http_post("/v3/content_analysis/rating_distribution/live", &data)
            .await
    }

    /// Citation stats by date for the target keyword.
    /// <https://docs.dataforseo.com/v3/content_analysis/phrase_trends/live/>
    pub async fn phrase_trends(
        &self,
        data: Vec<ContentAnalysisApiPhraseTrendsPost>,
    ) -> DataForSeoApiResponse<ContentAnalysisApiPhraseTrends> {
        self.client
            .http_post("/v3/content_analysis/phrase_trends/live", &data)
            .await
    }

    /// Citation stats by date for the target category.
    /// <https://docs.dataforseo.com/v3/content_analysis/category_trends/live/>
    pub async fn category_trends(
        &self,
        data: Vec<ContentAnalysisApiCategoryTrendsPost>,
    ) -> DataForSeoApiResponse<ContentAnalysisApiCategoryTrends> {
        self.client
            .http_post("/v3/content_analysis/category_trends/live", &data)
            .await
    }

    /// Lists tasks set within the specified datetime range.
    /// <https://docs.dataforseo.com/v3/content_analysis/id_list/>
    pub async fn id_list(
        &self,
        data: Vec<ContentAnalysisApiIdListPost>,
    ) -> DataForSeoApiResponse<ContentAnalysisApiIdList> {
        self.client
            .http_post("/v3/content_analysis/id_list", &data)
            .await
    }

    /// Categories available in the content-analysis taxonomy.
    /// <https://docs.dataforseo.com/v3/content_analysis/categories/>
    pub async fn categories(&self) -> DataForSeoApiResponse<ContentAnalysisApiCategory> {
        self.client
            .http_get("/v3/content_analysis/categories")
            .await
    }

    /// Locations supported by the Content Analysis API.
    /// <https://docs.dataforseo.com/v3/content_analysis/locations/>
    pub async fn locations(&self) -> DataForSeoApiResponse<ContentAnalysisApiLocation> {
        self.client.http_get("/v3/content_analysis/locations").await
    }

    /// Languages supported by the Content Analysis API.
    /// <https://docs.dataforseo.com/v3/content_analysis/languages/>
    pub async fn languages(&self) -> DataForSeoApiResponse<ContentAnalysisApiLanguage> {
        self.client.http_get("/v3/content_analysis/languages").await
    }

    /// Filters that can be used in `filters` / `initial_dataset_filters`.
    /// <https://docs.dataforseo.com/v3/content_analysis/available_filters/>
    pub async fn available_filters(
        &self,
    ) -> DataForSeoApiResponse<ContentAnalysisApiAvailableFilters> {
        self.client
            .http_get("/v3/content_analysis/available_filters")
            .await
    }
}

/// Request for `content_analysis/search/live`.
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct ContentAnalysisApiSearchPost {
    /// Target keyword (UTF-8, lowercased); wrap in escaped double quotes for an
    /// exact phrase.
    pub keyword: String,
    /// Restrict matches to keywords appearing in given fields (title,
    /// main_title, previous_title, snippet).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub keyword_fields: Option<HashMap<String, String>>,
    /// Filter by page types (ecommerce, news, blogs, message-boards,
    /// organization).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_type: Option<Vec<String>>,
    /// Results grouping: `as_is` (default) or `one_per_domain`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_mode: Option<String>,
    /// Maximum citations to return (default 100, max 1000).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i32>,
    /// Up to 8 filtering conditions joined by `and`/`or`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filters: Option<Vec<Value>>,
    /// Up to 3 sorting rules, e.g. `content_info.sentiment_connotations.anger,desc`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order_by: Option<Vec<String>>,
    /// Number of leading citations to skip; use for up to 10,000 results.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<i32>,
    /// Token from a previous response to page beyond 10,000 results.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset_token: Option<String>,
    /// Scale for rank values: `one_hundred` or `one_thousand` (default).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rank_scale: Option<String>,
    /// User-defined task identifier (max 255 characters).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

impl ContentAnalysisApiSearchPost {
    /// Creates a request for the given target keyword.
    pub fn new(keyword: &str) -> ContentAnalysisApiSearchPost {
        ContentAnalysisApiSearchPost {
            keyword: keyword.to_string(),
            ..Default::default()
        }
    }
}

/// Request for `content_analysis/summary/live`.
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct ContentAnalysisApiSummaryPost {
    /// Target keyword (UTF-8, lowercased); wrap in escaped double quotes for an
    /// exact phrase.
    pub keyword: String,
    /// Restrict matches to keywords appearing in given fields (title,
    /// main_title, previous_title, snippet).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub keyword_fields: Option<HashMap<String, String>>,
    /// Filter by page types (ecommerce, news, blogs, message-boards,
    /// organization).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_type: Option<Vec<String>>,
    /// Maximum elements within internal arrays like top_domains and countries
    /// (default 1, max 20).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub internal_list_limit: Option<i32>,
    /// Minimum positive-sentiment probability for a citation to count
    /// (0–1, default 0.4).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub positive_connotation_threshold: Option<f64>,
    /// Minimum per-sentiment probability for a citation to count
    /// (0–1, default 0.4).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sentiments_connotation_threshold: Option<f64>,
    /// Up to 8 filtering conditions applied to Search fields before aggregation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub initial_dataset_filters: Option<Vec<Value>>,
    /// Scale for rank values: `one_hundred` or `one_thousand` (default).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rank_scale: Option<String>,
    /// User-defined task identifier (max 255 characters).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

impl ContentAnalysisApiSummaryPost {
    /// Creates a request for the given target keyword.
    pub fn new(keyword: &str) -> ContentAnalysisApiSummaryPost {
        ContentAnalysisApiSummaryPost {
            keyword: keyword.to_string(),
            ..Default::default()
        }
    }
}

/// Request for `content_analysis/sentiment_analysis/live`.
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct ContentAnalysisApiSentimentAnalysisPost {
    /// Target keyword (UTF-8, lowercased); wrap in escaped double quotes for an
    /// exact phrase.
    pub keyword: String,
    /// Restrict matches to keywords appearing in given fields (title,
    /// main_title, previous_title, snippet).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub keyword_fields: Option<HashMap<String, String>>,
    /// Filter by page types (ecommerce, news, blogs, message-boards,
    /// organization).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_type: Option<Vec<String>>,
    /// Maximum elements within internal arrays like top_domains and countries
    /// (default 1, max 20).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub internal_list_limit: Option<i32>,
    /// Minimum positive-sentiment probability for a citation to count
    /// (0–1, default 0.4).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub positive_connotation_threshold: Option<f64>,
    /// Minimum per-sentiment probability for a citation to count
    /// (0–1, default 0.4).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sentiments_connotation_threshold: Option<f64>,
    /// Up to 8 filtering conditions applied to Search fields before aggregation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub initial_dataset_filters: Option<Vec<Value>>,
    /// Scale for rank values: `one_hundred` or `one_thousand` (default).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rank_scale: Option<String>,
    /// User-defined task identifier (max 255 characters).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

impl ContentAnalysisApiSentimentAnalysisPost {
    /// Creates a request for the given target keyword.
    pub fn new(keyword: &str) -> ContentAnalysisApiSentimentAnalysisPost {
        ContentAnalysisApiSentimentAnalysisPost {
            keyword: keyword.to_string(),
            ..Default::default()
        }
    }
}

/// Request for `content_analysis/rating_distribution/live`.
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct ContentAnalysisApiRatingDistributionPost {
    /// Target keyword (UTF-8, lowercased); wrap in escaped double quotes for an
    /// exact phrase.
    pub keyword: String,
    /// Restrict matches to keywords appearing in given fields (title,
    /// main_title, previous_title, snippet).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub keyword_fields: Option<HashMap<String, String>>,
    /// Filter by page types (ecommerce, news, blogs, message-boards,
    /// organization).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_type: Option<Vec<String>>,
    /// Maximum elements within internal arrays like top_domains and countries
    /// (default 1, max 20).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub internal_list_limit: Option<i32>,
    /// Results grouping: `as_is` (default) or `one_per_domain`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_mode: Option<String>,
    /// Minimum positive-sentiment probability for a citation to count
    /// (0–1, default 0.4).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub positive_connotation_threshold: Option<f64>,
    /// Minimum per-sentiment probability for a citation to count
    /// (0–1, default 0.4).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sentiments_connotation_threshold: Option<f64>,
    /// Up to 8 filtering conditions applied to Search fields before aggregation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub initial_dataset_filters: Option<Vec<Value>>,
    /// Scale for rank values: `one_hundred` or `one_thousand` (default).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rank_scale: Option<String>,
    /// User-defined task identifier (max 255 characters).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

impl ContentAnalysisApiRatingDistributionPost {
    /// Creates a request for the given target keyword.
    pub fn new(keyword: &str) -> ContentAnalysisApiRatingDistributionPost {
        ContentAnalysisApiRatingDistributionPost {
            keyword: keyword.to_string(),
            ..Default::default()
        }
    }
}

/// Request for `content_analysis/phrase_trends/live`.
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct ContentAnalysisApiPhraseTrendsPost {
    /// Target keyword (UTF-8, lowercased); wrap in escaped double quotes for an
    /// exact phrase.
    pub keyword: String,
    /// Start of the date range, `yyyy-mm-dd`.
    pub date_from: String,
    /// End of the date range, `yyyy-mm-dd` (defaults to today).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub date_to: Option<String>,
    /// Grouping interval: `day`, `week`, or `month` (default).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub date_group: Option<String>,
    /// Restrict matches to keywords appearing in given fields (title,
    /// main_title, previous_title, snippet).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub keyword_fields: Option<HashMap<String, String>>,
    /// Filter by page types (ecommerce, news, blogs, message-boards,
    /// organization).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_type: Option<Vec<String>>,
    /// Results grouping: `as_is` (default) or `one_per_domain`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_mode: Option<String>,
    /// Maximum elements within internal arrays like top_domains and countries
    /// (default 1, max 20).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub internal_list_limit: Option<i32>,
    /// Up to 8 filtering conditions applied to Search fields before aggregation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub initial_dataset_filters: Option<Vec<Value>>,
    /// Scale for rank values: `one_hundred` or `one_thousand` (default).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rank_scale: Option<String>,
    /// User-defined task identifier (max 255 characters).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

impl ContentAnalysisApiPhraseTrendsPost {
    /// Creates a request for the given keyword and start date (`yyyy-mm-dd`).
    pub fn new(keyword: &str, date_from: &str) -> ContentAnalysisApiPhraseTrendsPost {
        ContentAnalysisApiPhraseTrendsPost {
            keyword: keyword.to_string(),
            date_from: date_from.to_string(),
            ..Default::default()
        }
    }
}

/// Request for `content_analysis/category_trends/live`.
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct ContentAnalysisApiCategoryTrendsPost {
    /// Target category code; obtain it from the Categories endpoint.
    pub category_code: i64,
    /// Start of the date range, `yyyy-mm-dd` (minimum 2022-10-31).
    pub date_from: String,
    /// End of the date range, `yyyy-mm-dd` (defaults to today).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub date_to: Option<String>,
    /// Grouping interval: `day`, `week`, or `month` (default).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub date_group: Option<String>,
    /// Filter by page types (ecommerce, news, blogs, message-boards,
    /// organization).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_type: Option<Vec<String>>,
    /// Results grouping: `as_is` (default) or `one_per_domain`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_mode: Option<String>,
    /// Maximum elements within internal arrays like top_domains and countries
    /// (default 1, max 20).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub internal_list_limit: Option<i32>,
    /// Up to 8 filtering conditions applied to Search fields before aggregation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub initial_dataset_filters: Option<Vec<Value>>,
    /// Scale for rank values: `one_hundred` or `one_thousand` (default).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rank_scale: Option<String>,
    /// User-defined task identifier (max 255 characters).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

impl ContentAnalysisApiCategoryTrendsPost {
    /// Creates a request for the given category code and start date (`yyyy-mm-dd`).
    pub fn new(category_code: i64, date_from: &str) -> ContentAnalysisApiCategoryTrendsPost {
        ContentAnalysisApiCategoryTrendsPost {
            category_code,
            date_from: date_from.to_string(),
            ..Default::default()
        }
    }
}

/// Request for `content_analysis/id_list`.
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct ContentAnalysisApiIdListPost {
    /// Start of the range, UTC `yyyy-mm-dd hh-mm-ss +00:00`.
    pub datetime_from: String,
    /// End of the range, UTC `yyyy-mm-dd hh-mm-ss +00:00`.
    pub datetime_to: String,
    /// Maximum task IDs to return (default 1000, max 1000).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i32>,
    /// Number of leading task IDs to skip (default 0).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<i32>,
    /// Sort by task execution time: `asc` (default) or `desc`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort: Option<String>,
    /// Include each task's request metadata (default false).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_metadata: Option<bool>,
}

impl ContentAnalysisApiIdListPost {
    /// Creates a request for the given datetime range
    /// (`yyyy-mm-dd hh-mm-ss +00:00`).
    pub fn new(datetime_from: &str, datetime_to: &str) -> ContentAnalysisApiIdListPost {
        ContentAnalysisApiIdListPost {
            datetime_from: datetime_from.to_string(),
            datetime_to: datetime_to.to_string(),
            ..Default::default()
        }
    }
}