Compare commits
	
		
			7 Commits
		
	
	
		
			0fd76b1734
			...
			5850f19cab
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 5850f19cab | |||
| 2c8546e30a | |||
| 4e619d0ebc | |||
| 647c4cd324 | |||
| 7fab961d76 | |||
| d3fff194f4 | |||
| 3497312fd4 | 
@@ -43,7 +43,6 @@ impl Website {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn set_crawled(&mut self) {
 | 
			
		||||
        trace!("Set crawled to true");
 | 
			
		||||
        self.crawled = true
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,30 +1,37 @@
 | 
			
		||||
use std::{ffi::OsStr, io::ErrorKind, path::PathBuf};
 | 
			
		||||
use std::{io::ErrorKind, path::PathBuf};
 | 
			
		||||
 | 
			
		||||
use reqwest::header::HeaderValue;
 | 
			
		||||
use tokio::fs;
 | 
			
		||||
use tracing::{error, trace};
 | 
			
		||||
use tracing::{error, trace, warn};
 | 
			
		||||
use url::Url;
 | 
			
		||||
 | 
			
		||||
pub fn as_path(url: &Url) -> PathBuf {
 | 
			
		||||
pub fn as_path(url: &Url, content_type: &HeaderValue) -> PathBuf {
 | 
			
		||||
    // extract data from url to save it accurately
 | 
			
		||||
    let url_path = PathBuf::from("./downloaded/".to_string() + url.domain().unwrap_or("UnknownDomain") + url.path());
 | 
			
		||||
    let mut url_path = PathBuf::from("./downloaded/".to_string() + url.domain().unwrap_or("UnknownDomain") + url.path());
 | 
			
		||||
 | 
			
		||||
    // if it's a file
 | 
			
		||||
    let (basepath, filename) = if url_path.extension().filter(valid_file_extension).is_some() {
 | 
			
		||||
        // get everything up till the file
 | 
			
		||||
        let basepath = url_path.ancestors().skip(1).take(1).collect::<PathBuf>();
 | 
			
		||||
        // get the file name
 | 
			
		||||
        let filename = url_path.file_name().expect("This should exist").to_string_lossy();
 | 
			
		||||
        trace!("Save path: {:?} and base path: {:?}", &url_path, &basepath);
 | 
			
		||||
        (basepath, filename.to_string())
 | 
			
		||||
    if let Ok(header) = content_type.to_str() {
 | 
			
		||||
        // text/html; charset=UTF-8; option=value
 | 
			
		||||
        let ttype = if let Some((t, _)) = header.split_once(';') {
 | 
			
		||||
            t
 | 
			
		||||
        } else {
 | 
			
		||||
        (url_path.clone(), "index.html".into())
 | 
			
		||||
            header
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
    let mut path = PathBuf::new();
 | 
			
		||||
    path = path.join(basepath);
 | 
			
		||||
    path = path.join(filename);
 | 
			
		||||
        if let Some((ttype, subtype)) = ttype.split_once('/') {
 | 
			
		||||
            trace!("Found Content-Type to be: {ttype}/{subtype} for {}", url.to_string());
 | 
			
		||||
            // If the Content-Type header is "*/html" (most likely "text/html") and the path's
 | 
			
		||||
            // extension is anything but html:
 | 
			
		||||
            if subtype=="html" && !url_path.extension().is_some_and(|f| f=="html" || f=="htm" ) {
 | 
			
		||||
                // time to slap a index.html to the end of that path there!
 | 
			
		||||
                url_path = url_path.join("index.html");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        warn!("Header: {:?} couldn't be parsed into a string!", content_type);
 | 
			
		||||
    }
 | 
			
		||||
    trace!("Final path for {} is: {:?}", url, url_path);
 | 
			
		||||
 | 
			
		||||
    path
 | 
			
		||||
    url_path
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn init(filename: &PathBuf) -> Option<fs::File> {
 | 
			
		||||
@@ -50,29 +57,10 @@ pub async fn init(filename: &PathBuf) -> Option<fs::File> {
 | 
			
		||||
                    error!("Couldn't get file's parents: {:?}", &filename);
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                error!("File creation: {err} {:?}", filename);
 | 
			
		||||
                error!("File open error: {err} {:?}", filename);
 | 
			
		||||
            }
 | 
			
		||||
            // we don't care about other errors, we can't/shouldn't fix them
 | 
			
		||||
            None
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn valid_file_extension(take: &&OsStr) -> bool {
 | 
			
		||||
    let los = take.to_string_lossy();
 | 
			
		||||
    let all = los.split('.');
 | 
			
		||||
    match all.last() {
 | 
			
		||||
        Some(s) => {
 | 
			
		||||
            // FIXME it's worth noting that the dumb tlds like .zip are in here,
 | 
			
		||||
            // which could cause problems
 | 
			
		||||
            let all_domains = include_str!("tlds-alpha-by-domain.txt");
 | 
			
		||||
 | 
			
		||||
            // check if it is a domain
 | 
			
		||||
            match all_domains.lines().map(str::to_lowercase).find(|x| x==s.to_lowercase().as_str()) {
 | 
			
		||||
                Some(_) => false,
 | 
			
		||||
                None => true
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
        None => false,
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										33
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -19,8 +19,8 @@ use metrics::{counter, gauge};
 | 
			
		||||
use metrics_exporter_prometheus::PrometheusBuilder;
 | 
			
		||||
use serde::Deserialize;
 | 
			
		||||
use surrealdb::{engine::remote::ws::Client, Surreal};
 | 
			
		||||
use tokio::{io::AsyncWriteExt, task::JoinSet};
 | 
			
		||||
use tracing::{debug, error, info, instrument, level_filters::LevelFilter, trace, trace_span};
 | 
			
		||||
use tokio::{io::{AsyncWriteExt, BufWriter}, task::JoinSet};
 | 
			
		||||
use tracing::{debug, error, info, instrument, level_filters::LevelFilter, trace, trace_span, warn};
 | 
			
		||||
use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter, Layer, Registry};
 | 
			
		||||
 | 
			
		||||
mod db;
 | 
			
		||||
@@ -174,21 +174,41 @@ async fn process(mut site: Website, db: Surreal<Client>, reqwest: reqwest::Clien
 | 
			
		||||
    // Send the http request (get)
 | 
			
		||||
    if let Ok(response) = request_builder.send().await {
 | 
			
		||||
        // Get body from response
 | 
			
		||||
        let headers = response.headers();
 | 
			
		||||
 | 
			
		||||
        let path = filesystem::as_path(&site.site);
 | 
			
		||||
        #[allow(non_snake_case)]
 | 
			
		||||
        let CT = headers.get("Content-Type");
 | 
			
		||||
        let ct = headers.get("content-type");
 | 
			
		||||
 | 
			
		||||
        if CT.is_none() && ct.is_none() {
 | 
			
		||||
        }
 | 
			
		||||
        let ct = match (CT,ct) {
 | 
			
		||||
            (None, None) => {
 | 
			
		||||
                warn!("Server did not respond with Content-Type header. Url: {} Headers: ({:?})", site.site.to_string(), headers);
 | 
			
		||||
                return
 | 
			
		||||
            },
 | 
			
		||||
            (None, Some(a)) => a,
 | 
			
		||||
            (Some(a), None) => a,
 | 
			
		||||
            (Some(a), Some(_)) => a,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        let path = filesystem::as_path(&site.site, ct);
 | 
			
		||||
 | 
			
		||||
        // make sure that the file is good to go
 | 
			
		||||
        if let Some(mut file) = filesystem::init(&path).await {
 | 
			
		||||
        if let Some(file) = filesystem::init(&path).await {
 | 
			
		||||
            let should_parse = path.to_string_lossy().ends_with(".html");
 | 
			
		||||
            let mut buf: Vec<u8> = Vec::new();
 | 
			
		||||
 | 
			
		||||
            let mut writer = BufWriter::new(file);
 | 
			
		||||
 | 
			
		||||
            // stream the response onto the disk
 | 
			
		||||
            let mut stream = response.bytes_stream();
 | 
			
		||||
 | 
			
		||||
            info!("Writing at: {:?}", path);
 | 
			
		||||
            while let Some(data) = stream.next().await {
 | 
			
		||||
                match data {
 | 
			
		||||
                    Ok(data) => {
 | 
			
		||||
                        debug!("Writing at: {:?}", path);
 | 
			
		||||
                        let _ = file.write_all(&data).await;
 | 
			
		||||
                        let _ = writer.write_all(&data).await;
 | 
			
		||||
                        // If we are going to parse this file later, we will save it
 | 
			
		||||
                        // into memory as well as the disk.
 | 
			
		||||
                        if should_parse {
 | 
			
		||||
@@ -200,6 +220,7 @@ async fn process(mut site: Website, db: Surreal<Client>, reqwest: reqwest::Clien
 | 
			
		||||
                    },
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            let _ = writer.flush();
 | 
			
		||||
 | 
			
		||||
            if should_parse {
 | 
			
		||||
                // Parse document and get relationships
 | 
			
		||||
 
 | 
			
		||||
@@ -39,7 +39,7 @@ impl TokenSink for Website {
 | 
			
		||||
                                    if let Some(mut parsed) = url {
 | 
			
		||||
                                        parsed.set_query(None);
 | 
			
		||||
                                        parsed.set_fragment(None);
 | 
			
		||||
                                        debug!("Final cleaned URL: `{}`", parsed.to_string());
 | 
			
		||||
                                        trace!("Final cleaned URL: `{}`", parsed.to_string());
 | 
			
		||||
                                        let web = Website::new(&parsed.to_string(), false);
 | 
			
		||||
                                        links.push(web);
 | 
			
		||||
                                    }
 | 
			
		||||
@@ -127,16 +127,15 @@ fn try_get_url(parent: &Url, link: &str) -> Option<Url> {
 | 
			
		||||
                        }
 | 
			
		||||
                        let url = origin.clone() + link;
 | 
			
		||||
 | 
			
		||||
                        trace!("Built `{url}` from `{origin} + `{}`", link.to_string());
 | 
			
		||||
 | 
			
		||||
                        if let Ok(url) = Url::parse(&url) {
 | 
			
		||||
                            trace!("Saved relative url `{}` AS: `{}`", link, url);
 | 
			
		||||
                            trace!("Built `{url}` from `{origin} + `{}`", link.to_string());
 | 
			
		||||
                            Some(url)
 | 
			
		||||
                        } else {
 | 
			
		||||
                            error!(
 | 
			
		||||
                                "Failed to reconstruct a url from relative url: `{}` on site: `{}`",
 | 
			
		||||
                                "Failed to reconstruct a url from relative url: `{}` on site: `{}`. Failed url was: {}",
 | 
			
		||||
                                link,
 | 
			
		||||
                                parent.to_string()
 | 
			
		||||
                                parent.to_string(),
 | 
			
		||||
                                url
 | 
			
		||||
                            );
 | 
			
		||||
                            None
 | 
			
		||||
                        }
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user