Skip to content

Commit

Permalink
BatoTo: 3.1.0 (#34)
Browse files Browse the repository at this point in the history
* workflows: Use newer version of node

* BatoTo: Combine english and english US

* BatoTo: 3.1.0 - Implement language filtering

* BatoTo: Update crypto-js.min.js to 4.2.0

Still a bit meh about how we are not installing it
as a dev package...

* BatoTo: Forgot to remove debugging console.log
  • Loading branch information
niclimcy authored Feb 27, 2024
1 parent b55206f commit d0e0aed
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ jobs:

strategy:
matrix:
node-version: [14.x]
node-version: [18.x]

steps:
- name: Checkout Branch
uses: actions/checkout@v2

- name: Setup Node.js environment
uses: actions/setup-node@v2.1.2
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

Expand All @@ -38,7 +38,7 @@ jobs:
- run: npm run bundle -- --folder=${{ steps.extract_branch.outputs.branch }}

- name: Deploy to GitHub Pages
uses: JamesIves/github-pages-deploy-action@4.1.0
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages
folder: bundles
58 changes: 48 additions & 10 deletions src/BatoTo/BatoTo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
BadgeColor,
Chapter,
ChapterDetails,
ChapterProviding,
ContentRating,
DUISection,
HomePageSectionsProviding,
HomeSection,
MangaProviding,
Expand Down Expand Up @@ -30,19 +32,34 @@ import {
parseViewMore
} from './BatoToParser'

import {
BTLanguages,
Metadata
} from './BatoToHelper'

import {
languageSettings,
resetSettings
} from './BatoToSettings'

const BATO_DOMAIN = 'https://bato.to'

export const BatoToInfo: SourceInfo = {
version: '3.0.4',
version: '3.1.0',
name: 'BatoTo',
icon: 'icon.png',
author: 'Nicholas',
authorWebsite: 'https://github.com/niclimcy',
description: 'Extension that pulls manga from bato.to',
contentRating: ContentRating.MATURE,
websiteBaseURL: BATO_DOMAIN,
sourceTags: [],
intents: SourceIntents.MANGA_CHAPTERS | SourceIntents.HOMEPAGE_SECTIONS | SourceIntents.CLOUDFLARE_BYPASS_REQUIRED
sourceTags: [
{
text: 'Multi Language',
type: BadgeColor.BLUE
}
],
intents: SourceIntents.MANGA_CHAPTERS | SourceIntents.HOMEPAGE_SECTIONS | SourceIntents.SETTINGS_UI | SourceIntents.CLOUDFLARE_BYPASS_REQUIRED
}

export class BatoTo implements SearchResultsProviding, MangaProviding, ChapterProviding, HomePageSectionsProviding {
Expand Down Expand Up @@ -73,6 +90,20 @@ export class BatoTo implements SearchResultsProviding, MangaProviding, ChapterPr
}
});

stateManager = App.createSourceStateManager()

async getSourceMenu(): Promise<DUISection> {
return Promise.resolve(App.createDUISection({
id: 'main',
header: 'Source Settings',
isHidden: false,
rows: async () => [
languageSettings(this.stateManager),
resetSettings(this.stateManager)
]
}))
}

getMangaShareUrl(mangaId: string): string { return `${BATO_DOMAIN}/series/${mangaId}` }

async getMangaDetails(mangaId: string): Promise<SourceManga> {
Expand Down Expand Up @@ -123,7 +154,7 @@ export class BatoTo implements SearchResultsProviding, MangaProviding, ChapterPr
parseHomeSections($, sectionCallback)
}

async getViewMoreItems(homepageSectionId: string, metadata: any): Promise<PagedResults> {
async getViewMoreItems(homepageSectionId: string, metadata: Metadata | undefined): Promise<PagedResults> {
const page: number = metadata?.page ?? 1
let param = ''

Expand All @@ -137,6 +168,11 @@ export class BatoTo implements SearchResultsProviding, MangaProviding, ChapterPr
default:
throw new Error('Requested to getViewMoreItems for a section ID which doesn\'t exist')
}

const langHomeFilter: boolean = await this.stateManager.retrieve('language_home_filter') ?? false
const langs: string[] = langHomeFilter ? await this.stateManager.retrieve('languages') : BTLanguages.getDefault()
param += langs ? `&langs=${langs.join(',')}` : ''

const request = App.createRequest({
url: `${BATO_DOMAIN}/browse`,
method: 'GET',
Expand All @@ -155,7 +191,7 @@ export class BatoTo implements SearchResultsProviding, MangaProviding, ChapterPr
})
}

async getSearchResults(query: SearchRequest, metadata: any): Promise<PagedResults> {
async getSearchResults(query: SearchRequest, metadata: Metadata | undefined): Promise<PagedResults> {
const page: number = metadata?.page ?? 1
let request

Expand All @@ -165,18 +201,20 @@ export class BatoTo implements SearchResultsProviding, MangaProviding, ChapterPr
url: `${BATO_DOMAIN}/search?word=${encodeURI(query.title ?? '')}&page=${page}`,
method: 'GET'
})
// Tag Search
// Tag Search
} else {
request = App.createRequest({
url: `${BATO_DOMAIN}/browse`,
method: 'GET',
param: `?genres=${query?.includedTags?.map((x: Tag) => x.id)[0]}&page=${page}`
url: `${BATO_DOMAIN}/browse?genres=${query?.includedTags?.map((x: Tag) => x.id)[0]}&page=${page}`,
method: 'GET'
})
}

const langSearchFilter: boolean = await this.stateManager.retrieve('language_search_filter') ?? false
const langs: string[] = langSearchFilter ? await this.stateManager.retrieve('languages') : BTLanguages.getDefault()

