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
use std::collections::HashMap;
use crate::{
newsdata_io::{NewsdataIO, Requests},
ApiResult, Json,
};
/// Trait for the News Archive API.
pub trait NewsArchive {
/// Get news articles from the archive.
///
/// # Arguments
///
/// * `params`: The parameters for the request.
///
/// # Returns
///
/// An `ApiResult` containing the JSON response from the API.
fn get_news_archive(&self, params: &GetNewsArchiveParams) -> ApiResult<Json>;
}
impl NewsArchive for NewsdataIO {
fn get_news_archive(&self, params: &GetNewsArchiveParams) -> ApiResult<Json> {
let mut query_params = HashMap::new();
// Add id parameter to query params
if let Some(id) = ¶ms.id {
query_params.insert("id".to_string(), id.join(","));
}
// Add from_date parameter to query params
if let Some(from_date) = ¶ms.from_date {
query_params.insert("from_date".to_string(), from_date.clone());
}
// Add to_date parameter to query params
if let Some(to_date) = ¶ms.to_date {
query_params.insert("to_date".to_string(), to_date.clone());
}
// Add q parameter to query params
if let Some(q) = ¶ms.q {
query_params.insert("q".to_string(), q.clone());
}
// Add q_in_title parameter to query params
if let Some(q_in_title) = ¶ms.q_in_title {
query_params.insert("qInTitle".to_string(), q_in_title.clone());
}
// Add q_in_meta parameter to query params
if let Some(q_in_meta) = ¶ms.q_in_meta {
query_params.insert("qInMeta".to_string(), q_in_meta.clone());
}
// Add country parameter to query params
if let Some(country) = ¶ms.country {
query_params.insert("country".to_string(), country.join(","));
}
// Add category parameter to query params
if let Some(category) = ¶ms.category {
query_params.insert("category".to_string(), category.join(","));
}
// Add exclude_category parameter to query params
if let Some(exclude_category) = ¶ms.exclude_category {
query_params.insert("excludecategory".to_string(), exclude_category.join(","));
}
// Add language parameter to query params
if let Some(language) = ¶ms.language {
query_params.insert("language".to_string(), language.clone());
}
// Add domain parameter to query params
if let Some(domain) = ¶ms.domain {
query_params.insert("domain".to_string(), domain.clone());
}
// Add exclude_domain parameter to query params
if let Some(exclude_domain) = ¶ms.exclude_domain {
query_params.insert("excludedomain".to_string(), exclude_domain.clone());
}
// Add domain_url parameter to query params
if let Some(domain_url) = ¶ms.domain_url {
query_params.insert("domainurl".to_string(), domain_url.clone());
}
// Add exclude_field parameter to query params
if let Some(exclude_field) = ¶ms.exclude_field {
query_params.insert("excludefield".to_string(), exclude_field.clone());
}
// Add priority_domain parameter to query params
if let Some(priority_domain) = ¶ms.priority_domain {
query_params.insert("prioritydomain".to_string(), priority_domain.clone());
}
// Add timezone parameter to query params
if let Some(timezone) = ¶ms.timezone {
query_params.insert("timezone".to_string(), timezone.clone());
}
// Add full_content parameter to query params
if let Some(full_content) = ¶ms.full_content {
println!("full_content: {}", full_content.value().to_string());
query_params.insert("full_content".to_string(), full_content.value().to_string());
}
// Add image parameter to query params
if let Some(image) = ¶ms.image {
query_params.insert("image".to_string(), image.value().to_string());
}
// Add video parameter to query params
if let Some(video) = ¶ms.video {
query_params.insert("video".to_string(), video.value().to_string());
}
// Add size parameter to query params
if let Some(size) = ¶ms.size {
query_params.insert("size".to_string(), size.to_string());
}
// Add page parameter to query params
if let Some(page) = ¶ms.page {
query_params.insert("page".to_string(), page.to_string());
}
// Make the GET request to the archive endpoint
self.get("archive", Some(query_params))
}
}
/// Enum for representing boolean values as strings.
#[derive(Debug)]
pub enum Flag {
/// False value.
False,
/// True value.
True,
}
impl Flag {
/// Returns the string representation of the flag.
fn value(&self) -> &str {
match self {
Flag::True => "1",
Flag::False => "0",
}
}
}
/// Parameters for the `get_news_archive` method.
#[derive(Debug, Default)]
pub struct GetNewsArchiveParams {
/// Unique identifier of the news article.\
/// Max no. of id could be added: 50
pub id: Option<Vec<String>>,
/// Start date for the news articles.\
/// If not specified, the api will fetch the data from the past 1 years if you have professional subscription.\
/// Format: YYYY-MM-DD
pub from_date: Option<String>,
/// End date for the news articles.\
/// If not specified, it will be today\
/// Format: YYYY-MM-DD
pub to_date: Option<String>,
/// Keywords to search for in the news articles.\
/// Max characters: 512.\
/// Exclusive with q_in_title and q_in_meta
pub q: Option<String>,
/// Keywords to search for in the title of the news articles.\
/// Max characters: 512.\
/// Exclusive with q and q_in_meta
pub q_in_title: Option<String>,
/// Keywords to search for in the meta description of the news articles.\
/// Max characters: 512.\
/// Exclusive with q and q_in_title
pub q_in_meta: Option<String>,
/// Country code for the news articles.\
/// Max no. of country could be added: 5.\
/// Examples: "hk", "us", "wo"
pub country: Option<Vec<String>>,
/// Category for the news articles.\
/// Max no. of category could be added: 5.\
/// Exclusive with exclude_category.\
/// Possible values: "business", "crime", "domestic", "education", "entertainment", "environment", "food", "health", "lifestyle", "other", "politics", "science", "sports", "technology", "top", "tourism", "world"
pub category: Option<Vec<String>>,
/// Category to exclude from the results.\
/// Max no. of exclude_category could be added: 5.\
/// Exclusive with category.\
/// Possible values: "business", "crime", "domestic", "education", "entertainment", "environment", "food", "health", "lifestyle", "other", "politics", "science", "sports", "technology", "top", "tourism", "world"
pub exclude_category: Option<Vec<String>>,
/// Language code for the news articles.\
/// Max no. of language could be added: 5.
pub language: Option<String>,
/// Domain for the news articles.\
/// Max no. of domain could be added: 5.\
/// Possible values in [here](https://newsdata.io/documentation/#latest-news)
pub domain: Option<String>,
/// Domain to exclude from the results.\
/// Max no. of domain could be added: 5.\
/// Possible values in [here](https://newsdata.io/documentation/#latest-news)
pub exclude_domain: Option<String>,
/// Domain URL for the news articles.\
/// Max no. of domain could be added: 5.
pub domain_url: Option<String>,
/// Field to exclude from the results.\
/// "article_id" is not excludable in response
pub exclude_field: Option<String>,
/// Priority domain for the news articles.\
/// Top: Fetches news articles from the top 10% of the news domains\
/// Medium: Fetches news articles from the top 30% of the news domains. It means it already includes all the news articles of "top" priority.\
/// Low: Fetches news articles from the top 50% of the news domains. It means it already includes all the news articles of "top" and "medium" priorities.
pub priority_domain: Option<String>,
/// Timezone for the news articles.
pub timezone: Option<String>,
/// Whether to include full content in the results.
pub full_content: Option<Flag>,
/// Whether to include images in the results.
pub image: Option<Flag>,
/// Whether to include videos in the results.
pub video: Option<Flag>,
/// Number of results to return.\
/// Could only be 1 to 50
pub size: Option<i32>,
/// page parameter from last result\
/// [Detail](https://newsdata.io/documentation/#pagination)
pub page: Option<String>,
}
impl GetNewsArchiveParams {
/// Creates a new `GetNewsArchiveParams` with default values.
///
/// This method sets the default values for all parameters, which are:
///
/// * `id`: `None`
/// * `q`: `None`
/// * `q_in_title`: `None`
/// * `q_in_meta`: `None`
/// * `country`: `None`
/// * `category`: `None`
/// * `exclude_category`: `None`
/// * `language`: `None`
/// * `domain`: `None`
/// * `exclude_domain`: `None`
/// * `domain_url`: `None`
/// * `exclude_field`: `None`
/// * `priority_domain`: `None`
/// * `timezone`: `None`
/// * `full_content`: `None`
/// * `image`: `None`
/// * `video`: `None`
/// * `size`: `None`
/// * `page`: `None`
/// * `from_date`: `None`
/// * `to_date`: `None`
///
/// This allows you to easily create a `GetNewsArchiveParams` object without having to specify all the parameters manually.
pub fn default() -> Self {
GetNewsArchiveParams {
id: None,
q: None,
q_in_title: None,
q_in_meta: None,
country: None,
category: None,
exclude_category: None,
language: None,
domain: None,
exclude_domain: None,
domain_url: None,
exclude_field: None,
priority_domain: None,
timezone: None,
full_content: None,
image: None,
video: None,
size: None,
page: None,
from_date: None,
to_date: None,
}
}
}