use Promise#all(), use substr(-11)

This commit is contained in:
Bnyro 2022-11-29 19:57:58 +01:00
parent 166430770e
commit be53eefd03

View file

@ -127,23 +127,21 @@ export default {
}); });
return json; return json;
}, },
exportPlaylists() { async exportPlaylists() {
if (!this.playlists) return; if (!this.playlists) return;
let json = { let json = {
format: "Piped", format: "Piped",
version: 1, version: 1,
playlists: [], playlists: [],
}; };
let playlistsSize = this.playlists.length; let tasks = [];
for (var i = 0; i < playlistsSize; i++) { for (var i = 0; i < this.playlists.length; i++) {
this.fetchPlaylistJson(this.playlists[i].id, playlist => { tasks.push(this.fetchPlaylistJson(this.playlists[i].id));
json.playlists.push(playlist);
if (playlistsSize != json.playlists.length) return;
this.download(JSON.stringify(json), "playlists.json", "application/json");
});
} }
json.playlists = await Promise.all(tasks);
this.download(JSON.stringify(json), "playlists.json", "application/json");
}, },
async fetchPlaylistJson(playlistId, onSuccess) { async fetchPlaylistJson(playlistId) {
let playlist = await this.fetchJson(this.authApiUrl() + "/playlists/" + playlistId); let playlist = await this.fetchJson(this.authApiUrl() + "/playlists/" + playlistId);
let playlistJson = { let playlistJson = {
name: playlist.name, name: playlist.name,
@ -157,32 +155,28 @@ export default {
for (var i = 0; i < playlist.relatedStreams.length; i++) { for (var i = 0; i < playlist.relatedStreams.length; i++) {
playlistJson.videos.push("https://youtube.com" + playlist.relatedStreams[i].url); playlistJson.videos.push("https://youtube.com" + playlist.relatedStreams[i].url);
} }
onSuccess(playlistJson); return playlistJson;
}, },
importPlaylists() { async importPlaylists() {
const file = this.$refs.fileSelector.files[0]; const file = this.$refs.fileSelector.files[0];
file.text().then(text => { let text = await file.text();
let playlists = JSON.parse(text).playlists; let playlists = JSON.parse(text).playlists;
if (!playlists.length) { if (!playlists.length) {
alert(this.$t("actions.no_valid_playlists")); alert(this.$t("actions.no_valid_playlists"));
return; return;
} }
let importedCount = 0; let tasks = [];
for (var i = 0; i < playlists.length; i++) { for (var i = 0; i < playlists.length; i++) {
this.createPlaylistWithVideos(playlists[i], () => { tasks.push(this.createPlaylistWithVideos(playlists[i]));
importedCount++; }
if (playlists.length != importedCount) return; await Promise.all(tasks);
window.location.reload(); window.location.reload();
});
}
});
}, },
async createPlaylistWithVideos(playlist, onSuccess) { async createPlaylistWithVideos(playlist) {
let newPlaylist = await this.createPlaylist(playlist.name); let newPlaylist = await this.createPlaylist(playlist.name);
console.log(newPlaylist); console.log(newPlaylist);
let videoIds = playlist.videos.map(url => url.replace("https://youtube.com/watch?v=", "")); let videoIds = playlist.videos.map(url => url.substr(-11));
await this.addVideosToPlaylist(newPlaylist.playlistId, videoIds); await this.addVideosToPlaylist(newPlaylist.playlistId, videoIds);
onSuccess();
}, },
async addVideosToPlaylist(playlistId, videoIds) { async addVideosToPlaylist(playlistId, videoIds) {
await this.fetchJson(this.authApiUrl() + "/user/playlists/add", null, { await this.fetchJson(this.authApiUrl() + "/user/playlists/add", null, {