Files
tuliprox/src/processing/m3u_parser.rs
T

224 lines
7.6 KiB
Rust
Raw Normal View History

2024-05-10 10:13:50 +02:00
use std::borrow::BorrowMut;
use std::cell::RefCell;
use std::rc::Rc;
2024-05-10 10:13:50 +02:00
2024-05-05 19:15:54 +02:00
use crate::model::config::{Config, ConfigInput};
2024-05-10 10:13:50 +02:00
use crate::model::playlist::{PlaylistGroup, PlaylistItem, PlaylistItemHeader, PlaylistItemType, XtreamCluster};
use crate::utils::default_utils::{default_as_empty_rc_str, default_playlist_item_type, default_stream_cluster};
use crate::utils::string_utils;
2023-03-19 17:16:05 +01:00
fn token_value(it: &mut std::str::Chars) -> String {
if let Some(oc) = it.next() {
if oc == '"' {
return get_value(it);
}
}
2024-05-10 12:01:48 +02:00
String::new()
2023-03-19 17:16:05 +01:00
}
fn get_value(it: &mut std::str::Chars) -> String {
let mut result: Vec<char> = vec![];
2023-05-03 17:46:39 +02:00
for oc in it.by_ref() {
2023-03-19 17:16:05 +01:00
if oc == '"' {
break;
}
result.push(oc);
}
2023-05-03 17:46:39 +02:00
result.iter().collect::<String>()
2023-03-19 17:16:05 +01:00
}
fn token_till(it: &mut std::str::Chars, stop_char: char) -> Option<String> {
let mut result: Vec<char> = vec![];
2023-05-03 17:46:39 +02:00
for ch in it.by_ref() {
if ch == stop_char {
break;
} else if ch.is_whitespace() && result.is_empty() {
continue;
2023-03-19 17:16:05 +01:00
}
2024-05-10 12:01:48 +02:00
result.push(ch);
2023-03-19 17:16:05 +01:00
}
2024-05-10 12:01:48 +02:00
if result.is_empty() { None } else { Some(result.iter().collect::<String>()) }
2023-03-19 17:16:05 +01:00
}
fn skip_digit(it: &mut std::str::Chars) -> Option<char> {
loop {
match it.next() {
Some(c) => {
2023-05-03 17:46:39 +02:00
if !(c == '-' || c == '+' || c.is_ascii_digit()) {
2023-03-19 17:16:05 +01:00
return Some(c);
}
}
None => return None,
}
}
}
2024-05-10 12:01:48 +02:00
fn create_empty_playlistitem_header(input_id: u16, url: &str) -> PlaylistItemHeader {
2023-09-29 16:03:20 +02:00
PlaylistItemHeader {
id: default_as_empty_rc_str(),
2024-03-15 18:34:39 +01:00
stream_id: default_as_empty_rc_str(),
name: default_as_empty_rc_str(),
chno: default_as_empty_rc_str(),
logo: default_as_empty_rc_str(),
logo_small: default_as_empty_rc_str(),
group: default_as_empty_rc_str(),
title: default_as_empty_rc_str(),
parent_code: default_as_empty_rc_str(),
audio_track: default_as_empty_rc_str(),
time_shift: default_as_empty_rc_str(),
rec: default_as_empty_rc_str(),
2024-05-10 12:01:48 +02:00
url: Rc::new(url.to_owned()),
2023-11-03 09:45:22 +01:00
epg_channel_id: None,
item_type: default_playlist_item_type(),
xtream_cluster: default_stream_cluster(),
additional_properties: None,
series_fetched: false,
2024-05-05 19:15:54 +02:00
category_id: 0,
2024-05-10 10:13:50 +02:00
input_id,
2023-09-29 16:03:20 +02:00
}
2023-09-26 19:41:02 +02:00
}
2023-03-19 17:16:05 +01:00
macro_rules! process_header_fields {
($header:expr, $token:expr, $(($prop:ident, $field:expr)),*; $val:expr) => {
match $token {
$(
$field => $header.$prop = Rc::new($val),
)*
_ => {}
}
};
}
2024-05-10 12:01:48 +02:00
fn process_header(input: &ConfigInput, video_suffixes: &Vec<&str>, content: &str, url: &str) -> PlaylistItemHeader {
let mut plih = create_empty_playlistitem_header(input.id, url);
2023-03-19 17:16:05 +01:00
let mut it = content.chars();
let line_token = token_till(&mut it, ':');
if line_token == Some(String::from("#EXTINF")) {
let mut c = skip_digit(&mut it);
loop {
2023-05-03 17:46:39 +02:00
if c.is_none() {
2023-03-19 17:16:05 +01:00
break;
}
2024-05-10 12:01:48 +02:00
if let ',' = c.unwrap() {
plih.title = Rc::new(get_value(&mut it));
} else {
let token = token_till(&mut it, '=');
if let Some(t) = token {
let value = token_value(&mut it);
process_header_fields!(plih, t.as_str(),
(id, "tvg-id"),
(group, "group-title"),
(name, "tvg-name"),
(chno, "tvg-chno"),
(parent_code, "parent-code"),
(audio_track, "audio-track"),
(logo, "tvg-logo"),
(logo_small, "tvg-logo-small"),
(time_shift, "timeshift"),
2024-05-10 12:01:48 +02:00
(rec, "tvg-rec"); value);
2023-03-19 17:16:05 +01:00
}
}
c = it.next();
}
if plih.id.is_empty() {
2024-05-10 12:01:48 +02:00
if let Some(chanid) = extract_id_from_url(url) {
plih.id = Rc::new(chanid);
}
}
plih.epg_channel_id = Some(Rc::clone(&plih.id));
2023-03-19 17:16:05 +01:00
}
2023-09-26 19:41:02 +02:00
for suffix in video_suffixes {
2023-10-25 09:57:44 +02:00
if url.ends_with(suffix) {
// TODO find Series based on group or configured names
plih.xtream_cluster = XtreamCluster::Video;
plih.item_type = PlaylistItemType::Movie;
2023-10-25 09:57:44 +02:00
break;
}
}
let header = plih.borrow_mut();
if header.name.is_empty() {
if !header.title.is_empty() {
header.name = header.title.clone();
} else if !header.id.is_empty() {
header.name = header.id.clone();
header.title = header.id.clone();
}
2023-09-26 19:41:02 +02:00
}
2023-03-19 17:16:05 +01:00
plih
}
fn extract_id_from_url(url: &str) -> Option<String> {
if let Some(filename) = url.split('/').last() {
2024-05-10 12:01:48 +02:00
return filename.rsplit('.').next().map(std::string::ToString::to_string);
}
None
}
2023-09-26 19:41:02 +02:00
2024-05-05 19:15:54 +02:00
pub(crate) fn consume_m3u<F: FnMut(PlaylistItem)>(cfg: &Config, input: &ConfigInput, lines: impl Iterator<Item=String>, mut visit: F) {
2023-03-19 17:16:05 +01:00
let mut header: Option<String> = None;
let mut group: Option<String> = None;
2024-05-10 12:01:48 +02:00
let video_suffixes = cfg.video.as_ref().unwrap().extensions.iter().map(std::string::String::as_str).collect();
2023-03-19 17:16:05 +01:00
for line in lines {
if line.starts_with("#EXTINF") {
header = Some(line);
2023-03-19 17:16:05 +01:00
continue;
}
if line.starts_with("#EXTGRP") {
group = Some(String::from(&line[8..]));
continue;
}
2023-05-03 17:46:39 +02:00
if line.starts_with('#') {
2023-03-19 17:16:05 +01:00
continue;
}
2023-10-11 14:33:17 +02:00
if let Some(header_value) = header {
2024-05-10 12:01:48 +02:00
let item = PlaylistItem { header: RefCell::new(process_header(input, &video_suffixes, &header_value, &line)) };
2024-05-05 19:15:54 +02:00
let mut header = item.header.borrow_mut();
if header.group.is_empty() {
2024-03-26 11:01:59 +01:00
if let Some(group_value) = group {
2024-05-05 19:15:54 +02:00
header.group = Rc::new(group_value);
2024-03-26 11:01:59 +01:00
} else {
2024-05-10 12:01:48 +02:00
let current_title = header.title.clone();
2024-05-05 19:15:54 +02:00
header.group = Rc::new(string_utils::get_title_group(current_title.as_str()));
2023-10-11 14:33:17 +02:00
}
2023-03-19 17:16:05 +01:00
}
2024-05-05 19:15:54 +02:00
drop(header);
visit(item);
2023-03-19 17:16:05 +01:00
}
header = None;
group = None;
}
}
2023-03-19 17:16:05 +01:00
2024-05-05 19:15:54 +02:00
pub(crate) fn parse_m3u(cfg: &Config, input: &ConfigInput, lines: &[String]) -> Vec<PlaylistGroup> {
let mut groups: std::collections::HashMap<Rc<String>, Vec<PlaylistItem>> = std::collections::HashMap::new();
let mut sort_order: Vec<Rc<String>> = vec![];
let mut playlist = Vec::new();
2024-05-05 19:15:54 +02:00
consume_m3u(cfg, input, lines.iter().cloned(), |item| playlist.push(item));
playlist.drain(..).for_each(|item| {
let key = Rc::clone(&item.header.borrow().group);
// let key2 = String::from(&item.header.group);
match groups.entry(Rc::clone(&key)) {
std::collections::hash_map::Entry::Vacant(e) => {
e.insert(vec![item]);
sort_order.push(Rc::clone(&key));
}
std::collections::hash_map::Entry::Occupied(mut e) => { e.get_mut().push(item); }
}
});
2023-03-19 17:16:05 +01:00
let mut result: Vec<PlaylistGroup> = vec![];
for (grp_id, (key, channels)) in (1_u32..).zip(groups.into_iter()) {
let cluster = channels.first().map(|pli| pli.header.borrow().xtream_cluster.clone());
result.push(PlaylistGroup { id: grp_id, xtream_cluster: cluster.unwrap(), title: Rc::clone(&key), channels });
2023-03-19 17:16:05 +01:00
}
result.sort_by(|f, s| {
let i1 = sort_order.iter().position(|r| **r == *f.title).unwrap();
let i2 = sort_order.iter().position(|r| **r == *s.title).unwrap();
2023-05-03 17:46:39 +02:00
i1.cmp(&i2)
2023-03-19 17:16:05 +01:00
});
result
}