aboutsummaryrefslogtreecommitdiff
path: root/import/src/plugins/wikimedia_commons.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-12-10 16:21:38 +0100
committermetamuffin <metamuffin@disroot.org>2025-12-10 16:21:38 +0100
commita0cfd77b4d19c43a28c4d82072e6ff136e336af3 (patch)
tree05df9f5faa54cef0ae4136fffddea57fbbafee6b /import/src/plugins/wikimedia_commons.rs
parent242d5763d451eed2402be7afde50cd9fa0d6bc79 (diff)
downloadjellything-a0cfd77b4d19c43a28c4d82072e6ff136e336af3.tar
jellything-a0cfd77b4d19c43a28c4d82072e6ff136e336af3.tar.bz2
jellything-a0cfd77b4d19c43a28c4d82072e6ff136e336af3.tar.zst
refactor import plugins part 1
Diffstat (limited to 'import/src/plugins/wikimedia_commons.rs')
-rw-r--r--import/src/plugins/wikimedia_commons.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/import/src/plugins/wikimedia_commons.rs b/import/src/plugins/wikimedia_commons.rs
new file mode 100644
index 0000000..86d934c
--- /dev/null
+++ b/import/src/plugins/wikimedia_commons.rs
@@ -0,0 +1,63 @@
+/*
+ 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) 2025 metamuffin <metamuffin.org>
+*/
+
+use crate::USER_AGENT;
+use anyhow::{Context, Result};
+use jellycache::{cache_store, EscapeKey};
+use jellycommon::Asset;
+use reqwest::{
+ header::{HeaderMap, HeaderName, HeaderValue},
+ redirect::Policy,
+ Client, ClientBuilder,
+};
+use tokio::runtime::Handle;
+
+pub struct WikimediaCommons {
+ client: Client,
+}
+impl Default for WikimediaCommons {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl WikimediaCommons {
+ pub fn new() -> Self {
+ let client = ClientBuilder::new()
+ .default_headers(HeaderMap::from_iter([(
+ HeaderName::from_static("user-agent"),
+ HeaderValue::from_static(USER_AGENT),
+ )]))
+ .redirect(Policy::limited(5))
+ .build()
+ .unwrap();
+ Self { client }
+ }
+
+ pub fn image_by_filename(&self, filename: String, rt: &Handle) -> Result<Asset> {
+ cache_store(
+ format!("ext/wikimedia-commons/image/{}.image", EscapeKey(&filename)),
+ move || {
+ rt.block_on(async {
+ Ok(self
+ .client
+ .get(format!(
+ "https://commons.wikimedia.org/wiki/Special:FilePath/{}",
+ filename.replace(" ", "_")
+ ))
+ .send()
+ .await?
+ .error_for_status()?
+ .bytes()
+ .await?
+ .to_vec())
+ })
+ },
+ )
+ .context("mediawiki image by filename")
+ .map(Asset)
+ }
+}