Skip to content

Commit

Permalink
[mod] 支持快手直播源获取
Browse files Browse the repository at this point in the history
  • Loading branch information
Borber committed Jan 11, 2023
1 parent c98af8c commit 0a70924
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- 支持快手直播源获取

## [0.1.5] - 2023-01-11

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
- [ ] 根据画质排序
- [ ] gui
- [ ] mpv播放
- [ ] 是否一次性链接标识字段

# 说明

Expand Down
56 changes: 56 additions & 0 deletions src/live/kuaishou.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use anyhow::{Ok, Result};
use regex::Regex;

use crate::{
common::{CLIENT, USER_AGENT},
modle::{Node, ShowType},
};

const URL: &str = "https://live.kuaishou.com/u/";

/// 快手直播
///
/// https://live.kuaishou.com/
pub async fn get(rid: &str) -> Result<ShowType> {
let text = CLIENT
.get(format!("{}{}", URL, rid))
.header("User-Agent", USER_AGENT)
.header("Cookie", "did=web_d563dca728d28b00336877723e0359ed;")
.send()
.await?
.text()
.await?;
let re = Regex::new(r#"<script>window.__INITIAL_STATE__=([\s\S]*?);\(function"#).unwrap();
let stream = match re.captures(&text) {
Some(caps) => caps.get(1).unwrap().as_str(),
None => {
return Ok(ShowType::Error("直播间不存在".to_string()));
}
};
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 => {
return Ok(ShowType::Off);
}
reps => {
let list = reps.as_array().unwrap();
let url = list[list.len() - 1]["url"].as_str().unwrap();
Ok(ShowType::On(vec![Node {
rate: "蓝光8M".to_string(),
url: url.to_string(),
}]))
}
}
}

#[cfg(test)]
mod tests {
use crate::live::kuaishou::get;
use crate::util::match_show_type;

#[tokio::test]
async fn test_kuaishou() {
match_show_type(get("3xgexgpig9gwwi2").await.unwrap());
}
}
1 change: 1 addition & 0 deletions src/live/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub mod douyin;
pub mod douyu;
pub mod huajiao;
pub mod huya;
pub mod kuaishou;
pub mod mht;
pub mod yqs;
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ________ _______ _______ _______
|__ | ___| | |
|_______|_______|___|___|__|_|__|
获取直播源地址, 目前支持 B站, 斗鱼, 抖音, 虎牙, 花椒, 艺气山, 棉花糖", long_about = None)]
获取直播源地址, 目前支持 B站, 斗鱼, 抖音, 虎牙, 快手, 花椒, 艺气山, 棉花糖", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
Expand Down Expand Up @@ -43,6 +43,11 @@ enum Commands {
/// 房间号
rid: String,
},
/// 快手
Kuaishou {
/// 房间号
rid: String,
},
/// 花椒
Huajiao {
/// 房间号
Expand All @@ -68,6 +73,7 @@ async fn main() -> Result<()> {
Commands::Douyu { rid } => util::match_show_type(live::douyu::get(&rid).await?),
Commands::Douyin { rid } => util::match_show_type(live::douyin::get(&rid).await?),
Commands::Huya { rid } => util::match_show_type(live::huya::get(&rid).await?),
Commands::Kuaishou { rid } => util::match_show_type(live::kuaishou::get(&rid).await?),
Commands::Huajiao { rid } => util::match_show_type(live::huajiao::get(&rid).await?),
Commands::Yqs { rid } => util::match_show_type(live::yqs::get(&rid).await?),
Commands::Mht { rid } => util::match_show_type(live::mht::get(&rid).await?),
Expand Down

0 comments on commit 0a70924

Please sign in to comment.