@@ -42,30 +42,54 @@ pub struct Episode {
4242}
4343
4444impl 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+ }
0 commit comments