Improve cli args
This commit is contained in:
parent
f6af36e999
commit
c9a1c59eb4
2 changed files with 32 additions and 17 deletions
|
|
@ -13,3 +13,4 @@ chrono = "0.4.41"
|
||||||
rss = "2.0.12"
|
rss = "2.0.12"
|
||||||
mini-redis = "0.4.1"
|
mini-redis = "0.4.1"
|
||||||
tokio-postgres = "0.7.13"
|
tokio-postgres = "0.7.13"
|
||||||
|
clap = { version = "4.5.42", features = ["derive"] }
|
||||||
|
|
|
||||||
48
src/main.rs
48
src/main.rs
|
|
@ -1,31 +1,43 @@
|
||||||
use chrono::{NaiveDate, NaiveDateTime, TimeZone, prelude::Local};
|
use chrono::{NaiveDate, NaiveDateTime, TimeZone, prelude::Local};
|
||||||
|
use clap::Parser;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use reqwest::get;
|
use reqwest::get;
|
||||||
use scraper::{Html, Selector};
|
use scraper::{Html, Selector};
|
||||||
|
use std::env;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use tokio::{
|
use tokio::runtime::Runtime;
|
||||||
runtime::Runtime,
|
|
||||||
sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel},
|
|
||||||
};
|
|
||||||
use tokio_postgres;
|
use tokio_postgres;
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
struct Cli {
|
||||||
|
path: std::path::PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let rt = Runtime::new().unwrap();
|
let rt = Runtime::new().unwrap();
|
||||||
|
|
||||||
|
let args = Cli::parse();
|
||||||
|
let db_connection_string = &format!(
|
||||||
|
"host={} dbname={} user={} password={}",
|
||||||
|
env::var("POSTGRES_HOST").unwrap_or(String::from("localhost")),
|
||||||
|
env::var("POSTGRES_USER").unwrap_or(String::from("dev")),
|
||||||
|
env::var("POSTGRES_USER").unwrap_or(String::from("dev")),
|
||||||
|
env::var("POSTGRES_PASSWORD").unwrap_or(String::from("dev"))
|
||||||
|
);
|
||||||
|
|
||||||
|
println!("Connection string: {}", db_connection_string);
|
||||||
|
|
||||||
rt.block_on(async {
|
rt.block_on(async {
|
||||||
// Connect to the database.
|
// Connect to the database.
|
||||||
if let Ok((client, connection)) = tokio_postgres::connect(
|
if let Ok((client, connection)) =
|
||||||
"host=localhost dbname=dev user=root password=root",
|
tokio_postgres::connect(db_connection_string, tokio_postgres::NoTls).await
|
||||||
tokio_postgres::NoTls,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
{
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Err(e) = connection.await {
|
if let Err(e) = connection.await {
|
||||||
eprintln!("connection error: {}", e);
|
eprintln!("[x] Connection db error: {}", e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -42,7 +54,7 @@ fn main() {
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
eprintln!("table creation error: {}", e);
|
eprintln!("[x] Table creation error: {}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get new [$] articles
|
// Get new [$] articles
|
||||||
|
|
@ -83,7 +95,7 @@ fn main() {
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
eprintln!("Error insert: {}", e);
|
eprintln!("[x] Error insert: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -95,7 +107,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: How to manage the RSS xml file
|
// TODO: How to manage the RSS xml file
|
||||||
let mut channel = match File::open("rss.xml") {
|
let mut channel = match File::open(&args.path) {
|
||||||
Ok(file) => rss::Channel::read_from(BufReader::new(file)).unwrap(),
|
Ok(file) => rss::Channel::read_from(BufReader::new(file)).unwrap(),
|
||||||
_ => rss::ChannelBuilder::default()
|
_ => rss::ChannelBuilder::default()
|
||||||
.title("[$] lwn.net")
|
.title("[$] lwn.net")
|
||||||
|
|
@ -132,15 +144,17 @@ fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
channel.set_items(items);
|
channel.set_items(items);
|
||||||
if let Err(e) = save_xml(&channel.to_string()) {
|
if let Err(e) = save_xml(&channel.to_string(), args.path) {
|
||||||
eprintln!("failed to save xml: {}", e);
|
eprintln!("[x] Failed to save xml: {}", e);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
eprintln!("[x] Failed to connect to the db");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_xml(rss_string: &str) -> std::io::Result<()> {
|
fn save_xml(rss_string: &str, path: std::path::PathBuf) -> std::io::Result<()> {
|
||||||
let mut file = File::create("paid_lwn_net_rss.xml")?;
|
let mut file = File::create(path)?;
|
||||||
file.write_all(rss_string.as_bytes())?;
|
file.write_all(rss_string.as_bytes())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue