refactor + add download OVA

This commit is contained in:
Francesco Grazioso 2024-05-05 12:45:43 +02:00
parent d4e48ebb84
commit 1f8412fba8
3 changed files with 61 additions and 44 deletions

View File

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

View File

@ -55,4 +55,9 @@ export interface Season {
export interface SeasonResponse { export interface SeasonResponse {
episodes: Season; episodes: Season;
}
export interface DownloadResponse {
error: string;
message: string;
} }

View File

@ -1,8 +1,8 @@
<script setup lang="ts"> <script setup lang="ts">
import { useRoute } from 'vue-router' import { useRoute } from 'vue-router'
import type {Episode, MediaItem, Season, SeasonResponse} from "@/api/interfaces"; import type {DownloadResponse, Episode, MediaItem, Season, SeasonResponse} from "@/api/interfaces";
import { onMounted, ref } from "vue"; import { onMounted, ref } from "vue";
import {downloadFilm, getEpisodesInfo} from "@/api/api"; import {downloadAnimeFilm, downloadFilm, getEpisodesInfo} from "@/api/api";
const route = useRoute() const route = useRoute()
@ -54,18 +54,30 @@ const toggleEpisodeSelection = () => {
const downloadItems = async () => { const downloadItems = async () => {
try { try {
if (item.type === 'MOVIE') { let res: DownloadResponse;
const res = await downloadFilm(item.id, item.slug, item.type) switch (item.type) {
if (res.error) { case 'MOVIE':
throw new Error(res.error + ' - ' + res.message) res = (await downloadFilm(item.id, item.slug)).data;
} break;
alertDownload() case 'OVA':
return case 'SPECIAL':
res = (await downloadAnimeFilm(item.id, item.slug)).data;
break;
default:
throw new Error('Tipo di media non supportato');
} }
console.log(res)
if (res.error) {
throw new Error(`${res.error} - ${res.message}`);
}
alertDownload();
} catch (error) { } catch (error) {
alertDownload(error) alertDownload(error);
} }
} };
const alertDownload = (message?: any) => { const alertDownload = (message?: any) => {
if (message) { if (message) {