wayback_rs/util/mod.rs
1use chrono::naive::NaiveDateTime;
2
3mod retries;
4pub use retries::{retry_future, Retryable};
5
6const DATE_FMT: &str = "%Y%m%d%H%M%S";
7
8/// Parse a 14-digit Wayback Machine timestamp into a date-time value.
9pub fn parse_timestamp(input: &str) -> Option<NaiveDateTime> {
10 NaiveDateTime::parse_from_str(input, DATE_FMT).ok()
11}
12
13/// Encode a date-time value as a 14-digit Wayback Machine timestamp.
14pub fn to_timestamp(input: &NaiveDateTime) -> String {
15 input.format(DATE_FMT).to_string()
16}
17
18pub mod redirect {
19 /// Attempt to guess the contents of a redirect page stored by the Wayback
20 /// Machine.
21 ///
22 /// When an item is listed as a 302 redirect in CDX results, the content of
23 /// the page usually (but not always) has the following format, where the
24 /// URL is the value of the location header.
25 pub fn guess_redirect_content(url: &str) -> String {
26 format!(
27 "<html><body>You are being <a href=\"{}\">redirected</a>.</body></html>",
28 url
29 )
30 }
31}