From f8eb0fd2e007ac299d0342cfe7d2ed595cc869b3 Mon Sep 17 00:00:00 2001 From: TOwInOK <60252419+TOwInOK@users.noreply.github.com> Date: Sat, 2 Mar 2024 20:27:41 +0700 Subject: [PATCH] change perspectiv of code --- Cargo.lock | 453 ++------------------------------------------------- Cargo.toml | 2 +- src/main.rs | 231 ++------------------------ src/model.rs | 67 ++++++++ 4 files changed, 104 insertions(+), 649 deletions(-) create mode 100644 src/model.rs diff --git a/Cargo.lock b/Cargo.lock index dfe027f..07c124a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,8 +7,8 @@ name = "MinecraftAddonController" version = "0.0.1" dependencies = [ "reqwest", - "scraper", "serde", + "serde_json", "tokio", "toml", ] @@ -28,19 +28,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "ahash" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91429305e9f0a25f6205c5b8e0d2db09e0708a7a6df0f42212bb56c32c8ac97a" -dependencies = [ - "cfg-if", - "getrandom", - "once_cell", - "version_check", - "zerocopy", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -86,12 +73,6 @@ version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" -[[package]] -name = "byteorder" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" - [[package]] name = "bytes" version = "1.5.0" @@ -129,61 +110,6 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" -[[package]] -name = "cssparser" -version = "0.31.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b3df4f93e5fbbe73ec01ec8d3f68bba73107993a5b1e7519273c32db9b0d5be" -dependencies = [ - "cssparser-macros", - "dtoa-short", - "itoa", - "phf 0.11.2", - "smallvec", -] - -[[package]] -name = "cssparser-macros" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331" -dependencies = [ - "quote", - "syn 2.0.39", -] - -[[package]] -name = "derive_more" -version = "0.99.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "dtoa" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcbb2bf8e87535c23f7a8a321e364ce21462d0ff10cb6407820e8e96dfff6653" - -[[package]] -name = "dtoa-short" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbaceec3c6e4211c79e7b1800fb9680527106beb2f9c51904a3210c03a448c74" -dependencies = [ - "dtoa", -] - -[[package]] -name = "ego-tree" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591" - [[package]] name = "encoding_rs" version = "0.8.33" @@ -245,16 +171,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "futf" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df420e2e84819663797d1ec6544b13c5be84629e7bb00dc960d6917db2987843" -dependencies = [ - "mac", - "new_debug_unreachable", -] - [[package]] name = "futures-channel" version = "0.3.29" @@ -294,35 +210,6 @@ dependencies = [ "pin-utils", ] -[[package]] -name = "fxhash" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" -dependencies = [ - "byteorder", -] - -[[package]] -name = "getopts" -version = "0.2.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" -dependencies = [ - "unicode-width", -] - -[[package]] -name = "getrandom" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - [[package]] name = "gimli" version = "0.28.1" @@ -360,20 +247,6 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" -[[package]] -name = "html5ever" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bea68cab48b8459f17cf1c944c67ddc572d272d9f2b274140f223ecb1da4a3b7" -dependencies = [ - "log", - "mac", - "markup5ever", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "http" version = "0.2.11" @@ -520,26 +393,6 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" -[[package]] -name = "mac" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4" - -[[package]] -name = "markup5ever" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a2629bb1404f3d34c2e921f21fd34ba00b206124c81f65c50b43b6aaefeb016" -dependencies = [ - "log", - "phf 0.10.1", - "phf_codegen", - "string_cache", - "string_cache_codegen", - "tendril", -] - [[package]] name = "memchr" version = "2.6.4" @@ -590,12 +443,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "new_debug_unreachable" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" - [[package]] name = "num_cpus" version = "1.16.0" @@ -644,7 +491,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn", ] [[package]] @@ -694,86 +541,6 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" -[[package]] -name = "phf" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fabbf1ead8a5bcbc20f5f8b939ee3f5b0f6f281b6ad3468b84656b658b455259" -dependencies = [ - "phf_shared 0.10.0", -] - -[[package]] -name = "phf" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" -dependencies = [ - "phf_macros", - "phf_shared 0.11.2", -] - -[[package]] -name = "phf_codegen" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb1c3a8bc4dd4e5cfce29b44ffc14bedd2ee294559a294e2a4d4c9e9a6a13cd" -dependencies = [ - "phf_generator 0.10.0", - "phf_shared 0.10.0", -] - -[[package]] -name = "phf_generator" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6" -dependencies = [ - "phf_shared 0.10.0", - "rand", -] - -[[package]] -name = "phf_generator" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" -dependencies = [ - "phf_shared 0.11.2", - "rand", -] - -[[package]] -name = "phf_macros" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" -dependencies = [ - "phf_generator 0.11.2", - "phf_shared 0.11.2", - "proc-macro2", - "quote", - "syn 2.0.39", -] - -[[package]] -name = "phf_shared" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096" -dependencies = [ - "siphasher", -] - -[[package]] -name = "phf_shared" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" -dependencies = [ - "siphasher", -] - [[package]] name = "pin-project-lite" version = "0.2.13" @@ -792,66 +559,24 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" -[[package]] -name = "ppv-lite86" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" - -[[package]] -name = "precomputed-hash" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" - [[package]] name = "proc-macro2" -version = "1.0.70" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "libc", - "rand_chacha", - "rand_core", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom", -] - [[package]] name = "redox_syscall" version = "0.4.1" @@ -939,22 +664,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "scraper" -version = "0.18.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "585480e3719b311b78a573db1c9d9c4c1f8010c2dee4cc59c2efe58ea4dbc3e1" -dependencies = [ - "ahash", - "cssparser", - "ego-tree", - "getopts", - "html5ever", - "once_cell", - "selectors", - "tendril", -] - [[package]] name = "security-framework" version = "2.9.2" @@ -978,50 +687,31 @@ dependencies = [ "libc", ] -[[package]] -name = "selectors" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eb30575f3638fc8f6815f448d50cb1a2e255b0897985c8c59f4d37b72a07b06" -dependencies = [ - "bitflags 2.4.1", - "cssparser", - "derive_more", - "fxhash", - "log", - "new_debug_unreachable", - "phf 0.10.1", - "phf_codegen", - "precomputed-hash", - "servo_arc", - "smallvec", -] - [[package]] name = "serde" -version = "1.0.193" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn", ] [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" dependencies = [ "itoa", "ryu", @@ -1049,15 +739,6 @@ dependencies = [ "serde", ] -[[package]] -name = "servo_arc" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d036d71a959e00c77a63538b90a6c2390969f9772b096ea837205c6bd0491a44" -dependencies = [ - "stable_deref_trait", -] - [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -1067,12 +748,6 @@ dependencies = [ "libc", ] -[[package]] -name = "siphasher" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" - [[package]] name = "slab" version = "0.4.9" @@ -1108,54 +783,11 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "stable_deref_trait" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" - -[[package]] -name = "string_cache" -version = "0.8.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f91138e76242f575eb1d3b38b4f1362f10d3a43f47d182a5b359af488a02293b" -dependencies = [ - "new_debug_unreachable", - "once_cell", - "parking_lot", - "phf_shared 0.10.0", - "precomputed-hash", - "serde", -] - -[[package]] -name = "string_cache_codegen" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bb30289b722be4ff74a408c3cc27edeaad656e06cb1fe8fa9231fa59c728988" -dependencies = [ - "phf_generator 0.10.0", - "phf_shared 0.10.0", - "proc-macro2", - "quote", -] - -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" -version = "2.0.39" +version = "2.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" dependencies = [ "proc-macro2", "quote", @@ -1196,17 +828,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "tendril" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d24a120c5fc464a3458240ee02c299ebcb9d67b5249c8848b09d639dca8d7bb0" -dependencies = [ - "futf", - "mac", - "utf-8", -] - [[package]] name = "tinyvec" version = "1.6.0" @@ -1249,7 +870,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn", ] [[package]] @@ -1362,12 +983,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-width" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" - [[package]] name = "url" version = "2.5.0" @@ -1379,24 +994,12 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "utf-8" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" - [[package]] name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - [[package]] name = "want" version = "0.3.1" @@ -1433,7 +1036,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.39", + "syn", "wasm-bindgen-shared", ] @@ -1467,7 +1070,7 @@ checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" dependencies = [ "proc-macro2", "quote", - "syn 2.0.39", + "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1660,23 +1263,3 @@ dependencies = [ "cfg-if", "windows-sys 0.48.0", ] - -[[package]] -name = "zerocopy" -version = "0.7.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f43de342578a3a14a9314a2dab1942cbfcbe5686e1f91acdc513058063eafe18" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.7.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1012d89e3acb79fad7a799ce96866cfb8098b74638465ea1b1533d35900ca90" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.39", -] diff --git a/Cargo.toml b/Cargo.toml index 4a59eab..b24770e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] reqwest = "0.11.22" -scraper = "0.18.1" serde = { version = "1.0.193", features = ["derive"] } +serde_json = "1.0.114" tokio = { version = "1.34.0", features = ["full"] } toml = "0.8.8" diff --git a/src/main.rs b/src/main.rs index 31fb4eb..fec549f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ -use reqwest::Client; -use scraper::{Html, Selector}; use serde::{Deserialize, Serialize}; use std::fs; +mod model; +use model::*; #[tokio::main] async fn main() { @@ -12,195 +12,6 @@ async fn main() { let path = "./config.toml".to_string(); let config = load_config(path); - // //Metadata for plugins to download - // let data = MetaData { - // name: "simple-voice-chat".to_string().to_lowercase(), - // version: "1.20.2".to_string(), - // core: "paper".to_string().to_lowercase(), - // actuality: "release".to_string().to_lowercase(), - // }; - - // //List of links for download - // let list = finder(&client, data.clone()).await; - - // if let Some(list) = list { - // list.iter().for_each(|x| { - // println!("{:?}, {:?}, {:?}, {:?}", x.code, x.version, x.name, x.link); - // }); - // } - - // let one = finder_first(&client, data).await; - // if let Some(one) = one{ - // println!("{:?}, {:?}, {:?}, {:?}", one.code, one.version, one.name, one.link); - // }else { - // println!("not found"); - // } -} - -/// выдёт список ссылок плагинов на скачивание -/// {```Code, Version, Name, Link```} -async fn finder(client: &Client, data: MetaData) -> Option> { - let url = format!( - "https://modrinth.com/{}/{}/versions?c={}&g={}&l={}", - data.is, data.name, data.actuality, data.version, data.core - ); - let response = client - .get(url) - .send() - .await - .expect("Failed to get the response"); - let body = match response.text_with_charset("utf-8").await { - Ok(body) => body, - Err(_) => { - return None; - } - }; - let fragment = Html::parse_document(&body); - let selector = match Selector::parse(r"#all-versions a:not(style)") { - Ok(selector) => selector, - Err(_) => return None, - }; - let mut url_list: Vec = Vec::new(); - for element in fragment.select(&selector) { - let url: &str = element.value().attr("href")?; - if url.starts_with("https://cdn.modrinth.com/data/") - && element.value().attr("aria-label").is_some() - { - if url_list.iter().any(|x| x.link == url) { - continue; - } - let label = element.value().attr("aria-label")?; - url_list.push(PluginInfo { - code: url.split('/').collect::>()[4].to_owned(), - name: label[9..].to_owned(), - link: url.to_owned(), - version: url.split('/').collect::>()[6].to_owned(), - }); - } - } - Some(url_list) -} - -///Поиск плагина и вывод его одиночной актуальной версии -async fn finder_first(client: &Client, data: MetaData) -> Option { - let url = format!( - "https://modrinth.com/{}/{}/versions?c={}&g={}&l={}", - data.is, data.name, data.actuality, data.version, data.core - ); - let response = client - .get(url) - .send() - .await - .expect("Failed to get the response"); - let body = match response.text_with_charset("utf-8").await { - Ok(body) => body, - Err(_) => { - return None; - } - }; - let fragment = Html::parse_document(&body); - let selector = match Selector::parse(r"#all-versions a:not(style)") { - Ok(selector) => selector, - Err(_) => return None, - }; - if let Some(element) = fragment.select(&selector).next() { - let url: &str = element.value().attr("href")?; - if url.starts_with("https://cdn.modrinth.com/data/") - && element.value().attr("aria-label").is_some() - { - let label = element.value().attr("aria-label")?; - let plugin = PluginInfo { - code: url.split('/').collect::>()[4].to_owned(), - name: label[9..].to_owned(), - link: url.to_owned(), - version: url.split('/').collect::>()[6].to_owned(), - }; - return Some(plugin); - } else { - return None; - } - } - None -} - -/// Отдаём конфиг и скачиаем -async fn catcher(client: Client, config: Config) { - let plugins: Vec = { - match config.plugins { - Some(p) => { - let mut plugin_list: Vec = Vec::new(); - // Создаём более интуитивный вектор - let plugin_collections = vec![p.modrinth, p.paper, p.spigot]; - //Если есть frozen, то проходимся по нему и удаляем названия - if let Some(f) = p.frozen { - for c in plugin_collections.into_iter().flatten() { - check_frozen(&mut plugin_list, &f, c).await; - } - } - // Если нет frozen, то просто проходимся и удаляем названия - else { - for c in plugin_collections.into_iter().flatten() { - plugin_list.extend(c) - } - } - plugin_list - } - None => { - println!("Конфигурация плагинов не была обнаружена"); - let plugin_list: Vec = Vec::new(); - plugin_list - } - } - }; - let datapacks: Vec = { - match config.datapacks { - Some(d) => { - let mut datapack_list: Vec = Vec::new(); - let datapack_collections = vec![d.modrinth, d.paper, d.spigot]; - //Если есть frozen, то проходимся по нему удаляя и добавляя названия - if let Some(f) = d.frozen { - for c in datapack_collections.into_iter().flatten() { - check_frozen(&mut datapack_list, &f, c).await; - } - } - // Если нет frozen, то просто проходимся добавляя названия - else { - for c in datapack_collections.into_iter().flatten() { - datapack_list.extend(c) - } - } - datapack_list - } - None => { - println!("Конфигурация датапаков не была обнаружена"); - let datapack_list: Vec = Vec::new(); - datapack_list - } - } - }; - todo!("Скачиваем ссылки из download") -} - -async fn download() { - todo!() -} - -async fn check_frozen( - list_to_add: &mut Vec, - words_to_remove: &[String], - dirty_list: Vec, -) { - // Создаем временный вектор для хранения слов, которые нужно оставить - let mut temp_vec: Vec = Vec::new(); - - // Проверяем каждое слово в plugin_list - for word in dirty_list.iter() { - // Если слово не содержится в words_to_remove, добавляем его в temp_vec - if !words_to_remove.contains(word) { - temp_vec.push(word.clone()); - } - } - list_to_add.extend(temp_vec); } async fn load_config(path: String) -> Config { @@ -252,28 +63,7 @@ async fn load_config(path: String) -> Config { config } -struct PluginInfo { - code: String, - version: String, - name: String, - link: String, -} - -struct DatapackInfo { - code: String, - version: String, - name: String, - link: String, -} - -struct MetaData { - name: String, - core: String, - version: String, - actuality: String, - is: String, -} - +///Struct to load config from toml file. #[derive(Deserialize, Serialize)] struct Config { version: Version, @@ -281,24 +71,39 @@ struct Config { datapacks: Option, } + +///Settings of server core #[derive(Deserialize, Serialize)] struct Version { + //version of core core: String, + //version version: String, + //stop update this version of core frozen: Option, } +///Lists of plugins #[derive(Deserialize, Serialize)] struct Plugin { + //list to download from https://modrinth.com/ modrinth: Option>, + //list to download from https://www.spigotmc.org/ spigot: Option>, + //list to download from https://hangar.papermc.io/ paper: Option>, + //List of plugins to stop updating frozen: Option>, } +///Lists of Datapacks #[derive(Deserialize, Serialize)] struct Datapack { + //list to download from https://modrinth.com/ modrinth: Option>, + //list to download from https://www.spigotmc.org/ spigot: Option>, + //list to download from https://hangar.papermc.io/ paper: Option>, + //List of plugins to stop updating frozen: Option>, } diff --git a/src/model.rs b/src/model.rs new file mode 100644 index 0000000..8ba3421 --- /dev/null +++ b/src/model.rs @@ -0,0 +1,67 @@ +use serde::Deserialize; +use serde::Serialize; +use serde_json::Value; + +pub type Root = Vec; + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Root2 { + #[serde(rename = "game_versions")] + pub game_versions: Vec, + pub loaders: Vec, + pub id: String, + #[serde(rename = "project_id")] + pub project_id: String, + #[serde(rename = "author_id")] + pub author_id: String, + pub featured: bool, + pub name: String, + #[serde(rename = "version_number")] + pub version_number: String, + pub changelog: String, + #[serde(rename = "changelog_url")] + pub changelog_url: Value, + #[serde(rename = "date_published")] + pub date_published: String, + pub downloads: i64, + #[serde(rename = "version_type")] + pub version_type: String, + pub status: String, + #[serde(rename = "requested_status")] + pub requested_status: Value, + pub files: Vec, + pub dependencies: Vec, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct File { + pub hashes: Hashes, + pub url: String, + pub filename: String, + pub primary: bool, + pub size: i64, + #[serde(rename = "file_type")] + pub file_type: Value, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Hashes { + pub sha512: String, + pub sha1: String, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Dependency { + #[serde(rename = "version_id")] + pub version_id: Value, + #[serde(rename = "project_id")] + pub project_id: String, + #[serde(rename = "file_name")] + pub file_name: Value, + #[serde(rename = "dependency_type")] + pub dependency_type: String, +}