-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |