Use opensubsonic types in module and code

This commit is contained in:
Neshura 2024-04-29 07:12:43 +02:00
parent b0ec1c7d56
commit 7488b7cfeb
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 54 additions and 35 deletions

View file

@ -10,17 +10,28 @@ export module OpenSubsonic {
const apiVer = "1.16.1"; // Version supported by Navidrome. Variable for easier updating const apiVer = "1.16.1"; // Version supported by Navidrome. Variable for easier updating
const clientName = "Lydstyrke"; const clientName = "Lydstyrke";
export async function get(path: string, parameters: {parameter: string, value: string}[] = []) { export async function get(path: string, parameters: Array<Parameter> = []) {
const apiPath = getApiUrl(path, parameters); const apiPath = getApiUrl(path, parameters);
const res = (await fetch(apiPath)); const res = (await fetch(apiPath));
if (res.ok) { if (res.ok) {
switch (res.headers.get("content-type")) { const contentType =res.headers.get("content-type");
switch (contentType) {
case("application/json"): { case("application/json"): {
return (await res.json())["subsonic-response"]; return (await res.json())["subsonic-response"];
} }
case("audio/mp4"): { case("audio/mp4"): {
return res; return res;
} }
case("image/jpeg"): {
return res;
}
case("image/png"): {
return res;
}
default: {
console.warn(`Content Type: '${contentType}' is not supported`)
}
} }
} }
else { else {
@ -28,10 +39,10 @@ export module OpenSubsonic {
} }
} }
export function getApiUrl(path: string, parameters: {parameter: string, value: string}[] = []) { export function getApiUrl(path: string, parameters: Array<Parameter> = []) {
let apiPath = generateBasePath(path); let apiPath = generateBasePath(path);
parameters.forEach(({parameter, value}) => { parameters.forEach((parameter) => {
apiPath = apiPath + `&${parameter}=${value}`; apiPath = apiPath + `&${parameter.key}=${parameter.value}`;
}) })
return apiPath; return apiPath;

View file

@ -1,7 +1,10 @@
<svelte:options runes={true} /> <svelte:options runes={true} />
<script lang="ts"> <script lang="ts">
import {OpenSubsonic} from "$lib/opensubsonic"; import {
type Parameter, type Song, type NowPlayingResponse, type GetPlayQueueResponse,
OpenSubsonic
} from "$lib/opensubsonic";
import {onMount} from "svelte"; import {onMount} from "svelte";
import QueueFrame from "$lib/components/custom/QueueFrame.svelte"; import QueueFrame from "$lib/components/custom/QueueFrame.svelte";
import {Button} from "$lib/components/ui/button"; import {Button} from "$lib/components/ui/button";
@ -31,18 +34,18 @@
let progress = $state(0); let progress = $state(0);
let duration = $state(0); let duration = $state(0);
let queue: Array<unknown> = $state([]); let queue: Array<Song> = $state([]);
async function fetchQueue() { async function fetchQueue() {
const data = await OpenSubsonic.get("getPlayQueue"); const data: GetPlayQueueResponse = await OpenSubsonic.get("getPlayQueue");
if (data) { if (data && data.playQueue.entry) {
queue = []; queue = [];
queue = queue.concat(data.playQueue.entry); queue = queue.concat(data.playQueue.entry);
} }
} }
async function fetchNowPlaying() { async function fetchNowPlaying() {
const data = await OpenSubsonic.get("getNowPlaying"); const data: NowPlayingResponse = await OpenSubsonic.get("getNowPlaying");
let foundInNowPlaying = false; let foundInNowPlaying = false;
if (data && data.nowPlaying.entry) { if (data && data.nowPlaying.entry) {
data.nowPlaying.entry.forEach((entry) => { data.nowPlaying.entry.forEach((entry) => {
@ -59,18 +62,23 @@
} }
async function saveQueue() { async function saveQueue() {
let songs = []; let songs: Array<Parameter> = [];
queue.forEach((song, idx) => { if (queue.length === 0) {
if (idx === 0) { songs.push({key: "id", value: ""})
songs.push({parameter: "current", value: song.id}) }
songs.push({parameter: "id", value: song.id}) else {
// Add Progress within current song queue.forEach((song, idx) => {
} if (idx === 0) {
else { songs.push({key: "current", value: song.id})
songs.push({parameter: "id", value: song.id}) songs.push({key: "id", value: song.id})
} // Add Progress within current song
}) }
else {
songs.push({key: "id", value: song.id})
}
})
}
const data = await OpenSubsonic.get("savePlayQueue", songs); const data = await OpenSubsonic.get("savePlayQueue", songs);
if (data) { if (data) {
await fetchQueue(); await fetchQueue();
@ -83,22 +91,22 @@
} }
} }
function playSong(song: unknown, songIndex: number) { function playSong(song: Song, songIndex: number) {
pause(); pause();
newSong(song); newSong(song);
currentSong.queueIndex = songIndex; currentSong.queueIndex = songIndex;
play(); play();
} }
function newSong(song: unknown) { function newSong(song: Song) {
let parameters = [ let parameters: Array<Parameter> = [
{ parameter: "id", value: song.id }, { key: "id", value: song.id },
//{ parameter: "maxBitRate", value: } // TODO //{ key: "maxBitRate", value: } // TODO
//{ parameter: "format", value: } // TODO //{ key: "format", value: } // TODO
//{ parameter: "timeOffset", value: } // TODO? Only Video related //{ key: "timeOffset", value: } // TODO? Only Video related
//{ parameter: "size", value: } // TODO? Only Video related //{ key: "size", value: } // TODO? Only Video related
{ parameter: "estimateContentLength", value: "true" }, { key: "estimateContentLength", value: "true" },
//{ parameter: "converted", value: } // TODO? Only Video related //{ key: "converted", value: } // TODO? Only Video related
]; ];
let url = OpenSubsonic.getApiUrl("stream", parameters); let url = OpenSubsonic.getApiUrl("stream", parameters);
currentSong.source = new Audio(url); // Assign new URL currentSong.source = new Audio(url); // Assign new URL
@ -113,10 +121,10 @@
currentSong.source.volume = volume; currentSong.source.volume = volume;
isPaused = currentSong.source.paused; isPaused = currentSong.source.paused;
let time: number = Date.now(); let time: number = Date.now();
let parameters = [ let parameters: Array<Parameter> = [
{ parameter: "id", value: song.id }, { key: "id", value: song.id },
{ parameter: "time", value: time}, { key: "time", value: time},
{ parameter: "submission", value: false} { key: "submission", value: false}
]; ];
OpenSubsonic.get("scrobble", parameters); OpenSubsonic.get("scrobble", parameters);
} }