Skip to content

Commit 53a589c

Browse files
asimpsonAdam Simpson
authored andcommitted
fix: use proper sort for quality
1 parent b3a8882 commit 53a589c

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

src/json.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ impl Episode {
4747
}
4848

4949
pub fn get_video_url(&self) -> String {
50-
let mut url = String::new();
51-
let videos: Vec<&Video> = self
50+
let mut videos: Vec<&Video> = self
5251
.videos
5352
.iter()
5453
.filter(|x| x.format == Some("mp4".to_string()))
@@ -59,45 +58,39 @@ impl Episode {
5958
})
6059
.collect();
6160

62-
let preferred_quality: String = videos
63-
.iter()
64-
.filter(|x| x.bitrate == Some("720p".to_string()))
65-
.map(|x| x.url.to_string())
66-
.collect();
61+
videos.sort_by(|a, b| a.quality_rating().cmp(&b.quality_rating()));
6762

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();
63+
return videos[0].proper_url();
64+
}
65+
}
7366

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();
67+
#[derive(Debug, Deserialize, Clone)]
68+
pub struct Video {
69+
pub url: String,
70+
pub bitrate: Option<String>,
71+
pub format: Option<String>,
72+
}
7973

80-
if third_quality.len() > 0 {
81-
url = third_quality;
74+
impl Video {
75+
fn quality_rating(&self) -> u8 {
76+
if self.bitrate == Some("720p".to_string()) {
77+
return 1;
8278
}
8379

84-
if second_quality.len() > 0 {
85-
url = second_quality;
80+
if self.bitrate == Some("4500k".to_string()) {
81+
return 2;
8682
}
8783

88-
if preferred_quality.len() > 0 {
89-
url = preferred_quality;
84+
if self.bitrate == Some("1200k".to_string()) {
85+
return 3;
9086
}
9187

92-
return url;
88+
return 99;
9389
}
94-
}
9590

96-
#[derive(Debug, Deserialize, Clone)]
97-
pub struct Video {
98-
pub url: String,
99-
pub bitrate: Option<String>,
100-
pub format: Option<String>,
91+
fn proper_url(&self) -> String {
92+
return self.url.to_string();
93+
}
10194
}
10295

10396
#[cfg(test)]

0 commit comments

Comments
 (0)