From 62960ddeb858b3d6fd6acdcbcb5d89d571fd0dee Mon Sep 17 00:00:00 2001 From: euzu Date: Sun, 8 Oct 2023 12:23:04 +0200 Subject: [PATCH] removed polodb, now writing to files --- Cargo.lock | 311 ++------------------------------------- Cargo.toml | 2 +- src/main.rs | 2 +- src/repository.rs | 200 +++++++++++++------------ src/xtream_parser.rs | 2 +- src/xtream_player_api.rs | 26 ++-- 6 files changed, 138 insertions(+), 405 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dd35b7d05..b0e7d4a54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,8 +67,8 @@ dependencies = [ "actix-rt", "actix-service", "actix-utils", - "ahash 0.7.6", - "base64 0.21.0", + "ahash", + "base64", "bitflags", "brotli", "bytes", @@ -184,7 +184,7 @@ dependencies = [ "actix-service", "actix-utils", "actix-web-codegen", - "ahash 0.7.6", + "ahash", "bytes", "bytestring", "cfg-if", @@ -239,18 +239,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "ahash" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" -dependencies = [ - "cfg-if", - "getrandom", - "once_cell", - "version_check", -] - [[package]] name = "aho-corasick" version = "1.1.1" @@ -290,24 +278,12 @@ version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" -[[package]] -name = "atomic" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c59bdb34bc650a32731b31bd8f0829cc15d24a708ee31559e0bb34f2bc320cba" - [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" -[[package]] -name = "base64" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" - [[package]] name = "base64" version = "0.21.0" @@ -320,18 +296,6 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "bitvec" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" -dependencies = [ - "funty", - "radium", - "tap", - "wyz", -] - [[package]] name = "block-buffer" version = "0.10.3" @@ -362,39 +326,12 @@ dependencies = [ "alloc-stdlib", ] -[[package]] -name = "bson" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58da0ae1e701ea752cc46c1bb9f39d5ecefc7395c3ecd526261a566d4f16e0c2" -dependencies = [ - "ahash 0.8.3", - "base64 0.13.1", - "bitvec", - "hex", - "indexmap 1.9.3", - "js-sys", - "once_cell", - "rand", - "serde", - "serde_bytes", - "serde_json", - "time 0.3.17", - "uuid", -] - [[package]] name = "bumpalo" version = "3.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" -[[package]] -name = "byteorder" -version = "1.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" - [[package]] name = "bytes" version = "1.4.0" @@ -538,12 +475,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "crc64fast" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd3d10c557924c719a61e583165e31a0d8131b20415d6858983e67d1eb8a82bd" - [[package]] name = "cron" version = "0.12.0" @@ -661,12 +592,6 @@ dependencies = [ "syn 1.0.107", ] -[[package]] -name = "equivalent" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" - [[package]] name = "errno" version = "0.2.8" @@ -743,12 +668,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "funty" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" - [[package]] name = "futures" version = "0.3.26" @@ -855,10 +774,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ "cfg-if", - "js-sys", "libc", "wasi 0.11.0+wasi-snapshot-preview1", - "wasm-bindgen", ] [[package]] @@ -873,7 +790,7 @@ dependencies = [ "futures-sink", "futures-util", "http", - "indexmap 1.9.3", + "indexmap", "slab", "tokio", "tokio-util", @@ -886,21 +803,6 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -[[package]] -name = "hashbrown" -version = "0.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" -dependencies = [ - "ahash 0.8.3", -] - -[[package]] -name = "hashbrown" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" - [[package]] name = "heck" version = "0.4.1" @@ -922,12 +824,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" -[[package]] -name = "hex" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" - [[package]] name = "http" version = "0.2.8" @@ -1046,18 +942,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", - "hashbrown 0.12.3", - "serde", -] - -[[package]] -name = "indexmap" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad227c3af19d4914570ad36d30409928b75967c298feb9ea1969db3a610bb14e" -dependencies = [ - "equivalent", - "hashbrown 0.14.1", + "hashbrown", ] [[package]] @@ -1191,15 +1076,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "lz4_flex" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b8c72594ac26bfd34f2d99dfced2edfaddfe8a476e3ff2ca0eb293d925c4f83" -dependencies = [ - "twox-hash", -] - [[package]] name = "m3u-filter" version = "1.0.1" @@ -1214,12 +1090,12 @@ dependencies = [ "cron", "enum-iterator", "futures", + "mime", "openssl", "path-absolutize", "pest", "pest_derive", "petgraph", - "polodb_core", "regex", "reqwest", "serde", @@ -1235,20 +1111,11 @@ version = "2.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" -[[package]] -name = "memmap2" -version = "0.5.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327" -dependencies = [ - "libc", -] - [[package]] name = "mime" -version = "0.3.16" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" [[package]] name = "mime_guess" @@ -1344,27 +1211,6 @@ dependencies = [ "libc", ] -[[package]] -name = "num_enum" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a015b430d3c108a207fd776d2e2196aaf8b1cf8cf93253e3a097ff3085076a1" -dependencies = [ - "num_enum_derive", -] - -[[package]] -name = "num_enum_derive" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96667db765a921f7b295ffee8b60472b686a51d4f21c2ee4ffdb94c7013b65a6" -dependencies = [ - "proc-macro-crate", - "proc-macro2", - "quote", - "syn 2.0.37", -] - [[package]] name = "once_cell" version = "1.17.1" @@ -1536,7 +1382,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dd7d28ee937e54fe3080c91faa1c3a46c06de6252988a7f4592ba2310ef22a4" dependencies = [ "fixedbitset", - "indexmap 1.9.3", + "indexmap", ] [[package]] @@ -1557,51 +1403,12 @@ version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" -[[package]] -name = "polodb_core" -version = "4.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fef3dbdf0bd44fa0d6e5630bc02f721f8238f7e58a0b50661c099fc759d355de" -dependencies = [ - "bson", - "byteorder", - "crc64fast", - "getrandom", - "hashbrown 0.13.2", - "indexmap 1.9.3", - "js-sys", - "libc", - "lz4_flex", - "memmap2", - "num_enum", - "regex", - "serde", - "serde-wasm-bindgen", - "smallvec", - "thiserror", - "uuid", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", - "winapi", -] - [[package]] name = "ppv-lite86" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" -[[package]] -name = "proc-macro-crate" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" -dependencies = [ - "once_cell", - "toml_edit", -] - [[package]] name = "proc-macro-error" version = "1.0.4" @@ -1644,12 +1451,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "radium" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" - [[package]] name = "rand" version = "0.8.5" @@ -1733,7 +1534,7 @@ version = "0.11.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "21eed90ec8570952d53b772ecf8f206aa1ec9a3d76b2521c56c42973f2d91ee9" dependencies = [ - "base64 0.21.0", + "base64", "bytes", "encoding_rs", "futures-core", @@ -1852,26 +1653,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-wasm-bindgen" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3b143e2833c57ab9ad3ea280d21fd34e285a42837aeb0ee301f4f41890fa00e" -dependencies = [ - "js-sys", - "serde", - "wasm-bindgen", -] - -[[package]] -name = "serde_bytes" -version = "0.11.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab33ec92f677585af6d88c65593ae2375adde54efdbf16d597f2cbc7a6d368ff" -dependencies = [ - "serde", -] - [[package]] name = "serde_derive" version = "1.0.188" @@ -1889,7 +1670,6 @@ version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cad406b69c91885b5107daf2c29572f6c8cdb3c66826821e286c533490c0bc76" dependencies = [ - "indexmap 1.9.3", "itoa", "ryu", "serde", @@ -1913,7 +1693,7 @@ version = "0.9.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fb06d4b6cdaef0e0c51fa881acb721bed3c924cfaa71d9c94a3b771dfdf6567" dependencies = [ - "indexmap 1.9.3", + "indexmap", "itoa", "ryu", "serde", @@ -1976,12 +1756,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "strsim" version = "0.10.0" @@ -2010,12 +1784,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "tap" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" - [[package]] name = "tempfile" version = "3.3.0" @@ -2155,23 +1923,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "toml_datetime" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" - -[[package]] -name = "toml_edit" -version = "0.19.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" -dependencies = [ - "indexmap 2.0.1", - "toml_datetime", - "winnow", -] - [[package]] name = "tower-service" version = "0.3.2" @@ -2205,16 +1956,6 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" -[[package]] -name = "twox-hash" -version = "1.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" -dependencies = [ - "cfg-if", - "static_assertions", -] - [[package]] name = "typenum" version = "1.16.0" @@ -2286,18 +2027,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "uuid" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" -dependencies = [ - "atomic", - "getrandom", - "serde", - "wasm-bindgen", -] - [[package]] name = "vcpkg" version = "0.2.15" @@ -2520,15 +2249,6 @@ version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" -[[package]] -name = "winnow" -version = "0.5.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c2e3184b9c4e92ad5167ca73039d0c42476302ab603e2fec4487511f38ccefc" -dependencies = [ - "memchr", -] - [[package]] name = "winreg" version = "0.10.1" @@ -2538,15 +2258,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "wyz" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" -dependencies = [ - "tap", -] - [[package]] name = "zstd" version = "0.12.3+zstd.1.5.2" diff --git a/Cargo.toml b/Cargo.toml index a8684f1f6..09ee4fdbb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,4 +34,4 @@ enum-iterator = "1.3" unidecode = "0" petgraph = "0" openssl = { version = "*", features = ["vendored"] } #https://docs.rs/openssl/0.10.34/openssl/#vendored -polodb_core = "4.4.0" +mime = "0.3.17" diff --git a/src/main.rs b/src/main.rs index c0388ebde..699faad0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,7 +137,7 @@ fn read_mapping(mapping_file: &str, verbose: bool) -> Option { } } -fn read_user(user_file: &str, _verbose: bool) -> Option { +fn read_user(user_file: &str, verbose: bool) -> Option { match utils::open_file(&std::path::PathBuf::from(user_file), false) { Some(file) => { let mapping: Result = serde_yaml::from_reader(file); diff --git a/src/repository.rs b/src/repository.rs index 65d5eec98..0858948a2 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -1,6 +1,10 @@ -use polodb_core::bson::{Bson, doc, Document}; -use polodb_core::{Collection, Database}; -use serde_json::Value; +use std::fs; +use std::fs::File; +use std::io::{BufWriter, Error, Write}; +use std::iter::FromIterator; +use std::path::PathBuf; +use serde::Serialize; +use serde_json::{json, Value}; use crate::config::{Config, ConfigTarget}; use crate::messaging::send_message; use crate::model_m3u::{PlaylistGroup, XtreamCluster}; @@ -13,126 +17,138 @@ pub(crate) static COL_LIVE: &str = "live"; pub(crate) static COL_SERIES: &str = "series"; pub(crate) static COL_VOD: &str = "vod"; -fn value_to_bson(value: &Value) -> Bson { - match value { - Value::Null => Bson::Null, - Value::Bool(value) => Bson::Boolean(value.clone()), - Value::Number(value) => { - if value.is_f64() { - Bson::Double(value.as_f64().unwrap()) - } else { - Bson::Int64(value.as_i64().unwrap()) +// fn value_to_bson(value: &Value) -> Bson { +// match value { +// Value::Null => Bson::Null, +// Value::Bool(value) => Bson::Boolean(value.clone()), +// Value::Number(value) => { +// if value.is_f64() { +// Bson::Double(value.as_f64().unwrap()) +// } else { +// Bson::Int64(value.as_i64().unwrap()) +// } +// } +// Value::String(value) => Bson::String(value.clone()), +// Value::Array(value) => Bson::Array(value.iter().map(value_to_bson).collect()), +// Value::Object(value) => { +// let mut document = Document::new(); +// for (key, val) in value { +// document.insert(key, value_to_bson(val)); +// } +// Bson::Document(document) +// } +// } +// } + +fn write_to_file(file: &PathBuf, value: &T) -> Result<(), Error> + where + T: ?Sized + Serialize { + match File::create(file) { + Ok(file) => { + let mut writer = BufWriter::new(file); + serde_json::to_writer(&mut writer, value)?; + match writer.flush() { + Ok(_) => Ok(()), + Err(e) => Err(e) } } - Value::String(value) => Bson::String(value.clone()), - Value::Array(value) => Bson::Array(value.iter().map(value_to_bson).collect()), - Value::Object(value) => { - let mut document = Document::new(); - for (key, val) in value { - document.insert(key, value_to_bson(val)); - } - Bson::Document(document) - } + Err(e) => Err(e) } } +fn get_storage_path(cfg: &Config, target_name: &str) -> Option { + utils::get_file_path(&cfg.working_dir, Some(std::path::PathBuf::from(target_name.replace(" ", "_")))) +} + pub(crate) fn save_playlist(target: &ConfigTarget, cfg: &Config, playlist: &mut Vec) -> Result<(), std::io::Error> { - //let mut new_playlist = playlist.to_owned(); let mut failed = false; - if let Some(path) = utils::get_file_path(&cfg.working_dir, Some(std::path::PathBuf::from(format!("{}.db", &target.name)))) { - let db = Database::open_file(&path).unwrap(); - let cat_live_col = db.collection(COL_CAT_LIVE); - let cat_series_col = db.collection(COL_CAT_SERIES); - let cat_vod_col = db.collection(COL_CAT_VOD); - let live_col = db.collection(COL_LIVE); - let series_col = db.collection(COL_SERIES); - let vod_col = db.collection(COL_VOD); - // TODO this could be a problem when a user tries to access the db now - cat_live_col.drop().unwrap(); - cat_series_col.drop().unwrap(); - cat_vod_col.drop().unwrap(); - live_col.drop().unwrap(); - series_col.drop().unwrap(); - vod_col.drop().unwrap(); + if let Some(path) = get_storage_path(&cfg, &target.name) { + if fs::create_dir_all(&path).is_err() { + let msg = format!("Failed to save, can't create directory {}", &path.into_os_string().into_string().unwrap()); + send_message(msg.as_str()); + return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); + } + + let mut cat_live_col = vec![]; + let mut cat_series_col = vec![]; + let mut cat_vod_col = vec![]; + let mut live_col = vec![]; + let mut series_col = vec![]; + let mut vod_col = vec![]; for plg in playlist { - let cat_collection = match &plg.xtream_cluster { - XtreamCluster::LIVE => &cat_live_col, - XtreamCluster::SERIES => &cat_series_col, - XtreamCluster::VIDEO => &cat_vod_col, - }; - match cat_collection.insert_one( - doc! { + match &plg.xtream_cluster { + XtreamCluster::LIVE => &mut cat_live_col, + XtreamCluster::SERIES => &mut cat_series_col, + XtreamCluster::VIDEO => &mut cat_vod_col, + }.push( + json!({ "category_id": plg.id, "category_name": plg.title.clone(), "parent_id": 0 - } - ) { - Ok(_) => {} - Err(e) => { - println!("failed to write to collection {:?}", e); - failed = true; - } - }; + }) + ); for pli in &plg.channels { let header = &pli.header.borrow(); - let collection = match header.xtream_cluster { - XtreamCluster::LIVE => &live_col, - XtreamCluster::SERIES => &series_col, - XtreamCluster::VIDEO => &vod_col, - }; - - let mut document = doc! { - "category_id": format!("{}", plg.id), - "category_ids": [plg.id], - "custom_sid": "", - "direct_source": header.source.clone(), - "name": header.name.clone(), - "title": header.title.clone(), - //"num": channel_num, - "stream_icon": header.logo.clone(), - "stream_id": header.id.clone(), - "thumbnail": header.logo_small.clone(), - }; + let mut document = serde_json::Map::from_iter([ + ("category_id".to_string(), Value::String(format!("{}", &plg.id))), + ("category_ids".to_string(), Value::Array(Vec::from([Value::Number(serde_json::Number::from((&plg.id).to_owned()))]))), + //("custom_sid", ""), + ("direct_source".to_string(), Value::String(header.source.clone())), + ("name".to_string(), Value::String(header.name.clone())), + ("title".to_string(), Value::String(header.title.clone())), + //"num": channel_num), + ("stream_icon".to_string(), Value::String(header.logo.clone())), + ("stream_id".to_string(), Value::String(header.id.clone())), + ("thumbnail".to_string(), Value::String(header.logo_small.clone())), + ]); if let Some(add_props) = &header.additional_properties { for (field_name, field_value) in add_props { - document.insert(field_name, value_to_bson(field_value)); + document.insert(field_name.to_string(), field_value.to_owned()); } } - match collection.insert_one(document) { - Ok(_) => {} - Err(e) => { - println!("failed to write to collection {:?}", e); - failed = true; - } - }; + match header.xtream_cluster { + XtreamCluster::LIVE => &mut live_col, + XtreamCluster::SERIES => &mut series_col, + XtreamCluster::VIDEO => &mut vod_col, + }.push(Value::Object(document)); + } + } + + for (col_path, data) in [ + (&path.join(COL_CAT_LIVE), &cat_live_col), + (&path.join(COL_CAT_VOD), &cat_vod_col), + (&path.join(COL_CAT_SERIES), &cat_series_col), + (&path.join(COL_LIVE), &live_col), + (&path.join(COL_VOD), &vod_col), + (&path.join(COL_SERIES), &series_col)] { + match write_to_file(col_path, data) { + Ok(()) => {} + Err(err) => { + failed = true; + println!("Persisting collection failed: {}: {}", &col_path.clone().into_os_string().into_string().unwrap(), err); + } } } } else { - return Err(std::io::Error::new(std::io::ErrorKind::Other, format!("Persisting playlist failed: {}.db", &target.name))); + return Err(Error::new(std::io::ErrorKind::Other, format!("Persisting playlist failed: {}.db", &target.name))); } if failed { - send_message(format!("Something went wrong persisting target to db {}", &target.name).as_str()); + send_message(format!("Something went wrong persisting target {}", &target.name).as_str()); } Ok(()) } -pub(crate) fn get_all(cfg: &Config, target_name: &str, collection_name: &str) -> Option> { - if let Some(path) = utils::get_file_path(&cfg.working_dir, Some(std::path::PathBuf::from(format!("{}.db", target_name)))) { - if path.exists() { - let db = Database::open_file(&path).unwrap(); - let collection: Collection = db.collection::(collection_name); - let a: Vec = collection - .find(None) - .unwrap() - .map(|r| r.unwrap()) - .collect(); - return Some(a); +pub(crate) fn get_all(cfg: &Config, target_name: &str, collection_name: &str) -> Result { + if let Some(path) = get_storage_path(&cfg, target_name) { + let col_path = path.join(collection_name); + if col_path.exists() { + return Ok(col_path); } } - None + return Err(Error::new(std::io::ErrorKind::Other, format!("Cant find collection: {}/{}", target_name, collection_name))); } \ No newline at end of file diff --git a/src/xtream_parser.rs b/src/xtream_parser.rs index dbb03752f..ec4047eaa 100644 --- a/src/xtream_parser.rs +++ b/src/xtream_parser.rs @@ -151,7 +151,7 @@ impl XtreamStream { let mut result = vec![]; if let Some(bdpath) = self.backdrop_path.as_ref() { if !bdpath.is_empty() { - result.push((String::from("backdrop_path"), Value::String(String::from(bdpath.get(0).unwrap())))); + result.push((String::from("backdrop_path"), Value::Array(Vec::from([Value::String(String::from(bdpath.get(0).unwrap()))])))); } } add_str_property_if_exists!(result, self.added, "added"); diff --git a/src/xtream_player_api.rs b/src/xtream_player_api.rs index c59cbce6c..966246f7b 100644 --- a/src/xtream_player_api.rs +++ b/src/xtream_player_api.rs @@ -1,4 +1,4 @@ -use actix_web::{HttpResponse, web, get}; +use actix_web::{HttpResponse, web, get, HttpRequest}; use crate::model_api::AppState; use crate::repository::{COL_CAT_LIVE, COL_CAT_SERIES, COL_CAT_VOD, COL_LIVE, COL_SERIES, COL_VOD, get_all}; @@ -12,20 +12,26 @@ pub struct XtreamApiRequest { #[get("/player_api.php")] pub(crate) async fn xtream_player_api( api_req: web::Query, - //req: HttpRequest, + req: HttpRequest, _app_state: web::Data, ) -> HttpResponse { match _app_state.config.get_target_for_user(api_req.username.as_str(), api_req.password.as_str()) { Some(target_name) => { let target = target_name.as_str(); - match api_req.action.as_str() { - "get_live_categories" => HttpResponse::Ok().json(get_all(&_app_state.config, target, COL_CAT_LIVE)), - "get_vod_categories" => HttpResponse::Ok().json(get_all(&_app_state.config, target, COL_CAT_VOD)), - "get_series_categories" => HttpResponse::Ok().json(get_all(&_app_state.config, target, COL_CAT_SERIES)), - "get_live_streams" => HttpResponse::Ok().json(get_all(&_app_state.config, target, COL_LIVE)), - "get_vod_streams" => HttpResponse::Ok().json(get_all(&_app_state.config, target, COL_VOD)), - "get_series" => HttpResponse::Ok().json(get_all(&_app_state.config, target, COL_SERIES)), - _ => HttpResponse::BadRequest().finish() + match match api_req.action.as_str() { + "get_live_categories" => get_all(&_app_state.config, target, COL_CAT_LIVE), + "get_vod_categories" => get_all(&_app_state.config, target, COL_CAT_VOD), + "get_series_categories" => get_all(&_app_state.config, target, COL_CAT_SERIES), + "get_live_streams" => get_all(&_app_state.config, target, COL_LIVE), + "get_vod_streams" => get_all(&_app_state.config, target, COL_VOD), + "get_series" => get_all(&_app_state.config, target, COL_SERIES), + _ => Err(std::io::Error::new(std::io::ErrorKind::Unsupported, format!("Cant find collection: {}/{}", target, &api_req.action))), + } { + Ok(file_path) => { + let file = actix_files::NamedFile::open_async(file_path).await.unwrap().set_content_type(mime::APPLICATION_JSON); + file.into_response(&req) + } + Err(_) => HttpResponse::BadRequest().finish() } } _ => HttpResponse::BadRequest().finish()