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

View File

@ -1,8 +1,8 @@
<script setup lang="ts">
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 {downloadFilm, getEpisodesInfo} from "@/api/api";
import {downloadAnimeFilm, downloadFilm, getEpisodesInfo} from "@/api/api";
const route = useRoute()
@ -54,18 +54,30 @@ const toggleEpisodeSelection = () => {
const downloadItems = async () => {
try {
if (item.type === 'MOVIE') {
const res = await downloadFilm(item.id, item.slug, item.type)
if (res.error) {
throw new Error(res.error + ' - ' + res.message)
}
alertDownload()
return
let res: DownloadResponse;
switch (item.type) {
case 'MOVIE':
res = (await downloadFilm(item.id, item.slug)).data;
break;
case 'OVA':
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) {
alertDownload(error)
alertDownload(error);
}
}
};
const alertDownload = (message?: any) => {
if (message) {