revert base

This commit is contained in:
Francesco Grazioso 2024-05-27 12:51:59 +02:00
parent ad132b7c53
commit 28b0338f23
3 changed files with 310 additions and 420 deletions

View File

@ -1,81 +1,44 @@
import axios from "axios"; import axios from 'axios'
import type { AxiosResponse } from "axios"; import type {MediaItemResponse} from '@/api/interfaces'
import type { DownloadResponse, MediaItemResponse } from "@/api/interfaces";
const BASE_URL = "http://localhost:8000/api"; const BASE_URL = 'http://localhost:8000/api'
const api = axios.create({ function get(url: string): Promise<any> {
baseURL: BASE_URL, return axios.get(`${BASE_URL}${url}`)
}); .then(response => response.data)
.catch(error => {
async function get<T>(url: string): Promise<AxiosResponse<T>> { throw error;
return api.get(url);
}
async function post<T>(url: string, data: any): Promise<AxiosResponse<T>> {
return api.post(url, data);
}
export default function search(
query: string,
type: string
): Promise<AxiosResponse<MediaItemResponse>> {
return get(`/search?search_terms=${query}&type=${type}`);
}
export async function getEpisodesInfo(
mediaId: number,
mediaSlug: string,
mediaType: string
): Promise<Response> {
const url = `/search/get_episodes_info?media_id=${mediaId}&media_slug=${mediaSlug}&type_media=${mediaType}`;
return fetch(`${BASE_URL}${url}`, {
method: "GET",
headers: {
"Content-Type": "text/event-stream",
},
}); });
} }
export async function getPreview( function post(url: string, data: any): Promise<any> {
mediaId: number, return axios.post(`${BASE_URL}${url}`, data)
mediaSlug: string, .then(response => response.data)
mediaType: string .catch(error => {
): Promise<AxiosResponse<any>> { throw error;
const url = `/search/get_preview?media_id=${mediaId}&media_slug=${mediaSlug}&type_media=${mediaType}`; });
return get(url);
} }
async function downloadMedia( export default function search(query: string, type: string) : Promise<MediaItemResponse> {
mediaId: number, return get(`/search?search_terms=${query}&type=${type}`)
mediaSlug: string, }
mediaType: string,
downloadId?: number, export async function getEpisodesInfo(mediaId: number, mediaSlug: string, mediaType: string): Promise<Response> {
tvSeriesEpisodeId?: number const url = `${BASE_URL}/search/get_episodes_info?media_id=${mediaId}&media_slug=${mediaSlug}&type_media=${mediaType}`;
): Promise<AxiosResponse<DownloadResponse>> { return await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'text/event-stream'
}
});
}
export async function downloadFilm(mediaId: number, mediaSlug: string, mediaType: string): Promise<Response> {
const url = `/download/`; const url = `/download/`;
const data = { const data = {
media_id: mediaId, media_id: mediaId,
media_slug: mediaSlug, media_slug: mediaSlug,
type_media: mediaType, type_media: mediaType
download_id: downloadId,
tv_series_episode_id: tvSeriesEpisodeId,
}; };
return post(url, data); return post(url, data);
} }
export const downloadFilm = (mediaId: number, mediaSlug: string) =>
downloadMedia(mediaId, mediaSlug, "MOVIE");
export const downloadTvSeries = (
mediaId: number,
mediaSlug: string,
downloadId: number,
tvSeriesEpisodeId?: number
) => downloadMedia(mediaId, mediaSlug, "TV", downloadId, tvSeriesEpisodeId);
export const downloadAnimeFilm = (mediaId: number, mediaSlug: string) =>
downloadMedia(mediaId, mediaSlug, "OVA");
export const downloadAnimeSeries = (
mediaId: number,
mediaSlug: string,
downloadId: number
) => downloadMedia(mediaId, mediaSlug, "TV_ANIME", downloadId);

View File

