Ignore submissions of other types.

This commit is contained in:
Syfaro 2020-10-09 15:04:59 -04:00
parent c9a6706d5c
commit 0b42564c5e
2 changed files with 29 additions and 32 deletions

View File

@ -28,6 +28,19 @@
"nullable": [] "nullable": []
} }
}, },
"364c5c10ad748d1822c3e909aca601993f0ddb7690368a82ae467b3b0950478e": {
"query": "INSERT INTO WEASYL (id, data) VALUES ($1, $2)",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int4",
"Jsonb"
]
},
"nullable": []
}
},
"7ef3d8fa00b1245440aae6f91bfc23bddee7730fc2de67e2f359762ce8db3bf4": { "7ef3d8fa00b1245440aae6f91bfc23bddee7730fc2de67e2f359762ce8db3bf4": {
"query": "SELECT id FROM weasyl WHERE id = $1", "query": "SELECT id FROM weasyl WHERE id = $1",
"describe": { "describe": {
@ -65,23 +78,5 @@
null null
] ]
} }
},
"a4f9a907d9cc275ffece26e43063eb767418509519fb3c845fac7100af94e713": {
"query": "SELECT max(id) FROM weasyl",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "max",
"type_info": "Int4"
}
],
"parameters": {
"Left": []
},
"nullable": [
null
]
}
} }
} }

View File

@ -72,10 +72,10 @@ async fn load_submission(
client: &reqwest::Client, client: &reqwest::Client,
api_key: &str, api_key: &str,
id: i32, id: i32,
) -> anyhow::Result<Option<(WeasylSubmission, serde_json::Value)>> { ) -> anyhow::Result<(Option<WeasylSubmission>, serde_json::Value)> {
println!("Loading submission {}", id); println!("Loading submission {}", id);
let body: Result<serde_json::Value, _> = client let body: serde_json::Value = client
.get(&format!( .get(&format!(
"https://www.weasyl.com/api/submissions/{}/view", "https://www.weasyl.com/api/submissions/{}/view",
id id
@ -84,18 +84,16 @@ async fn load_submission(
.send() .send()
.await? .await?
.json() .json()
.await; .await?;
let body = match body { let data: WeasylResponse<WeasylSubmission> = match serde_json::from_value(body.clone()) {
Err(_err) => return Ok(None), Ok(data) => data,
Ok(body) => body, Err(_err) => return Ok((None, body)),
}; };
let data: WeasylResponse<WeasylSubmission> = serde_json::from_value(body.clone())?;
let res = match data { let res = match data {
WeasylResponse::Response(sub) if sub.subtype == WeasylSubmissionSubtype::Visual => { WeasylResponse::Response(sub) if sub.subtype == WeasylSubmissionSubtype::Visual => {
Some((sub, body)) Some(sub)
} }
WeasylResponse::Response(_sub) => None, WeasylResponse::Response(_sub) => None,
WeasylResponse::Error { WeasylResponse::Error {
@ -106,7 +104,7 @@ async fn load_submission(
} => return Err(anyhow::anyhow!(name)), } => return Err(anyhow::anyhow!(name)),
}; };
Ok(res) Ok((res, body))
} }
async fn process_submission( async fn process_submission(
@ -153,10 +151,14 @@ async fn process_submission(
Ok(()) Ok(())
} }
async fn insert_null(pool: &sqlx::Pool<sqlx::Postgres>, id: i32) -> anyhow::Result<()> { async fn insert_null(
pool: &sqlx::Pool<sqlx::Postgres>,
body: serde_json::Value,
id: i32,
) -> anyhow::Result<()> {
println!("Inserting null for submission {}", id); println!("Inserting null for submission {}", id);
sqlx::query!("INSERT INTO WEASYL (id) VALUES ($1)", id) sqlx::query!("INSERT INTO WEASYL (id, data) VALUES ($1, $2)", id, body)
.execute(pool) .execute(pool)
.await?; .await?;
@ -194,8 +196,8 @@ async fn main() {
} }
match load_submission(&client, &api_key, id).await.unwrap() { match load_submission(&client, &api_key, id).await.unwrap() {
Some((sub, json)) => process_submission(&pool, &client, json, sub).await.unwrap(), (Some(sub), json) => process_submission(&pool, &client, json, sub).await.unwrap(),
None => insert_null(&pool, id).await.unwrap(), (None, body) => insert_null(&pool, body, id).await.unwrap(),
} }
} }
} }