Added Error handling for dropped connections
This commit is contained in:
parent
cf82a4535b
commit
813f661ee2
1 changed files with 41 additions and 17 deletions
58
src/main.rs
58
src/main.rs
|
@ -44,36 +44,53 @@ impl Bot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn login(&mut self) {
|
/// Get JWT Token
|
||||||
|
///
|
||||||
|
/// * `return` : Returns true if token was succesfully retrieved, false otherwise
|
||||||
|
pub(crate) fn login(&mut self) -> bool {
|
||||||
let login_params = Login {
|
let login_params = Login {
|
||||||
username_or_email: self.secrets.lemmy.get_username(),
|
username_or_email: self.secrets.lemmy.get_username(),
|
||||||
password: self.secrets.lemmy.get_password(),
|
password: self.secrets.lemmy.get_password(),
|
||||||
totp_2fa_token: None,
|
totp_2fa_token: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = CLIENT
|
let res = match CLIENT
|
||||||
.post(self.config.instance.clone() + "/api/v3/user/login")
|
.post(self.config.instance.clone() + "/api/v3/user/login")
|
||||||
.json(&login_params)
|
.json(&login_params)
|
||||||
.send()
|
.send() {
|
||||||
.unwrap();
|
Ok(data) => data,
|
||||||
|
Err(_) => return false,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
if res.status() == StatusCode::OK {
|
if res.status() == StatusCode::OK {
|
||||||
let data: &LoginResponse = &res.json().unwrap();
|
let data: &LoginResponse = &res.json().unwrap();
|
||||||
|
|
||||||
let jwt = data.jwt.clone().expect("JWT Token could not be acquired");
|
let jwt = data.jwt.clone().expect("JWT Token could not be acquired");
|
||||||
self.auth = jwt;
|
self.auth = jwt;
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
println!("Error Code: {:?}", res.status());
|
println!("Error Code: {:?}", res.status());
|
||||||
panic!("JWT Token could not be acquired");
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn post(&mut self, post_data: CreatePost) {
|
/// Make Post to Lemmy Instance
|
||||||
let res = CLIENT
|
///
|
||||||
|
/// * `post_data` : Object of type [CreatePost] containing post info
|
||||||
|
/// * `return` : Returns true if Post was succesful, false otherwise
|
||||||
|
pub(crate) fn post(&mut self, post_data: CreatePost) -> bool {
|
||||||
|
let res = match CLIENT
|
||||||
.post(self.config.instance.clone() + "/api/v3/post")
|
.post(self.config.instance.clone() + "/api/v3/post")
|
||||||
.json(&post_data)
|
.json(&post_data)
|
||||||
.send()
|
.send() {
|
||||||
.unwrap();
|
Ok(data) => data,
|
||||||
|
Err(_) => return false
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: process res to get info about if post was successfuly (mostly if jwt token was valid)
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn run_once(&mut self, mut prev_time: NaiveTime) {
|
pub(crate) fn run_once(&mut self, mut prev_time: NaiveTime) {
|
||||||
|
@ -91,7 +108,10 @@ impl Bot {
|
||||||
|
|
||||||
post_queue.iter().for_each(|post| {
|
post_queue.iter().for_each(|post| {
|
||||||
println!("Posting: {}", post.name);
|
println!("Posting: {}", post.name);
|
||||||
self.post(post.clone());
|
loop {
|
||||||
|
if self.post(post.clone()) {break};
|
||||||
|
println!("Post attempt failed, retrying");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +119,10 @@ impl Bot {
|
||||||
while Utc::now().time() - self.start_time.time() < chrono::Duration::seconds(30) {
|
while Utc::now().time() - self.start_time.time() < chrono::Duration::seconds(30) {
|
||||||
sleep(time::Duration::from_secs(10));
|
sleep(time::Duration::from_secs(10));
|
||||||
}
|
}
|
||||||
let _ = reqwest::blocking::get("https://status.neshweb.net/api/push/7s1CjPPzrV?status=up&msg=OK&ping=");
|
match reqwest::blocking::get("https://status.neshweb.net/api/push/7s1CjPPzrV?status=up&msg=OK&ping=") {
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(err) => println!("{}", err)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn print_info(&self) {
|
pub(crate) fn print_info(&self) {
|
||||||
|
@ -136,19 +159,20 @@ fn list_posts(auth: &Sensitive<String>, base: String) -> GetPostsResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_bot() {
|
fn run_bot() {
|
||||||
// Get all needed auth tokens at the start
|
// Get all needed auth tokens at the start
|
||||||
let mut old = Utc::now().time();
|
let mut old = Utc::now().time();
|
||||||
let mut this = Bot::new();
|
let mut this = Bot::new();
|
||||||
this.login();
|
if this.login() {
|
||||||
this.community_ids.load(&this.auth, &this.config.instance);
|
this.community_ids.load(&this.auth, &this.config.instance);
|
||||||
|
|
||||||
// Enter a loop (not for debugging)
|
// Enter a loop (not for debugging)
|
||||||
loop {
|
loop {
|
||||||
this.idle();
|
this.idle();
|
||||||
this.run_once(old);
|
this.run_once(old);
|
||||||
this.print_info();
|
this.print_info();
|
||||||
this.idle();
|
this.idle();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn main() -> Result<(), io::Error> {
|
fn main() -> Result<(), io::Error> {
|
||||||
run_bot();
|
run_bot();
|
||||||
|
|
Loading…
Reference in a new issue