simplify remote playlist code (WIP)
This commit is contained in:
parent
08ded412c9
commit
704053e599
@ -80,42 +80,9 @@ impl CurrentProgram {
|
|||||||
} else if Path::new(&self.json_path.clone().unwrap()).is_file()
|
} else if Path::new(&self.json_path.clone().unwrap()).is_file()
|
||||||
|| is_remote(&self.json_path.clone().unwrap())
|
|| is_remote(&self.json_path.clone().unwrap())
|
||||||
{
|
{
|
||||||
let mut is_playlist_changed = false;
|
|
||||||
|
|
||||||
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());
|
let mod_time = modified_time(&self.json_path.clone().unwrap());
|
||||||
|
|
||||||
if let Some(m) = mod_time {
|
if self.json_mod != mod_time {
|
||||||
if !m.to_string().eq(&self.json_mod.clone().unwrap()) {
|
|
||||||
is_playlist_changed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if is_playlist_changed {
|
|
||||||
// when playlist has changed, reload it
|
// when playlist has changed, reload it
|
||||||
info!(
|
info!(
|
||||||
"Reload playlist <b><magenta>{}</></b>",
|
"Reload playlist <b><magenta>{}</></b>",
|
||||||
@ -137,11 +104,6 @@ impl CurrentProgram {
|
|||||||
self.index.fetch_add(1, Ordering::SeqCst);
|
self.index.fetch_add(1, Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.on_check_update_error();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_check_update_error(&mut self) {
|
|
||||||
error!(
|
error!(
|
||||||
"Playlist <b><magenta>{}</></b> not exists!",
|
"Playlist <b><magenta>{}</></b> not exists!",
|
||||||
self.json_path.clone().unwrap()
|
self.json_path.clone().unwrap()
|
||||||
@ -157,6 +119,7 @@ impl CurrentProgram {
|
|||||||
self.playout_stat.list_init.store(true, Ordering::SeqCst);
|
self.playout_stat.list_init.store(true, Ordering::SeqCst);
|
||||||
self.index.store(0, Ordering::SeqCst);
|
self.index.store(0, Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check if day is past and it is time for a new playlist.
|
// Check if day is past and it is time for a new playlist.
|
||||||
fn check_for_next_playlist(&mut self) {
|
fn check_for_next_playlist(&mut self) {
|
||||||
|
@ -8,7 +8,9 @@ use std::{
|
|||||||
|
|
||||||
use simplelog::*;
|
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;
|
pub const DUMMY_LEN: f64 = 60.0;
|
||||||
|
|
||||||
@ -39,7 +41,7 @@ impl Playlist {
|
|||||||
date,
|
date,
|
||||||
start_sec: Some(start),
|
start_sec: Some(start),
|
||||||
current_file: None,
|
current_file: None,
|
||||||
modified: Some(String::new()),
|
modified: None,
|
||||||
program: vec![media],
|
program: vec![media],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,7 +61,7 @@ pub fn read_json(
|
|||||||
let mut start_sec = config.playlist.start_sec.unwrap();
|
let mut start_sec = config.playlist.start_sec.unwrap();
|
||||||
let date = get_date(seek, start_sec, next_start);
|
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();
|
let d: Vec<&str> = date.split('-').collect();
|
||||||
playlist_path = playlist_path
|
playlist_path = playlist_path
|
||||||
.join(d[0])
|
.join(d[0])
|
||||||
@ -75,48 +77,36 @@ pub fn read_json(
|
|||||||
current_file = p
|
current_file = p
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut playlist: Playlist;
|
let mut playlist = Playlist::new(date, start_sec);
|
||||||
|
|
||||||
if is_remote(¤t_file) {
|
if is_remote(¤t_file) {
|
||||||
let resp = reqwest::blocking::Client::new().get(¤t_file).send();
|
let response = reqwest::blocking::Client::new().get(¤t_file).send();
|
||||||
|
|
||||||
match resp {
|
if let Ok(resp) = response {
|
||||||
Ok(resp) => {
|
|
||||||
if resp.status().is_success() {
|
if resp.status().is_success() {
|
||||||
info!("Read Remote Playlist: <b><magenta>{current_file}</></b>");
|
info!("Read Remote Playlist: <b><magenta>{current_file}</></b>");
|
||||||
|
|
||||||
let headers = resp.headers().clone();
|
let headers = resp.headers().clone();
|
||||||
let body = resp.text().unwrap();
|
|
||||||
|
|
||||||
|
if let Ok(body) = resp.text() {
|
||||||
playlist =
|
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) {
|
if let Some(time) = time_from_header(&headers) {
|
||||||
Some(t) => {
|
playlist.modified = Some(time.to_string());
|
||||||
playlist.modified = Some(t.to_str().unwrap().to_string());
|
}
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error!(
|
error!("Can't read remote playlist <b><magenta>{current_file}</></b>");
|
||||||
"Get Remote Playlist <b><magenta>{current_file}</></b> not success!: {}",
|
|
||||||
resp.text().unwrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
return Playlist::new(date, start_sec);
|
return playlist;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("Remote Playlist <b><magenta>{current_file}</></b>: {}", e);
|
|
||||||
|
|
||||||
return Playlist::new(date, start_sec);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
if !playlist_path.is_file() {
|
if !playlist_path.is_file() {
|
||||||
error!("Playlist <b><magenta>{current_file}</></b> not exists!");
|
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>");
|
info!("Read Playlist: <b><magenta>{current_file}</></b>");
|
||||||
@ -126,13 +116,8 @@ pub fn read_json(
|
|||||||
.write(false)
|
.write(false)
|
||||||
.open(¤t_file)
|
.open(¤t_file)
|
||||||
.expect("Could not open json playlist file.");
|
.expect("Could not open json playlist file.");
|
||||||
playlist = serde_json::from_reader(f).expect("Could not read json playlist file.");
|
playlist = serde_json::from_reader(f).expect("Could't read json playlist file.");
|
||||||
|
playlist.modified = modified_time(¤t_file);
|
||||||
let modify = modified_time(¤t_file);
|
|
||||||
|
|
||||||
if let Some(modi) = modify {
|
|
||||||
playlist.modified = Some(modi.to_string());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
playlist.current_file = Some(current_file);
|
playlist.current_file = Some(current_file);
|
||||||
|
@ -8,7 +8,9 @@ use std::{
|
|||||||
|
|
||||||
use chrono::{prelude::*, Duration};
|
use chrono::{prelude::*, Duration};
|
||||||
use ffprobe::{ffprobe, Format, Stream};
|
use ffprobe::{ffprobe, Format, Stream};
|
||||||
|
use jsonrpc_http_server::hyper::HeaderMap;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use reqwest::header;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use simplelog::*;
|
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()
|
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.
|
/// 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()) {
|
if let Ok(time) = metadata(path).and_then(|metadata| metadata.modified()) {
|
||||||
let date_time: DateTime<Local> = time.into();
|
let date_time: DateTime<Local> = time.into();
|
||||||
return Some(date_time);
|
return Some(date_time.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
|
Loading…
Reference in New Issue
Block a user