Skip to content

Instantly share code, notes, and snippets.

@ksqsf
Created May 18, 2022 18:49
Show Gist options
  • Save ksqsf/3209df5ce1af63b254027000364550bb to your computer and use it in GitHub Desktop.
Save ksqsf/3209df5ce1af63b254027000364550bb to your computer and use it in GitHub Desktop.
下载 Bilibili 音频
#!/usr/bin/env cargo play -q
//# anyhow = "*"
//# reqwest = {version="*", features=["gzip","blocking"]}
//# serde_json = "*"
fn main() -> anyhow::Result<()> {
let audio_id = "832257";
let client = reqwest::blocking::Client::new();
let body = client.get(format!("https://www.bilibili.com/audio/music-service-c/web/song/info?sid={}", audio_id)).send()?.text()?;
let resp = &serde_json::from_str::<serde_json::Value>(&body)?["data"]["cover"];
let cover_url = resp.as_str().unwrap();
let cover = client.get(cover_url).send()?.bytes()?;
std::fs::write("/tmp/ccc.jpg", cover)?;
let body = client.get(format!("https://www.bilibili.com/audio/music-service-c/web/url?sid={}", audio_id)).send()?.text()?;
let resp = &serde_json::from_str::<serde_json::Value>(&body)?["data"]["cdns"][0];
let file_url = resp.as_str().unwrap();
let file = client.get(file_url)
.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0")
.header("Referer", "https://www.bilibili.com/")
.send()?.bytes()?;
std::fs::write("/tmp/aaa.m4a", file)?;
Ok(())
}
@ksqsf
Copy link
Author

ksqsf commented May 18, 2022

Rust so far is the best (statically) typed scripting language!!

Reasons:

  1. cargo-play has pretty good startup time. 0.3s is acceptable for most user-facing utilities. jvm-based langs are terrible at this.
  2. it has very very good ecosystem...
  3. editor support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment