simplify remote playlist code (WIP)

This commit is contained in:
jb-alvarado 2022-06-02 18:21:43 +02:00
parent 08ded412c9
commit 704053e599
3 changed files with 68 additions and 92 deletions

View File

@ -80,42 +80,9 @@ impl CurrentProgram {
} else if Path::new(&self.json_path.clone().unwrap()).is_file()
|| is_remote(&self.json_path.clone().unwrap())
{
let mut is_playlist_changed = false;
let mod_time = modified_time(&self.json_path.clone().unwrap());
if is_remote(&self.json_path.clone().unwrap()) {
let resp = reqwest::blocking::Client::new()
.head(self.json_path.clone().unwrap())
.send();
match resp {
Ok(resp) => {
if resp.status().is_success() {
match resp.headers().get(reqwest::header::LAST_MODIFIED) {
Some(last_modified) => {
if !last_modified
.to_str()
.unwrap()
.eq(&self.json_mod.clone().unwrap())
{
is_playlist_changed = true
}
}
None => {}
}
}
}
Err(_) => self.on_check_update_error(),
};
} else {
let mod_time = modified_time(&self.json_path.clone().unwrap());
if let Some(m) = mod_time {
if !m.to_string().eq(&self.json_mod.clone().unwrap()) {
is_playlist_changed = true;
}
}
}
if is_playlist_changed {
if self.json_mod != mod_time {
// when playlist has changed, reload it
info!(
"Reload playlist <b><magenta>{}</></b>",
@ -137,27 +104,23 @@ impl CurrentProgram {
self.index.fetch_add(1, Ordering::SeqCst);
}
} else {
self.on_check_update_error();
error!(
"Playlist <b><magenta>{}</></b> not exists!",
self.json_path.clone().unwrap()
);
let mut media = Media::new(0, String::new(), false);
media.begin = Some(get_sec());
media.duration = DUMMY_LEN;
media.out = DUMMY_LEN;
self.json_path = None;
*self.nodes.lock().unwrap() = vec![media.clone()];
self.current_node = media;
self.playout_stat.list_init.store(true, Ordering::SeqCst);
self.index.store(0, Ordering::SeqCst);
}
}
fn on_check_update_error(&mut self) {
error!(
"Playlist <b><magenta>{}</></b> not exists!",
self.json_path.clone().unwrap()
);
let mut media = Media::new(0, String::new(), false);
media.begin = Some(get_sec());
media.duration = DUMMY_LEN;
media.out = DUMMY_LEN;
self.json_path = None;
*self.nodes.lock().unwrap() = vec![media.clone()];
self.current_node = media;
self.playout_stat.list_init.store(true, Ordering::SeqCst);
self.index.store(0, Ordering::SeqCst);
}
// Check if day is past and it is time for a new playlist.
fn check_for_next_playlist(&mut self) {
let current_time = get_sec();

View File

@ -8,7 +8,9 @@ use std::{
use simplelog::*;
use crate::utils::{get_date, is_remote, modified_time, validate_playlist, GlobalConfig, Media};
use crate::utils::{
get_date, is_remote, modified_time, time_from_header, validate_playlist, GlobalConfig, Media,
};
pub const DUMMY_LEN: f64 = 60.0;
@ -39,7 +41,7 @@ impl Playlist {
date,
start_sec: Some(start),
current_file: None,
modified: Some(String::new()),
modified: None,
program: vec![media],
}
}
@ -59,7 +61,7 @@ pub fn read_json(
let mut start_sec = config.playlist.start_sec.unwrap();
let date = get_date(seek, start_sec, next_start);
if playlist_path.is_dir() {
if playlist_path.is_dir() || is_remote(&config.playlist.path) {
let d: Vec<&str> = date.split('-').collect();
playlist_path = playlist_path
.join(d[0])
@ -75,48 +77,36 @@ pub fn read_json(
current_file = p
}
let mut playlist: Playlist;
let mut playlist = Playlist::new(date, start_sec);
if is_remote(&current_file) {
let resp = reqwest::blocking::Client::new().get(&current_file).send();
let response = reqwest::blocking::Client::new().get(&current_file).send();
match resp {
Ok(resp) => {
if resp.status().is_success() {
info!("Read Remote Playlist: <b><magenta>{current_file}</></b>");
if let Ok(resp) = response {
if resp.status().is_success() {
info!("Read Remote Playlist: <b><magenta>{current_file}</></b>");
let headers = resp.headers().clone();
let body = resp.text().unwrap();
let headers = resp.headers().clone();
if let Ok(body) = resp.text() {
playlist =
serde_json::from_str(&body).expect("Could not read json playlist str.");
serde_json::from_str(&body).expect("Could't read remote json playlist.");
match headers.get(reqwest::header::LAST_MODIFIED) {
Some(t) => {
playlist.modified = Some(t.to_str().unwrap().to_string());
}
None => {}
if let Some(time) = time_from_header(&headers) {
playlist.modified = Some(time.to_string());
}
} else {
error!(
"Get Remote Playlist <b><magenta>{current_file}</></b> not success!: {}",
resp.text().unwrap()
);
return Playlist::new(date, start_sec);
}
}
Err(e) => {
error!("Remote Playlist <b><magenta>{current_file}</></b>: {}", e);
} else {
error!("Can't read remote playlist <b><magenta>{current_file}</></b>");
return Playlist::new(date, start_sec);
}
};
return playlist;
}
} else {
if !playlist_path.is_file() {
error!("Playlist <b><magenta>{current_file}</></b> not exists!");
return Playlist::new(date, start_sec);
return playlist;
}
info!("Read Playlist: <b><magenta>{current_file}</></b>");
@ -126,13 +116,8 @@ pub fn read_json(
.write(false)
.open(&current_file)
.expect("Could not open json playlist file.");
playlist = serde_json::from_reader(f).expect("Could not read json playlist file.");
let modify = modified_time(&current_file);
if let Some(modi) = modify {
playlist.modified = Some(modi.to_string());
}
playlist = serde_json::from_reader(f).expect("Could't read json playlist file.");
playlist.modified = modified_time(&current_file);
}
playlist.current_file = Some(current_file);

View File

@ -8,7 +8,9 @@ use std::{
use chrono::{prelude::*, Duration};
use ffprobe::{ffprobe, Format, Stream};
use jsonrpc_http_server::hyper::HeaderMap;
use regex::Regex;
use reqwest::header;
use serde::{Deserialize, Serialize};
use serde_json::json;
use simplelog::*;
@ -233,11 +235,37 @@ pub fn get_date(seek: bool, start: f64, next_start: f64) -> String {
local.format("%Y-%m-%d").to_string()
}
pub fn time_from_header(headers: &HeaderMap) -> Option<DateTime<Local>> {
if let Some(time) = headers.get(header::LAST_MODIFIED) {
if let Ok(t) = time.to_str() {
let time = DateTime::parse_from_rfc2822(t);
let date_time: DateTime<Local> = time.unwrap().into();
return Some(date_time);
};
}
None
}
/// Get file modification time.
pub fn modified_time(path: &str) -> Option<DateTime<Local>> {
pub fn modified_time(path: &str) -> Option<String> {
if is_remote(path) {
let response = reqwest::blocking::Client::new().head(path).send();
if let Ok(resp) = response {
if resp.status().is_success() {
if let Some(time) = time_from_header(resp.headers()) {
return Some(time.to_string());
}
}
}
return None;
}
if let Ok(time) = metadata(path).and_then(|metadata| metadata.modified()) {
let date_time: DateTime<Local> = time.into();
return Some(date_time);
return Some(date_time.to_string());
}
None