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 3c1031c commit dbab461
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/target
config.yaml
body.html
live_id.json
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.3"
version = "0.1.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
23 changes: 17 additions & 6 deletions src/plugins/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub async fn get_live_id(channel_name: &str) -> Result<String, Box<dyn Error>> {
.build();
let url = format!("https://www.youtube.com/channel/{}", channel_name);
tracing::debug!("{}", url);
// println!("channel地址为:{}", url);
let res = client.get(&url).send().await?;
let body = res.text().await?;
// 保存body为文件,后缀为html
Expand All @@ -131,12 +132,19 @@ pub async fn get_live_id(channel_name: &str) -> Result<String, Box<dyn Error>> {
// let json = json.split("\"").nth(1).unwrap();
// let json = json.split("\"").nth(0).unwrap();
let j: Value = serde_json::from_str(json).unwrap();
let video_id = j["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][0]["tabRenderer"]
["content"]["sectionListRenderer"]["contents"][1]["itemSectionRenderer"]["contents"][0]
["shelfRenderer"]["content"]["horizontalListRenderer"]["items"][0]["gridVideoRenderer"]
["videoId"]
let mut video_id = j["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][0]
["tabRenderer"]["content"]["sectionListRenderer"]["contents"][1]["itemSectionRenderer"]
["contents"][0]["shelfRenderer"]["content"]["horizontalListRenderer"]["items"][0]
["gridVideoRenderer"]["videoId"]
.to_string();

if video_id == "null" {
video_id = j["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][0]["tabRenderer"]
["content"]["sectionListRenderer"]["contents"][2]["itemSectionRenderer"]
["contents"][0]["shelfRenderer"]["content"]["horizontalListRenderer"]["items"][0]
["gridVideoRenderer"]["videoId"]
.to_string();
}
// println!("获取到的videoId为:{}", video_id);
tracing::debug!(
"{}",
j["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][0]["tabRenderer"]["content"]
Expand All @@ -147,7 +155,10 @@ pub async fn get_live_id(channel_name: &str) -> Result<String, Box<dyn Error>> {
// 将结果保存为一个json文件
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);
if video_id == "null" {
} else {
return Ok(video_id);
}
}

Err("获取video_id失败".into())
Expand Down

0 comments on commit dbab461

Please sign in to comment.