Skip to content

Commit

Permalink
完善预告类型直播支持,全面支持youtube预告类型.
Browse files Browse the repository at this point in the history
  • Loading branch information
limitcool committed Aug 15, 2022
1 parent ead4b09 commit eae38b8
Show file tree
Hide file tree
Showing 8 changed files with 1,742 additions and 2,842 deletions.
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 = "bilistream"
version = "0.1.2"
version = "0.1.3"
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 live_id.json

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ async fn main() {
.with(fmt::layer())
.init();
let cfg = load_config(Path::new("./config.yaml")).unwrap();
let r = select_live(cfg.clone()).await.unwrap();
let mut r = select_live(cfg.clone()).await.unwrap();
// 设置tracing日志等级为Info


loop {
if r.get_status().await.unwrap_or(false) {
Expand Down Expand Up @@ -75,6 +74,13 @@ async fn main() {
tracing::info!("B站已关播");
}
}
// 判断是否预告类型
if cfg.platform == "YoutubePreviewLive" {
tracing::info!("检测到预告类型,正在重新获取直播间");
r.set_room(get_live_id(cfg.youtube_preview_live.channel_id.as_str())
.await
.unwrap().as_str())
}
// 每60秒检测一下直播状态
tokio::time::sleep(Duration::from_secs(cfg.interval)).await;
}
Expand Down
22 changes: 20 additions & 2 deletions src/plugins/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub trait Live {
async fn get_status(&self) -> Result<bool, Box<dyn Error>>;
fn room(&self) -> &str;
async fn get_real_m3u8_url(&self) -> Result<String, Box<dyn Error>>;
fn set_room(&mut self, room: &str);
}
pub async fn select_live(cfg: Config) -> Result<Box<dyn Live>, Box<dyn Error>> {
// 设置最大重试次数为4294967295次
Expand Down Expand Up @@ -65,6 +66,7 @@ pub async fn select_live(cfg: Config) -> Result<Box<dyn Live>, Box<dyn Error>> {

// https://www.youtube.com/channel/UC1zFJrfEKvCixhsjNSb1toQ
// 通过channel_name获取channel_id
#[allow(dead_code)]
async fn get_channel_id(channel_name: &str) -> Result<String, Box<dyn Error>> {
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(4294967295);
let raw_client = reqwest::Client::builder()
Expand Down Expand Up @@ -143,8 +145,8 @@ pub async fn get_live_id(channel_name: &str) -> Result<String, Box<dyn Error>> {
["gridVideoRenderer"]["videoId"]
);
// 将结果保存为一个json文件
// let mut file = std::fs::File::create("live_id.json").unwrap();
// std::io::Write::write_all(&mut file, json.as_bytes()).unwrap();
let mut file = std::fs::File::create("live_id.json").unwrap();
std::io::Write::write_all(&mut file, json.as_bytes()).unwrap();
return Ok(video_id);
}

Expand Down Expand Up @@ -173,4 +175,20 @@ mod tests {
let r = aw!(get_live_id(channel_id)).unwrap();
println!("id:{}", r);
}
#[test]
fn test_json_path_to_string() {
let re = json_path_to_map_string("materials.canvases.0.image_id");
println!("re:{}", re);
}
}

#[allow(dead_code)]
// 传入materials.canvases.0.image_id,返回 ["materials"]["canvases"][0]["image_id"]
fn json_path_to_map_string(path: &str) -> String {
let r = path.split(".");
let mut s = String::new();
r.for_each(|x| {
s.push_str(&format!("[\"{}\"]", x));
});
return s;
}
Loading

0 comments on commit eae38b8

Please sign in to comment.