From 9a6128d6aa7f5801488624eb03cfb57efcf16039 Mon Sep 17 00:00:00 2001 From: Borber Date: Wed, 18 Jan 2023 00:28:42 +0800 Subject: [PATCH] =?UTF-8?q?[mod]=20=E6=94=AF=E6=8C=81winktv=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0,=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 13 +++++++++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 1 + src/declarative.rs | 2 +- src/live/afreeca.rs | 8 ++------ src/live/bili.rs | 10 +++------- src/live/cc.rs | 8 ++------ src/live/douyin.rs | 7 ++----- src/live/douyu.rs | 12 +++++++----- src/live/flex.rs | 10 +++------- src/live/huajiao.rs | 8 ++------ src/live/huya.rs | 8 ++------ src/live/kuaishou.rs | 9 ++++----- src/live/mht.rs | 8 ++------ src/live/mod.rs | 2 +- src/live/panda.rs | 8 ++------ src/live/wink.rs | 11 +++++------ src/live/yqs.rs | 8 ++------ src/model.rs | 12 +++++++----- src/util.rs | 5 +++-- 21 files changed, 66 insertions(+), 88 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a1e7b7..9f1eba3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.1.11] + +### Added + +- 添加 winktv 直播源获取 + +### Changed + +- 修改Node结构 +- 规范化 format 判定, 规范化输出方法 +- 简化代码 + + ## [0.1.10] ### Added diff --git a/Cargo.lock b/Cargo.lock index efaaa86..8e46ea1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -943,7 +943,7 @@ checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" [[package]] name = "seam" -version = "0.1.10" +version = "0.1.11" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index bac32e3..04f39f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "seam" -version = "0.1.10" +version = "0.1.11" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 6faae77..4b51052 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ |[afreeca](https://afreecatv.com/)|`https://bj.afreecatv.com/` 主播名字而非直播间号| |[pandalive](https://www.pandalive.co.kr/)|`https://www.pandalive.co.kr/channel/` 主播名字而非直播间号| |[flex](https://www.flextv.co.kr/)|`https://www.flextv.co.kr/channels/` 主播名字而非直播间号| +|[wink](https://www.winktv.co.kr/)|`https://www.winktv.co.kr/channel/` 主播名字而非直播间号| # 路线 diff --git a/src/declarative.rs b/src/declarative.rs index 5ce069e..00bee6a 100644 --- a/src/declarative.rs +++ b/src/declarative.rs @@ -23,7 +23,7 @@ macro_rules! get_source_url_command { match Cli::parse().command { $( Commands::$name { rid } => { - println!("{}", live::[<$name: lower>]::get(&rid).await?.to_string()); + println!("{}", live::[<$name: lower>]::get(&rid).await?); } )* } diff --git a/src/live/afreeca.rs b/src/live/afreeca.rs index 3773b56..a9738a3 100644 --- a/src/live/afreeca.rs +++ b/src/live/afreeca.rs @@ -3,11 +3,7 @@ use std::collections::HashMap; use anyhow::{Ok, Result}; use regex::Regex; -use crate::{ - common::CLIENT, - model::ShowType, - util::parse_url, -}; +use crate::{common::CLIENT, model::ShowType, util::parse_url}; const URL: &str = "https://play.afreecatv.com/"; const PLAY_URL: &str = "https://live.afreecatv.com/afreeca/player_live_api.php?bjid="; @@ -58,6 +54,6 @@ mod tests { #[tokio::test] async fn test_get() { - println!("{}", get("dasl8121").await.unwrap().to_string()); + println!("{}", get("dasl8121").await.unwrap()); } } diff --git a/src/live/bili.rs b/src/live/bili.rs index e3417ab..49e71ea 100644 --- a/src/live/bili.rs +++ b/src/live/bili.rs @@ -52,12 +52,8 @@ pub async fn get(rid: &str) -> Result { for url_info in codec["url_info"].as_array().unwrap() { let host = url_info["host"].as_str().unwrap(); let extra = url_info["extra"].as_str().unwrap(); - stream_urls.push(parse_url(format!( - "{}{}{}", - host, - base_url.clone(), - extra - ))); + stream_urls + .push(parse_url(format!("{}{}{}", host, base_url, extra))); } } } @@ -97,6 +93,6 @@ mod tests { #[tokio::test] async fn test_get_url() { - println!("{}", get("23356199").await.unwrap().to_string()); + println!("{}", get("23356199").await.unwrap()); } } diff --git a/src/live/cc.rs b/src/live/cc.rs index 40b2080..830f282 100644 --- a/src/live/cc.rs +++ b/src/live/cc.rs @@ -1,8 +1,4 @@ -use crate::{ - common::CLIENT, - model::ShowType, - util::parse_url, -}; +use crate::{common::CLIENT, model::ShowType, util::parse_url}; use anyhow::{Ok, Result}; use regex::Regex; @@ -52,6 +48,6 @@ mod tests { #[tokio::test] async fn test_get_url() { - println!("{}", get("361433").await.unwrap().to_string()); + println!("{}", get("361433").await.unwrap()); } } diff --git a/src/live/douyin.rs b/src/live/douyin.rs index 76059fe..72cd4c0 100644 --- a/src/live/douyin.rs +++ b/src/live/douyin.rs @@ -1,7 +1,4 @@ -use crate::{ - common::CLIENT, - model::ShowType, util::parse_url, -}; +use crate::{common::CLIENT, model::ShowType, util::parse_url}; use anyhow::{Ok, Result}; use regex::Regex; @@ -52,6 +49,6 @@ mod tests { #[tokio::test] async fn test_get_url() { - println!("{}", get("228619203678").await.unwrap().to_string()); + println!("{}", get("228619203678").await.unwrap()); } } diff --git a/src/live/douyu.rs b/src/live/douyu.rs index 45c9eed..8903d23 100644 --- a/src/live/douyu.rs +++ b/src/live/douyu.rs @@ -71,14 +71,13 @@ async fn douyu_do_js(rid: &str) -> Result { for cap in re.captures_iter(&text) { let script = cap.get(1).unwrap().as_str(); let re2 = Regex::new("\"([0-9]{12})\"").unwrap(); - match re2.captures(script){ + match re2.captures(script) { Some(t_cap) => { v = t_cap.get(1).unwrap().as_str(); func = script.to_owned(); - }, + } None => continue, } - } // 将eval运行字符串更改为直接返回字符串 @@ -92,7 +91,10 @@ async fn douyu_do_js(rid: &str) -> Result { // 构建函数, 替换数值 let res = res.replace("(function", "let ccc = function"); - let res = res.replace("rt;});", format!("rt;}}; ccc({rid}, \"{DID}\", {dt})").as_str()); + let res = res.replace( + "rt;});", + format!("rt;}}; ccc({rid}, \"{DID}\", {dt})").as_str(), + ); // 替换md5值避免js依赖 let cb = format!("{rid}{DID}{dt}{v}"); @@ -130,6 +132,6 @@ mod tests { #[tokio::test] async fn test_get_url() { - println!("{}", get("33").await.unwrap().to_string()); + println!("{}", get("33").await.unwrap()); } } diff --git a/src/live/flex.rs b/src/live/flex.rs index 2cc22c4..2c15384 100644 --- a/src/live/flex.rs +++ b/src/live/flex.rs @@ -1,10 +1,6 @@ use anyhow::{Ok, Result}; -use crate::{ - common::CLIENT, - model::ShowType, - util::parse_url, -}; +use crate::{common::CLIENT, model::ShowType, util::parse_url}; const URL: &str = "https://api.flextv.co.kr/api/channels/rid/stream?option=all"; @@ -19,7 +15,7 @@ pub async fn get(rid: &str) -> Result { .json() .await?; match &json["sources"][0]["url"] { - serde_json::Value::Null => return Ok(ShowType::Off), + serde_json::Value::Null => Ok(ShowType::Off), url => Ok(ShowType::On(vec![parse_url( url.as_str().unwrap().to_string(), )])), @@ -32,6 +28,6 @@ mod tests { #[tokio::test] async fn test_flex() { - println!("{}", get("399291").await.unwrap().to_string()); + println!("{}", get("399291").await.unwrap()); } } diff --git a/src/live/huajiao.rs b/src/live/huajiao.rs index 42a26f1..e05d406 100644 --- a/src/live/huajiao.rs +++ b/src/live/huajiao.rs @@ -1,11 +1,7 @@ use anyhow::{Ok, Result}; use regex::Regex; -use crate::{ - common::CLIENT, - model::ShowType, - util::parse_url, -}; +use crate::{common::CLIENT, model::ShowType, util::parse_url}; const URL: &str = "https://www.huajiao.com/l/"; @@ -48,6 +44,6 @@ mod tests { #[tokio::test] async fn test_get_url() { - println!("{}", get("337633032").await.unwrap().to_string()); + println!("{}", get("337633032").await.unwrap()); } } diff --git a/src/live/huya.rs b/src/live/huya.rs index 4b167bd..16badea 100644 --- a/src/live/huya.rs +++ b/src/live/huya.rs @@ -1,11 +1,7 @@ use anyhow::{Ok, Result}; use regex::Regex; -use crate::{ - common::CLIENT, - model::ShowType, - util::parse_url, -}; +use crate::{common::CLIENT, model::ShowType, util::parse_url}; const URL: &str = "https://www.huya.com/"; @@ -59,6 +55,6 @@ mod tests { #[tokio::test] async fn test_get_url() { - println!("{}", get("28328839").await.unwrap().to_string()); + println!("{}", get("28328839").await.unwrap()); } } diff --git a/src/live/kuaishou.rs b/src/live/kuaishou.rs index b5b531b..a1cc406 100644 --- a/src/live/kuaishou.rs +++ b/src/live/kuaishou.rs @@ -3,7 +3,8 @@ use regex::Regex; use crate::{ common::{CLIENT, USER_AGENT}, - model::ShowType, util::parse_url, + model::ShowType, + util::parse_url, }; const URL: &str = "https://live.kuaishou.com/u/"; @@ -30,9 +31,7 @@ pub async fn get(rid: &str) -> Result { let json: serde_json::Value = serde_json::from_str(stream).unwrap(); // TODO 更改其他逻辑 多用Null match &json["liveroom"]["liveStream"]["playUrls"][0]["adaptationSet"]["representation"] { - serde_json::Value::Null => { - Ok(ShowType::Off) - } + serde_json::Value::Null => Ok(ShowType::Off), reps => { let list = reps.as_array().unwrap(); let url = list[list.len() - 1]["url"].as_str().unwrap(); @@ -47,6 +46,6 @@ mod tests { #[tokio::test] async fn test_kuaishou() { - println!("{}", get("3xgexgpig9gwwi2").await.unwrap().to_string()); + println!("{}", get("3xgexgpig9gwwi2").await.unwrap()); } } diff --git a/src/live/mht.rs b/src/live/mht.rs index 64fcdbd..11221b4 100644 --- a/src/live/mht.rs +++ b/src/live/mht.rs @@ -1,8 +1,4 @@ -use crate::{ - common::CLIENT, - model::ShowType, - util::parse_url, -}; +use crate::{common::CLIENT, model::ShowType, util::parse_url}; use anyhow::{Ok, Result}; use serde_json::Value; @@ -41,6 +37,6 @@ mod tests { #[tokio::test] async fn test_get_url() { - println!("{}", get("932055").await.unwrap().to_string()); + println!("{}", get("932055").await.unwrap()); } } diff --git a/src/live/mod.rs b/src/live/mod.rs index 13e3f6a..b35ced7 100644 --- a/src/live/mod.rs +++ b/src/live/mod.rs @@ -9,5 +9,5 @@ pub mod huya; pub mod kuaishou; pub mod mht; pub mod panda; -pub mod yqs; pub mod wink; +pub mod yqs; diff --git a/src/live/panda.rs b/src/live/panda.rs index 17bb58c..c31222f 100644 --- a/src/live/panda.rs +++ b/src/live/panda.rs @@ -4,11 +4,7 @@ use anyhow::{Ok, Result}; const URL: &str = "https://api.pandalive.co.kr/v1/live/play/"; -use crate::{ - common::CLIENT, - model::ShowType, - util::parse_url, -}; +use crate::{common::CLIENT, model::ShowType, util::parse_url}; /// pandalive /// @@ -40,6 +36,6 @@ mod tests { #[tokio::test] async fn test_panda() { - println!("{}", get("wpsl1002").await.unwrap().to_string()); + println!("{}", get("wpsl1002").await.unwrap()); } } diff --git a/src/live/wink.rs b/src/live/wink.rs index 0488f06..1aa7ae0 100644 --- a/src/live/wink.rs +++ b/src/live/wink.rs @@ -4,10 +4,7 @@ use anyhow::{Ok, Result}; const URL: &str = "https://api.winktv.co.kr/v1/live/play"; -use crate::{ - common::CLIENT, - model::ShowType, util::parse_url, -}; +use crate::{common::CLIENT, model::ShowType, util::parse_url}; /// winktv /// @@ -23,7 +20,9 @@ pub async fn get(rid: &str) -> Result { let mut nodes = vec![]; for item in ["hls", "hls2", "hls3", "rtmp"] { if list.get(item).is_some() { - nodes.push(parse_url(list[item][0]["url"].as_str().unwrap().to_string())); + nodes.push(parse_url( + list[item][0]["url"].as_str().unwrap().to_string(), + )); } } Ok(ShowType::On(nodes)) @@ -37,6 +36,6 @@ mod tests { #[tokio::test] async fn test_panda() { - println!("{}", get("csp1208").await.unwrap().to_string()); + println!("{}", get("csp1208").await.unwrap()); } } diff --git a/src/live/yqs.rs b/src/live/yqs.rs index e541f4f..2a1ba2b 100644 --- a/src/live/yqs.rs +++ b/src/live/yqs.rs @@ -1,8 +1,4 @@ -use crate::{ - common::CLIENT, - model::ShowType, - util::parse_url, -}; +use crate::{common::CLIENT, model::ShowType, util::parse_url}; use anyhow::{Ok, Result}; use std::collections::HashMap; @@ -29,6 +25,6 @@ mod tests { #[tokio::test] async fn test_get_url() { - println!("{}", get("96").await.unwrap().to_string()); + println!("{}", get("96").await.unwrap()); } } diff --git a/src/model.rs b/src/model.rs index 694bc19..f63f266 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,3 +1,5 @@ +use std::fmt::{Display, Formatter, Result as FmtResult}; + use serde::{Serialize, Serializer}; #[derive(Serialize, Debug)] @@ -10,12 +12,12 @@ pub enum ShowType { Error(String), } -impl ShowType { - pub fn to_string(&self) -> String { +impl Display for ShowType { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { match self { - ShowType::On(nodes) => serde_json::to_string_pretty(&nodes).unwrap(), - ShowType::Off => "未开播".to_string(), - ShowType::Error(msg) => msg.to_owned(), + ShowType::On(nodes) => write!(f, "{}", serde_json::to_string_pretty(&nodes).unwrap()), + ShowType::Off => write!(f, "未开播"), + ShowType::Error(msg) => write!(f, "{}", msg), } } } diff --git a/src/util.rs b/src/util.rs index 992652a..0221570 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,6 @@ use crate::{ - common::{CLIENT, DO_JS_URL}, model::{Format, Node}, + common::{CLIENT, DO_JS_URL}, + model::{Format, Node}, }; use md5::{Digest, Md5}; use serde_json::json; @@ -41,4 +42,4 @@ pub fn parse_url(url: String) -> Node { format: match_format(&url), url: url.to_owned(), } -} \ No newline at end of file +}