diff --git a/sqlx-data.json b/sqlx-data.json index d669295..fb3fae9 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -28,6 +28,19 @@ "nullable": [] } }, + "364c5c10ad748d1822c3e909aca601993f0ddb7690368a82ae467b3b0950478e": { + "query": "INSERT INTO WEASYL (id, data) VALUES ($1, $2)", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int4", + "Jsonb" + ] + }, + "nullable": [] + } + }, "7ef3d8fa00b1245440aae6f91bfc23bddee7730fc2de67e2f359762ce8db3bf4": { "query": "SELECT id FROM weasyl WHERE id = $1", "describe": { @@ -65,23 +78,5 @@ null ] } - }, - "a4f9a907d9cc275ffece26e43063eb767418509519fb3c845fac7100af94e713": { - "query": "SELECT max(id) FROM weasyl", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "max", - "type_info": "Int4" - } - ], - "parameters": { - "Left": [] - }, - "nullable": [ - null - ] - } } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a033d72..79add7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,10 +72,10 @@ async fn load_submission( client: &reqwest::Client, api_key: &str, id: i32, -) -> anyhow::Result> { +) -> anyhow::Result<(Option, serde_json::Value)> { println!("Loading submission {}", id); - let body: Result = client + let body: serde_json::Value = client .get(&format!( "https://www.weasyl.com/api/submissions/{}/view", id @@ -84,18 +84,16 @@ async fn load_submission( .send() .await? .json() - .await; + .await?; - let body = match body { - Err(_err) => return Ok(None), - Ok(body) => body, + let data: WeasylResponse = match serde_json::from_value(body.clone()) { + Ok(data) => data, + Err(_err) => return Ok((None, body)), }; - let data: WeasylResponse = serde_json::from_value(body.clone())?; - let res = match data { WeasylResponse::Response(sub) if sub.subtype == WeasylSubmissionSubtype::Visual => { - Some((sub, body)) + Some(sub) } WeasylResponse::Response(_sub) => None, WeasylResponse::Error { @@ -106,7 +104,7 @@ async fn load_submission( } => return Err(anyhow::anyhow!(name)), }; - Ok(res) + Ok((res, body)) } async fn process_submission( @@ -153,10 +151,14 @@ async fn process_submission( Ok(()) } -async fn insert_null(pool: &sqlx::Pool, id: i32) -> anyhow::Result<()> { +async fn insert_null( + pool: &sqlx::Pool, + body: serde_json::Value, + id: i32, +) -> anyhow::Result<()> { 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) .await?; @@ -194,8 +196,8 @@ async fn main() { } match load_submission(&client, &api_key, id).await.unwrap() { - Some((sub, json)) => process_submission(&pool, &client, json, sub).await.unwrap(), - None => insert_null(&pool, id).await.unwrap(), + (Some(sub), json) => process_submission(&pool, &client, json, sub).await.unwrap(), + (None, body) => insert_null(&pool, body, id).await.unwrap(), } } }