1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/*
This file is part of jellything (https://codeberg.org/metamuffin/jellything)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2026 metamuffin <metamuffin.org>
*/
use crate::{
USER_AGENT,
plugins::{ImportPlugin, PluginContext, PluginInfo},
source_rank::ObjectImportSourceExt,
};
use anyhow::{Context, Result};
use jellycache::{Cache, HashKey};
use jellycommon::*;
use jellydb::RowNum;
use log::info;
use regex::Regex;
use reqwest::{
Client, ClientBuilder,
header::{HeaderMap, HeaderName, HeaderValue},
};
use std::{
sync::{Arc, LazyLock},
time::Duration,
};
use tokio::{
runtime::Handle,
sync::Semaphore,
time::{Instant, sleep_until},
};
pub struct Vgmdb {
client: Client,
rate_limit: Arc<Semaphore>,
}
static RE_IMAGE_URL_FROM_HTML: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"href='(?<url>https://media.vgm.io/artists/[-/\w\.]+)'"#).unwrap()
});
impl Default for Vgmdb {
fn default() -> Self {
Self::new()
}
}
impl Vgmdb {
pub fn new() -> Self {
let client = ClientBuilder::new()
.default_headers(HeaderMap::from_iter([
(
HeaderName::from_static("user-agent"),
HeaderValue::from_static(USER_AGENT),
),
(
HeaderName::from_static("x-comment"),
HeaderValue::from_static("Please add an API, thanks!"),
),
]))
.build()
.unwrap();
Self {
client,
rate_limit: Arc::new(Semaphore::new(3)),
}
}
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,
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()));
}
}
Ok(None)
}
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();
tokio::task::spawn(async move {
sleep_until(permit_drop_ts).await;
drop(_permit);
});
Ok(resp)
})
})
.context("vgmdb artist page scrape")
}
}
impl ImportPlugin for Vgmdb {
fn info(&self) -> PluginInfo {
PluginInfo {
name: "vgmdb",
tag: MSOURCE_VGMDB,
handle_process: true,
..Default::default()
}
}
fn process(&self, ct: &PluginContext, node: RowNum) -> Result<()> {
let data = ct.ic.get_node(node)?.unwrap();
let data = data.as_object();
let Some(id) = data
.get(NO_IDENTIFIERS)
.unwrap_or_default()
.get(IDENT_VGMDB_ARTIST)
else {
return Ok(());
};
let id = id.parse()?;
let Some(image) = self.get_artist_image(&ct.ic.cache, id, ct.rt)? else {
return Ok(());
};
ct.ic.update_node(node, |node| {
node.as_object()
.update(NO_PICTURES, |pics| pics.insert_s(ct.is, PICT_COVER, &image))
})?;
Ok(())
}
}
|