adjusted the metadata parsing and started the initial work for parsing
other file formats.
This commit is contained in:
+3
-1
@@ -4,8 +4,10 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
calamine = "0.36.0"
|
||||||
clap = { version = "4.6.1", features = ["derive"] }
|
clap = { version = "4.6.1", features = ["derive"] }
|
||||||
extractous = "0.3.0"
|
docx-rust = "0.1.11"
|
||||||
|
lopdf = "0.44.0"
|
||||||
rayon = "1.12.0"
|
rayon = "1.12.0"
|
||||||
reqwest = { version = "0.13.3", features = ["blocking"] }
|
reqwest = { version = "0.13.3", features = ["blocking"] }
|
||||||
serpapi = "1.1.0"
|
serpapi = "1.1.0"
|
||||||
|
|||||||
+37
-12
@@ -1,13 +1,18 @@
|
|||||||
|
use calamine::{Reader, Xlsx};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use extractous::Extractor;
|
use docx_rust::DocxFile;
|
||||||
|
use lopdf::{Document, Object};
|
||||||
use rayon::iter::IntoParallelRefMutIterator;
|
use rayon::iter::IntoParallelRefMutIterator;
|
||||||
use rayon::{ThreadPoolBuilder, iter::ParallelIterator};
|
use rayon::{ThreadPoolBuilder, iter::ParallelIterator};
|
||||||
use reqwest::blocking::get;
|
use reqwest::blocking::get;
|
||||||
use serpapi::serpapi::Client;
|
use serpapi::serpapi::Client;
|
||||||
use std::fs::File;
|
use std::error::Error;
|
||||||
|
use std::fs::{File, Metadata};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
use std::process::exit;
|
||||||
|
use std::result;
|
||||||
use std::{collections::HashMap, fs::create_dir_all, path::PathBuf};
|
use std::{collections::HashMap, fs::create_dir_all, path::PathBuf};
|
||||||
use tokio;
|
use tokio::{self, fs};
|
||||||
use urlencoding::decode;
|
use urlencoding::decode;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
@@ -46,6 +51,12 @@ struct Args {
|
|||||||
markdown: Option<PathBuf>,
|
markdown: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum FileType {
|
||||||
|
Pdf,
|
||||||
|
Docx,
|
||||||
|
Xlsx,
|
||||||
|
}
|
||||||
|
|
||||||
struct FoundFile {
|
struct FoundFile {
|
||||||
filename: String,
|
filename: String,
|
||||||
url: String,
|
url: String,
|
||||||
@@ -53,6 +64,7 @@ struct FoundFile {
|
|||||||
downloaded: Option<bool>,
|
downloaded: Option<bool>,
|
||||||
parsed: Option<bool>,
|
parsed: Option<bool>,
|
||||||
metadata: HashMap<String, String>,
|
metadata: HashMap<String, String>,
|
||||||
|
filetype: FileType,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FoundFile {
|
impl FoundFile {
|
||||||
@@ -76,20 +88,29 @@ impl FoundFile {
|
|||||||
self.downloaded = Some(false);
|
self.downloaded = Some(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(&mut self) {
|
fn parse(&mut self) -> Result<(), Box<dyn Error>> {
|
||||||
println!("parsing {}", self.filename);
|
println!("parsing {}", self.filename);
|
||||||
let extractor = Extractor::new();
|
match self.filetype {
|
||||||
if let Ok((_content, metadata)) =
|
FileType::Pdf => {
|
||||||
extractor.extract_file(&self.filepath.display().to_string())
|
let doc = Document::load(&self.filepath)?;
|
||||||
{
|
if let Ok(info) = doc.trailer.get_deref("Info".as_bytes(), &doc) {
|
||||||
if !metadata.is_empty() {
|
if let Ok(data) = info.as_dict() {
|
||||||
for (key, value) in metadata {
|
data.as_hashmap().iter().for_each(|(key, value)| {
|
||||||
self.metadata.insert(key, value.join(", "));
|
let key_string = String::from_utf8_lossy(&key).to_string();
|
||||||
|
if let Ok(value_vec) = value.as_str() {
|
||||||
|
let value_string = String::from_utf8_lossy(&value_vec).to_string();
|
||||||
|
self.metadata.insert(key_string, value_string);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
FileType::Docx => {}
|
||||||
|
FileType::Xlsx => {}
|
||||||
}
|
}
|
||||||
self.parsed = Some(true);
|
self.parsed = Some(true);
|
||||||
println!("{} parsed!", self.filename);
|
println!("{} parsed!", self.filename);
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,6 +174,7 @@ async fn main() {
|
|||||||
downloaded: None,
|
downloaded: None,
|
||||||
parsed: None,
|
parsed: None,
|
||||||
metadata: HashMap::new(),
|
metadata: HashMap::new(),
|
||||||
|
filetype: FileType::Pdf,
|
||||||
};
|
};
|
||||||
files.push(new_file);
|
files.push(new_file);
|
||||||
}
|
}
|
||||||
@@ -167,7 +189,10 @@ async fn main() {
|
|||||||
files.par_iter_mut().for_each(|file| {
|
files.par_iter_mut().for_each(|file| {
|
||||||
file.download();
|
file.download();
|
||||||
if file.downloaded == Some(true) {
|
if file.downloaded == Some(true) {
|
||||||
file.parse();
|
match file.parse() {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => eprintln!("error parsing {}: {e}", file.filename),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for file in &files {
|
for file in &files {
|
||||||
|
|||||||
Reference in New Issue
Block a user