Fixed: Panic if no image attached or post has newlines
This commit is contained in:
parent
2c26ea5ddc
commit
dbf817e97a
1 changed files with 36 additions and 13 deletions
49
src/main.rs
49
src/main.rs
|
@ -8,7 +8,10 @@ use std::{
|
||||||
io::{self, Read, Seek, Write},
|
io::{self, Read, Seek, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
const APPID: &str = "227300";
|
const APPID: &str = "227300"; // ETS2: 227300, AssettoCorsa: 244210
|
||||||
|
const CLANIMGURLBASE: &str =
|
||||||
|
"https://cdn.akamai.steamstatic.com/steamcommunity/public/images/clans/";
|
||||||
|
const LOGOURL: &str = "https://eurotrucksimulator2.com/images/logo.png";
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct SUResp {
|
struct SUResp {
|
||||||
|
@ -88,32 +91,44 @@ fn main() {
|
||||||
.split("[img]{STEAM_CLAN_IMAGE}")
|
.split("[img]{STEAM_CLAN_IMAGE}")
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let tmptext2: Vec<&str> = tmptext[1].split("[/img]").collect();
|
let imgurl = if tmptext.len() > 1 {
|
||||||
let imgurl = &tmptext2[0];
|
let splitend: Vec<&str> = tmptext[1].split("[/img]").collect();
|
||||||
|
format!("{}{}", CLANIMGURLBASE, splitend[0])
|
||||||
|
} else {
|
||||||
|
LOGOURL.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
let whreq: String = format!(
|
let whreq: String = format!(
|
||||||
r#"
|
r#"
|
||||||
{{
|
{{
|
||||||
"username": "ETS2 Updates",
|
"username": "ETS2 Updates",
|
||||||
"avatar_url": "https://eurotrucksimulator2.com/images/logo.png",
|
"avatar_url": "{}",
|
||||||
"content": "",
|
"content": "",
|
||||||
"embeds": [{{
|
"embeds": [{{
|
||||||
"title": "{}",
|
"title": "{}",
|
||||||
"description": "{}",
|
"description": "{}",
|
||||||
"url": "{}",
|
"url": "{}",
|
||||||
"timestamp": "{}",
|
"timestamp": "{}",
|
||||||
"thumbnail": {{"url": "https://cdn.akamai.steamstatic.com/steamcommunity/public/images/clans/{}"}}
|
"thumbnail": {{"url": "{}"}}
|
||||||
}}]
|
}}]
|
||||||
}}
|
}}
|
||||||
"#,
|
"#,
|
||||||
|
LOGOURL,
|
||||||
body.appnews.newsitems.post0.title,
|
body.appnews.newsitems.post0.title,
|
||||||
if body.appnews.newsitems.post0.contents.len() > 250 {
|
if body.appnews.newsitems.post0.contents.len() > 250 {
|
||||||
format!(
|
format!(
|
||||||
"{} ...",
|
"{} ...",
|
||||||
body.appnews.newsitems.post0.contents[..250].to_string()
|
body.appnews.newsitems.post0.contents[..250]
|
||||||
|
.to_string()
|
||||||
|
.replace("\n", " ")
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
body.appnews.newsitems.post0.contents.to_string()
|
body.appnews
|
||||||
|
.newsitems
|
||||||
|
.post0
|
||||||
|
.contents
|
||||||
|
.to_string()
|
||||||
|
.replace("\n", " ")
|
||||||
},
|
},
|
||||||
body.appnews.newsitems.post0.url,
|
body.appnews.newsitems.post0.url,
|
||||||
t_str,
|
t_str,
|
||||||
|
@ -123,15 +138,23 @@ fn main() {
|
||||||
let resp = ureq::post(&webhook)
|
let resp = ureq::post(&webhook)
|
||||||
.set("Content-Type", "application/json;charset=utf-8")
|
.set("Content-Type", "application/json;charset=utf-8")
|
||||||
.send_string(&whreq);
|
.send_string(&whreq);
|
||||||
resp.expect("Error sending Discord webhook");
|
|
||||||
|
match resp {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("Successfully updated webhook");
|
||||||
|
|
||||||
|
f.set_len(0).expect("Cannot empty the file");
|
||||||
|
f.seek(io::SeekFrom::Start(0))
|
||||||
|
.expect("Cannot seek to file beginning");
|
||||||
|
write!(f, "{}", body.appnews.newsitems.post0.date).expect("Can't write file");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("Error updating webhook:\n{}\nRequest JSON:\n{}", e, &whreq);
|
||||||
|
}
|
||||||
|
}
|
||||||
fdt = body.appnews.newsitems.post0.date;
|
fdt = body.appnews.newsitems.post0.date;
|
||||||
}
|
}
|
||||||
|
|
||||||
f.set_len(0).expect("Cannot empty the file");
|
|
||||||
f.seek(io::SeekFrom::Start(0))
|
|
||||||
.expect("Cannot seek to file beginning");
|
|
||||||
write!(f, "{}", body.appnews.newsitems.post0.date).expect("Can't write file");
|
|
||||||
|
|
||||||
println!("{}: Sleeping...", chrono::offset::Utc::now());
|
println!("{}: Sleeping...", chrono::offset::Utc::now());
|
||||||
thread::sleep(time::Duration::from_secs(60 * 15));
|
thread::sleep(time::Duration::from_secs(60 * 15));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue