stream_lib 0.3.0

Tool to download differnt types of streams
Documentation
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use std::{
    collections::{HashSet, VecDeque},
    io::{copy, BufWriter, Write},
    sync::Arc,
    thread,
    time::{self, Duration},
};

#[cfg(feature = "spinner")]
use indicatif::{ProgressBar, ProgressStyle};

use reqwest::{Client as ReqwestClient, Request, Url};

use hls_m3u8::{MasterPlaylist, MediaPlaylistOptions};

use parking_lot::{Mutex, RwLock};

use crate::error::Error;

/// Write buffer
const WRITE_SIZE: usize = 131_072;

/// HLS will try and look for new segments 12 times,
const HLS_MAX_RETRIES: usize = 12;

/// A Enum with the types of streams supported
#[derive(Debug)]
pub enum StreamType {
    /// A stream that is just a chunked http response.
    Chuncked(Request),
    /// A m3u8 playlist, which may be a stream.
    HLS(Request),
    /// A m3u8 master playlist and a string which is the name of the stream to download.
    NamedPlaylist(Request, String),
}

#[derive(Debug, Clone)]
enum _StreamType {
    Chuncked,
    HLS,
    NamedPlaylist(String),
}

#[derive(Debug)]
pub struct Stream {
    request: Request,
    stream_type: _StreamType,
    #[allow(dead_code)]
    spinner: bool,
}

#[derive(Clone)]
enum HlsQueue {
    Url(Url),
    StreamOver,
}

impl Stream {
    /// Creates a new stream handler.
    pub fn new(request: StreamType) -> Self {
        match request {
            StreamType::HLS(req) => Stream {
                request: req,
                stream_type: _StreamType::HLS,
                spinner: true,
            },
            StreamType::Chuncked(req) => Stream {
                request: req,
                stream_type: _StreamType::Chuncked,
                spinner: true,
            },
            StreamType::NamedPlaylist(req, name) => Stream {
                request: req,
                stream_type: _StreamType::NamedPlaylist(name),
                spinner: true,
            },
        }
    }
    /// Writes the stream to a writer.
    pub fn write_file<W>(self, client: &ReqwestClient, writer: W) -> Result<u64, Error>
    where
        W: Write,
    {
        match self.stream_type {
            _StreamType::Chuncked => Ok(self.chunked(client, writer)?),
            _StreamType::HLS => Ok(self.hls(client, writer)?),
            _StreamType::NamedPlaylist(ref name) => {
                let name = name.to_owned();
                Ok(self.named_playlist(client, writer, name)?)
            }
        }
    }

