iTunes collector: retry server errors, skip a failing artist instead of stopping

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 16:57:03 +00:00
co-authored by Claude Opus 5.5
parent 46fbec76af
commit e80d7960a2
+4 -3
View File
@@ -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<String>) ->
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()])?;