reorganise modules
This commit is contained in:
114
src/playlist.rs
Normal file
114
src/playlist.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
use std::{fs, path, collections::BTreeMap};
|
||||
|
||||
use crate::{
|
||||
input,
|
||||
folders,
|
||||
manage::Specification,
|
||||
};
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
use sanitise_file_name::sanitise;
|
||||
|
||||
pub(crate) fn generate_master_m3u(
|
||||
config: &input::Config,
|
||||
root: &path::Path,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut all_episodes = BTreeMap::<chrono::NaiveDateTime, Vec<_>>::new();
|
||||
|
||||
for (podcast, _) in &config.podcasts {
|
||||
let output = folders::podcast_folder(root, podcast.as_str());
|
||||
let spec_file = output.join("spec.toml");
|
||||
let spec = Specification::read_from(&spec_file)?;
|
||||
|
||||
let (feed, files) = spec.into_feed_and_files();
|
||||
|
||||
for (published, episodes) in feed {
|
||||
for episode in episodes {
|
||||
let path = output.join(
|
||||
files.get(episode.id()).unwrap()
|
||||
);
|
||||
let entry = m3u::path_entry({
|
||||
let relative = path.strip_prefix(
|
||||
output.parent().unwrap()
|
||||
).unwrap();
|
||||
path::Path::new("/Podcasts").join(relative)
|
||||
});
|
||||
|
||||
match all_episodes.get_mut(&published) {
|
||||
Some(existing) => {
|
||||
existing.push(entry)
|
||||
},
|
||||
None => {
|
||||
all_episodes.insert(
|
||||
published,
|
||||
vec![entry]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let playlists_folder = root.join("Playlists");
|
||||
if !playlists_folder.exists() {
|
||||
fs::create_dir(&playlists_folder)
|
||||
.context(format!("failed to create output directory for playlists"))?;
|
||||
}
|
||||
let mut path = playlists_folder.join("_master");
|
||||
path.set_extension("m3u");
|
||||
|
||||
let mut file = fs::File::create(path)?;
|
||||
|
||||
let mut writer = m3u::Writer::new(&mut file);
|
||||
for entry in all_episodes.values().map(|entries| entries.iter()).flatten() {
|
||||
writer.write_entry(entry)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn generate_podcast_m3u(
|
||||
alias: &str,
|
||||
root: &path::Path,
|
||||
) -> anyhow::Result<()> {
|
||||
let output = folders::podcast_folder(root, alias);
|
||||
let spec_file = output.join("spec.toml");
|
||||
|
||||
let spec = Specification::read_from(&spec_file)?;
|
||||
|
||||
let mut playlist = Vec::new();
|
||||
|
||||
for episode in spec.feed_iter().map(|(_, eps)| eps.iter()).flatten() {
|
||||
let path = output.join(
|
||||
spec.path_from_id(episode.id()).unwrap()
|
||||
);
|
||||
playlist.push(m3u::path_entry({
|
||||
let relative = path.strip_prefix(
|
||||
output.parent().unwrap()
|
||||
).unwrap();
|
||||
path::Path::new("/Podcasts").join(relative)
|
||||
}));
|
||||
}
|
||||
|
||||
// Write the playlist file
|
||||
{
|
||||
let playlists_folder = root.join("Playlists");
|
||||
if !playlists_folder.exists() {
|
||||
fs::create_dir(&playlists_folder)
|
||||
.context(format!("failed to create output directory for playlists"))?;
|
||||
}
|
||||
let mut path = playlists_folder.join(sanitise(&alias));
|
||||
path.set_extension("m3u");
|
||||
|
||||
let mut file = fs::File::create(path)?;
|
||||
|
||||
let mut writer = m3u::Writer::new(&mut file);
|
||||
for entry in &playlist {
|
||||
writer.write_entry(entry)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user