    fn chunked<W>(self, client: &ReqwestClient, writer: W) -> Result<u64, Error>
    where
        W: Write,
    {
        #[cfg(feature = "spinner")]
        let spinner = ProgressBar::new(0);
        #[cfg(feature = "spinner")]
        spinner.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.green} [{elapsed_precise}] Streamed {bytes}")
                .tick_chars(
                    "⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈ ",
                ),
        );

        let mut buf_writer = BufWriter::with_capacity(WRITE_SIZE, writer);
        #[cfg(feature = "spinner")]
        let source = client.execute(self.request)?;
        #[cfg(not(feature = "spinner"))]
        let mut source = client.execute(self.request)?;
        #[cfg(feature = "spinner")]
        let size = copy(&mut spinner.wrap_read(source), &mut buf_writer);
        #[cfg(not(feature = "spinner"))]
        let size = copy(&mut source, &mut buf_writer);
        Ok(size?)
    }

    // This currently clones the client to get a client to run the inner calls as well.
    fn hls<W>(self, client: &ReqwestClient, writer: W) -> Result<u64, Error>
    where
        W: Write,
    {
        let to_work = Arc::new(Mutex::new(VecDeque::<HlsQueue>::new())); // User in Inner
        let other_work = to_work.clone(); // Used in Outer
        let links: Arc<RwLock<HashSet<String>>> = Arc::new(RwLock::new(HashSet::new())); // Used in Inner
        let _outer_links = links.clone(); // Used in Outer (Not currently in use)
        let headers = self.request.headers().clone();

        // Inner loop -- Start
        // Here the handling of the m3u8 file is happening
        // it pushes it through the `to_work` mutex

        // Only used if the body of the request is not able to be cloned.
        let inner_url = self.request.url().clone();
        let inner_headers = self.request.headers().clone();

        let master_url = self.request.url().clone().join(".")?;
        let inner_client = client.to_owned();
        thread::spawn(move || {
            let mut counter = 0;

            loop {
                if counter > HLS_MAX_RETRIES {
                    // There have either been errors or no new segments
                    // for `HLS_MAX_RETRIES` times the segment duration given
                    // in the m3u8 playlist file.
                    let work_queue = &mut to_work.lock();
                    work_queue.push_back(HlsQueue::StreamOver);
                    break;
                }

                // Use the same headers as the original request
                let req = match self.request.try_clone() {
                    Some(r) => r,
                    // If the body is not able to be cloned it will only clone the headers.
                    None => {
                        warn!("[HLS] body not able to be cloned only clones headers.");
                        match inner_client
                            .get(inner_url.clone())
                            .headers(inner_headers.clone())
                            .build()
                        {
                            Ok(br) => br,
                            Err(e) => {
                                warn!("[HLS] Request creation failed!\n{}", e);
                                counter += 1;
                                continue;
                            }
                        }
                    }
                };

                let mut res = match inner_client.execute(req) {
                    Ok(r) => r,
                    Err(e) => {
                        warn!("[HLS] Playlist download failed!\n{}", e);
                        counter += 1;
                        continue;
                    }
                };

                let m3u8_string = match res.text() {
                    Ok(t) => t,
                    Err(e) => {
                        warn!("[HLS] Playlist text failed!\n{}", e);
                        counter += 1;
                        continue;
                    }
                };

                // Allow excess segment duration because a lot of video sites have
                // not very high quality m3u8 playlists, where the video segments,
                // may be longer than what the file specifies as max.
                let m3u8 = match MediaPlaylistOptions::new()
                    .allowable_excess_segment_duration(Duration::from_secs(10))
                    .parse(&m3u8_string)
                {
                    Ok(p) => p,
                    Err(e) => {
                        warn!("[HLS] Parsing failed!\n{}", e);
                        trace!("[HLS]\n{}", &m3u8_string);
                        counter += 1;
                        continue;
                    }
                };

                // Get the target duration of a segment
                let target_duration = m3u8.target_duration_tag().duration();

                // Makes a iterator with the url parts from the playlist
                let m3u8_iterator = m3u8.segments().iter().map(|e| String::from(e.uri().trim()));

                for e in m3u8_iterator {
                    trace!("[HLS] Tries to inserts: {}", e);
                    // Check if we have the segment in our set already
                    if links.write().insert(e.clone()) {
                        // Reset the counter as we got a new segment.
                        counter = 0;

                        // Construct a url from the master and the segment.
                        let url_formatted = if let Ok(u) = Url::parse(&e) {
                            u
                        } else {
                            Url::parse(&format!("{}{}", master_url.as_str(), &e))
                                .expect("The m3u8 does not currently work with stream_lib, please report the issue on the github repo, with an example of the playlistfile.")
                        };
                        let work_queue = &mut to_work.lock();

                        // Check if the segment is a Afreeca preloading segment.
                        if !(e.contains("preloading")) {
                            info!("[HLS] Adds {}!", url_formatted);
                            // Add the segment to the queue.
                            work_queue.push_back(HlsQueue::Url(url_formatted));
                        }
                    }
                }
                warn!("[HLS] Sleeps for {:#?}", target_duration);
                // Sleeps for the target duration.
                thread::sleep(target_duration);
                counter += 1;
            }
        });

        let mut total_size = 0;

        #[cfg(feature = "spinner")]
        let spinner = ProgressBar::new(0);
        #[cfg(feature = "spinner")]
        spinner.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.green} [{elapsed_precise}] {bytes} Segments")
                .tick_chars(
                    "⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈ ",
                ),
        );
        #[cfg(feature = "spinner")]
        spinner.enable_steady_tick(100);

        let mut buf_writer = BufWriter::with_capacity(WRITE_SIZE, writer);

        loop {
            let to_download = other_work.lock().pop_front();
            match to_download {
                Some(HlsQueue::Url(u)) => {
                    let req = client.get(u).headers(headers.clone()).build()?;
                    let size = download_to_file(client, req, &mut buf_writer)?;
                    #[cfg(feature = "spinner")]
                    spinner.inc(size);
                    total_size += size;
                }
                Some(HlsQueue::StreamOver) => break,
                None => {
                    trace!("[HLS] None to download!");
                    thread::sleep(time::Duration::from_secs(5));
                }
            }
        }
        Ok(total_size)
    }

    // This currently clones the client to get a client to run the inner calls as well.
    fn named_playlist<W>(
        self,
        client: &ReqwestClient,
        writer: W,
        name: String,
    ) -> Result<u64, Error>
    where
        W: Write,
    {
        let to_work = Arc::new(Mutex::new(VecDeque::<HlsQueue>::new())); // User in Inner
        let other_work = to_work.clone(); // Used in Outer
        let links: Arc<RwLock<HashSet<String>>> = Arc::new(RwLock::new(HashSet::new())); // Used in Inner
        let _outer_links = links.clone(); // Used in Outer (Not currently in use)
        let headers = self.request.headers().clone();

        // Inner loop -- Start
        // Here the handling of the m3u8 file is happening
        // it pushes it through the `to_work` mutex

        // Only used if the body of the request is not able to be cloned.
        let inner_url = self.request.url().clone();
        let inner_headers = self.request.headers().clone();

        let inner_client = client.to_owned();
        thread::spawn(move || {
            let mut counter = 0;

            loop {
                if counter > HLS_MAX_RETRIES {
                    // There have either been errors or no new segments
                    // for `HLS_MAX_RETRIES` times the segment duration given
                    // in the m3u8 playlist file.
                    let work_queue = &mut to_work.lock();
                    work_queue.push_back(HlsQueue::StreamOver);
                    break;
                }

                // Use the same headers as the original request
                let req = match self.request.try_clone() {
                    Some(r) => r,
                    // If the body is not able to be cloned it will only clone the headers.
                    None => {
                        warn!("[HLS] body not able to be cloned only clones headers.");
                        match inner_client
                            .get(inner_url.clone())
                            .headers(inner_headers.clone())
                            .build()
                        {
                            Ok(br) => br,
                            Err(e) => {
                                warn!("[HLS] Request creation failed!\n{}", e);
                                counter += 1;
                                continue;
                            }
                        }
                    }
                };

                let mut master_res = match inner_client.execute(req) {
                    Ok(r) => r,
                    Err(e) => {
                        warn!("[HLS] Playlist download failed!\n{}", e);
                        counter += 1;
                        continue;
                    }
                };

                let master_string = match master_res.text() {
                    Ok(t) => t,
                    Err(e) => {
                        warn!("[HLS] Playlist text failed!\n{}", e);
                        counter += 1;
                        continue;
                    }
                };

                let master_playlist = master_string.parse::<MasterPlaylist>().unwrap();

                let segment_pos = master_playlist
                    .media_tags()
                    .iter()
                    .position(|e| e.name().trim() == name)
                    .unwrap();

                #[allow(clippy::redundant_closure)]
                let master_iter: Vec<String> = master_playlist
                    .stream_inf_tags()
                    .iter()
                    .map(|e| e.uri())
                    .map(|e| String::from(e.trim()))
                    .collect();

                let segment = master_iter[segment_pos].clone();
                let master_url = (&segment)
                    .parse::<reqwest::Url>()
                    .unwrap()
                    .join(".")
                    .unwrap();

                let mp_hls = match inner_client
                    .get(&segment)
                    .headers(inner_headers.clone())
                    .build()
                {
                    Ok(p) => p,
                    Err(e) => {
                        warn!("[HLS] URI!\n{}", e);
                        trace!("[HLS]\n{}", segment);
                        counter += 1;
                        continue;
                    }
                };

                let mut res = match inner_client.execute(mp_hls) {
                    Ok(r) => r,
                    Err(e) => {
                        warn!("[HLS] Playlist download failed!\n{}", e);
                        counter += 1;
                        continue;
                    }
                };

                let m3u8_string = match res.text() {
                    Ok(t) => t,
                    Err(e) => {
                        warn!("[HLS] Playlist text failed!\n{}", e);
                        counter += 1;
                        continue;
                    }
                };

                // Allow excess segment duration because a lot of video sites have
                // not very high quality m3u8 playlists, where the video segments,
                // may be longer than what the file specifies as max.
                let m3u8 = match MediaPlaylistOptions::new()
                    .allowable_excess_segment_duration(Duration::from_secs(10))
                    .parse(&m3u8_string)
                {
                    Ok(p) => p,
                    Err(e) => {
                        warn!("[HLS] Parsing failed!\n{}", e);
                        trace!("[HLS]\n{}", &m3u8_string);
                        counter += 1;
                        continue;
                    }
                };

                // Get the target duration of a segment
                let target_duration = m3u8.target_duration_tag().duration();

                // Makes a iterator with the url parts from the playlist
                let m3u8_iterator = m3u8.segments().iter().map(|e| String::from(e.uri().trim()));

                for e in m3u8_iterator {
                    trace!("[HLS] Tries to inserts: {}", e);
                    // Check if we have the segment in our set already
                    if links.write().insert(e.clone()) {
                        // Reset the counter as we got a new segment.
                        counter = 0;

                        // Construct a url from the master and the segment.
                        let url_formatted = if let Ok(u) = Url::parse(&e) {
                            u
                        } else {
                            Url::parse(&format!("{}{}", master_url.as_str(), &e))
                                .expect("The m3u8 does not currently work with stream_lib, please report the issue on the github repo, with an example of the playlistfile.")
                        };
                        let work_queue = &mut to_work.lock();

                        // Check if the segment is a Afreeca preloading segment.
                        if !(e.contains("preloading")) {
                            info!("[HLS] Adds {}!", url_formatted);
                            // Add the segment to the queue.
                            work_queue.push_back(HlsQueue::Url(url_formatted));
                        }
                    }
                    warn!("[HLS] Sleeps for {:?}", target_duration);
                    // Sleeps for the target duration.
                    thread::sleep(target_duration);
                    counter += 1;
                }
            }
        });

        let mut total_size = 0;

        #[cfg(feature = "spinner")]
        let spinner = ProgressBar::new(0);
        #[cfg(feature = "spinner")]
        spinner.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.green} [{elapsed_precise}] {bytes} Segments")
                .tick_chars(
                    "⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈ ",
                ),
        );
        #[cfg(feature = "spinner")]
        spinner.enable_steady_tick(100);

        let mut buf_writer = BufWriter::with_capacity(WRITE_SIZE, writer);

        loop {
            let to_download = other_work.lock().pop_front();
            match to_download {
                Some(HlsQueue::Url(u)) => {
                    info!("[MASTER] Downloads: {}", u);
                    let req = client.get(u).headers(headers.clone()).build()?;
                    let size = download_to_file(client, req, &mut buf_writer)?;
                    #[cfg(feature = "spinner")]
                    spinner.inc(size);
                    total_size += size;
                }
                Some(HlsQueue::StreamOver) => break,
                None => {
                    trace!("[HLS] None to download!");
                    thread::sleep(Duration::from_secs(5));
                }
            }
        }
        Ok(total_size)
    }
}

#[inline]
fn download_to_file<W>(
    client: &ReqwestClient,
    request: Request,
    mut file: &mut BufWriter<W>,
) -> Result<u64, Error>
where
    W: Write,
{
    let mut source = client.execute(request)?;
    let size = copy(&mut source, &mut file)?;
    info!("[MASTER] Downloaded: {}", size);
    Ok(size)
}