diff options
Diffstat (limited to 'import/src/plugins/vgmdb.rs')
| -rw-r--r-- | import/src/plugins/vgmdb.rs | 99 |
1 files changed, 53 insertions, 46 deletions
diff --git a/import/src/plugins/vgmdb.rs b/import/src/plugins/vgmdb.rs index c62eb90..534b241 100644 --- a/import/src/plugins/vgmdb.rs +++ b/import/src/plugins/vgmdb.rs @@ -9,7 +9,7 @@ use crate::{ plugins::{ImportPlugin, PluginInfo}, }; use anyhow::{Context, Result}; -use jellycache::{HashKey, cache, cache_store}; +use jellycache::{Cache, HashKey}; use log::info; use regex::Regex; use reqwest::{ @@ -62,34 +62,40 @@ impl Vgmdb { } } - pub fn get_artist_image(&self, id: u64, rt: &Handle) -> Result<Option<String>> { - if let Some(url) = self.get_artist_image_url(id, rt)? { - cache_store( - format!("ext/vgmdb/artist-image/{}.image", HashKey(&url)), - move || { - rt.block_on(async { - info!("downloading image {url:?}"); - Ok(self - .client - .get(url) - .send() - .await? - .error_for_status()? - .bytes() - .await? - .to_vec()) - }) - }, - ) - .context("vgmdb media download") - .map(Some) + pub fn get_artist_image(&self, cache: &Cache, id: u64, rt: &Handle) -> Result<Option<String>> { + if let Some(url) = self.get_artist_image_url(cache, id, rt)? { + cache + .store( + format!("ext/vgmdb/artist-image/{}.image", HashKey(&url)), + move || { + rt.block_on(async { + info!("downloading image {url:?}"); + Ok(self + .client + .get(url) + .send() + .await? + .error_for_status()? + .bytes() + .await? + .to_vec()) + }) + }, + ) + .context("vgmdb media download") + .map(Some) } else { Ok(None) } } - pub fn get_artist_image_url(&self, id: u64, rt: &Handle) -> Result<Option<String>> { - let html = self.scrape_artist_page(id, rt)?; + pub fn get_artist_image_url( + &self, + cache: &Cache, + id: u64, + rt: &Handle, + ) -> Result<Option<String>> { + let html = self.scrape_artist_page(cache, id, rt)?; if let Some(cap) = RE_IMAGE_URL_FROM_HTML.captures(&str::from_utf8(&html).unwrap()) { if let Some(url) = cap.name("url").map(|m| m.as_str()) { return Ok(Some(url.to_string())); @@ -98,32 +104,33 @@ impl Vgmdb { Ok(None) } - pub fn scrape_artist_page(&self, id: u64, rt: &Handle) -> Result<Vec<u8>> { - cache(&format!("ext/vgmdb/artist-page/{id}.html"), move || { - rt.block_on(async { - let _permit = self.rate_limit.clone().acquire_owned().await?; - let permit_drop_ts = Instant::now() + Duration::from_secs(1); - info!("scrape artist: {id}"); + pub fn scrape_artist_page(&self, cache: &Cache, id: u64, rt: &Handle) -> Result<Vec<u8>> { + cache + .cache(&format!("ext/vgmdb/artist-page/{id}.html"), move || { + rt.block_on(async { + let _permit = self.rate_limit.clone().acquire_owned().await?; + let permit_drop_ts = Instant::now() + Duration::from_secs(1); + info!("scrape artist: {id}"); - let resp = self - .client - .get(format!("https://vgmdb.net/artist/{id}")) - .send() - .await? - .error_for_status()? - .bytes() - .await? - .to_vec(); + let resp = self + .client + .get(format!("https://vgmdb.net/artist/{id}")) + .send() + .await? + .error_for_status()? + .bytes() + .await? + .to_vec(); - tokio::task::spawn(async move { - sleep_until(permit_drop_ts).await; - drop(_permit); - }); + tokio::task::spawn(async move { + sleep_until(permit_drop_ts).await; + drop(_permit); + }); - Ok(resp) + Ok(resp) + }) }) - }) - .context("vgmdb artist page scrape") + .context("vgmdb artist page scrape") } } |