git is the worst thing to ever be invented

This commit is contained in:
SadlyNotSappho 2023-08-18 15:59:58 -07:00
parent 7fff3a4ceb
commit ce1a08975b
2 changed files with 43 additions and 12 deletions

41
src/lib.rs Normal file → Executable file
View File

@ -1,5 +1,6 @@
use std::{fs, path::Path};
use scraper::{Html, Selector};
use serde::{Deserialize, Serialize};
pub fn get_home() -> String {
@ -42,18 +43,46 @@ pub fn read_config(file: &String) -> Config {
serde_json::from_str(&read[..]).expect("couldn't parse json")
}
pub fn ensure_exists(path: &String) {
let replaced = path.replace('~', &get_home()[..]); // replace ~ with $HOME
let p = Path::new(&replaced);
// println!("{}", replaced);
// println!("{:?}", p.exists());
pub fn ensure_exists(path: String) {
let p = Path::new(&path);
if !p.exists() {
fs::create_dir_all(p).expect("couldn't create folder")
}
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
pub struct Config {
pub latest_date: String,
pub cache_folder: String,
}
pub struct Page {
pub date: String,
pub next_page: bool,
pub prev_page: bool,
pub image: String,
}
pub async fn get_page(date: String) {
let page_html = &reqwest::get(format!(
"https://girlgeniusonline.com/comic.php?date={date}"
))
.await
.unwrap()
.text()
.await
.unwrap()[..];
// println!("{page_html}");
let parsed = Html::parse_document(page_html);
let selector = Selector::parse("img[alt=Comic]").unwrap();
let mut url = String::new();
for element in parsed.select(&selector) {
url = element.value().attr("src").unwrap().replace('"', "");
}
println!("{url}");
}
// url: https://girlgeniusonline.com/comic.php?date={date}

14
src/main.rs Normal file → Executable file
View File

@ -7,14 +7,16 @@ struct Args {
config_file: String,
}
fn main() {
#[tokio::main]
async fn main() {
let args = Args::parse();
let config_file = args.config_file;
// println!("{}", config_file);
ggg::create_config(&config_file);
let config = ggg::read_config(&config_file);
ggg::ensure_exists(&config.cache_folder);
println!("{:?}", &config);
ggg::create_config(&config_file);
let config = ggg::read_config(&config_file);
ggg::ensure_exists(config.cache_folder);
ggg::get_page(config.latest_date).await;
}