feat: get songs from jellyfin
This commit is contained in:
parent
49cf01c668
commit
12ad788452
7 changed files with 131 additions and 18 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1 +1,2 @@
|
||||||
/target
|
/target
|
||||||
|
.env
|
||||||
|
|
|
||||||
59
Cargo.lock
generated
59
Cargo.lock
generated
|
|
@ -924,6 +924,15 @@ dependencies = [
|
||||||
"zerovec",
|
"zerovec",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.101"
|
version = "1.0.101"
|
||||||
|
|
@ -948,6 +957,35 @@ version = "5.3.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.9.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
|
||||||
|
dependencies = [
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.9.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom 0.3.4",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.18"
|
version = "0.5.18"
|
||||||
|
|
@ -1439,6 +1477,7 @@ dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
|
"rand",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|
@ -1891,6 +1930,26 @@ dependencies = [
|
||||||
"synstructure",
|
"synstructure",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy"
|
||||||
|
version = "0.8.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0894878a5fa3edfd6da3f88c4805f4c8558e2b996227a3d864f47fe11e38282c"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy-derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy-derive"
|
||||||
|
version = "0.8.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zerofrom"
|
name = "zerofrom"
|
||||||
version = "0.1.6"
|
version = "0.1.6"
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ edition = "2024"
|
||||||
anyhow = "1.0.100"
|
anyhow = "1.0.100"
|
||||||
chrono = { version = "0.4.42", features = ["serde"] }
|
chrono = { version = "0.4.42", features = ["serde"] }
|
||||||
clap = { version = "4.5.49", features = ["derive"] }
|
clap = { version = "4.5.49", features = ["derive"] }
|
||||||
|
rand = "0.9.2"
|
||||||
reqwest = { version = "0.12.24", features = ["gzip", "json"] }
|
reqwest = { version = "0.12.24", features = ["gzip", "json"] }
|
||||||
serde = "1.0.228"
|
serde = "1.0.228"
|
||||||
serde_json = "1.0.145"
|
serde_json = "1.0.145"
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,5 @@
|
||||||
|
[env]
|
||||||
|
_.file = '.env'
|
||||||
|
|
||||||
[tasks.dev]
|
[tasks.dev]
|
||||||
run = "cargo run"
|
run = "cargo run"
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
|
use rand::seq::IteratorRandom;
|
||||||
use reqwest::header::{HeaderMap, HeaderValue};
|
use reqwest::header::{HeaderMap, HeaderValue};
|
||||||
|
|
||||||
use crate::jellyfin::models::GetItemsResponseModel;
|
use crate::jellyfin::models::{GetItemsQueryString, GetItemsResponseModel, ItemModel};
|
||||||
|
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
|
||||||
static JELLYFIN_VGM_FOLDER: &str = "";
|
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
_client: reqwest::Client,
|
_client: reqwest::Client,
|
||||||
host: String,
|
host: String,
|
||||||
|
|
@ -18,7 +17,7 @@ impl Client {
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
let auth_str = format!(
|
let auth_str = format!(
|
||||||
r#"MediaBrowser Client="Jellyfin Web", Device="Firefox", DeviceId="TW96aWxsYS81LjAgKFgxMTsgTGludXggeDg2XzY0OyBydjo5NC4wKSBHZWNrby8yMDEwMDEwMSBGaXJlZm94Lzk0LjB8MTYzODA1MzA2OTY4Mw11", Version="10.7.6", Token="{}""#,
|
r#"MediaBrowser Client="Jellyfin Web", Device="Firefox", DeviceId="TW96aWxsYS81LjAgKFgxMTsgTGludXggeDg2XzY0OyBydjo5NC4wKSBHZWNrby8yMDEwMDEwMSBGaXJlZm94Lzk0LjB8MTYzODA1MzA2OTY4Mw11", Version="10.7.6", Token="{}""#,
|
||||||
"e9d4caa8c94f429280b43ba45a50ba6b"
|
std::env::var("JELLYFIN_API_KEY")?
|
||||||
);
|
);
|
||||||
|
|
||||||
headers.insert("Authorization", HeaderValue::from_str(auth_str.as_str())?);
|
headers.insert("Authorization", HeaderValue::from_str(auth_str.as_str())?);
|
||||||
|
|
@ -32,24 +31,24 @@ impl Client {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
_client,
|
_client,
|
||||||
host: host.into(),
|
host: host.into(),
|
||||||
user_id: String::from("4a80af8c258e417e98f86e870e17d929"),
|
user_id: std::env::var("JELLYFIN_USER_ID")?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_items<T: Into<String> + std::fmt::Display>(
|
pub async fn get_items<T: Into<String> + std::fmt::Display>(
|
||||||
&self,
|
&self,
|
||||||
parent_id: T,
|
parent_id: T,
|
||||||
|
query: GetItemsQueryString,
|
||||||
) -> anyhow::Result<GetItemsResponseModel> {
|
) -> anyhow::Result<GetItemsResponseModel> {
|
||||||
// INFO: needs to be created manually, escaped characters mess with Jellyfin server
|
// INFO: needs to be created manually, escaped characters mess with Jellyfin server
|
||||||
let query = format!(
|
|
||||||
"parentId={parent_id}&sort_by=SortName&fields=Path,RecursiveItemCount&enable_images=false"
|
|
||||||
);
|
|
||||||
|
|
||||||
let response = self
|
let response = self
|
||||||
._client
|
._client
|
||||||
.get(format!(
|
.get(format!(
|
||||||
"{}/Users/{}/items?{}",
|
"{}/Users/{}/items?{}",
|
||||||
self.host, self.user_id, query
|
self.host,
|
||||||
|
self.user_id,
|
||||||
|
query.build(parent_id)
|
||||||
))
|
))
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
@ -69,4 +68,42 @@ impl Client {
|
||||||
|
|
||||||
Ok(context)
|
Ok(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_random_vgm_album(&self) -> anyhow::Result<ItemModel> {
|
||||||
|
let response_folders = self
|
||||||
|
.get_items(
|
||||||
|
std::env::var("JELLYFIN_VGM_FOLDER")?,
|
||||||
|
GetItemsQueryString::Folder,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let mut rng = rand::rng();
|
||||||
|
|
||||||
|
let folder = response_folders
|
||||||
|
.items
|
||||||
|
.into_iter()
|
||||||
|
.filter(|item| item.recursive_item_count > 0)
|
||||||
|
.choose(&mut rng)
|
||||||
|
.ok_or(anyhow!("Failed to get a VGM folder from Jellyfin!"))?;
|
||||||
|
|
||||||
|
let response_albums = self
|
||||||
|
.get_items(&folder.id, GetItemsQueryString::Album)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let folder = response_albums
|
||||||
|
.items
|
||||||
|
.into_iter()
|
||||||
|
.filter(|item| item.recursive_item_count > 0)
|
||||||
|
.choose(&mut rng)
|
||||||
|
.ok_or(anyhow!("Failed to get a VGM album from Jellyfin!"))?;
|
||||||
|
|
||||||
|
Ok(folder)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_songs<T: Into<String> + std::fmt::Display>(
|
||||||
|
&self,
|
||||||
|
album_id: T,
|
||||||
|
) -> anyhow::Result<GetItemsResponseModel> {
|
||||||
|
self.get_items(album_id, GetItemsQueryString::Songs).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,22 @@
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
pub enum GetItemsQueryString {
|
||||||
#[serde(rename_all = "camelCase")]
|
Folder,
|
||||||
pub struct GetItemsQueryStringModel {
|
Album,
|
||||||
pub parent_id: String,
|
Songs,
|
||||||
pub sort_by: String,
|
}
|
||||||
pub fields: String,
|
|
||||||
pub enable_images: bool,
|
impl GetItemsQueryString {
|
||||||
|
pub fn build(&self, parent_id: impl Into<String> + std::fmt::Display) -> String {
|
||||||
|
let common = format!("parentId={parent_id}&enable_images=false");
|
||||||
|
match self {
|
||||||
|
Self::Folder => format!("{common}&SortBy=SortName&fields=Path,RecursiveItemCount"),
|
||||||
|
Self::Album => format!("{common}&SortBy=SortName&fields=Path,RecursiveItemCount"),
|
||||||
|
Self::Songs => format!(
|
||||||
|
"{common}&SortBy=ParentIndexNumber,IndexNumber,SortName&fields=ItemCounts,MediaSourceCount,Path"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
|
@ -32,6 +42,7 @@ pub struct ItemModel {
|
||||||
pub album_artist: Option<String>,
|
pub album_artist: Option<String>,
|
||||||
pub album_artists: Option<Vec<AlbumArtistModel>>,
|
pub album_artists: Option<Vec<AlbumArtistModel>>,
|
||||||
pub media_type: String,
|
pub media_type: String,
|
||||||
|
#[serde(default)]
|
||||||
pub recursive_item_count: u64,
|
pub recursive_item_count: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,9 @@ async fn main() -> anyhow::Result<()> {
|
||||||
|
|
||||||
let jelly = jellyfin::Client::new("https://media.hoshikusu.xyz")?;
|
let jelly = jellyfin::Client::new("https://media.hoshikusu.xyz")?;
|
||||||
|
|
||||||
let items = jelly.get_items("574563ff9a41f2c98c234db69157cc87").await?;
|
let item = jelly.get_random_vgm_album().await?;
|
||||||
println!("{:#?}", items);
|
let songs = jelly.get_songs(item.id).await?;
|
||||||
|
println!("{:#?}", songs);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue