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
use std::collections::HashMap;
use crate::{
newsdata_io::{NewsdataIO, Requests},
ApiResult, Json,
};
/// Trait for the Latest News API.
pub trait LatestNews {
/// Get the latest news articles.
///
/// # Arguments
///
/// * `params`: The parameters for the request.
///
/// # Returns
///
/// An `ApiResult` containing the JSON response from the API.
fn get_latest(&self, params: &GetLatestNewsParams) -> ApiResult<Json>;
}
impl LatestNews for NewsdataIO {
fn get_latest(&self, params: &GetLatestNewsParams) -> ApiResult<Json> {
let mut query_params = HashMap::new();
if let Some(id) = ¶ms.id {
query_params.insert("id".to_string(), id.join(","));
}
if let Some(q) = ¶ms.q {
query_params.insert("q".to_string(), q.clone());
}
if let Some(q_in_title) = ¶ms.q_in_title {
query_params.insert("qInTitle".to_string(), q_in_title.clone());
}
if let Some(q_in_meta) = ¶ms.q_in_meta {
query_params.insert("qInMeta".to_string(), q_in_meta.clone());
}
if let Some(timeframe) = ¶ms.timeframe {
query_params.insert("timeframe".to_string(), timeframe.clone());
}
if let Some(country) = ¶ms.country {
query_params.insert("country".to_string(), country.join(","));
}
if let Some(category) = ¶ms.category {
query_params.insert("category".to_string(), category.join(","));
}
if let Some(exclude_category) = ¶ms.exclude_category {
query_params.insert("excludecategory".to_string(), exclude_category.join(","));
}
if let Some(language) = ¶ms.language {
query_params.insert("language".to_string(), language.clone());
}
if let Some(tag) = ¶ms.tag {
query_params.insert("tag".to_string(), tag.clone());
}
if let Some(sentiment) = ¶ms.sentiment {
query_params.insert("sentiment".to_string(), sentiment.clone());
}
if let Some(region) = ¶ms.region {
query_params.insert("region".to_string(), region.clone());
}
if let Some(domain) = ¶ms.domain {
query_params.insert("domain".to_string(), domain.clone());
}
if let Some(exclude_domain) = ¶ms.exclude_domain {
query_params.insert("excludedomain".to_string(), exclude_domain.clone());
}
if let Some(domain_url) = ¶ms.domain_url {
query_params.insert("domainurl".to_string(), domain_url.clone());
}
if let Some(exclude_field) = ¶ms.exclude_field {
query_params.insert("excludefield".to_string(), exclude_field.clone());
}
if let Some(priority_domain) = ¶ms.priority_domain {
query_params.insert("prioritydomain".to_string(), priority_domain.clone());
}
if let Some(timezone) = ¶ms.timezone {
query_params.insert("timezone".to_string(), timezone.clone());
}
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());
}
if let Some(image) = ¶ms.image {
query_params.insert("image".to_string(), image.value().to_string());
}
if let Some(video) = ¶ms.video {
query_params.insert("video".to_string(), video.value().to_string());
}
if let Some(size) = ¶ms.size {
query_params.insert("size".to_string(), size.to_string());
}
if let Some(page) = ¶ms.page {
query_params.insert("page".to_string(), page.to_string());
}
self.get("latest", 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_latest` method.
#[derive(Debug, Default)]
pub struct GetLatestNewsParams {
/// Unique identifier of the news article.\
/// Max no. of id could be added: 50
pub id: Option<Vec<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>,
/// Timeframe for the news articles.\
/// Only hours or minutes is permitted.\
/// Examples: 6 for 6 hours, 15m for 15 min
pub timeframe: 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>,
/// Tag for the news articles.\
/// Max no. of tag could be added: 5.\
/// **Available only for Professional and Corporate users**\
/// Possible values in [here](https://newsdata.io/documentation/#latest-news)
pub tag: Option<String>,
/// Sentiment for the news articles.\
/// Possible values: "positive", "negative", "neutral".\
/// **Available only for Professional and Corporate users**
pub sentiment: Option<String>,
/// Region for the news articles.\
/// Max no. of region could be added: 5.\
/// **Available only for Corporate users**
pub region: 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 GetLatestNewsParams {
/// Creates a new `GetLatestNewsParams` 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`
/// * `timeframe`: `None`
/// * `country`: `None`
/// * `category`: `None`
/// * `exclude_category`: `None`
/// * `language`: `None`
/// * `tag`: `None`
/// * `sentiment`: `None`
/// * `region`: `None`
/// * `domain`: `None`
/// * `exclude_domain`: `None`
/// * `domain_url`: `None`
/// * `exclude_field`: `None`
/// * `priority_domain`: `None`
/// * `timezone`: `None`
/// * `full_content`: `Some(Flag::False)`
/// * `image`: `Some(Flag::False)`
/// * `video`: `Some(Flag::False)`
/// * `size`: `None`
/// * `page`: `None`
///
/// This allows you to easily create a `GetLatestNewsParams` object without having to specify all the parameters manually.
pub fn default() -> Self {
GetLatestNewsParams {
id: None,
q: None,
q_in_title: None,
q_in_meta: None,
timeframe: None,
country: None,
category: None,
exclude_category: None,
language: None,
tag: None,
sentiment: None,
region: 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,
}
}
}