const response = await this.requestManager.schedule(request, 1)
const $ = this.cheerio.load(response.data as string)
const manga = parseSearch($)
const manga = parseSearch($, langSearchFilter, langs)

metadata = !isLastPage($) ? { page: page + 1 } : undefined
return App.createPagedResults({
Expand Down
13 changes: 6 additions & 7 deletions src/BatoTo/BatoToHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class BTLanguagesClass {
Languages: Language[] = [
{
name: 'English',
BTCode: 'en',
BTCode: 'en,en_us',
lang: '🇬🇧',
default: true
},
Expand Down Expand Up @@ -395,11 +395,6 @@ class BTLanguagesClass {
BTCode: 'hr',
lang: '🇭🇷'
},
{
name: 'English (United States)',
BTCode: 'en_us',
lang: '🇺🇸'
},
{
name: 'Esperanto',
BTCode: 'eo',
Expand Down Expand Up @@ -706,4 +701,8 @@ class BTLanguagesClass {
}
}

export const BTLanguages = new BTLanguagesClass()
export const BTLanguages = new BTLanguagesClass()

export interface Metadata {
page: number;
}
8 changes: 4 additions & 4 deletions src/BatoTo/BatoToParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {
BTLanguages
} from './BatoToHelper'

const CryptoJS = require('./external/crypto-js.min.js') // 4.1.1

import * as CryptoJS from './external/crypto-js.min.js' // 4.2.0

Check failure on line 17 in src/BatoTo/BatoToParser.ts

View workflow job for this annotation

GitHub Actions / Bundle and Publish Sources (18.x)

Could not find a declaration file for module './external/crypto-js.min.js'. '/home/runner/work/community-extensions/community-extensions/src/BatoTo/external/crypto-js.min.js' implicitly has an 'any' type.
import entities = require('entities')

export const parseMangaDetails = ($: CheerioStatic, mangaId: string): SourceManga => {
Expand Down Expand Up @@ -246,17 +245,18 @@ export const parseTags = (): TagSection[] => {
return tagSections
}

export const parseSearch = ($: CheerioStatic): PartialSourceManga[] => {
export const parseSearch = ($: CheerioStatic, langFilter: boolean, langs: string[]): PartialSourceManga[] => {
const mangas: PartialSourceManga[] = []
for (const obj of $('.item', '#series-list').toArray()) {
const id = $('.item-cover', obj).attr('href')?.replace('/series/', '')?.trim().split('/')[0] ?? ''
const title: string = $('.item-title', obj).text() ?? ''
const btcode = $('em', obj).attr('data-lang')
const btcode = $('em', obj).attr('data-lang') ?? 'en,en_us'
const lang: string = btcode ? BTLanguages.getLangCode(btcode) : '🇬🇧'
const subtitle = lang + ' ' + $('.visited', obj).text().trim()
const image = $('img', obj).attr('src') ?? ''

if (!id || !title) continue
if (langFilter && !langs.includes(btcode)) continue

mangas.push(App.createPartialSourceManga({
image: image,
Expand Down
80 changes: 80 additions & 0 deletions src/BatoTo/BatoToSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
DUIButton,
DUINavigationButton,
SourceStateManager
} from '@paperback/types'

import {
BTLanguages
} from './BatoToHelper'

const getLanguages = async (stateManager: SourceStateManager): Promise<string[]> => {
return (await stateManager.retrieve('languages') ?? BTLanguages.getDefault())
}

const getLanguageHomeFilter = async (stateManager: SourceStateManager): Promise<boolean> => {
return (await stateManager.retrieve('language_home_filter') ?? false)
}

const getLanguageSearchFilter = async (stateManager: SourceStateManager): Promise<boolean> => {
return (await stateManager.retrieve('language_search_filter') ?? false)
}

export const languageSettings = (stateManager: SourceStateManager): DUINavigationButton => {
return App.createDUINavigationButton({
id: 'language_settings',
label: 'Language Settings',
form: App.createDUIForm({
sections: async () => [
App.createDUISection({
id: 'content',
footer: 'When enabled, mangas will be filtered by the selected languages.',
isHidden: false,
rows: async () => [
App.createDUISelect({
id: 'languages',
label: 'Languages',
options: BTLanguages.getBTCodeList(),
labelResolver: async (option) => BTLanguages.getName(option),
value: App.createDUIBinding({
get: () => getLanguages(stateManager),
set: async (newValue) => await stateManager.store('languages', newValue)
}),
allowsMultiselect: true
}),
App.createDUISwitch({
id: 'language_home_filter',
label: 'Filter Homepage Language',
value: App.createDUIBinding({
get: () => getLanguageHomeFilter(stateManager),
set: async (newValue) => await stateManager.store('language_home_filter', newValue)
})
}),
App.createDUISwitch({
id: 'language_search_filter',
label: 'Filter Search Language',
value: App.createDUIBinding({
get: () => getLanguageSearchFilter(stateManager),
set: async (newValue) => await stateManager.store('language_search_filter', newValue)
})
})
]
})
]
})
})
}

export const resetSettings = (stateManager: SourceStateManager): DUIButton => {
return App.createDUIButton({
id: 'reset',
label: 'Reset to Default',
onTap: async () => {
await Promise.all([
stateManager.store('languages', BTLanguages.getDefault()),
stateManager.store('language_home_filter', false),
stateManager.store('language_search_filter', false)
])
}
})
}
2 changes: 1 addition & 1 deletion src/BatoTo/external/crypto-js.min.js

Large diffs are not rendered by default.

0 comments on commit d0e0aed

Please sign in to comment.