Refactor localStorage import and update .gitignore for localstorage.json
This commit is contained in:
parent
67dce9ddc0
commit
43667b79f0
4 changed files with 61 additions and 30 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -35,5 +35,5 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||||
|
|
||||||
|
|
||||||
tmp
|
tmp
|
||||||
localstorage
|
localstorage.json
|
||||||
downloaded
|
downloaded
|
|
@ -1,4 +1,4 @@
|
||||||
import "../localstorage";
|
import "./localStorage";
|
||||||
|
|
||||||
import { init, initializeDeviceLogin, finalizeDeviceLogin, credentialsProvider } from "@tidal-music/auth";
|
import { init, initializeDeviceLogin, finalizeDeviceLogin, credentialsProvider } from "@tidal-music/auth";
|
||||||
|
|
||||||
|
|
29
src/helpers/localStorage.ts
Normal file
29
src/helpers/localStorage.ts
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
const database = Bun.file('localStorage.json');
|
||||||
|
|
||||||
|
if (!await database.exists()) {
|
||||||
|
await Bun.write(database, JSON.stringify({}));
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = {};
|
||||||
|
|
||||||
|
data = await database.json() || {};
|
||||||
|
|
||||||
|
globalThis.localStorage = {
|
||||||
|
getItem: (key) => {
|
||||||
|
return data[key] || null;
|
||||||
|
},
|
||||||
|
setItem: (key, value) => {
|
||||||
|
data[key] = value;
|
||||||
|
Bun.write(database, JSON.stringify(data));
|
||||||
|
},
|
||||||
|
removeItem: (key) => {
|
||||||
|
delete data[key];
|
||||||
|
Bun.write(database, JSON.stringify(data));
|
||||||
|
},
|
||||||
|
clear: () => {
|
||||||
|
data = {};
|
||||||
|
Bun.write(database, JSON.stringify(data));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { }
|
|
@ -10,7 +10,7 @@ export default class {
|
||||||
async fetchTrack(id: number) {
|
async fetchTrack(id: number) {
|
||||||
const audio = await fetch(`https://api.tidal.com/v1/tracks/${id}/playbackinfopostpaywall/v4?audioquality=HI_RES_LOSSLESS&playbackmode=STREAM&assetpresentation=FULL`, {
|
const audio = await fetch(`https://api.tidal.com/v1/tracks/${id}/playbackinfopostpaywall/v4?audioquality=HI_RES_LOSSLESS&playbackmode=STREAM&assetpresentation=FULL`, {
|
||||||
headers: this.authHeaders
|
headers: this.authHeaders
|
||||||
})
|
})
|
||||||
return await audio.json() as {
|
return await audio.json() as {
|
||||||
trackId: number,
|
trackId: number,
|
||||||
audioPresentation: string,
|
audioPresentation: string,
|
||||||
|
@ -36,21 +36,21 @@ export default class {
|
||||||
return await tracks.json();
|
return await tracks.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
async downloadFlac(manifestMimeType:string, manifest: string) {
|
async downloadFlac(manifestMimeType: string, manifest: string) {
|
||||||
const id = Bun.nanoseconds().toString(36);
|
const id = Bun.nanoseconds().toString(36);
|
||||||
if (manifestMimeType === "application/dash+xml") {
|
if (manifestMimeType === "application/dash+xml") {
|
||||||
await Bun.write(`tmp/${id}.mpd`, Buffer.from(manifest, "base64").toString("utf-8"))
|
await Bun.write(`tmp/${id}.mpd`, Buffer.from(manifest, "base64").toString("utf-8"))
|
||||||
|
|
||||||
await Bun.$`mpv --ao=null --stream-record=tmp/${id}.flac --speed=100 tmp/${id}.mpd`.quiet()
|
await Bun.$`mpv --ao=null --stream-record=tmp/${id}.flac --speed=100 tmp/${id}.mpd`.quiet()
|
||||||
|
|
||||||
await Bun.file(`tmp/${id}.mpd`).delete();
|
await Bun.file(`tmp/${id}.mpd`).delete();
|
||||||
|
|
||||||
const flac = Bun.file(`tmp/${id}.flac`)
|
const flac = Bun.file(`tmp/${id}.flac`)
|
||||||
|
|
||||||
const audioBuffer = await flac.arrayBuffer();
|
const audioBuffer = await flac.arrayBuffer();
|
||||||
|
|
||||||
await flac.delete()
|
await flac.delete()
|
||||||
|
|
||||||
return audioBuffer;
|
return audioBuffer;
|
||||||
} else if (manifestMimeType === "application/vnd.tidal.bts") {
|
} else if (manifestMimeType === "application/vnd.tidal.bts") {
|
||||||
const data = JSON.parse(Buffer.from(manifest, "base64").toString("utf-8")) as {
|
const data = JSON.parse(Buffer.from(manifest, "base64").toString("utf-8")) as {
|
||||||
|
@ -61,7 +61,7 @@ export default class {
|
||||||
}
|
}
|
||||||
|
|
||||||
const flac = await fetch(data.urls[0])
|
const flac = await fetch(data.urls[0])
|
||||||
|
|
||||||
return await flac.arrayBuffer();
|
return await flac.arrayBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ export default class {
|
||||||
const trackReq = await fetch(`https://api.tidal.com/v1/tracks/${id}/?countryCode=US`, {
|
const trackReq = await fetch(`https://api.tidal.com/v1/tracks/${id}/?countryCode=US`, {
|
||||||
headers: this.authHeaders
|
headers: this.authHeaders
|
||||||
})
|
})
|
||||||
|
|
||||||
const track = await trackReq.json() as {
|
const track = await trackReq.json() as {
|
||||||
title: string,
|
title: string,
|
||||||
replayGain: number,
|
replayGain: number,
|
||||||
|
@ -95,11 +95,11 @@ export default class {
|
||||||
cover: string,
|
cover: string,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const alubmReq = await fetch(`https://api.tidal.com/v1/albums/${track.album.id}/?countryCode=US`, {
|
const alubmReq = await fetch(`https://api.tidal.com/v1/albums/${track.album.id}/?countryCode=US`, {
|
||||||
headers: this.authHeaders
|
headers: this.authHeaders
|
||||||
})
|
})
|
||||||
|
|
||||||
const album = await alubmReq.json() as {
|
const album = await alubmReq.json() as {
|
||||||
title: string,
|
title: string,
|
||||||
numberOfTracks: number,
|
numberOfTracks: number,
|
||||||
|
@ -112,25 +112,27 @@ export default class {
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(track)
|
||||||
|
|
||||||
return FlacStreamTagger.fromBuffer(Buffer.from(audioBuffer), {
|
return FlacStreamTagger.fromBuffer(Buffer.from(audioBuffer), {
|
||||||
tagMap: {
|
tagMap: {
|
||||||
title: track.title,
|
title: track.title,
|
||||||
trackNumber: track.trackNumber.toString(),
|
trackNumber: track.trackNumber.toString(),
|
||||||
discNumber: track.volumeNumber.toString(),
|
discNumber: track.volumeNumber.toString(),
|
||||||
bpm: track.bpm.toString(),
|
bpm: (track.bpm || 0).toString(),
|
||||||
date: track.streamStartDate,
|
date: track.streamStartDate,
|
||||||
copyright: track.copyright,
|
copyright: track.copyright,
|
||||||
REPLAYGAIN_TRACK_GAIN: track.replayGain.toString(),
|
REPLAYGAIN_TRACK_GAIN: track.replayGain.toString(),
|
||||||
REPLAYGAIN_TRACK_PEAK: track.peak.toString(),
|
REPLAYGAIN_TRACK_PEAK: track.peak.toString(),
|
||||||
comment: track.url,
|
comment: track.url,
|
||||||
isrc: track.isrc,
|
isrc: track.isrc,
|
||||||
upc: album.upc,
|
upc: album.upc,
|
||||||
artist: track.artists.map((a) => a.name),
|
artist: track.artists.map((a) => a.name),
|
||||||
album: album.title,
|
album: album.title,
|
||||||
albumArtist: album.artists.map((a) => a.name),
|
albumArtist: album.artists.map((a) => a.name),
|
||||||
totalTracks: album.numberOfTracks.toString(),
|
totalTracks: album.numberOfTracks.toString(),
|
||||||
year: album.releaseDate.split("-")[0] || "",
|
year: album.releaseDate.split("-")[0] || "",
|
||||||
},
|
},
|
||||||
picture: {
|
picture: {
|
||||||
buffer: Buffer.from(await (await fetch(`https://resources.tidal.com/images/${album.cover.replaceAll("-", "/")}/1280x1280.jpg`)).arrayBuffer()),
|
buffer: Buffer.from(await (await fetch(`https://resources.tidal.com/images/${album.cover.replaceAll("-", "/")}/1280x1280.jpg`)).arrayBuffer()),
|
||||||
|
|
Loading…
Add table
Reference in a new issue