2023-03-22 22:56:07 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021–2022 Marceline Cramer <mars@tebibyte.media>
|
2023-03-23 04:18:42 +00:00
|
|
|
|
* Copyright (c) 2022–2023 Emma Tebibyte <emma@tebibyte.media>
|
2023-03-22 22:56:07 +00:00
|
|
|
|
* Copyright (c) 2022 Spookdot <https://git.tebibyte.media/spookdot/>
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
*
|
|
|
|
|
* This file is part of Hopper.
|
|
|
|
|
*
|
|
|
|
|
* Hopper is free software: you can redistribute it and/or modify it under the
|
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
|
* version.
|
|
|
|
|
*
|
|
|
|
|
* Hopper is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
|
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
|
* Hopper. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2023-03-23 04:18:42 +00:00
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
api::{
|
|
|
|
|
ModInfo,
|
|
|
|
|
ModResult,
|
|
|
|
|
ModVersion,
|
|
|
|
|
ModVersionFile,
|
|
|
|
|
SearchResponse,
|
|
|
|
|
Error as APIError,
|
|
|
|
|
},
|
|
|
|
|
config::{
|
|
|
|
|
Config,
|
|
|
|
|
},
|
|
|
|
|
args::{
|
|
|
|
|
Arguments,
|
|
|
|
|
Loader,
|
|
|
|
|
PackageType,
|
|
|
|
|
Server,
|
|
|
|
|
SearchArgs,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use std::cmp::min;
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
2023-03-24 18:50:10 +00:00
|
|
|
|
use curl::easy::{ Easy };
|
2023-03-23 04:18:42 +00:00
|
|
|
|
use futures_util::StreamExt;
|
|
|
|
|
|
|
|
|
|
pub struct HopperClient {
|
|
|
|
|
config: Config,
|
2023-03-24 18:50:10 +00:00
|
|
|
|
client: Easy,
|
2023-03-23 04:18:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl HopperClient {
|
|
|
|
|
pub fn new(config: Config) -> Self {
|
2023-03-24 18:50:10 +00:00
|
|
|
|
curl::init();
|
2023-03-23 04:18:42 +00:00
|
|
|
|
Self {
|
|
|
|
|
config: config,
|
2023-03-25 23:21:15 +00:00
|
|
|
|
client: Easy::new(),
|
2023-03-23 04:18:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn search_mods(
|
2023-03-24 18:50:10 +00:00
|
|
|
|
&mut self,
|
2023-03-23 04:18:42 +00:00
|
|
|
|
search_args: &SearchArgs,
|
2023-03-24 18:50:10 +00:00
|
|
|
|
) /*-> Result<SearchResponse, (String, u32)>*/ {
|
2023-03-23 04:18:42 +00:00
|
|
|
|
println!("Searching with query “{}”...", search_args.package_name);
|
|
|
|
|
|
2023-03-24 18:50:10 +00:00
|
|
|
|
let mut urls = Vec::new();
|
2023-03-23 04:18:42 +00:00
|
|
|
|
|
2023-03-25 09:03:27 +00:00
|
|
|
|
for entry in self.config.sources.modrinth.iter() {
|
|
|
|
|
urls.push(format!("{}/v2/search", entry));
|
2023-03-23 04:18:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut params = vec![("query", search_args.package_name.to_owned())];
|
|
|
|
|
let mut facets: Vec<String> = Vec::new();
|
|
|
|
|
if let versions = &search_args.mc_version {
|
|
|
|
|
let versions_facets = versions
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|e| format!("[\"versions:{}\"]", e))
|
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
|
.join(",");
|
|
|
|
|
facets.push(format!("{}", versions_facets));
|
|
|
|
|
}
|
2023-03-25 10:11:43 +00:00
|
|
|
|
if let package_type = search_args.package_type {
|
2023-03-23 04:18:42 +00:00
|
|
|
|
let project_type = match package_type {
|
2023-03-25 10:11:43 +00:00
|
|
|
|
PackageType::Dummy => "",
|
2023-03-23 04:18:42 +00:00
|
|
|
|
PackageType::Mod(_) => "[\"project_type:mod\"]",
|
|
|
|
|
PackageType::Pack(_) => "[\"project_type:modpack\"]",
|
|
|
|
|
PackageType::Plugin(_) => "[\"project_type:mod\"]",
|
|
|
|
|
PackageType::ResourcePack => "[\"project_type:resourcepack\"]",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let project_category = match package_type {
|
2023-03-25 10:11:43 +00:00
|
|
|
|
PackageType::Dummy => "",
|
2023-03-23 04:18:42 +00:00
|
|
|
|
PackageType::Mod(kind) | PackageType::Pack(kind) => {
|
|
|
|
|
match kind {
|
|
|
|
|
Loader::Fabric => "[\"categories:fabric\"]",
|
|
|
|
|
Loader::Forge => "[\"categories:forge\"]",
|
|
|
|
|
Loader::Quilt => "[\"categories:quilt\"]",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
PackageType::Plugin(kind) => {
|
|
|
|
|
match kind {
|
|
|
|
|
Server::Bukkit => "[\"categories:bukkit\"]",
|
|
|
|
|
Server::Paper => "[\"categories:paper\"]",
|
|
|
|
|
Server::Purpur => "[\"categories:purpur\"]",
|
|
|
|
|
Server::Spigot => "[\"categories:spigot\"]",
|
|
|
|
|
Server::Sponge => "[\"categories:sponge\"]",
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-24 18:50:10 +00:00
|
|
|
|
PackageType::ResourcePack => "",
|
2023-03-23 04:18:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let package_type_facet = format!(
|
|
|
|
|
"{},{}",
|
|
|
|
|
project_type,
|
|
|
|
|
project_category,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
facets.push(package_type_facet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !facets.is_empty() {
|
|
|
|
|
params.push(("facets", format!("[{}]", facets.join(","))));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|