ilovetv/src/lib.rs

66 lines
1.6 KiB
Rust
Raw Normal View History

mod m3u8;
mod parser;
use std::{
fs,
2023-01-19 01:39:50 +01:00
io::{stdin, stdout, Stdin, StdoutLock, Write},
process,
};
pub use m3u8::M3u8;
pub use parser::Parser;
mod config;
2023-01-18 21:23:04 +01:00
mod downloader;
2023-01-17 18:36:28 +01:00
use directories::ProjectDirs;
2023-01-19 01:22:29 +01:00
pub use downloader::download_with_progress;
pub fn setup() -> String {
2023-01-17 18:36:28 +01:00
let project_dirs = ProjectDirs::from("com", "billenius", "iptvnator_rs").unwrap();
let config_dir = project_dirs.config_dir();
let ilovetv_config_file = config_dir.join("iptv_url.txt");
if ilovetv_config_file.exists() {
return fs::read_to_string(&ilovetv_config_file).expect("Failed to read iptv_url");
}
2023-01-19 01:39:50 +01:00
let mut readline = Readline::new();
2023-01-17 20:59:19 +01:00
println!("Hello, I would need an url to your iptv/m3u/m3u8 stream");
let url = loop {
2023-01-19 01:39:50 +01:00
let url = readline.input("enter url: ");
let yn = readline.input("Are you sure? (Y/n) ");
2023-01-17 20:59:19 +01:00
2023-01-19 01:39:50 +01:00
if yn.trim().to_lowercase() != "n" {
2023-01-17 20:59:19 +01:00
break url.trim().to_string();
}
};
let _ = fs::create_dir_all(config_dir);
2023-01-17 20:59:19 +01:00
if let Err(e) = fs::write(ilovetv_config_file, &url) {
eprintln!("{:?}", e);
process::exit(-1);
}
2023-01-17 20:59:19 +01:00
url
}
2023-01-19 01:39:50 +01:00
pub struct Readline<'a> {
stdout: StdoutLock<'a>,
stdin: Stdin,
}
impl<'a> Readline<'a> {
pub fn new() -> Self {
Self {
stdout: stdout().lock(),
stdin: stdin(),
}
}
pub fn input(&mut self, toprint: &str) -> String {
print!("{}", toprint);
self.stdout.flush().unwrap();
let mut buffer = String::new();
self.stdin.read_line(&mut buffer).unwrap();
2023-01-19 14:14:35 +01:00
buffer.to_lowercase()
2023-01-19 01:39:50 +01:00
}
}