sintonia_webapp/resources/js/composables/content/podcastEpisode_page.ts

105 lines
No EOL
3.5 KiB
TypeScript

import {reactive, ref} from "vue";
import axios, {type AxiosResponse} from "axios";
import {type PodcastEpisode, PodcastEpisodeTableHeader} from "@models/podcast/podcastEpisode.ts";
import {DateTime} from "luxon";
import {usePodcastStore} from "@/stores/podcast.store.ts";
export function podcast_episode_page(url: String) {
const items = ref([]);
const loading = ref(false);
const listData = reactive({
'itemsPerPage': 5,
'first_page': null,
'last_page': null,
'total_items': 0,
'page': 1,
});
const headers = PodcastEpisodeTableHeader;
const downloadingEpisode = reactive({});
const importedPodcastEpisodes = async (podcast_id, element) => {
return await axios.get('/podcast_episode', {
params: {
podcast_id: podcast_id
}
}).then( (response) => {
// console.log(response.data);
const episode = response.data.filter(imp => {
if (imp.episode_guid === element.guid) {
return true;
}
});
if (episode.length > 0) {
return (episode[0].file_id === null) ? -1 : episode[0].file_id;
}
return null;
});
}
const getItems = async (load, podcast_id = 0) => {
if (load) {
loading.value = true;
}
return await axios.get(`/rss_podcast_load`, {
params: {
url: url
}
}).then( async (podcastEpisodesList: AxiosResponse) => {
const episodes = podcastEpisodesList.data.episodes;
items.value = episodes.map(element => {
//element.imported = -1;
element.short_description = '';
if (typeof element.description === "string") {
const arr = element.description.split(' ');
const limit = arr.length >= 20 ? 20 : arr.length;
for (let j = 0; j < limit; j++) {
element.short_description += arr[j];
element.short_description += (j === 19) ? '...' : ' ';
}
element.short_description = (element.short_description + '').replace(/&#\d+;/gm, function(s) {
return String.fromCharCode(s.match(/\d+/gm)[0]);
});
}
return element;
});
if (load) {
loading.value = false;
}
return podcastEpisodesList.data.podcast.title;
}).catch((error: Error) => {
console.log("Error: " + error);
});
}
const downloadEpisode = async (podcast_id, item) => {
return await axios.post('/podcast_episode', {
podcast_id: podcast_id,
episode: item,
episode_url: item.enclosure["@attributes"].url,
episode_title: item.title,
}).then((response) => {
console.log(response);
}).catch((error) => {
console.log("Error: "+error);
});
}
const checkDownloadEpisode = async (episode_id) => {
return await axios.get(`/check_podcast_episode_download/${episode_id}`, {
params: {
}
}).then( (response) => {
if (response.data === 'SUCCESS') {
return true;
}
checkDownloadEpisode(episode_id);
})
}
getItems(true);
return { items, listData, headers, loading, downloadingEpisode, getItems, downloadEpisode }
}