Skip to content

Commit efced0d

Browse files
committed
small 1.3 api improvements, fix 1.3 icon url
1 parent e14f9db commit efced0d

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/api13/api.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::api13::responses::*;
1111

1212
use super::Api13State;
1313

14+
const API_URL: &str = "http://javid.ddns.net/tModLoader";
15+
1416
async fn get_html(url: &str) -> Result<Html, reqwest::Error> {
1517
let res = reqwest::get(url).await?;
1618
let body = res.text().await?;
@@ -19,7 +21,7 @@ async fn get_html(url: &str) -> Result<Html, reqwest::Error> {
1921

2022
#[get("/count")]
2123
pub async fn count_1_3() -> Result<Value, APIError> {
22-
let html = get_html("http://javid.ddns.net/tModLoader/modmigrationprogress.php").await?;
24+
let html = get_html(&format!("{API_URL}/modmigrationprogress.php")).await?;
2325
let selector = Selector::parse("table > tbody > tr")?; // get all 'tr' inside 'tbody' and 'table'
2426
let selection = html.select(&selector); // generate iterator based on selector
2527
let count = selection.skip(1).count(); // count the number of elements except the first one
@@ -40,31 +42,29 @@ pub async fn mod_1_3(modname: &str, state: &State<Api13State>) -> Result<CacheRe
4042
let mod_info = match cache {
4143
Some(cached_value) => cached_value.item,
4244
None => {
45+
let client = reqwest::Client::new();
46+
4347
// get mod info
44-
let mut modinfo: ModInfo = crate::get_json(&format!("http://javid.ddns.net/tModLoader/tools/modinfo.php?modname={}", modname)).await.map_err(|_| {
48+
let res = client.get(&format!("{API_URL}/tools/modinfo.php?modname={}", modname)).send().await?;
49+
let mut modinfo: ModInfo = res.json::<ModInfo>().await.map_err(|_| {
4550
APIError::InvalidModName(modname.to_owned())
4651
})?;
4752

48-
4953
// get description response; save info in DescriptionResponse struct
50-
let response = reqwest::Client::new()
51-
.post("http://javid.ddns.net/tModLoader/moddescription.php")
54+
let description: DescriptionResponse = client
55+
.post(format!("{API_URL}/moddescription.php"))
5256
.form(&HashMap::from([("modname", &modname)]))
53-
.send()
54-
.await?;
55-
56-
let description_json = response.text().await.map_err(|_| {
57-
APIError::ReqwestError("Post request on 'http://javid.ddns.net/tModLoader/moddescription.php' failed".to_string())
58-
})?;
57+
.send().await?
58+
.json().await?;
5959

60-
let description_res: DescriptionResponse = serde_json::from_str(&description_json)?;
61-
modinfo.description = Some(description_res.description);
62-
modinfo.homepage = Some(description_res.homepage);
60+
modinfo.description = Some(description.description);
61+
modinfo.homepage = Some(description.homepage);
6362

6463
// get icon url if it exists
65-
let res = reqwest::get(format!("https://mirror.sgkoi.dev/direct/{}.png", modname)).await;
64+
let icon_url = format!("{API_URL}/modicons/modiconuploads/{}_{}.png", modname, modinfo.version);
65+
let res = client.get(&icon_url).send().await;
6666
modinfo.icon = match res {
67-
Ok(_) => Some(format!("https://mirror.sgkoi.dev/direct/{}.png", modname)),
67+
Ok(_) => Some(icon_url),
6868
Err(_) => None
6969
};
7070

@@ -106,7 +106,7 @@ async fn get_author_info(steamid: u64, state: &State<Api13State>) -> Result<Cach
106106

107107
let td_selector = &Selector::parse("td")?;
108108

109-
let html = get_html(&format!("http://javid.ddns.net/tModLoader/tools/ranksbysteamid.php?steamid64={}", steamid)).await?;
109+
let html = get_html(&format!("{API_URL}/tools/ranksbysteamid.php?steamid64={}", steamid)).await?;
110110
let table_selector = Selector::parse("table > tbody")?;
111111
let mut tables = html.select(&table_selector); // there are 4 tables
112112

@@ -195,7 +195,7 @@ pub async fn list_1_3(state: &State<Api13State>) -> Result<CacheResponse<Value>,
195195

196196
// new scopes because funny errors
197197
{
198-
let html = get_html("http://javid.ddns.net/tModLoader/modmigrationprogressalltime.php").await?;
198+
let html = get_html(&format!("{API_URL}/modmigrationprogressalltime.php")).await?;
199199
let mod_infos = html.select(mod_selector);
200200

201201
for info in mod_infos {
@@ -216,7 +216,7 @@ pub async fn list_1_3(state: &State<Api13State>) -> Result<CacheResponse<Value>,
216216
}
217217

218218
{
219-
let html = get_html("http://javid.ddns.net/tModLoader/modmigrationprogress.php").await?;
219+
let html = get_html(&format!("{API_URL}/modmigrationprogress.php")).await?;
220220
let mod_infos = html.select(mod_selector);
221221

222222
for info in mod_infos {
@@ -247,7 +247,7 @@ pub async fn list_1_3(state: &State<Api13State>) -> Result<CacheResponse<Value>,
247247

248248
#[get("/history/<modname>")]
249249
pub async fn history_1_3(modname: &str) -> Result<CacheResponse<Value>, APIError> {
250-
let html = get_html(&format!("http://javid.ddns.net/tModLoader/tools/moddownloadhistory.php?modname={}", modname)).await?;
250+
let html = get_html(&format!("{API_URL}/tools/moddownloadhistory.php?modname={}", modname)).await?;
251251
let versions_selector = &Selector::parse("table > tbody > tr:not(:first-child)")?;
252252
let versions = html.select(versions_selector);
253253

src/steamapi.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ pub struct CountResponse {
2626
#[derive(Deserialize, Debug)]
2727
#[serde(crate = "rocket::serde")]
2828
pub struct IDResponse {
29-
pub steamid: Option<String>
29+
pub steamid: Option<String>,
30+
#[allow(dead_code)] pub success: u32
3031
}
3132

3233
#[derive(Deserialize, Debug)]

0 commit comments

Comments
 (0)