From e80d7960a25c93299b0d73ad6969ddf22d7ad27f Mon Sep 17 00:00:00 2001 From: moritsbw-cpu Date: Wed, 18 Nov 2026 16:57:03 +0000 Subject: [PATCH] iTunes collector: retry server errors, skip a failing artist instead of stopping Co-Authored-By: Claude Opus 5.5 --- src/itunes.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/itunes.rs b/src/itunes.rs index 9c100cb..86a5840 100644 --- a/src/itunes.rs +++ b/src/itunes.rs @@ -44,7 +44,7 @@ impl Client { std::thread::sleep(self.delay); match self.http.get(url).send() { Ok(r) if r.status().is_success() => return Ok(r.json().unwrap_or(Value::Null)), - Ok(r) if matches!(r.status().as_u16(), 403 | 429 | 503) => { eprintln!("itunes: {} — waiting {wait} s", r.status()); } + Ok(r) if matches!(r.status().as_u16(), 403 | 429) || r.status().is_server_error() => { eprintln!("itunes: {} — waiting {wait} s", r.status()); } Ok(r) => bail!("{} from {url}", r.status()), Err(e) => { eprintln!("itunes: {e} — waiting {wait} s"); } } @@ -80,14 +80,15 @@ pub fn collect(db: &Db, limit: usize, refresh: usize, log: &mut Vec) -> for (dr_id, name) in todo.into_iter().take(limit) { if key(&name).is_empty() { continue; } let v = match c.get(&format!("https://itunes.apple.com/search?term={}&entity=musicArtist&country=dk&limit=10", urlencode(&name))) { - Ok(v) => v, Err(e) => { log.push(format!("itunes search '{name}': {e}")); break; } + // A refusal that outlasts the backoff means the rate limit: stop. Anything else: skip this artist. + Ok(v) => v, Err(e) => { log.push(format!("itunes search '{name}': {e}")); if e.to_string().contains("still refused") { break; } continue; } }; let hit = v["results"].as_array().into_iter().flatten().find(|r| key(r["artistName"].as_str().unwrap_or_default()) == key(&name)); let (iid, iname) = (hit.and_then(|h| h["artistId"].as_i64()), hit.and_then(|h| h["artistName"].as_str()).map(str::to_string)); if let Some(id) = iid { let have: i64 = db.conn.query_row("SELECT count(*) FROM itunes_release_lookups WHERE itunes_artist_id = ?1", [id], |r| r.get(0))?; if have == 0 { - if let Err(e) = releases(&c, db, id) { log.push(format!("itunes releases {id}: {e}")); break; } + if let Err(e) = releases(&c, db, id) { log.push(format!("itunes releases {id}: {e}")); if e.to_string().contains("still refused") { break; } continue; } } } db.conn.execute("INSERT OR REPLACE INTO itunes_artist_map VALUES (?1, ?2, ?3, ?4)", params![dr_id, iid, iname, Utc::now().to_rfc3339()])?;