Skip to content

Commit b3a8882

Browse files
author
Adam Simpson
committed
fix: correct quality chosen now
1 parent 4cd12a9 commit b3a8882

File tree

2 files changed

+82
-71
lines changed

2 files changed

+82
-71
lines changed

src/json.rs

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,54 @@ pub struct Episode {
4242
}
4343

4444
impl Episode {
45-
pub fn return_episode_number(&self) -> String {
46-
let length = self.nola_episode.len();
47-
self.nola_episode[6..length].to_string()
48-
}
49-
50-
pub fn return_season_number(&self) -> String {
51-
self.nola_episode[4..6].to_string()
52-
}
53-
5445
pub fn return_slug(&self) -> String {
5546
self.title.replace(" ", "-").replace("/", "-")
5647
}
5748

5849
pub fn get_video_url(&self) -> String {
59-
self.videos
50+
let mut url = String::new();
51+
let videos: Vec<&Video> = self
52+
.videos
6053
.iter()
54+
.filter(|x| x.format == Some("mp4".to_string()))
6155
.filter(|x| {
62-
x.format == Some("mp4".to_string())
63-
&& (x.bitrate == Some("720p".to_string())
64-
|| x.bitrate == Some("4500k".to_string())
65-
|| x.bitrate == Some("1200k".to_string()))
56+
x.bitrate == Some("720p".to_string())
57+
|| x.bitrate == Some("4500k".to_string())
58+
|| x.bitrate == Some("1200k".to_string())
6659
})
60+
.collect();
61+
62+
let preferred_quality: String = videos
63+
.iter()
64+
.filter(|x| x.bitrate == Some("720p".to_string()))
6765
.map(|x| x.url.to_string())
68-
.fold("".to_string(), |_acc, x| x)
66+
.collect();
67+
68+
let second_quality: String = videos
69+
.iter()
70+
.filter(|x| x.bitrate == Some("4500k".to_string()))
71+
.map(|x| x.url.to_string())
72+
.collect();
73+
74+
let third_quality: String = videos
75+
.iter()
76+
.filter(|x| x.bitrate == Some("1200k".to_string()))
77+
.map(|x| x.url.to_string())
78+
.collect();
79+
80+
if third_quality.len() > 0 {
81+
url = third_quality;
82+
}
83+
84+
if second_quality.len() > 0 {
85+
url = second_quality;
86+
}
87+
88+
if preferred_quality.len() > 0 {
89+
url = preferred_quality;
90+
}
91+
92+
return url;
6993
}
7094
}
7195

@@ -75,3 +99,45 @@ pub struct Video {
7599
pub bitrate: Option<String>,
76100
pub format: Option<String>,
77101
}
102+
103+
#[cfg(test)]
104+
mod tests {
105+
use super::*;
106+
107+
#[test]
108+
fn episode_quality() {
109+
let video1: Video = Video {
110+
url: "https://1200.com".to_string(),
111+
bitrate: Some("1200k".to_string()),
112+
format: Some("mp4".to_string()),
113+
};
114+
let video2: Video = Video {
115+
url: "https://900k.com".to_string(),
116+
bitrate: Some("900k".to_string()),
117+
format: Some("mp4".to_string()),
118+
};
119+
let video3: Video = Video {
120+
url: "https://720.com".to_string(),
121+
bitrate: Some("720p".to_string()),
122+
format: Some("mp4".to_string()),
123+
};
124+
let video4: Video = Video {
125+
url: "https://second-1200k.com".to_string(),
126+
bitrate: Some("1200k".to_string()),
127+
format: Some("mp4".to_string()),
128+
};
129+
let video5: Video = Video {
130+
url: "https://last.com".to_string(),
131+
bitrate: Some("1000k".to_string()),
132+
format: Some("h264".to_string()),
133+
};
134+
let test: Episode = Episode {
135+
id: "123".to_string(),
136+
nola_episode: "SAST4921".to_string(),
137+
videos: vec![video1, video2, video3, video4, video5],
138+
title: "Foo".to_string(),
139+
};
140+
141+
assert_eq!(test.get_video_url(), "https://720.com");
142+
}
143+
}

src/main.rs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ extern crate reqwest;
33
extern crate simple_logger;
44
use log::error;
55
use log::info;
6-
use serde::Deserialize;
76
use std::fs;
87
use std::io::copy;
98
use std::path::PathBuf;
@@ -42,60 +41,6 @@ enum Command {
4241
List,
4342
}
4443

45-
#[cfg(test)]
46-
mod tests {
47-
use super::*;
48-
49-
#[test]
50-
fn season_number() {
51-
let video: json::Video = json::Video {
52-
url: "https://url.com".to_string(),
53-
bitrate: Some("720p".to_string()),
54-
format: Some("mp4".to_string()),
55-
};
56-
let test: json::Episode = json::Episode {
57-
id: "123".to_string(),
58-
nola_episode: "SAST4921".to_string(),
59-
videos: vec![video],
60-
title: "Foo".to_string(),
61-
};
62-
63-
assert_eq!(test.return_season_number(), "49");
64-
}
65-
66-
#[test]
67-
fn episode_number() {
68-
let video: json::Video = json::Video {
69-
url: "https://url.com".to_string(),
70-
bitrate: Some("720p".to_string()),
71-
format: Some("mp4".to_string()),
72-
};
73-
let test: json::Episode = json::Episode {
74-
id: "123".to_string(),
75-
nola_episode: "SAST4921".to_string(),
76-
videos: vec![video],
77-
title: "Foo".to_string(),
78-
};
79-
80-
assert_eq!(test.return_episode_number(), "21");
81-
82-
let dtvideo: json::Video = json::Video {
83-
url: "https://url.com".to_string(),
84-
bitrate: Some("720p".to_string()),
85-
format: Some("mp4".to_string()),
86-
};
87-
88-
let dtest: json::Episode = json::Episode {
89-
id: "123".to_string(),
90-
nola_episode: "DTIG101".to_string(),
91-
videos: vec![dtvideo],
92-
title: "Foo".to_string(),
93-
};
94-
95-
assert_eq!(dtest.return_episode_number(), "1");
96-
}
97-
}
98-
9944
fn gen_history_path() -> String {
10045
let home = match dirs::home_dir() {
10146
Some(p) => p.to_string_lossy().into_owned(),
@@ -161,7 +106,7 @@ fn main() {
161106
}
162107

163108
process::exit(0)
164-
},
109+
}
165110
None => {}
166111
}
167112

0 commit comments

Comments
 (0)