Compare commits
7 commits
Author | SHA1 | Date | |
---|---|---|---|
f3bb504cfd | |||
42aff098bd | |||
204d413779 | |||
10111ff612 | |||
13954f26aa | |||
3abfaf55c1 | |||
da4220e027 |
4 changed files with 40 additions and 19 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -52,7 +52,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aob-lemmy-bot"
|
name = "aob-lemmy-bot"
|
||||||
version = "3.2.1"
|
version = "3.3.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
authors = ["Neshura"]
|
authors = ["Neshura"]
|
||||||
name = "aob-lemmy-bot"
|
name = "aob-lemmy-bot"
|
||||||
version = "3.2.1"
|
version = "3.3.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "Bot for automatically posting new chapters of 'Ascendance of a Bookworm' released by J-Novel Club"
|
description = "Bot for automatically posting new chapters of 'Ascendance of a Bookworm' released by J-Novel Club"
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
|
|
|
@ -81,7 +81,10 @@ impl Bot {
|
||||||
loop {
|
loop {
|
||||||
let mut lemmy = match Lemmy::new(&self.shared_config).await {
|
let mut lemmy = match Lemmy::new(&self.shared_config).await {
|
||||||
Ok(data) => data,
|
Ok(data) => data,
|
||||||
Err(_) => continue,
|
Err(_) => {
|
||||||
|
sleep(Duration::seconds(10).to_std().unwrap()).await;
|
||||||
|
continue;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
lemmy.get_communities().await;
|
lemmy.get_communities().await;
|
||||||
|
|
50
src/lemmy.rs
50
src/lemmy.rs
|
@ -23,6 +23,15 @@ macro_rules! debug {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! info {
|
||||||
|
($msg:tt) => {
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => log::info!("[INFO] {}", $msg),
|
||||||
|
false => println!("[INFO] {}", $msg),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! error {
|
macro_rules! error {
|
||||||
($msg:tt) => {
|
($msg:tt) => {
|
||||||
match connected_to_journal() {
|
match connected_to_journal() {
|
||||||
|
@ -218,7 +227,7 @@ impl Lemmy {
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = match HTTP_CLIENT
|
let response = match HTTP_CLIENT
|
||||||
.post(read_config.instance.to_owned() + "/api/v3/user/login")
|
.post(read_config.instance.to_owned() + "/api/alpha/user/login")
|
||||||
.json(&login_params)
|
.json(&login_params)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
|
@ -259,12 +268,12 @@ impl Lemmy {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn logout(&self) {
|
pub(crate) async fn logout(&self) {
|
||||||
let _ = self.post_data_json("/api/v3/user/logout", &"").await;
|
let _ = self.post_data_json("/api/alpha/user/logout", &"").await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub(crate) async fn post(&self, post: CreatePost) -> Option<PostId> {
|
pub(crate) async fn post(&self, post: CreatePost) -> Option<PostId> {
|
||||||
let response: String = match self.post_data_json("/api/v3/post", &post).await {
|
let response: String = match self.post_data_json("/api/alpha/post", &post).await {
|
||||||
Some(data) => data,
|
Some(data) => data,
|
||||||
None => return None,
|
None => return None,
|
||||||
};
|
};
|
||||||
|
@ -277,7 +286,7 @@ impl Lemmy {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn feature(&self, params: FeaturePost) -> Option<PostView> {
|
async fn feature(&self, params: FeaturePost) -> Option<PostView> {
|
||||||
let response: String = match self.post_data_json("/api/v3/post/feature", ¶ms).await {
|
let response: String = match self.post_data_json("/api/alpha/post/feature", ¶ms).await {
|
||||||
Some(data) => data,
|
Some(data) => data,
|
||||||
None => return None,
|
None => return None,
|
||||||
};
|
};
|
||||||
|
@ -314,7 +323,7 @@ impl Lemmy {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let response: String = match self.get_data_query("/api/v3/post/list", &list_params).await {
|
let response: String = match self.get_data_query("/api/alpha/post/list", &list_params).await {
|
||||||
Some(data) => data,
|
Some(data) => data,
|
||||||
None => return None,
|
None => return None,
|
||||||
};
|
};
|
||||||
|
@ -337,7 +346,7 @@ impl Lemmy {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let response: String = match self.get_data_query("/api/v3/post/list", &list_params).await {
|
let response: String = match self.get_data_query("/api/alpha/post/list", &list_params).await {
|
||||||
Some(data) => data,
|
Some(data) => data,
|
||||||
None => return None,
|
None => return None,
|
||||||
};
|
};
|
||||||
|
@ -360,7 +369,7 @@ impl Lemmy {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let response: String = match self.get_data_query("/api/v3/community/list", &list_params).await {
|
let response: String = match self.get_data_query("/api/alpha/community/list", &list_params).await {
|
||||||
Some(data) => data,
|
Some(data) => data,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
@ -395,26 +404,35 @@ impl Lemmy {
|
||||||
page_cursor: None,
|
page_cursor: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let response: String = match self.get_data_query("/api/v3/post/list", &get_params).await {
|
let response: String = match self.get_data_query("/api/alpha/post/list", &get_params).await {
|
||||||
Some(data) => data,
|
Some(data) => data,
|
||||||
None => return None,
|
None => {
|
||||||
|
error!("Unable to query post list");
|
||||||
|
return None
|
||||||
|
},
|
||||||
};
|
};
|
||||||
let json_data: GetPostsResponse = match self.parse_json(&response).await {
|
let json_data: GetPostsResponse = match self.parse_json(&response).await {
|
||||||
Some(data) => data,
|
Some(data) => data,
|
||||||
None => return None,
|
None => {
|
||||||
|
error!("Unable to parse post data");
|
||||||
|
return None
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if json_data.posts[0].post.name == post.name {
|
for api_post in json_data.posts {
|
||||||
Some(json_data.posts[0].post.id)
|
if api_post.post.name == post.name {
|
||||||
} else {
|
return Some(api_post.post.id);
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let msg = format!("Unable to find post {}", post.name);
|
||||||
|
info!(msg);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
async fn post_data_json<T: Serialize>(&self, route: &str, json: &T ) -> Option<String> {
|
async fn post_data_json<T: Serialize>(&self, route: &str, json: &T ) -> Option<String> {
|
||||||
let res = HTTP_CLIENT
|
let res = HTTP_CLIENT
|
||||||
.post(format!("{}{route}", &self.instance))
|
.post(format!("{}{route}", &self.instance))
|
||||||
.bearer_auth(&self.jwt_token.to_string())
|
.bearer_auth(self.jwt_token.to_string())
|
||||||
.json(&json)
|
.json(&json)
|
||||||
.send()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
|
@ -424,7 +442,7 @@ impl Lemmy {
|
||||||
async fn get_data_query<T: Serialize>(&self, route: &str, param: &T ) -> Option<String> {
|
async fn get_data_query<T: Serialize>(&self, route: &str, param: &T ) -> Option<String> {
|
||||||
let res = HTTP_CLIENT
|
let res = HTTP_CLIENT
|
||||||
.get(format!("{}{route}", &self.instance))
|
.get(format!("{}{route}", &self.instance))
|
||||||
.bearer_auth(&self.jwt_token.to_string())
|
.bearer_auth(self.jwt_token.to_string())
|
||||||
.query(¶m)
|
.query(¶m)
|
||||||
.send()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue