Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warnings and rebase master #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_version::{version_meta, Channel};
use rustc_version::{Channel, version_meta};

fn main() {
// Set cfg flags depending on release channel
Expand All @@ -8,5 +8,5 @@ fn main() {
Channel::Nightly => "CHANNEL_NIGHTLY",
Channel::Dev => "CHANNEL_DEV",
};
println!("cargo:rustc-cfg={}", channel)
}
println!("cargo:rustc-cfg={channel}");
}
3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ clap = { version = "3.0.0-rc.7", features = ["derive", "wrap_help"] }
fern = { version = "0.6.0", features = ["colored"] }
log = "0.4.14"
mime = "0.3.16"
rustube = { path = "..", version = "0.6", features = ["download", "std"] }
rustube = { path = "..", version = "0.6", features = ["download", "std", "callback"] }
tokio = { version = "1.12.0", features = ["rt-multi-thread"] }
serde = "1.0.130"
strum = { version = "0.22.0", features = ["derive"] }
serde_json = "1.0.68"
serde_yaml = "0.8.21"
pbr = "1.0.4"
19 changes: 18 additions & 1 deletion cli/src/args/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub struct LoggingArgs {
#[clap(long, short, parse(from_occurrences))]
verbose: u8,

/// Show a progress bar
#[clap(long, short, conflicts_with = "verbose")]
progress: bool,

/// When to log coloredd
#[clap(long, default_value = "always", possible_values = & ["always", "never"], value_name = "WHEN")]
color: ColorUsage,
Expand All @@ -25,7 +29,7 @@ pub struct LoggingArgs {

impl LoggingArgs {
pub fn init_logger(&self) {
if self.quiet { return; }
if self.quiet || self.progress { return; }

let formatter = self.log_msg_formatter();

Expand All @@ -38,6 +42,19 @@ impl LoggingArgs {
.expect("The global logger was already initialized");
}

pub fn init_progress_bar(&self, total: u64) -> pbr::ProgressBar<Box<dyn std::io::Write + Send + Sync>> {
let writer = match self.progress {
true => Box::new(std::io::stderr()) as _,
false => Box::new(std::io::sink()) as _,
};
let mut pb = pbr::ProgressBar::on(writer, total);
pb.set_units(pbr::Units::Bytes);
pb.format("[=> ]");
pb.add(0);

pb
}

fn log_msg_formatter(&self) -> fn(FormatCallback, &Arguments, &Record) {
#[inline(always)]
fn format_msg(
Expand Down
2 changes: 1 addition & 1 deletion cli/src/args/stream_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ impl StreamFilter {
}

fn parse_json<T: for<'de> serde::Deserialize<'de>>(s: &str) -> anyhow::Result<T> {
let args = format!("\"{}\"", s);
let args = format!("\"{s}\"");
Ok(serde_json::from_str(&args)?)
}
57 changes: 32 additions & 25 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::Parser;
use args::DownloadArgs;
use args::StreamFilter;
use rustube::{Error, Id, IdBuf, Stream, Video, VideoFetcher, VideoInfo};
use rustube::Callback;

use crate::args::{CheckArgs, Command, FetchArgs};
use crate::video_serializer::VideoSerializer;
Expand Down Expand Up @@ -48,8 +49,11 @@ async fn check(args: CheckArgs) -> Result<()> {
let (video_info, streams) = get_streams(id, &args.stream_filter).await?;
let video_serializer = VideoSerializer::new(video_info, streams, args.output.output_level);

let output = args.output.output_format.serialize_output(&video_serializer)?;
println!("{}", output);
let output = args
.output
.output_format
.serialize_output(&video_serializer)?;
println!("{output}");

Ok(())
}
Expand All @@ -59,22 +63,27 @@ async fn download(args: DownloadArgs) -> Result<()> {

let id = args.identifier.id()?;
let (video_info, stream) = get_stream(id.as_owned(), args.stream_filter).await?;
let download_path = download_path(
args.filename,
stream.mime.subtype().as_str(),
args.dir,
id,
);
let download_path = download_path(args.filename, stream.mime.subtype().as_str(), args.dir, id);

let mut pb = args.logging.init_progress_bar(stream.content_length().await?);
let callback = Callback::new()
.connect_on_progress_closure(|cargs| {
// update progress bar
pb.add(cargs.current_chunk.saturating_sub(cargs.current_chunk) as u64);
});

stream.download_to(download_path).await?;
stream
.download_to_with_callback(&download_path, callback)
.await?;
pb.finish_println(&format!("Finished downloading video to {download_path:?}\n"));

let video_serializer = VideoSerializer::new(
video_info,
std::iter::once(stream),
args.output.output_level,
);
let output = args.output.output_format.serialize_output(&video_serializer)?;
println!("{}", output);
let output = args.output.output_format.serialize_output(&video_serializer).unwrap();
println!("{output}");

Ok(())
}
Expand All @@ -83,20 +92,15 @@ async fn fetch(args: FetchArgs) -> Result<()> {
args.logging.init_logger();

let id = args.identifier.id()?;
let video_info = rustube::VideoFetcher::from_id(id)?
.fetch_info()
.await?;
let video_info = rustube::VideoFetcher::from_id(id)?.fetch_info().await?;

let output = args.output.output_format.serialize_output(&video_info)?;
println!("{}", output);
println!("{output}");

Ok(())
}

async fn get_stream(
id: IdBuf,
stream_filter: StreamFilter,
) -> Result<(VideoInfo, Stream)> {
async fn get_stream(id: IdBuf, stream_filter: StreamFilter) -> Result<(VideoInfo, Stream)> {
let (video_info, streams) = get_streams(id, &stream_filter).await?;

let stream = streams
Expand All @@ -111,9 +115,7 @@ async fn get_streams(
id: IdBuf,
stream_filter: &'_ StreamFilter,
) -> Result<(VideoInfo, impl Iterator<Item=Stream> + '_)> {
let (video_info, streams) = get_video(id)
.await?
.into_parts();
let (video_info, streams) = get_video(id).await?.into_parts();

let streams = streams
.into_iter()
Expand All @@ -131,9 +133,14 @@ async fn get_video(id: IdBuf) -> Result<Video> {
.context("Could not descramble the video information")
}

pub fn download_path(filename: Option<PathBuf>, extension: &str, dir: Option<PathBuf>, video_id: Id<'_>) -> PathBuf {
let filename = filename
.unwrap_or_else(|| format!("{}.{}", video_id.as_str(), extension).into());
pub fn download_path(
filename: Option<PathBuf>,
extension: &str,
dir: Option<PathBuf>,
video_id: Id<'_>,
) -> PathBuf {
let filename =
filename.unwrap_or_else(|| format!("{}.{}", video_id.as_str(), extension).into());

let mut path = dir.unwrap_or_else(PathBuf::new);

Expand Down
4 changes: 2 additions & 2 deletions cli/src/output_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ impl OutputFormat {
use OutputFormat::*;

match self {
Debug => Ok(format!("{:?}", output)),
PrettyDebug => Ok(format!("{:#?}", output)),
Debug => Ok(format!("{output:?}")),
PrettyDebug => Ok(format!("{output:#?}")),
Json => Ok(serde_json::to_string(output)?),
PrettyJson => Ok(serde_json::to_string_pretty(output)?),
Yaml => Ok(serde_yaml::to_string(output)?)
Expand Down
28 changes: 11 additions & 17 deletions src/descrambler/cipher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use once_cell::sync::Lazy;

use once_cell::sync::Lazy;
use regex::Regex;

use crate::{Error, Result, TryCollect};
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Cipher {
let js_fun = self.transform_map
.get(name)
.ok_or_else(|| Error::UnexpectedResponse(format!(
"no matching transform function for `{}`", js_fun_name
"no matching transform function for `{js_fun_name}`",
).into()))?
.0;
js_fun(signature, argument);
Expand Down Expand Up @@ -93,8 +93,7 @@ impl Cipher {
)
)),
(name, arg) => Err(Error::UnexpectedResponse(format!(
"expected a Javascript transformer function and an argument, got: `{:?}` and `{:?}`",
name, arg
"expected a Javascript transformer function and an argument, got: `{name:?}` and `{arg:?}`",
).into()))
}
}
Expand All @@ -109,8 +108,8 @@ impl Cipher {
transform_map: {:?}",
signature, self.transform_plan, self.transform_map_dbg()
);
log::error!("{}", error);
eprintln!("{}", error);
log::error!("{error}");
eprintln!("{error}");
error
}

Expand All @@ -134,13 +133,12 @@ impl Cipher {

fn get_transform_plan(js: &str) -> Result<Vec<String>> {
let name = regex::escape(get_initial_function_name(js)?);
let pattern = Regex::new(&format!(r#"{}=function\(\w\)\{{[a-z=.(")]*;(.*);(?:.+)}}"#, name)).unwrap();
let pattern = Regex::new(&format!(r#"{name}=function\(\w\)\{{[a-z=.(")]*;(.*);(?:.+)}}"#)).unwrap();
Ok(
pattern
.captures(js)
.ok_or_else(|| Error::UnexpectedResponse(format!(
"could not extract the initial JavaScript function: {}",
pattern
"could not extract the initial JavaScript function: {pattern}",
).into()))?
.get(1)
.expect("the pattern must contain at least one capture group")
Expand Down Expand Up @@ -172,8 +170,7 @@ fn get_initial_function_name(js: &str) -> Result<&str> {
.find_map(|pattern| pattern.captures(js))
.map(|c| c.get(1).unwrap().as_str())
.ok_or_else(|| Error::UnexpectedResponse(format!(
"could not find the JavaScript function name: `{}`",
js
"could not find the JavaScript function name: `{js}`",
).into()))
}

Expand All @@ -186,8 +183,7 @@ fn get_transform_map(js: &str, var: &str) -> Result<HashMap<String, TransformerF
let (name, function) = obj
.split_once(':')
.ok_or_else(|| Error::UnexpectedResponse(format!(
"expected the transform-object to contain at least one ':', got {}",
obj
"expected the transform-object to contain at least one ':', got {obj}",
).into()))?;
let fun = map_functions(function)?;
mapper.insert(name.to_owned(), fun);
Expand Down Expand Up @@ -249,8 +245,7 @@ fn map_functions(js_func: &str) -> Result<TransformerFn> {
.find(|(pattern, _fun)| pattern.is_match(js_func))
.map(|(_pattern, fun)| *fun)
.ok_or_else(|| Error::UnexpectedResponse(format!(
"could not map the JavaScript function `{}` to any Rust equivalent",
js_func
"could not map the JavaScript function `{js_func}` to any Rust equivalent",
).into()))
}

Expand All @@ -260,8 +255,7 @@ fn get_transform_object(js: &str, var: &str) -> Result<String> {
.unwrap()
.captures(js)
.ok_or_else(|| Error::UnexpectedResponse(format!(
"could not extract the transform-object `{}`",
var
"could not extract the transform-object `{var}`",
).into()))?
.get(1)
.expect("the regex pattern must contain at least one capture group")
Expand Down
Loading