Skip to content

Commit

Permalink
[mod] 支持winktv平台, 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Borber committed Jan 17, 2023
1 parent 06d42e3 commit 9a6128d
Show file tree
Hide file tree
Showing 21 changed files with 66 additions and 88 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
|[afreeca](https://afreecatv.com/)|`https://bj.afreecatv.com/<RID>` 主播名字而非直播间号|
|[pandalive](https://www.pandalive.co.kr/)|`https://www.pandalive.co.kr/channel/<RID>` 主播名字而非直播间号|
|[flex](https://www.flextv.co.kr/)|`https://www.flextv.co.kr/channels/<RID>` 主播名字而非直播间号|
|[wink](https://www.winktv.co.kr/)|`https://www.winktv.co.kr/channel/<RID>` 主播名字而非直播间号|

# 路线

Expand Down
2 changes: 1 addition & 1 deletion src/declarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?);
}
)*
}
Expand Down
8 changes: 2 additions & 6 deletions src/live/afreeca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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=";
Expand Down Expand Up @@ -58,6 +54,6 @@ mod tests {

#[tokio::test]
async fn test_get() {
println!("{}", get("dasl8121").await.unwrap().to_string());
println!("{}", get("dasl8121").await.unwrap());
}
}
10 changes: 3 additions & 7 deletions src/live/bili.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ pub async fn get(rid: &str) -> Result<ShowType> {
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)));
}
}
}
Expand Down Expand Up @@ -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());
}
}
8 changes: 2 additions & 6 deletions src/live/cc.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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());
}
}
7 changes: 2 additions & 5 deletions src/live/douyin.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
}
12 changes: 7 additions & 5 deletions src/live/douyu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ async fn douyu_do_js(rid: &str) -> Result<Value> {
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运行字符串更改为直接返回字符串
Expand All @@ -92,7 +91,10 @@ async fn douyu_do_js(rid: &str) -> Result<Value> {

// 构建函数, 替换数值
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}");
Expand Down Expand Up @@ -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());
}
}
10 changes: 3 additions & 7 deletions src/live/flex.rs
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -19,7 +15,7 @@ pub async fn get(rid: &str) -> Result<ShowType> {
.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(),
)])),
Expand All @@ -32,6 +28,6 @@ mod tests {

#[tokio::test]
async fn test_flex() {
println!("{}", get("399291").await.unwrap().to_string());
println!("{}", get("399291").await.unwrap());
}
}
8 changes: 2 additions & 6 deletions src/live/huajiao.rs
Original file line number Diff line number Diff line change
@@ -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/";

Expand Down Expand Up @@ -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());
}
}
8 changes: 2 additions & 6 deletions src/live/huya.rs
Original file line number Diff line number Diff line change
@@ -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/";

Expand Down Expand Up @@ -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());
}
}
9 changes: 4 additions & 5 deletions src/live/kuaishou.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand All @@ -30,9 +31,7 @@ pub async fn get(rid: &str) -> Result<ShowType> {
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();
Expand All @@ -47,6 +46,6 @@ mod tests {

#[tokio::test]
async fn test_kuaishou() {
println!("{}", get("3xgexgpig9gwwi2").await.unwrap().to_string());
println!("{}", get("3xgexgpig9gwwi2").await.unwrap());
}
}
8 changes: 2 additions & 6 deletions src/live/mht.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/live/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 2 additions & 6 deletions src/live/panda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -40,6 +36,6 @@ mod tests {

#[tokio::test]
async fn test_panda() {
println!("{}", get("wpsl1002").await.unwrap().to_string());
println!("{}", get("wpsl1002").await.unwrap());
}
}
11 changes: 5 additions & 6 deletions src/live/wink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -23,7 +20,9 @@ pub async fn get(rid: &str) -> Result<ShowType> {
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))
Expand All @@ -37,6 +36,6 @@ mod tests {

#[tokio::test]
async fn test_panda() {
println!("{}", get("csp1208").await.unwrap().to_string());
println!("{}", get("csp1208").await.unwrap());
}
}
8 changes: 2 additions & 6 deletions src/live/yqs.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
}
12 changes: 7 additions & 5 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::{Display, Formatter, Result as FmtResult};

use serde::{Serialize, Serializer};

#[derive(Serialize, Debug)]
Expand All @@ -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),
}
}
}
Expand Down
Loading

0 comments on commit 9a6128d

Please sign in to comment.