mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Add translator
This commit is contained in:
@@ -3,8 +3,10 @@ export const SETTINGS_LANGUAGES = 'settings.languages';
|
||||
export const SEARCH_BOOK_PREFIX = 'sb_';
|
||||
export const SEARCH_AUTHORS_PREFIX = 'sa_';
|
||||
export const SEARCH_SERIES_PREFIX = 'ss_';
|
||||
export const SEARCH_TRANSLATORS_PREFIX = 'st_';
|
||||
|
||||
export const AUTHOR_BOOKS_PREFIX = 'ba_';
|
||||
export const TRANSLATOR_BOOKS_PREFIX = 'bt_';
|
||||
export const SEQUENCE_BOOKS_PREFIX = 'bs_';
|
||||
|
||||
export const RANDOM_BOOK = 'random_book';
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
import { AuthorBook, Book, Author, Sequence } from './services/book_library';
|
||||
import { AuthorBook, TranslatorBook, Book, Author, Sequence } from './services/book_library';
|
||||
|
||||
|
||||
function isBook(item: AuthorBook | Book): item is Book {
|
||||
type AllBookTypes = Book | AuthorBook | TranslatorBook;
|
||||
|
||||
|
||||
function isAuthorBook(item: AllBookTypes): item is AuthorBook {
|
||||
return 'translator' in item;
|
||||
}
|
||||
|
||||
|
||||
function isTranslatorBook(item: AllBookTypes): item is TranslatorBook {
|
||||
return 'authors' in item;
|
||||
}
|
||||
|
||||
|
||||
export function formatBook(book: AuthorBook | Book): string {
|
||||
export function formatBook(book: AllBookTypes): string {
|
||||
let response: string[] = [];
|
||||
|
||||
response.push(`📖 ${book.title} | ${book.lang}`);
|
||||
@@ -15,12 +23,12 @@ export function formatBook(book: AuthorBook | Book): string {
|
||||
response.push(`📝 Аннотация: /b_info_${book.id}`)
|
||||
}
|
||||
|
||||
if (isBook(book) && book.authors.length > 0) {
|
||||
if (isTranslatorBook(book) && book.authors.length > 0) {
|
||||
response.push('Авторы:')
|
||||
book.authors.forEach(author => response.push(`͏👤 ${author.last_name} ${author.first_name} ${author.middle_name}`));
|
||||
}
|
||||
|
||||
if (book.translators.length > 0) {
|
||||
if (isAuthorBook(book) && book.translators.length > 0) {
|
||||
response.push('Переводчики:');
|
||||
book.translators.forEach(author => response.push(`͏👤 ${author.last_name} ${author.first_name} ${author.middle_name}`));
|
||||
}
|
||||
@@ -45,6 +53,20 @@ export function formatAuthor(author: Author): string {
|
||||
}
|
||||
|
||||
|
||||
export function formatTranslator(author: Author): string {
|
||||
let response = [];
|
||||
|
||||
response.push(`👤 ${author.last_name} ${author.first_name} ${author.middle_name}`);
|
||||
response.push(`/t_${author.id}`);
|
||||
|
||||
if (author.annotation_exists) {
|
||||
response.push(`📝 Аннотация: /a_info_${author.id}`);
|
||||
}
|
||||
|
||||
return response.join('\n');
|
||||
}
|
||||
|
||||
|
||||
export function formatSequence(sequence: Sequence): string {
|
||||
let response = [];
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import { CachedMessage, getBookCache } from './services/book_cache';
|
||||
import { getBookCacheBuffer } from './services/book_cache_buffer';
|
||||
import { download } from './services/downloader';
|
||||
import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings';
|
||||
import { formatBook, formatAuthor, formatSequence } from './format';
|
||||
import { formatBook, formatAuthor, formatSequence, formatTranslator } from './format';
|
||||
import { getPaginatedMessage, registerLanguageSettingsCallback, registerPaginationCommand, registerRandomItemCallback } from './utils';
|
||||
import { getRandomKeyboard, getUserAllowedLangsKeyboard } from './keyboard';
|
||||
|
||||
@@ -72,10 +72,12 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
||||
});
|
||||
|
||||
registerPaginationCommand(bot, CallbackData.SEARCH_BOOK_PREFIX, BookLibrary.searchByBookName, formatBook);
|
||||
registerPaginationCommand(bot, CallbackData.SEARCH_TRANSLATORS_PREFIX, BookLibrary.searchTranslators, formatTranslator);
|
||||
registerPaginationCommand(bot, CallbackData.SEARCH_AUTHORS_PREFIX, BookLibrary.searchAuthors, formatAuthor);
|
||||
registerPaginationCommand(bot, CallbackData.SEARCH_SERIES_PREFIX, BookLibrary.searchSequences, formatSequence);
|
||||
|
||||
registerPaginationCommand(bot, CallbackData.AUTHOR_BOOKS_PREFIX, BookLibrary.getAuthorBooks, formatBook);
|
||||
registerPaginationCommand(bot, CallbackData.TRANSLATOR_BOOKS_PREFIX, BookLibrary.getTranslatorBooks, formatBook);
|
||||
registerPaginationCommand(bot, CallbackData.SEQUENCE_BOOKS_PREFIX, BookLibrary.getSequenceBooks, formatBook);
|
||||
|
||||
bot.command("random", async (ctx: Context) => {
|
||||
@@ -183,6 +185,23 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
||||
});
|
||||
});
|
||||
|
||||
bot.hears(/^\/t_[\d]+$/gm, async (ctx: Context) => {
|
||||
if (!ctx.message || !('text' in ctx.message)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const translatorId = ctx.message.text.split('_')[1];
|
||||
|
||||
const userSettings = await getUserSettings(ctx.message.from.id);
|
||||
const allowedLangs = userSettings.allowed_langs.map((lang) => lang.code);
|
||||
|
||||
const pMessage = await getPaginatedMessage(CallbackData.TRANSLATOR_BOOKS_PREFIX, translatorId, 1, allowedLangs, BookLibrary.getTranslatorBooks, formatBook);
|
||||
|
||||
await ctx.reply(pMessage.message, {
|
||||
reply_markup: pMessage.keyboard.reply_markup
|
||||
});
|
||||
});
|
||||
|
||||
bot.hears(/^\/s_[\d]+$/gm, async (ctx: Context) => {
|
||||
if (!ctx.message || !('text' in ctx.message)) {
|
||||
return;
|
||||
@@ -215,10 +234,10 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
||||
Markup.button.callback('Автора', `${CallbackData.SEARCH_AUTHORS_PREFIX}${query}_1`),
|
||||
],
|
||||
[
|
||||
Markup.button.callback('Серию', `${CallbackData.SEARCH_SERIES_PREFIX}${query}_1`)
|
||||
Markup.button.callback('Серию', `${CallbackData.SEARCH_SERIES_PREFIX}${query}_1`),
|
||||
],
|
||||
[
|
||||
Markup.button.callback('Переводчика', '# ToDO'),
|
||||
Markup.button.callback('Переводчика', `${CallbackData.SEARCH_TRANSLATORS_PREFIX}${query}_1`),
|
||||
]
|
||||
]);
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ interface BookAuthor {
|
||||
}
|
||||
|
||||
|
||||
export interface AuthorBook {
|
||||
export interface BaseBook {
|
||||
id: number;
|
||||
title: string;
|
||||
lang: string;
|
||||
@@ -32,12 +32,22 @@ export interface AuthorBook {
|
||||
available_types: string[];
|
||||
uploaded: string;
|
||||
annotation_exists: boolean;
|
||||
}
|
||||
|
||||
|
||||
export interface AuthorBook extends BaseBook {
|
||||
translators: BookAuthor[];
|
||||
}
|
||||
|
||||
|
||||
export interface TranslatorBook extends BaseBook {
|
||||
authors: BookAuthor[];
|
||||
}
|
||||
|
||||
|
||||
export interface Book extends AuthorBook {
|
||||
authors: BookAuthor[];
|
||||
translators: BookAuthor[];
|
||||
}
|
||||
|
||||
|
||||
@@ -121,6 +131,15 @@ export async function searchAuthors(query: string, page: number, allowedLangs: s
|
||||
}
|
||||
|
||||
|
||||
export async function searchTranslators(query: string, page: number, allowedLangs: string[]): Promise<Page<Author>> {
|
||||
const searchParams = getAllowedLangsSearchParams(allowedLangs);
|
||||
searchParams.append('page', page.toString());
|
||||
searchParams.append('size', PAGE_SIZE.toString());
|
||||
|
||||
return _makeRequest<Page<Author>>(`/api/v1/translators/search/${query}`, searchParams);
|
||||
}
|
||||
|
||||
|
||||
export async function searchSequences(query: string, page: number, allowedLangs: string[]): Promise<Page<Sequence>> {
|
||||
const searchParams = getAllowedLangsSearchParams(allowedLangs);
|
||||
searchParams.append('page', page.toString());
|
||||
@@ -149,6 +168,15 @@ export async function getAuthorBooks(authorId: number, page: number, allowedLang
|
||||
}
|
||||
|
||||
|
||||
export async function getTranslatorBooks(translatorId: number, page: number, allowedLangs: string[]): Promise<Page<AuthorBook>> {
|
||||
const searchParams = getAllowedLangsSearchParams(allowedLangs);
|
||||
searchParams.append('page', page.toString());
|
||||
searchParams.append('size', PAGE_SIZE.toString());
|
||||
|
||||
return _makeRequest<Page<AuthorBook>>(`/api/v1/translators/${translatorId}/books`, searchParams);
|
||||
}
|
||||
|
||||
|
||||
export async function getSequenceBooks(sequenceId: number, page: number, allowedLangs: string[]): Promise<Page<Book>> {
|
||||
const searchParams = getAllowedLangsSearchParams(allowedLangs);
|
||||
searchParams.append('page', page.toString());
|
||||
|
||||
Reference in New Issue
Block a user