meta: initial commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
.history/
|
||||||
3384
Cargo.lock
generated
Normal file
3384
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
17
Cargo.toml
Normal file
17
Cargo.toml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
[package]
|
||||||
|
name = "mirror-plex-to-navidrome"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
clap = { version = "4.5.54", features = ["derive", "env"] }
|
||||||
|
itertools = "0.14.0"
|
||||||
|
phf = { version = "0.13.1", features = ["macros", "uncased"] }
|
||||||
|
plex-api = "0.0.12"
|
||||||
|
reqwest = { version = "0.13.1", features = ["hickory-dns"] }
|
||||||
|
secrecy = { version = "0.10.3", features = ["serde"] }
|
||||||
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
|
snafu = { version = "0.8.9", features = ["futures"] }
|
||||||
|
tokio = { version = "1.49.0", features = ["rt-multi-thread", "macros", "fs"] }
|
||||||
|
uncased = "0.9.10"
|
||||||
|
url = { version = "2.5.8", features = ["serde"] }
|
||||||
124
src/filesystem.rs
Normal file
124
src/filesystem.rs
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
// https://www.reddit.com/r/radarr/comments/amj513/replacing_colons_with_equivalent_utf8_character/efpyrdw/?context=3
|
||||||
|
// https://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_(Unicode_block)?lang=en
|
||||||
|
// https://github.com/yt-dlp/yt-dlp/blob/cd94e7004036e0149d7d3fa236c7dd44cf460788/yt_dlp/utils/_utils.py#L627C5-L679
|
||||||
|
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file
|
||||||
|
|
||||||
|
use phf::{Set, phf_set};
|
||||||
|
use uncased::UncasedStr;
|
||||||
|
|
||||||
|
const SPACE: u32 = 0x20;
|
||||||
|
const HALFWIDTH_LOWER_END: u32 = 0x21;
|
||||||
|
const HALFWIDTH_UPPER_END: u32 = 0x7E;
|
||||||
|
|
||||||
|
const IDEOGRAPHIC_SPACE: char = '\u{3000}';
|
||||||
|
const IDEOGRAPHIC_SPACE_STR: &str = "\u{3000}";
|
||||||
|
const FULLWIDTH_LOWER_END: u32 = 0xFF01;
|
||||||
|
const FULLWIDTH_UPPER_END: u32 = 0xFF5E;
|
||||||
|
|
||||||
|
fn replace_with_fullwidth(c: char) -> char {
|
||||||
|
match c as u32 {
|
||||||
|
SPACE => IDEOGRAPHIC_SPACE,
|
||||||
|
c @ HALFWIDTH_LOWER_END..=HALFWIDTH_UPPER_END => {
|
||||||
|
char::from_u32(c - HALFWIDTH_LOWER_END + FULLWIDTH_LOWER_END)
|
||||||
|
.expect("this is a valid range of characters")
|
||||||
|
}
|
||||||
|
_other => c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replace_str_with_fullwidth(s: &str) -> String {
|
||||||
|
s.chars().map(replace_with_fullwidth).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replace_with_filesystem_safe(c: char) -> char {
|
||||||
|
match c {
|
||||||
|
forbidden_from_filenames @ ('\\' | '/' | ':' | '*' | '?' | '"' | '<' | '>' | '|') => {
|
||||||
|
replace_with_fullwidth(forbidden_from_filenames)
|
||||||
|
}
|
||||||
|
allowed => allowed,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static WINDOWS_FORBIDDEN: Set<&'static UncasedStr> = phf_set! {
|
||||||
|
UncasedStr::new("CON"),
|
||||||
|
UncasedStr::new("PRN"),
|
||||||
|
UncasedStr::new("AUX"),
|
||||||
|
|
||||||
|
UncasedStr::new("COM1"),
|
||||||
|
UncasedStr::new("COM2"),
|
||||||
|
UncasedStr::new("COM3"),
|
||||||
|
UncasedStr::new("COM4"),
|
||||||
|
UncasedStr::new("COM5"),
|
||||||
|
UncasedStr::new("COM6"),
|
||||||
|
UncasedStr::new("COM7"),
|
||||||
|
UncasedStr::new("COM8"),
|
||||||
|
UncasedStr::new("COM9"),
|
||||||
|
|
||||||
|
UncasedStr::new("COM¹"),
|
||||||
|
UncasedStr::new("COM²"),
|
||||||
|
UncasedStr::new("COM³"),
|
||||||
|
|
||||||
|
UncasedStr::new("LPT1"),
|
||||||
|
UncasedStr::new("LPT2"),
|
||||||
|
UncasedStr::new("LPT3"),
|
||||||
|
UncasedStr::new("LPT4"),
|
||||||
|
UncasedStr::new("LPT5"),
|
||||||
|
UncasedStr::new("LPT6"),
|
||||||
|
UncasedStr::new("LPT7"),
|
||||||
|
UncasedStr::new("LPT8"),
|
||||||
|
UncasedStr::new("LPT9"),
|
||||||
|
|
||||||
|
UncasedStr::new("LPT¹"),
|
||||||
|
UncasedStr::new("LPT²"),
|
||||||
|
UncasedStr::new("LPT³"),
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn make_filesystem_safe(segment: &str) -> String {
|
||||||
|
match segment {
|
||||||
|
forbidden_everywhere
|
||||||
|
if matches!(forbidden_everywhere.trim_start_matches(' '), "." | "..") =>
|
||||||
|
{
|
||||||
|
replace_str_with_fullwidth(forbidden_everywhere)
|
||||||
|
}
|
||||||
|
|
||||||
|
forbidden_on_windows
|
||||||
|
if WINDOWS_FORBIDDEN.contains(UncasedStr::new(forbidden_on_windows)) =>
|
||||||
|
{
|
||||||
|
replace_str_with_fullwidth(forbidden_on_windows)
|
||||||
|
}
|
||||||
|
|
||||||
|
nul if nul
|
||||||
|
.split_once(".")
|
||||||
|
.map(|(stem, _extension)| stem)
|
||||||
|
.unwrap_or(nul)
|
||||||
|
.eq_ignore_ascii_case("NUL") =>
|
||||||
|
{
|
||||||
|
replace_str_with_fullwidth(nul)
|
||||||
|
}
|
||||||
|
|
||||||
|
other => replace_trailing(other, ' ', IDEOGRAPHIC_SPACE)
|
||||||
|
.chars()
|
||||||
|
.map(replace_with_filesystem_safe)
|
||||||
|
.collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replace_trailing(s: &str, from: char, to: char) -> String {
|
||||||
|
let mut replacements = vec![];
|
||||||
|
let mut last_not_matching = None;
|
||||||
|
|
||||||
|
let mut chars = s.chars();
|
||||||
|
while let Some(char) = chars.next_back() {
|
||||||
|
if char == from {
|
||||||
|
replacements.push(to);
|
||||||
|
} else {
|
||||||
|
last_not_matching = Some(char);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
chars
|
||||||
|
.chain(last_not_matching)
|
||||||
|
.chain(replacements.into_iter())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod filesystem;
|
||||||
72
src/main.rs
Normal file
72
src/main.rs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
use clap::Parser;
|
||||||
|
use itertools::Itertools;
|
||||||
|
use mirror_plex_to_navidrome::filesystem::make_filesystem_safe;
|
||||||
|
use plex_api::{
|
||||||
|
HttpClientBuilder, Server,
|
||||||
|
library::{Library, MetadataItem},
|
||||||
|
};
|
||||||
|
use secrecy::SecretString;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
struct Args {
|
||||||
|
#[arg(long, env)]
|
||||||
|
api_url: Url,
|
||||||
|
|
||||||
|
#[arg(long, env)]
|
||||||
|
plex_token: SecretString,
|
||||||
|
|
||||||
|
#[arg(long, env)]
|
||||||
|
output_directory: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
let Args {
|
||||||
|
api_url,
|
||||||
|
plex_token,
|
||||||
|
output_directory,
|
||||||
|
} = Parser::parse();
|
||||||
|
|
||||||
|
let http_client = HttpClientBuilder::generic()
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
.set_x_plex_token(plex_token);
|
||||||
|
|
||||||
|
let server = Server::new(api_url.as_str(), http_client).await.unwrap();
|
||||||
|
|
||||||
|
for library in server.libraries() {
|
||||||
|
if let Library::Music(music_library) = library {
|
||||||
|
let playlists = music_library.playlists().await.unwrap();
|
||||||
|
|
||||||
|
for playlist in playlists {
|
||||||
|
let title = playlist.title();
|
||||||
|
let filename = make_filesystem_safe(&format!("{title}.m3u"));
|
||||||
|
let output_path = output_directory.join(filename);
|
||||||
|
|
||||||
|
let paths = playlist
|
||||||
|
.children()
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|track| {
|
||||||
|
let metadata = track.metadata();
|
||||||
|
metadata.media.as_ref().and_then(|medias| {
|
||||||
|
medias
|
||||||
|
.into_iter()
|
||||||
|
.flat_map(|media| {
|
||||||
|
media.parts.iter().flat_map(|part| part.file.clone()).next()
|
||||||
|
})
|
||||||
|
.next()
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut paths = paths;
|
||||||
|
let file_contents = paths.join("\n");
|
||||||
|
|
||||||
|
tokio::fs::write(output_path, file_contents).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user