Skip to content

Commit

Permalink
fix: correct quality chosen now
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Simpson committed Jul 31, 2020
1 parent 4cd12a9 commit b3a8882
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 71 deletions.
96 changes: 81 additions & 15 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,54 @@ pub struct Episode {
}

impl Episode {
pub fn return_episode_number(&self) -> String {
let length = self.nola_episode.len();
self.nola_episode[6..length].to_string()
}

pub fn return_season_number(&self) -> String {
self.nola_episode[4..6].to_string()
}

pub fn return_slug(&self) -> String {
self.title.replace(" ", "-").replace("/", "-")
}

pub fn get_video_url(&self) -> String {
self.videos
let mut url = String::new();
let videos: Vec<&Video> = self
.videos
.iter()
.filter(|x| x.format == Some("mp4".to_string()))
.filter(|x| {
x.format == Some("mp4".to_string())
&& (x.bitrate == Some("720p".to_string())
|| x.bitrate == Some("4500k".to_string())
|| x.bitrate == Some("1200k".to_string()))
x.bitrate == Some("720p".to_string())
|| x.bitrate == Some("4500k".to_string())
|| x.bitrate == Some("1200k".to_string())
})
.collect();

let preferred_quality: String = videos
.iter()
.filter(|x| x.bitrate == Some("720p".to_string()))
.map(|x| x.url.to_string())
.fold("".to_string(), |_acc, x| x)
.collect();

let second_quality: String = videos
.iter()
.filter(|x| x.bitrate == Some("4500k".to_string()))
.map(|x| x.url.to_string())
.collect();

let third_quality: String = videos
.iter()
.filter(|x| x.bitrate == Some("1200k".to_string()))
.map(|x| x.url.to_string())
.collect();

if third_quality.len() > 0 {
url = third_quality;
}

if second_quality.len() > 0 {
url = second_quality;
}

if preferred_quality.len() > 0 {
url = preferred_quality;
}

return url;
}
}

Expand All @@ -75,3 +99,45 @@ pub struct Video {
pub bitrate: Option<String>,
pub format: Option<String>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn episode_quality() {
let video1: Video = Video {
url: "https://1200.com".to_string(),
bitrate: Some("1200k".to_string()),
format: Some("mp4".to_string()),
};
let video2: Video = Video {
url: "https://900k.com".to_string(),
bitrate: Some("900k".to_string()),
format: Some("mp4".to_string()),
};
let video3: Video = Video {
url: "https://720.com".to_string(),
bitrate: Some("720p".to_string()),
format: Some("mp4".to_string()),
};
let video4: Video = Video {
url: "https://second-1200k.com".to_string(),
bitrate: Some("1200k".to_string()),
format: Some("mp4".to_string()),
};
let video5: Video = Video {
url: "https://last.com".to_string(),
bitrate: Some("1000k".to_string()),
format: Some("h264".to_string()),
};
let test: Episode = Episode {
id: "123".to_string(),
nola_episode: "SAST4921".to_string(),
videos: vec![video1, video2, video3, video4, video5],
title: "Foo".to_string(),
};

assert_eq!(test.get_video_url(), "https://720.com");
}
}
57 changes: 1 addition & 56 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate reqwest;
extern crate simple_logger;
use log::error;
use log::info;
use serde::Deserialize;
use std::fs;
use std::io::copy;
use std::path::PathBuf;
Expand Down Expand Up @@ -42,60 +41,6 @@ enum Command {
List,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn season_number() {
let video: json::Video = json::Video {
url: "https://url.com".to_string(),
bitrate: Some("720p".to_string()),
format: Some("mp4".to_string()),
};
let test: json::Episode = json::Episode {
id: "123".to_string(),
nola_episode: "SAST4921".to_string(),
videos: vec![video],
title: "Foo".to_string(),
};

assert_eq!(test.return_season_number(), "49");
}

#[test]
fn episode_number() {
let video: json::Video = json::Video {
url: "https://url.com".to_string(),
bitrate: Some("720p".to_string()),
format: Some("mp4".to_string()),
};
let test: json::Episode = json::Episode {
id: "123".to_string(),
nola_episode: "SAST4921".to_string(),
videos: vec![video],
title: "Foo".to_string(),
};

assert_eq!(test.return_episode_number(), "21");

let dtvideo: json::Video = json::Video {
url: "https://url.com".to_string(),
bitrate: Some("720p".to_string()),
format: Some("mp4".to_string()),
};

let dtest: json::Episode = json::Episode {
id: "123".to_string(),
nola_episode: "DTIG101".to_string(),
videos: vec![dtvideo],
title: "Foo".to_string(),
};

assert_eq!(dtest.return_episode_number(), "1");
}
}

fn gen_history_path() -> String {
let home = match dirs::home_dir() {
Some(p) => p.to_string_lossy().into_owned(),
Expand Down Expand Up @@ -161,7 +106,7 @@ fn main() {
}

process::exit(0)
},
}
None => {}
}

Expand Down

0 comments on commit b3a8882

Please sign in to comment.