@ -57,8 +57,3 @@ export interface Season {
export interface SeasonResponse { export interface SeasonResponse {
episodes: Season; episodes: Season;
} }
export interface DownloadResponse {
error: string;
message: string;
}

View File

@ -1,42 +1,23 @@
<script setup lang="ts"> <script setup lang="ts">
import { useRoute } from 'vue-router' import { useRoute } from 'vue-router'
import type {Episode, MediaItem, SeasonResponse} from "@/api/interfaces"; import type {Episode, MediaItem, Season, SeasonResponse} from "@/api/interfaces";
import { onBeforeMount, onMounted, ref } from "vue"; import { onMounted, ref } from "vue";
import { getEpisodesInfo, getPreview } from "@/api/api"; import {downloadFilm, getEpisodesInfo} from "@/api/api";
import {
alertDownload,
handleMovieDownload,
handleOVADownload,
handleTVAnimeDownload,
handleTvAnimeEpisodesDownload,
handleTVDownload, handleTVEpisodesDownload
} from "@/api/utils";
const route = useRoute() const route = useRoute()
const item: MediaItem = JSON.parse(<string>route.params.item) const item: MediaItem = JSON.parse(<string>route.params.item)
const imageUrl: string = <string>route.params.imageUrl const imageUrl: string = <string>route.params.imageUrl
const animeEpisodes = ref<Episode[]>([]) const animeEpisodes = ref<Episode[]>([])
const totalEpisodes = ref<number>(0)
const tvShowEpisodes = ref<any[]>([]) const tvShowEpisodes = ref<any[]>([])
const loading = ref(false) const loading = ref(false)
const selectingEpisodes = ref(false) const selectingEpisodes = ref(false)
const selectedEpisodes = ref<Episode[]>([])
const previewItem = ref<MediaItem>(item)
onBeforeMount(async () => {
const res = await getPreview(item.id, item.slug, item.type)
if (res && res.data) {
previewItem.plot = res.data.plot
}
})
onMounted(async () => { onMounted(async () => {
loading.value = true;
if (['MOVIE', 'OVA', 'SPECIAL'].includes(item.type)) { if (['MOVIE', 'OVA', 'SPECIAL'].includes(item.type)) {
loading.value = false;
return return
} else { } else {
loading.value = true;
const response = await getEpisodesInfo(item.id, item.slug, item.type) const response = await getEpisodesInfo(item.id, item.slug, item.type)
if (response && response.body) { if (response && response.body) {
loading.value = false; loading.value = false;
@ -50,7 +31,6 @@ onMounted(async () => {
if (item.type === 'TV_ANIME') { if (item.type === 'TV_ANIME') {
const episodesData:Episode = JSON.parse(value.trim()); const episodesData:Episode = JSON.parse(value.trim());
animeEpisodes.value.push(episodesData); animeEpisodes.value.push(episodesData);
totalEpisodes.value = episodesData.episode_total;
} else { } else {
const episodesData:SeasonResponse = JSON.parse(value.trim()); const episodesData:SeasonResponse = JSON.parse(value.trim());
for (const seasonKey in episodesData.episodes) { for (const seasonKey in episodesData.episodes) {
@ -70,59 +50,30 @@ onMounted(async () => {
const toggleEpisodeSelection = () => { const toggleEpisodeSelection = () => {
selectingEpisodes.value = !selectingEpisodes.value selectingEpisodes.value = !selectingEpisodes.value
selectedEpisodes.value = []
} }
const toggleEpisodeSelect = (episode: Episode, seasonNumber?: number) => { const downloadItems = async () => {
if (selectedEpisodes.value.includes(episode)) { try {
selectedEpisodes.value = selectedEpisodes.value.filter(e => e !== episode) if (item.type === 'MOVIE') {
} else { const res = await downloadFilm(item.id, item.slug, item.type)
if (seasonNumber) { if (res.error) {
episode.season_index = seasonNumber throw new Error(res.error + ' - ' + res.message)
} }
selectedEpisodes.value.push(episode) alertDownload()
return
}
} catch (error) {
alertDownload(error)
} }
} }
const downloadSelectedEpisodes = async () => { const alertDownload = (message?: any) => {
try { if (message) {
switch (item.type) { alert(message)
case 'TV': return;
await handleTVEpisodesDownload(selectedEpisodes.value, item);
case 'TV_ANIME':
await handleTvAnimeEpisodesDownload(selectedEpisodes.value, item);
break;
default:
throw new Error('Tipo di media non supportato');
} }
toggleEpisodeSelection(); alert('Il downlaod è iniziato, il file sarà disponibile tra qualche minuto nella cartella \'Video\' del progetto...')
} catch (error) { }
alertDownload(error);
}
};
const downloadAllItems = async () => {
try {
switch (item.type) {
case 'TV':
await handleTVDownload(tvShowEpisodes.value, item);
case 'MOVIE':
await handleMovieDownload(item);
break;
case 'TV_ANIME':
await handleTVAnimeDownload(totalEpisodes.value, item);
break;
case 'OVA':
case 'SPECIAL':
await handleOVADownload(item);
break;
default:
throw new Error('Tipo di media non supportato');
}
} catch (error) {
alertDownload(error);
}
};
</script> </script>
<template> <template>
@ -136,52 +87,34 @@ const downloadAllItems = async () => {
<h1 class="details-title">{{ item.name }}</h1> <h1 class="details-title">{{ item.name }}</h1>
<h3> {{ item.score }}</h3> <h3> {{ item.score }}</h3>
<div class="details-description"> <div class="details-description">
<p>{{ item.plot }}</p> <p v-if="item.type == 'TV_ANIME'">{{ item.plot }}</p>
<p v-else-if="tvShowEpisodes.length > 0">{{ tvShowEpisodes[0][0].plot }}</p>
</div> </div>
<h3 v-if="animeEpisodes.length > 0 && !loading">Numero episodi: {{ totalEpisodes }}</h3> <h3 v-if="animeEpisodes.length > 0 && !loading">Numero episodi: {{ animeEpisodes[0].episode_total }}</h3>
<h3 v-if="tvShowEpisodes.length > 0 && !loading">Numero stagioni: {{ tvShowEpisodes.length }}</h3> <h3 v-if="tvShowEpisodes.length > 0 && !loading">Numero stagioni: {{ tvShowEpisodes.length }}</h3>
<hr style="opacity: 0.2; margin-top: 10px"/> <hr style="opacity: 0.2"/>
<!--DOWNLOAD SECTION-->
<div class="download-section"> <div class="download-section">
<button :disabled="loading || selectingEpisodes" <button :disabled="loading || selectingEpisodes" @click="downloadItems">Scarica {{['TV_ANIME', 'TV'].includes(item.type)? 'tutto' : ''}}</button>
@click.prevent="downloadAllItems">
Scarica {{['TV_ANIME', 'TV'].includes(item.type)? 'tutto' : ''}}
</button>
<template v-if="!loading && ['TV_ANIME', 'TV'].includes(item.type)"> <template v-if="!loading && ['TV_ANIME', 'TV'].includes(item.type)">
<button @click="toggleEpisodeSelection"> <button @click="toggleEpisodeSelection">{{selectingEpisodes ? 'Disattiva' : 'Attiva'}} selezione episodi</button>
{{selectingEpisodes ? 'Disattiva' : 'Attiva'}} selezione episodi <button>Download episodi</button>
</button>
<button :disabled="selectedEpisodes.length == 0"
@click="downloadSelectedEpisodes">
Download episodi selezionati
</button>
</template> </template>
</div> </div>
</div> </div>
</div> </div>
<!--SERIES SECTION--> <!--SERIES SECTION-->
<div v-if="!loading && ['TV_ANIME', 'TV'].includes(item.type)" <div v-if="!loading && ['TV_ANIME', 'TV'].includes(item.type)" :class="item.type == 'TV_ANIME' ? 'episodes-container' : 'season-container'">
:class="item.type == 'TV_ANIME' ? 'episodes-container' : 'season-container'">
<div v-if="animeEpisodes.length == 0 && tvShowEpisodes.length == 0"> <div v-if="animeEpisodes.length == 0 && tvShowEpisodes.length == 0">
<p>Non ci sono episodi...</p> <p>Non ci sono episodi...</p>
</div> </div>
<div v-else-if="item.type == 'TV_ANIME'" <div v-else-if="item.type == 'TV_ANIME'" v-for="episode in animeEpisodes" :key="episode.id" class="episode-item">
v-for="episode in animeEpisodes"
:key="episode.id"
class="episode-item"
:style="{ backgroundColor: selectedEpisodes.includes(episode) ? '#42b883' : '#333' }"
@click="selectingEpisodes ? toggleEpisodeSelect(episode) : null">
<div class="episode-title">Episodio {{ episode.number }}</div> <div class="episode-title">Episodio {{ episode.number }}</div>
</div> </div>
<div v-else-if="item.type == 'TV'" v-for="(season, index) in tvShowEpisodes" v-bind:key="season.number" class="season-item"> <div v-else-if="item.type == 'TV'" v-for="(season, index) in tvShowEpisodes" class="season-item">
<div class="season-title">Stagione {{ index + 1 }}</div> <div class="season-title">Stagione {{ index + 1 }}</div>
<div class="episode-container"> <div class="episode-container">
<div v-for="episode in season" :key="episode.id"> <div v-for="episode in season" :key="episode.id" class="episode-item">
<div class="episode-item"
:style="{ backgroundColor: selectedEpisodes.includes(episode) ? '#42b883' : '#333' }"
@click="selectingEpisodes ? toggleEpisodeSelect(episode, index) : null">
<div class="episode-title"> <div class="episode-title">
Episodio {{ episode.number }} - Episodio {{ episode.number }} -
{{episode.name.slice(0, 40) + (episode.name.length > 39 ? '...' : '')}} {{episode.name.slice(0, 40) + (episode.name.length > 39 ? '...' : '')}}
@ -190,11 +123,10 @@ const downloadAllItems = async () => {
</div> </div>
</div> </div>
</div> </div>
</div>
<!--MOVIES SECTION--> <!--MOVIES SECTION-->
<div v-else-if="!loading && ['MOVIE', 'OVA', 'SPECIAL'].includes(item.type)"> <div v-else-if="!loading && ['MOVIE', 'OVA', 'SPECIAL'].includes(item.type)">
<p>Questo è un {{item.type}} (QUESTO TESTO E' A SCOPO DI TEST)</p> <p>Questo è un {{item.type}}</p>
</div> </div>
<!--LOADING SECTION--> <!--LOADING SECTION-->