From 1bf2f5603e057f8a047236a5c0fdaac9f95dc696 Mon Sep 17 00:00:00 2001 From: Sabu Siyad Date: Tue, 14 Apr 2020 17:09:05 +0530 Subject: Fix log file issues --- Cargo.toml | 1 + src/content.rs | 43 +++++++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 73cc860..7a19546 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ regex = "1" url = "1.7.2" clap = "2.32.0" lazy_static = "1.0.2" +dirs = "2.0.2" diff --git a/src/content.rs b/src/content.rs index 90fd936..6a06cc5 100644 --- a/src/content.rs +++ b/src/content.rs @@ -13,11 +13,13 @@ use reqwest::Url; use serde_json::Value; use CONFIGURATION; -use content::url::percent_encoding::{ utf8_percent_encode, DEFAULT_ENCODE_SET }; +use content::url::percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET}; -use std::fs::File; +use std::fs::OpenOptions; use std::io::prelude::*; +extern crate dirs; + pub fn query_url_gen(title: &str) -> Url { let url = Url::parse_with_params( &(CONFIGURATION.wiki_url.clone() + "/w/api.php"), @@ -26,15 +28,24 @@ pub fn query_url_gen(title: &str) -> Url { ("format", "json"), ("prop", "extracts|links"), ("indexpageids", "1"), - ("titles", &utf8_percent_encode(&title.replace(" ", "_"), DEFAULT_ENCODE_SET).to_string()[..]), + ( + "titles", + &utf8_percent_encode(&title.replace(" ", "_"), DEFAULT_ENCODE_SET).to_string()[..], + ), ("redirects", "1"), ("pllimit", "100"), ("explaintext", "1"), ], - ).unwrap(); + ) + .unwrap(); - let mut f = File::open("~/.taizen_logs").unwrap(); - f.write_all(url.as_str().as_bytes()).unwrap(); + let mut f = OpenOptions::new() + .create(true) + .write(true) + .append(true) + .open(dirs::home_dir().unwrap().join(".taizen_logs.txt")) + .unwrap(); + writeln!(f, "{}", title).ok(); return url; } @@ -45,14 +56,22 @@ pub fn search_url_gen(search: &str) -> Url { &[ ("action", "opensearch"), ("format", "json"), - ("search", &utf8_percent_encode(&search, DEFAULT_ENCODE_SET).to_string()[..]), + ( + "search", + &utf8_percent_encode(&search, DEFAULT_ENCODE_SET).to_string()[..], + ), ("limit", "20"), ], - ).unwrap(); - - let mut f = File::create("taizen_logs.txt").unwrap(); - f.write_all(url.as_str().as_bytes()).expect("failed to write unicode"); - f.write_all(search.as_bytes()).unwrap(); + ) + .unwrap(); + + let mut f = OpenOptions::new() + .create(true) + .write(true) + .append(true) + .open(dirs::home_dir().unwrap().join(".taizen_logs.txt")) + .unwrap(); + writeln!(f, "{}", search).ok(); return url; } -- cgit v1.2.3