diff --git a/Cargo.lock b/Cargo.lock index f58d1f2..f29d31c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -152,6 +152,27 @@ dependencies = [ "syn 2.0.29", ] +[[package]] +name = "csv" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "626ae34994d3d8d668f4269922248239db4ae42d538b14c398b74a52208e8086" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -927,6 +948,7 @@ dependencies = [ name = "rustysozluk" version = "0.1.0" dependencies = [ + "csv", "reqwest", "scraper", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 5db31ef..6abdc70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ documentation = "https://docs.rs/rustysozluk/0.1.0/rustysozluk/" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +csv = "1.2.2" reqwest = { version = "0.11.20", features = ["json", "async-compression"] } scraper = "0.17.1" serde_json = "1.0.105" diff --git a/examples/exporter.rs b/examples/exporter.rs new file mode 100644 index 0000000..1b2d694 --- /dev/null +++ b/examples/exporter.rs @@ -0,0 +1,14 @@ +/// This example shows how to export entries to JSON or CSV. +/// Bu örnekte, girdileri JSON veya CSV olarak nasıl dışa aktaracağınız gösterilmiştir. + +use rustysozluk::export_to_json; //import export_csv if you want to export to CSV //CSV'ye aktarmak istiyorsanız export_csv'yi import edin +use rustysozluk::fetch_title; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let entries = fetch_title("https://eksisozluk1923.com/rust-programlama-dili--5575227", 4).await?; + println!("Extracted {} entries:", entries.len()); + export_to_json(entries, "entries.json")?; //export_to_csv(entries, "entries.csv")?; if you want to export to CSV + + Ok(()) +} \ No newline at end of file diff --git a/src/exporter.rs b/src/exporter.rs new file mode 100644 index 0000000..c5d2598 --- /dev/null +++ b/src/exporter.rs @@ -0,0 +1,47 @@ +use csv::Writer; +use std::fs::File; +use std::io::{Error, Write}; +use serde_json; + +/// Export entries to a CSV file. +/// +/// # Arguments +/// +/// * `entries` - A `Vec` containing the entries to be exported. +/// * `file_name` - A `&str` specifying the name of the CSV file. +/// +/// # Returns +/// +/// A `Result` which is either: +/// * `Ok(())` - If the export was successful. +/// * `Err(Error)` - If an IO error occurred during export. +pub fn export_to_csv(entries: Vec, file_name: &str) -> Result<(), Error> { + let file = File::create(file_name)?; + let mut wtr = Writer::from_writer(file); + wtr.write_record(&["Entries"])?; + for entry in entries { + wtr.write_record(&[entry])?; + } + wtr.flush()?; + Ok(()) +} +/// Export entries to a JSON file. +/// +/// # Arguments +/// +/// * `entries` - A `Vec` containing the entries to be exported. +/// * `file_name` - A `&str` specifying the name of the JSON file. +/// +/// # Returns +/// +/// A `Result` which is either: +/// * `Ok(())` - If the export was successful. +/// * `Err(Error)` - If an IO error occurred during export. +pub fn export_to_json(entries: Vec, file_name: &str) -> Result<(), Error> { + let json_string = serde_json::to_string(&entries)?; + let mut file = File::create(file_name)?; + file.write_all(json_string.as_bytes())?; + Ok(()) +} + + diff --git a/src/lib.rs b/src/lib.rs index b57e7b7..e703603 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,7 @@ mod http_client; mod parser; +mod exporter; pub use http_client::fetch_page; pub use parser::{fetch_user, fetch_title}; +pub use exporter::export_to_csv; +pub use exporter::export_to_json;