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
use anyhow::Result;
use bootstrap::Bootstrapper;
use governor::{Quota, RateLimiter};
use nonzero_ext::nonzero;
use parser::ParseResult;
use reqwest::Client;
use spyglass_lens::LensConfig;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::{
    atomic::{AtomicUsize, Ordering},
    Arc,
};
use url::Url;

pub mod archive;
pub mod bootstrap;
mod cache;
mod cdx;
mod crawler;
pub mod parser;
pub mod s3;
pub mod site;
pub mod validator;

use crate::crawler::{handle_crawl, http_client};
use archive::{create_archives, ArchiveFiles, ArchiveRecord};

static APP_USER_AGENT: &str = concat!("netrunner", "/", env!("CARGO_PKG_VERSION"));

pub struct CrawlOpts {
    pub create_warc: bool,
    pub requests_per_second: u32,
}

impl Default for CrawlOpts {
    fn default() -> Self {
        Self {
            create_warc: false,
            requests_per_second: 2,
        }
    }
}

pub fn cache_storage_path(lens: &LensConfig) -> PathBuf {
    let storage = Path::new("archives").join(&lens.name);
    if !storage.exists() {
        // No point in continuing if we're unable to create this directory
        std::fs::create_dir_all(storage.clone()).expect("Unable to create crawl folder");
    }

    storage
}

pub fn tmp_storage_path(lens: &LensConfig) -> PathBuf {
    let storage = Path::new("tmp").join(&lens.name);
    if !storage.exists() {
        // No point in continuing if we're unable to create this directory
        std::fs::create_dir_all(storage.clone()).expect("Unable to create crawl folder");
    }

    storage
}

#[derive(Clone)]
pub struct NetrunnerState {
    pub has_urls: bool,
}

#[derive(Clone)]
pub struct Netrunner {
    bootstrapper: Bootstrapper,
    client: Client,
    lens: LensConfig,
    // Where the cached web archive will be storage
    pub storage: PathBuf,
    pub state: NetrunnerState,
}

impl Netrunner {
    pub fn new(lens: LensConfig) -> Self {
        let storage = cache_storage_path(&lens);
        let state = NetrunnerState {
            has_urls: storage.join("urls.txt").exists(),
        };
        let client = http_client();

        Netrunner {
            bootstrapper: Bootstrapper::new(&client),
            client,
            lens,
            storage,
            state,
        }
    }

    pub fn url_txt_path(&self) -> PathBuf {
        self.storage.join("urls.txt")
    }

    pub async fn get_urls(&mut self) -> Vec<String> {
        match self.bootstrapper.find_urls(&self.lens).await {
            Ok(urls) => urls,
            Err(err) => {
                log::error!("Unable to get_urls: {err}");
                Vec::new()
            }
        }
    }

    /// Kick off a crawl for URLs represented by <lens>.
    pub async fn crawl(&mut self, opts: CrawlOpts) -> Result<Option<ArchiveFiles>> {
        let crawl_queue = if !self.state.has_urls {
            self.bootstrapper.find_urls(&self.lens).await?
        } else {
            log::info!("Already collected URLs, skipping");
            // Load urls from file
            let file = std::fs::read_to_string(self.url_txt_path())?;
            file.lines().map(|x| x.to_string()).collect::<Vec<String>>()
        };

        if opts.create_warc {
            // CRAWL BABY CRAWL
            // Default to max 2 requests per second for a domain.
            let quota = Quota::per_second(nonzero!(2u32));
            let tmp_storage = tmp_storage_path(&self.lens);
            self.crawl_loop(&crawl_queue, &tmp_storage, quota).await?;
            let archives =
                create_archives(&self.storage, &self.cached_records(&tmp_storage)).await?;
            return Ok(Some(archives));
        }

        Ok(None)
    }

    pub async fn crawl_url(
        &mut self,
        url: String,
    ) -> Result<Vec<(ArchiveRecord, Option<ParseResult>)>> {
        let quota = Quota::per_second(nonzero!(2u32));
        let tmp_storage = tmp_storage_path(&self.lens);
        self.crawl_loop(&[url], &tmp_storage, quota).await?;
        let archived = self.cached_records(&tmp_storage);

        let mut records = Vec::new();
        for (_, path) in archived {
            if let Ok(Ok(rec)) =
                std::fs::read_to_string(path).map(|s| ron::from_str::<ArchiveRecord>(&s))
            {
                if rec.status >= 200 && rec.status <= 299 {
                    let parsed = crate::parser::html::html_to_text(&rec.url, &rec.content);
                    records.push((rec, Some(parsed)));
                } else {
                    records.push((rec, None));
                }
            }
        }

        Ok(records)
    }

