init commit
This commit is contained in:
13
Cargo.toml
Normal file
13
Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "m3u8download"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
m3u8-rs = "1.0.5"
|
||||
reqwest = "0.10.1"
|
||||
http = "0.2.0"
|
||||
rust_util = "0.2.1"
|
||||
63
src/main.rs
Normal file
63
src/main.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use http::Uri;
|
||||
use rust_util::*;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let u = "https://a:d@hatter.ink:443/aaa/bbb/cccc?aa=1#dd";
|
||||
let uri = u.parse::<Uri>().unwrap();
|
||||
println!("{}", uri);
|
||||
println!("{:?}", uri.scheme());
|
||||
println!("{:?}", uri.scheme_str());
|
||||
println!("{:?}", uri.authority());
|
||||
println!("{:?}", uri.host());
|
||||
println!("{:?}", uri.port());
|
||||
println!("{:?}", uri.path());
|
||||
println!("{:?}", uri.query());
|
||||
println!("{:?}", uri.path_and_query());
|
||||
println!("{:?}", uri.path().split('/').collect::<Vec<_>>());
|
||||
let mut v = uri.path().split('/').collect::<Vec<_>>();
|
||||
if !v.is_empty() {
|
||||
let l = v.len()-1;
|
||||
v[l]="xxxxx";
|
||||
}
|
||||
println!("{:?}", v.join("/"));
|
||||
}
|
||||
|
||||
fn _make_url(base_url: &str, ts_url: &str) -> XResult<String> {
|
||||
let lower_ts_url = ts_url.to_lowercase();
|
||||
if lower_ts_url.starts_with("http://") || lower_ts_url.starts_with("https://") {
|
||||
return Ok(ts_url.into());
|
||||
}
|
||||
|
||||
let parsed_base_url = base_url.parse::<Uri>()?;
|
||||
let lower_scheme_str = match parsed_base_url.scheme_str() {
|
||||
None => return Err(new_box_error(&format!("Base url has no scheme: {}", base_url))),
|
||||
Some(s) => s.to_lowercase(),
|
||||
};
|
||||
|
||||
if ts_url.starts_with("//") {
|
||||
return Ok(iff!(lower_scheme_str == "http", "http:", "https:").to_owned() + ts_url);
|
||||
}
|
||||
|
||||
let authority = match parsed_base_url.authority() {
|
||||
None => return Err(new_box_error(&format!("Base url has no authority: {}", base_url))),
|
||||
Some(a) => a,
|
||||
};
|
||||
|
||||
if ts_url.starts_with('/') {
|
||||
return Ok(format!("{}://{}{}", lower_scheme_str, authority, ts_url));
|
||||
}
|
||||
|
||||
let relative_base_url = format!("{}://{}{}", lower_scheme_str, authority, parsed_base_url.path());
|
||||
if relative_base_url.ends_with('/') {
|
||||
// ends with '/'
|
||||
Ok(relative_base_url + ts_url)
|
||||
} else {
|
||||
// not ends with '/'
|
||||
let mut splited_by_slash = relative_base_url.split('/').collect::<Vec<_>>();
|
||||
let splited_by_slash_len = splited_by_slash.len();
|
||||
splited_by_slash[splited_by_slash_len - 1] = ts_url;
|
||||
Ok(splited_by_slash.join("/"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user