okh-scraper 2.4.1

A scraper of Open Source Hardware (OSH) projects. based on the Open Know-How (OKH) standard.
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// SPDX-FileCopyrightText: 2025 Robin Vobruba <hoijui.quaero@gmail.com>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::{
    collections::HashMap,
    sync::{Arc, LazyLock},
    time::Duration,
};

use async_trait::async_trait;
use futures::stream::BoxStream;
use reqwest::{
    header::{self, HeaderMap},
    Client,
};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use serde::Deserialize;
use serde_json::Value;
use thiserror::Error;

use crate::{
    files_finder,
    model::{
        hosting_provider_id::HostingProviderId,
        hosting_type::HostingType,
        hosting_unit_id::{self, HostingUnitId},
        project::{Project, ProjectId},
    },
    settings::PartialSettings,
};

mod appropedia;
mod manifests_list;
mod manifests_repo;
mod oshwa;
mod thingiverse;

pub type RL = governor::RateLimiter<
    governor::state::NotKeyed,
    governor::state::InMemoryState,
    governor::clock::QuantaClock,
    governor::middleware::NoOpMiddleware<governor::clock::QuantaInstant>,
>;

const DEFAULT_RETRIES: u32 = 3;
const DEFAULT_TIMEOUT: u64 = 10000;

pub trait Config {
    fn hosting_provider(&self) -> HostingProviderId;
}

pub trait RetryConfig: Config {
    /// Number of retries for a specific fetch,
    /// e.g. a batch or single project.
    fn retries(&self) -> Option<u32>;

    /// Total timeout per request in milliseconds (ms)
    fn timeout(&self) -> Option<u64>;
}

pub trait AccessControlConfig: Config {
    fn access_token(&self) -> &str;
}

/// Serves as a common base-type for typical,
/// (de-)centralized web (HTTP) hosting platforms.
///
/// This might be git forges like GitHub,
/// Wikis like Appropedia
/// or custom hardware project hosting platforms like
/// OSHWAs or Thingiverse.
#[derive(Deserialize, Debug)]
pub struct PlatformBaseConfig {
    hosting_provider: HostingProviderId,
    // scraper_type: String,
    /// Number of retries for a specific fetch,
    /// e.g. a batch or single project.
    retries: Option<u32>,
    /// Request timeout in milliseconds (ms)
    timeout: Option<u64>,
}

impl Config for PlatformBaseConfig {
    fn hosting_provider(&self) -> HostingProviderId {
        self.hosting_provider
    }
}

// impl DownloadCreator for PlatformBaseConfig {
//     fn create_downloader(&self) -> Arc<ClientWithMiddleware> {
//         Arc::new(
//             create_downloader(
//                 self.retries().unwrap_or(DEFAULT_RETRIES),
//                 self.timeout().unwrap_or(DEFAULT_TIMEOUT),
//             None))
//     }
// }

impl RetryConfig for PlatformBaseConfig {
    fn retries(&self) -> Option<u32> {
        self.retries
    }

    fn timeout(&self) -> Option<u64> {
        self.timeout
    }
}

/// same like [`PlatformBaseConfig`] but with access control.
#[derive(Deserialize, Debug)]
pub struct ACPlatformBaseConfig {
    hosting_provider: HostingProviderId,
    // scraper_type: String,
    /// Number of retries for a specific fetch,
    /// e.g. a batch or single project.
    retries: Option<u32>,
    /// Request timeout in milliseconds (ms)
    timeout: Option<u64>,
    /// Batch size when fetching multiple items (e.g. projects)
    batch_size: Option<usize>,
    access_token: String,
}

impl Config for ACPlatformBaseConfig {
    fn hosting_provider(&self) -> HostingProviderId {
        self.hosting_provider
    }
}

impl RetryConfig for ACPlatformBaseConfig {
    fn retries(&self) -> Option<u32> {
        self.retries
    }

    fn timeout(&self) -> Option<u64> {
        self.timeout
    }
}

impl AccessControlConfig for ACPlatformBaseConfig {
    fn access_token(&self) -> &str {
        &self.access_token
    }
}

/// Thrown when creating a new [`Scraper`] failed.
#[derive(Error, Debug)]
pub enum CreationError {
    #[error("Unknown scraper type: '{0}'")]
    UnknownScraperType(String),
    #[error("Invalid config for scraper type '{0}': {1:#?}")]
    InvalidConfig(String, Option<serde_json::Error>),
}

/// Thrown when a [`Scraper`] failed to scrape in general,
/// or while trying to scrape a single or a batch of projects.
#[derive(Error, Debug)]
pub enum Error {
    // #[error("Unknown scraper type: '{0}'")]
    // UnknownScraperType(String),
    // #[error("Invalid config for scraper type '{0}': {1}")]
    // InvalidConfig(String, Value),
    #[error("Failed to clone a git repo (synchronously): '{0}'")]
    GitClone(#[from] git2::Error),
    #[error("Some I/O problem: '{0}'")]
    IO(#[from] std::io::Error), // TODO Too low level to be here, and no circumstances info
    #[error("Reached (and surpassed) the API rate-limit")]
    RateLimitReached,
    #[error("API access blocked; reason: {0}")]
    ApiAccessBlocked(String),
    #[error("Failed to fetch a git repo (asynchronously): '{0}'")]
    GitFetch(#[from] asyncgit::Error),
    #[error("Failed to do git operation: '{0}'")]
    Git(String),
    #[error("Error while searching files in a local directory: '{0}'")]
    Find(#[from] files_finder::FindError),
    #[error("Network/Internet download failed: '{0}'")]
    Download(#[from] reqwest::Error),
    #[error("Network/Internet download failed: '{0}'")]
    DownloadMiddleware(#[from] reqwest_middleware::Error),
    #[error("{0} reached (and very likely surpassed) a total number of projects that is higher than the max fetch-limit set in its API ({1}); please inform the {0} admins!")]
    FetchLimitReached(HostingProviderId, usize),
    #[error("Failed to deserialize a fetched result to JSON:\n{0}\ncontent:\n{1}")]
    DeserializeAsJson(#[source] serde_json::Error, String),
    #[error(
        "Failed to deserialize a fetched JSON result to our Rust model of the expected type.\nerror:\n{0}\ncontent:\n{1}"
    )]
    Deserialize(#[source] serde_json::Error, String),
    #[error("Hosting technology (e.g. platform) API returned error: {0}")]
    HostingApiMsg(String),
    #[error("Project that was tired to scrape is not publicly visible, either on purpose by the authors, or because it is flagged as violating some rules.")]
    ProjectNotPublic,
    #[error("Project that was tired to scrape does not exist")]
    ProjectDoesNotExist,
    #[error("Project that was tired to scrape does not exist: {0}")]
    ProjectNotOpenSource(HostingUnitId),
    #[error("Project that was tired to scrape is not Open Source: {0}")]
    ProjectDoesNotExistId(HostingUnitId),
    #[error("Failed to parse a hosting URL to a hosting-unit-id: {0}")]
    HostingUnitIdParse(#[from] hosting_unit_id::ParseError),
    #[error("Failed to pull git repo (asynchronously): {0}")]
    GitAsyncPull(#[from] crossbeam_channel::RecvError),
}

impl Error {
    #[must_use]
    pub const fn aborts(&self) -> bool {
        match self {
            Self::FetchLimitReached(_, _)
            | Self::IO(_)
            | Self::RateLimitReached
            | Self::ApiAccessBlocked(_) => true,
            Self::GitClone(_)
            | Self::GitFetch(_)
            | Self::Git(_)
            | Self::Find(_)
            | Self::Download(_)
            | Self::DownloadMiddleware(_)
            | Self::DeserializeAsJson(_, _)
            | Self::Deserialize(_, _)
            | Self::HostingApiMsg(_)
            | Self::ProjectNotPublic
            | Self::ProjectNotOpenSource(_)
            | Self::ProjectDoesNotExist
            | Self::ProjectDoesNotExistId(_)
            | Self::HostingUnitIdParse(_)
            | Self::GitAsyncPull(_) => false,
        }
    }
}

/// Contains descriptive data about the type of a scraper.
pub struct TypeInfo {
    /// Machine-readable name/id of this type of scraper.
    /// It should be in "kebab-case".
    name: &'static str,

    /// Human-readable description of this type of scraper.
    description: &'static str,

    hosting_type: HostingType,
}

/// Creates instances of scrapers of a specific type.
pub trait Factory {
    /// Info about the type of scrapers produced by this factory.
    fn info(&self) -> &'static TypeInfo;

    /// Creates a new instance of this type of scraper,
    /// following the supplied configuration.
    ///
    /// # Errors
    ///
    /// - Unknown scraper type
    /// - Invalid config for scraper type
    fn create(
        &self,
        config_all: Arc<PartialSettings>,
        config_scraper: Value,
    ) -> Result<Box<dyn Scraper>, CreationError>;
}

/// A scraper of a specific type,
/// usually tailored to scrape projects from a specific hosting technology.
#[async_trait(?Send)]
pub trait Scraper {
    /// Info about this type of scraper.
    fn info(&self) -> &'static TypeInfo;

    /// Potentially infinite scraping of projects.
    ///
    /// It may fail and return on grave errors.
    async fn scrape(&self) -> BoxStream<'static, Result<Project, Error>>;
}

impl std::fmt::Display for dyn Scraper {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}-scraper", self.info().name)
    }
}

use rand::Rng;

fn generate_random_string() -> String {
    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.?/:_-";
    let mut rng = rand::rng();
    let length = rng.random_range(8..64);

    (0..length)
        .map(|_| {
            let idx = rng.random_range(0..CHARSET.len());
            CHARSET[idx] as char
        })
        .collect()
}

/// Creates a default set of headers for downloads.
/// @param authorization This is not the bare access token,
///   but already has to contain the "Bearer " prefix
fn create_headers(
    config_all: &PartialSettings,
    authorization: Option<String>,
) -> header::HeaderMap {
    let mut headers = header::HeaderMap::new();
    headers.insert(
        header::USER_AGENT,
        config_all
            .user_agent
            .clone()
            .unwrap_or_else(generate_random_string)
            .parse()
            .unwrap(),
    );
    if let Some(access_token_val) = authorization {
        // Consider marking security-sensitive headers with `set_sensitive`.
        let mut auth_value = header::HeaderValue::from_str(&access_token_val)
            .expect("Invalid HTTP Authorization/access-token value");
        auth_value.set_sensitive(true);
        headers.insert(header::AUTHORIZATION, auth_value);
    }
    headers
}

/// Creates a new [`reqwest::Client`] with the supplied retry and timeout settings.
/// @param retries Number of retries for a single fetch
/// @param timeout Total timeout per request in milliseconds (ms)
fn create_downloader(
    retries: u32,
    timeout: u64,
    headers: Option<header::HeaderMap>,
) -> ClientWithMiddleware {
    let retry_policy = ExponentialBackoff::builder().build_with_max_retries(retries);
    let mut client_builder = Client::builder().timeout(Duration::from_millis(timeout));
    if let Some(headers_val) = headers {
        client_builder = client_builder.default_headers(headers_val);
    }
    ClientBuilder::new(client_builder.build().unwrap())
        .with(RetryTransientMiddleware::new_with_policy(retry_policy))
        .build()
}

pub fn create_downloader_retry(config: &impl RetryConfig) -> Arc<ClientWithMiddleware> {
    Arc::new(create_downloader(
        config.retries().unwrap_or(DEFAULT_RETRIES),
        config.timeout().unwrap_or(DEFAULT_TIMEOUT),
        None,
    ))
}

fn create_downloader_ac_retries(
    config_all: &PartialSettings,
    config: &impl AccessControlConfig,
    retries: u32,
    timeout: u64,
) -> Arc<ClientWithMiddleware> {
    let authorization = Some(format!("Bearer {}", config.access_token()));
    Arc::new(create_downloader(
        retries,
        timeout,
        Some(create_headers(config_all, authorization)),
    ))
}

pub fn create_downloader_ac(
    config_all: &PartialSettings,
    config: &impl AccessControlConfig,
) -> Arc<ClientWithMiddleware> {
    create_downloader_ac_retries(config_all, config, DEFAULT_RETRIES, DEFAULT_TIMEOUT)
}

pub fn create_downloader_retry_ac<T: RetryConfig + AccessControlConfig>(
    config_all: &PartialSettings,
    config: &T,
) -> Arc<ClientWithMiddleware> {
    create_downloader_ac_retries(
        config_all,
        config,
        config.retries().unwrap_or(DEFAULT_RETRIES),
        config.timeout().unwrap_or(DEFAULT_TIMEOUT),
    )
}

// pub fn assemble_scraper_factories() -> HashMap<String, impl ScraperFactory> {
//     let scrapers = vec![oshwa::ScraperFactory, appropedia::ScraperFactory];
//     scrapers.into_iter().map(|f| (f.name().to_string(), f)).collect()
// }

#[must_use]
pub fn assemble_factories() -> HashMap<String, Box<dyn Factory>> {
    let scrapers: Vec<Box<dyn Factory>> = vec![
        Box::new(oshwa::ScraperFactory),
        Box::new(appropedia::ScraperFactory),
        Box::new(manifests_list::ScraperFactory),
        Box::new(manifests_repo::ScraperFactory),
        Box::new(thingiverse::ScraperFactory),
    ];
    scrapers
        .into_iter()
        .map(|f| (f.info().name.to_string(), f))
        .collect()
}

// macro_rules! yield_err {
//     ($res:expr) => {
//         match $res {
//             Err(err) => yield Err(err.into()),
//             Ok(value) => {},
//         }
//     };
// }

// pub(crate) use yield_err;

macro_rules! ok_or_return_err_stream {
    ($res:expr) => {
        match $res {
            Err(err) => {
                return stream! {
                    yield Err(err.into())
                }
                .boxed()
            }
            Ok(value) => value,
        }
    };
}

pub(crate) use ok_or_return_err_stream;