mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 07:25:36 +01:00
Update downloading
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Context, Telegraf, Markup } from 'telegraf';
|
||||
|
||||
import { BotState } from '@/bots/manager';
|
||||
import { BotState, Cache } from '@/bots/manager';
|
||||
|
||||
import env from '@/config';
|
||||
|
||||
@@ -11,6 +11,7 @@ import * as CallbackData from "./callback_data";
|
||||
import * as BookLibrary from "./services/book_library";
|
||||
import { CachedMessage, getBookCache } from './services/book_cache';
|
||||
import { getBookCacheBuffer } from './services/book_cache_buffer';
|
||||
import { download } from './services/downloader';
|
||||
import { formatBook, formatAuthor, formatSequence } from './format';
|
||||
import { getPaginatedMessage, registerPaginationCommand } from './utils';
|
||||
import { getRandomKeyboard } from './keyboard';
|
||||
@@ -109,10 +110,17 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
||||
|
||||
let cache: CachedMessage;
|
||||
|
||||
if (state.privileged) {
|
||||
if (state.cache === Cache.ORIGINAL) {
|
||||
cache = await getBookCache(parseInt(id), format);
|
||||
} else {
|
||||
} else if (state.cache === Cache.BUFFER) {
|
||||
cache = await getBookCacheBuffer(parseInt(id), format);
|
||||
} else {
|
||||
const book = await BookLibrary.getBookById(parseInt(id));
|
||||
const data = await download(book.source.id, book.remote_id, format);
|
||||
ctx.telegram.sendDocument(ctx.message.chat.id, data, {
|
||||
reply_to_message_id: ctx.message.message_id
|
||||
})
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.telegram.copyMessage(ctx.message.chat.id, cache.chat_id, cache.message_id, {
|
||||
|
||||
@@ -40,6 +40,19 @@ export interface Book extends AuthorBook {
|
||||
}
|
||||
|
||||
|
||||
export interface Source {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
|
||||
export interface DetailBook extends Book {
|
||||
source: Source;
|
||||
remote_id: number;
|
||||
is_deleted: boolean;
|
||||
}
|
||||
|
||||
|
||||
export interface Author {
|
||||
id: number;
|
||||
last_name: string;
|
||||
@@ -76,6 +89,11 @@ async function _makeRequest<T>(url: string, searchParams?: string | Record<strin
|
||||
}
|
||||
|
||||
|
||||
export async function getBookById(book_id: number): Promise<DetailBook> {
|
||||
return _makeRequest<DetailBook>(`/api/v1/books/${book_id}`);
|
||||
}
|
||||
|
||||
|
||||
export async function searchByBookName(query: string, page: number): Promise<Page<Book>> {
|
||||
return _makeRequest<Page<Book>>(`/api/v1/books/search/${query}`, {
|
||||
page: page,
|
||||
|
||||
23
src/bots/factory/bots/approved/services/downloader.ts
Normal file
23
src/bots/factory/bots/approved/services/downloader.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import got from 'got';
|
||||
|
||||
import env from '@/config';
|
||||
|
||||
|
||||
export interface DownloadedFile {
|
||||
source: Buffer;
|
||||
filename: string;
|
||||
}
|
||||
|
||||
|
||||
export async function download(source_id: number, remote_id: number, file_type: string): Promise<DownloadedFile> {
|
||||
const response = await got<string>(`${env.DOWNLOADER_URL}/download/${source_id}/${remote_id}/${file_type}`, {
|
||||
headers: {
|
||||
'Authorization': env.DOWNLOADER_API_KEY,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
source: response.rawBody,
|
||||
filename: (response.headers['content-disposition'] || '').split('filename=')[1]
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,17 @@ import env from '@/config';
|
||||
import getBot, { BotStatuses } from './factory/index';
|
||||
import { Server } from 'http';
|
||||
|
||||
export enum Cache {
|
||||
ORIGINAL = "original",
|
||||
BUFFER = "buffer",
|
||||
NO_CACHE = "no_cache"
|
||||
}
|
||||
|
||||
export interface BotState {
|
||||
id: number;
|
||||
token: string;
|
||||
status: BotStatuses;
|
||||
privileged: boolean;
|
||||
cache: Cache;
|
||||
created_time: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,5 +12,7 @@ export default cleanEnv(process.env, {
|
||||
CACHE_SERVER_URL: str(),
|
||||
CACHE_SERVER_API_KEY: str(),
|
||||
BUFFER_SERVER_URL: str(),
|
||||
BUFFER_SERVER_API_KEY: str()
|
||||
BUFFER_SERVER_API_KEY: str(),
|
||||
DOWNLOADER_URL: str(),
|
||||
DOWNLOADER_API_KEY: str(),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user