aboutsummaryrefslogtreecommitdiff
path: root/import/src/wikimedia_commons.rs
blob: e0f647ea6a4551caf34781d7fbcbe1f2f61e24b2 (plain)
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
/*
    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::Result;
use jellybase::cache::{async_cache_file, CachePath};
use reqwest::{
    header::{HeaderMap, HeaderName, HeaderValue},
    redirect::Policy,
    Client, ClientBuilder,
};
use tokio::io::AsyncWriteExt;

pub struct WikimediaCommons {
    client: Client,
}
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 async fn image_by_filename(&self, filename: String) -> Result<CachePath> {
        async_cache_file(
            "api-wikimedia-commons-image",
            filename.clone(),
            |mut file| async move {
                let mut res = self
                    .client
                    .get(format!(
                        "https://commons.wikimedia.org/wiki/Special:FilePath/{}",
                        filename.replace(" ", "_")
                    ))
                    .send()
                    .await?
                    .error_for_status()?;

                while let Some(chunk) = res.chunk().await? {
                    file.write_all(&chunk).await?;
                }
                Ok(())
            },
        )
        .await
    }
}