rsget_lib 0.3.0

Library to get information about and download livestreams
Documentation
use crate::utils::error::RsgetError;
use crate::utils::error::StreamError;

use std::process::Command;

use serde::de::DeserializeOwned;

use reqwest;
use reqwest::Client as RClient;

#[derive(Debug, Clone)]
pub struct DownloadClient {
    pub rclient: RClient,
}

pub fn ffmpeg_download(url: String, path: String) -> Result<(), StreamError> {
    let comm = Command::new("ffmpeg")
        .arg("-i")
        .arg(url)
        .arg("-c")
        .arg("copy")
        .arg(path)
        .status()
        .expect("ffmpeg failed to start");
    match comm.code() {
        Some(c) => {
            info!("Ffmpeg returned: {}", c);
            Ok(())
        }
        None => {
            info!("Err: Ffmpeg failed");
            Err(StreamError::Rsget(RsgetError::new("Ffmpeg failed")))
        }
    }
}

impl DownloadClient {
    pub fn new() -> Result<Self, StreamError> {
        Ok(DownloadClient {
            rclient: RClient::new(),
        })
    }

    pub fn download_to_string(&self, req: reqwest::Request) -> Result<String, StreamError> {
        let c = &self.rclient;
        let mut res = c.execute(req)?;
        res.text().map_err(StreamError::from)
    }

    pub fn download_and_de<T: DeserializeOwned>(
        &self,
        req: reqwest::Request,
    ) -> Result<T, StreamError> {
        let c = &self.rclient;
        let mut res = c.execute(req)?;
        let json: T = res.json()?;
        Ok(json)
    }

    pub fn make_request(
        &self,
        uri: &str,
        headers: Option<(&str, &str)>,
    ) -> Result<reqwest::Request, StreamError> {
        let c = &self.rclient;
        match headers {
            Some(a) => c
                .get(uri)
                .header(a.0, a.1)
                .build()
                .map_err(StreamError::from),
            None => c.get(uri).build().map_err(StreamError::from),
        }
    }
}