import { Plugin } from '@/types/plugin'; import { FilterTypes, Filters } from '@libs/filterInputs'; import { fetchApi } from '@libs/fetch'; import { load as parseHTML } from 'cheerio'; class TopLiba implements Plugin.PluginBase { id = 'TopLiba'; name = 'ТопЛиба'; site = 'https://topliba.com'; version = '1.0.1'; icon = 'src/ru/topliba/icon.png'; _token = ''; async fetchNovels( page: number, showLatestNovels?: boolean, filters?: Plugin.PopularNovelsOptions['filters'], searchTerm?: string, ): Promise { const data = new URLSearchParams({ order_field: showLatestNovels ? 'date' : filters?.sort?.value || 'rating', p: page.toString(), }); if (searchTerm) data.append('q', searchTerm); const body = await fetchApi(this.site + '/?' + data.toString()).then(res => res.text(), ); const novels: Plugin.NovelItem[] = []; this._token = body.match( /, ) { return this.fetchNovels(page, showLatestNovels, filters); } async searchNovels(searchTerm: string, page: number) { return this.fetchNovels(page, false, undefined, searchTerm); } async parseNovel(novelPath: string): Promise { const body = await fetchApi(this.resolveUrl(novelPath, true)).then(res => res.text(), ); const loadedCheerio = parseHTML(body); const novel: Plugin.SourceNovel = { path: novelPath, name: loadedCheerio('div > h1').text().trim(), cover: this.site + '/covers/' + novelPath + '.jpg', summary: loadedCheerio('.description').text().trim(), author: loadedCheerio('.book-author > a').text().trim(), genres: loadedCheerio('.book-genres > div > a') .map((index, element) => loadedCheerio(element).text()) .get() .join(','), }; const chaptersHTML = await fetchApi(this.resolveUrl(novelPath)).then(res => res.text(), ); this._token = chaptersHTML.match(/([\s\S]*?) { const [, padding, capter, name] = chapter.match( /class="padding-(\d+)" data-capter="(\d+)">([\s\S]*?) { const [bookID, chapterID] = chapterPath.split('?'); if (!this._token) { const chaptersHTML = await fetchApi(this.resolveUrl(bookID)).then(res => res.text(), ); this._token = chaptersHTML.match( / res.text()); return chapterText; } resolveUrl = (path: string, isNovel?: boolean) => this.site + (isNovel ? '/books/' + path : '/reader/' + path.split('?')[0]); filters = { sort: { label: 'Сортировка:', value: 'rating', options: [ { label: 'По рейтингу', value: 'rating' }, { label: 'По популярности', value: 'num_downloads' }, { label: 'По году выхода', value: 'year' }, { label: 'По дате добавления', value: 'date' }, { label: 'По названию', value: 'title' }, ], type: FilterTypes.Picker, }, } satisfies Filters; } export default new TopLiba();