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
use super::{
item,
util::{retry_future, Retryable},
Item,
};
use futures::{Stream, TryStreamExt};
use reqwest::Client;
use std::io::{BufReader, Read};
use std::time::Duration;
use thiserror::Error;
use tryhard::RetryPolicy;
#[derive(Error, Debug)]
pub enum Error {
#[error("Item parsing error: {0}")]
ItemParsingError(#[from] item::Error),
#[error("HTTP client error: {0}")]
HttpClientError(#[from] reqwest::Error),
#[error("JSON decoding error: {0}")]
JsonError(#[from] serde_json::Error),
#[error("Blocked query: {0}")]
BlockedQuery(String),
}
impl Retryable for Error {
fn max_retries() -> u32 {
7
}
fn log_level() -> Option<log::Level> {
Some(log::Level::Warn)
}
fn default_initial_delay() -> Duration {
Duration::from_millis(250)
}
fn custom_retry_policy(&self) -> Option<RetryPolicy> {
match self {
Error::HttpClientError(_) => Some(RetryPolicy::Delay(Duration::from_secs(30))),
_ => Some(RetryPolicy::Break),
}
}
}
pub struct IndexClient {
base: String,
underlying: Client,
}
impl IndexClient {
const TCP_KEEPALIVE_SECS: u64 = 20;
const DEFAULT_CDX_BASE: &'static str = "http://web.archive.org/cdx/search/cdx";
const CDX_OPTIONS: &'static str =
"&output=json&fl=original,timestamp,digest,mimetype,length,statuscode";
const BLOCKED_SITE_ERROR_MESSAGE: &'static str =
"org.archive.util.io.RuntimeIOException: org.archive.wayback.exception.AdministrativeAccessControlException: Blocked Site Error\n";
pub fn new(base: String) -> Self {
Self {
base,
underlying: Client::builder()
.tcp_keepalive(Some(Duration::from_secs(Self::TCP_KEEPALIVE_SECS)))
.build()
.unwrap(),
}
}
fn decode_rows(rows: Vec<Vec<String>>) -> Result<Vec<Item>, Error> {
rows.into_iter()
.skip(1)
.map(|row| {
Item::parse_optional_record(
row.get(0).map(|v| v.as_str()),
row.get(1).map(|v| v.as_str()),
row.get(2).map(|v| v.as_str()),
row.get(3).map(|v| v.as_str()),
row.get(4).map(|v| v.as_str()),
row.get(5).map(|v| v.as_str()),
)
.map_err(From::from)
})
.collect()
}
pub fn load_json<R: Read>(reader: R) -> Result<Vec<Item>, Error> {
let buffered = BufReader::new(reader);
let rows = serde_json::from_reader::<BufReader<R>, Vec<Vec<String>>>(buffered)?;
Self::decode_rows(rows)
}
pub fn stream_search<'a>(
&'a self,
query: &'a str,
limit: usize,
) -> impl Stream<Item = Result<Item, Error>> + 'a {
futures::stream::try_unfold(Some(None), move |resume_key| async move {
let next = match resume_key {
Some(key) => {
let (items, resume_key) =
retry_future(|| self.search_with_resume_key(query, limit, &key)).await?;
log::warn!("{:?}", resume_key);
Some((items, resume_key.map(Some)))
}
None => None,
};
let result: Result<_, Error> = Ok(next);
result
})
.map_ok(|items| futures::stream::iter(items.into_iter().map(Ok)))
.try_flatten()
}
async fn search_with_resume_key(
&self,
query: &str,
limit: usize,
resume_key: &Option<String>,
) -> Result<(Vec<Item>, Option<String>), Error> {
let resume_key_param = resume_key
.as_ref()
.map(|key| format!("&resumeKey={}", key))
.unwrap_or_default();
let query_url = format!(
"{}?url={}{}&limit={}&showResumeKey=true{}",
self.base,
query,
resume_key_param,
limit,
Self::CDX_OPTIONS
);
log::warn!("{}", query_url);
let contents = self.underlying.get(&query_url).send().await?.text().await?;
if contents == Self::BLOCKED_SITE_ERROR_MESSAGE {
Err(Error::BlockedQuery(query.to_string()))
} else {
let mut rows = serde_json::from_str::<Vec<Vec<String>>>(&contents)?;
let len = rows.len();
let next_resume_key = if rows[len - 2].is_empty() {
let mut last = rows.remove(len - 1);
rows.remove(len - 2);
Some(last.remove(0))
} else {
None
};
log::warn!("received {}", rows.len());
Self::decode_rows(rows).map(|items| (items, next_resume_key))
}
}
pub async fn search(
&self,
query: &str,
timestamp: Option<&str>,
digest: Option<&str>,
) -> Result<Vec<Item>, Error> {
let mut filter = String::new();
if let Some(value) = timestamp {
filter.push_str(&format!("&filter=timestamp:{}", value));
}
if let Some(value) = digest {
filter.push_str(&format!("&filter=digest:{}", value));
}
let query_url = format!("{}?url={}{}{}", self.base, query, filter, Self::CDX_OPTIONS);
let contents = self.underlying.get(&query_url).send().await?.text().await?;
if contents == Self::BLOCKED_SITE_ERROR_MESSAGE {
Err(Error::BlockedQuery(query.to_string()))
} else {
let rows = serde_json::from_str(&contents)?;
Self::decode_rows(rows)
}
}
}
impl Default for IndexClient {
fn default() -> Self {
Self::new(Self::DEFAULT_CDX_BASE.to_string())
}
}
#[cfg(test)]
mod tests {
use super::IndexClient;
use std::fs::File;
#[test]
fn load_json() {
let file = File::open("examples/wayback/cdx-result.json").unwrap();
let result = IndexClient::load_json(file).unwrap();
assert_eq!(result.len(), 37);
}
}