    pub fn clear_cache(&self) -> Result<(), std::io::Error> {
        std::fs::remove_dir_all(tmp_storage_path(&self.lens))
    }

    fn cached_records(&self, tmp_storage: &PathBuf) -> Vec<(String, PathBuf)> {
        let paths = std::fs::read_dir(tmp_storage).expect("unable to read tmp storage dir");

        let mut existing = HashSet::new();
        let mut to_remove = Vec::new();
        let mut recs = Vec::new();
        for path in paths.flatten() {
            match std::fs::read_to_string(path.path()) {
                Ok(contents) => {
                    if let Ok(record) = ron::from_str::<ArchiveRecord>(&contents) {
                        let url = record.url;
                        if existing.contains(&url) {
                            to_remove.push(path.path());
                        } else {
                            existing.insert(url.clone());
                            recs.push((url, path.path()));
                        }
                    }
                }
                Err(_) => {
                    let _ = std::fs::remove_file(path.path());
                }
            }
        }

        log::info!("Removing {} existing caches", to_remove.len());
        for path in to_remove {
            let _ = std::fs::remove_file(path);
        }

        recs
    }

    /// Web Archive (WARC) file format definition: https://iipc.github.io/warc-specifications/specifications/warc-format/warc-1.1
    async fn crawl_loop(
        &mut self,
        crawl_queue: &[String],
        tmp_storage: &PathBuf,
        quota: Quota,
    ) -> anyhow::Result<()> {
        let lim = Arc::new(RateLimiter::<String, _, _>::keyed(quota));
        let progress = Arc::new(AtomicUsize::new(0));
        let total = crawl_queue.len();
        let mut already_crawled: HashSet<String> = HashSet::new();

        // Before we begin, check to see if we've already crawled anything
        let recs = self.cached_records(tmp_storage);
        log::debug!("found {} crawls in cache", recs.len());
        for (url, _) in recs {
            already_crawled.insert(url);
        }

        log::info!(
            "beginning crawl, already crawled {} urls",
            already_crawled.len()
        );
        progress.store(already_crawled.len(), Ordering::SeqCst);

        // Spin up tasks to crawl through everything
        for url in crawl_queue.iter().filter_map(|url| Url::parse(url).ok()) {
            if already_crawled.contains(&url.to_string()) {
                log::info!("-> skipping {}, already crawled", url);
                continue;
            }

            let progress = progress.clone();
            handle_crawl(&self.client, tmp_storage.clone(), lim.clone(), &url).await;
            let old_val = progress.fetch_add(1, Ordering::SeqCst);
            if old_val % 100 == 0 {
                log::info!("progress: {} / {}", old_val, total)
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod test {
    use spyglass_lens::LensConfig;
    use std::io;
    use std::path::Path;
    use tracing_log::LogTracer;
    use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter};

    use crate::{parser::ParseResult, validator::validate_lens, CrawlOpts, Netrunner};

    #[tokio::test]
    #[ignore]
    async fn test_crawl() {
        // Setup some nice console logging for tests
        let subscriber = tracing_subscriber::registry()
            .with(
                EnvFilter::from_default_env()
                    .add_directive(tracing::Level::INFO.into())
                    .add_directive("libnetrunner=TRACE".parse().expect("invalid log filter")),
            )
            .with(fmt::Layer::new().with_ansi(false).with_writer(io::stdout));
        tracing::subscriber::set_global_default(subscriber)
            .expect("Unable to set a global subscriber");
        LogTracer::init().expect("Unable to initialize logger");

        let lens_file = "fixtures/test.ron";
        let lens = LensConfig::from_path(Path::new(&lens_file).to_path_buf())
            .expect("Unable to load lens file");

        // Test crawling logic
        let mut netrunner = Netrunner::new(lens.clone());
        let archives = netrunner
            .crawl(CrawlOpts {
                create_warc: true,
                ..Default::default()
            })
            .await
            .expect("Unable to crawl");

        // Validate archives created are readable.
        if let Some(archives) = archives {
            assert!(archives.warc.exists());
            assert!(archives.parsed.exists());

            let reader =
                ParseResult::iter_from_gz(&archives.parsed).expect("Unable to read parsed archive");

            assert_eq!(reader.count(), 1);
        }

        // Test validation logic
        if let Err(err) = validate_lens(&lens) {
            eprintln!("Failed validation: {err}");
            panic!("Failed");
        }
    }
}