Skip to content

Commit

Permalink
export methods csv + json
Browse files Browse the repository at this point in the history
  • Loading branch information
altunenes committed Aug 27, 2023
1 parent bec1f92 commit 01f19a8
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions examples/exporter.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
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(())
}
47 changes: 47 additions & 0 deletions src/exporter.rs
Original file line number Diff line number Diff line change
@@ -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<String>` 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<String>, 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<String>` 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<String>, 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(())
}


3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 01f19a8

Please sign in to comment.