-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhls_playlist_sorter.rs
54 lines (46 loc) · 1.46 KB
/
hls_playlist_sorter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use reqwest;
use std::collections::VecDeque;
use std::fs::File;
use std::io::Write;
use std::path::Path;
#[derive(Debug, PartialEq, Eq)]
struct PlaylistItem {
url: String,
duration: f64,
}
impl Ord for PlaylistItem {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.duration.partial_cmp(&other.duration).unwrap()
}
}
impl PartialOrd for PlaylistItem {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let url = "https://example.com/playlist.m3u8"; // replace with your playlist URL
let response = reqwest::get(url).await?;
let body = response.text().await?;
let mut lines = body.lines();
let mut items = VecDeque::new();
while let Some(line) = lines.next() {
if line.starts_with("#EXTINF:") {
let duration = line.trim_start_matches("#EXTINF:").trim_end_matches(",");
let url = lines.next().unwrap();
items.push_back(PlaylistItem {
url: url.to_string(),
duration: duration.parse().unwrap(),
});
}
}
items.make_contiguous().sort();
let mut file = File::create(Path::new("sorted_playlist.m3u8")).unwrap();
writeln!(file, "#EXTM3U").unwrap();
for item in items {
writeln!(file, "#EXTINF:{},", item.duration).unwrap();
writeln!(file, "{}", item.url).unwrap();
}
Ok(())
}