- Relocated code from App.vue to their respective stores
- Linked to privacy page in wiki (related to #15)
This commit is contained in:
Shiny Nematoda 2022-09-09 18:49:13 +00:00
parent 1b5ee1d2d5
commit 44db54737b
No known key found for this signature in database
GPG key ID: 6506D50F5613A42D
17 changed files with 666 additions and 620 deletions

View file

@ -1,7 +1,8 @@
import { reactive } from 'vue';
import { defineStore } from 'pinia';
import { useStore } from '../scripts/util.js';
import { getJsonPiped, getJsonHyp } from '@/scripts/fetch.js';
import { useStore, useMetadata } from '@/scripts/util.js';
const store = useStore();
@ -18,7 +19,102 @@ export const useData = defineStore('data', () => {
urls: [],
});
return { state };
const player = usePlayer();
async function getSong(e) {
console.log(e);
const hash = new URLSearchParams(e.substring(e.indexOf('?'))).get('v'),
json = await getJsonPiped('/streams/' + hash);
console.log(json);
state.art = json.thumbnailUrl;
state.description = json.description;
state.title = json.title;
state.artist = json.uploader.replace(' - Topic', '');
state.artistUrl = json.uploaderUrl;
player.state.duration = json.duration;
player.state.hls = json.hls;
player.state.streams = json.audioStreams;
state.url = e;
await getNext(hash);
}
async function getNext(hash) {
if (
store.getItem('next') !== 'false' &&
(!state.urls ||
!state.urls.filter(s => s.url == state.url)[0] ||
state.urls.length == 1)
) {
const json = await getJsonHyp('/next/' + hash);
state.lyrics = json.lyricsId;
state.url = json.songs[0]
? '/watch?v=' + json.songs[0].id
: '/watch?v=' + hash;
console.log(json);
state.urls =
json.songs.length > 0
? json.songs.map(i => ({
...i,
...{
url: '/watch?v=' + i.id,
id: undefined,
},
}))
: state.urls;
useMetadata(state.url, state.urls, {
title: state.title,
artist: state.artist,
art: state.art,
});
console.log(state.urls);
} else {
if (state.urls.length == 0) {
state.urls = [
{
title: state.title,
url: state.url,
},
];
}
useMetadata(state.url, state.urls, {
title: state.title,
artist: state.artist,
art: state.art,
});
}
}
function playNext(u) {
const now = state.urls.filter(s => s.url === state.url)[0],
i = state.urls.indexOf(now),
next = state.urls[i + 1];
console.log('Index: ' + i);
console.log(state.url, state.urls, next);
if (state.urls.length > i && state.urls.length != 0 && next) {
getSong(next.url);
} else if (player.state.loop) {
console.log(state.url, state.urls[0]);
state.url = state.urls[0].url;
getSong(state.urls[0].url);
} else {
state.urls = [];
}
}
return { state, getSong, playNext };
});
export const usePlayer = defineStore('player', () => {

View file

@ -1,6 +1,11 @@
import { ref, reactive } from 'vue';
import { defineStore } from 'pinia';
import { useNav } from '@/stores/misc.js';
import { getJsonPiped, getJsonHyp } from '@/scripts/fetch.js';
import { useRoute } from '@/scripts/util.js';
export const useResults = defineStore('results', () => {
const items = ref({}),
search = ref('');
@ -11,12 +16,43 @@ export const useResults = defineStore('results', () => {
}
function resetItems() {
useArtist().reset();
for (let i in items.value) {
items.value[i] = undefined;
}
}
return { items, search, setItem, resetItems };
async function getExplore() {
const json = await getJsonHyp('/explore');
console.log(json);
useArtist().reset();
setItem('songs', { items: json.trending });
setItem('albums', { items: json.albums_and_singles });
}
async function getAlbum(e) {
console.log('Album: ', e);
const hash = new URLSearchParams(e.substring(e.indexOf('?'))).get('list'),
json = await getJsonPiped('/playlists/' + hash);
console.log(json, json.relatedStreams);
resetItems();
setItem('songs', {
items: json.relatedStreams,
title: json.name,
});
useRoute(e);
useNav().state.page = 'home';
useArtist().reset();
}
return { items, search, setItem, resetItems, getExplore, getAlbum };
});
export const useArtist = defineStore('artist', () => {
@ -40,5 +76,32 @@ export const useArtist = defineStore('artist', () => {
}
}
return { state, set, reset };
async function getArtist(e) {
console.log(e);
e = e.replace('/channel/', '');
const json = await getJsonHyp('/channel/' + e),
results = useResults();
console.log(json);
results.resetItems();
for (let i in json.items) {
results.setItem(i, { items: json.items[i] });
}
console.log(results.items);
json.items = undefined;
reset();
set(json);
useRoute('/channel/' + e);
useNav().state.page = 'home';
}
return { state, set, reset, getArtist };
});