Skip to content

Commit

Permalink
Merge pull request #147 from koikiss-dev/manganelo-filter-bug-fix
Browse files Browse the repository at this point in the history
fix(ManganeloFilter): error when genres query is not provided
  • Loading branch information
koikiss-dev authored Feb 10, 2024
2 parents 74043a0 + 273224a commit 572744d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/scraper/sites/manga/manganelo/ManganatoManagerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { ManganatoAdvancedSearchURLManager } from "./managers/ManganatoURLManage

export class ManganatoManagerUtils {
private static instance: ManganatoManagerUtils;
readonly url: ManganatoAdvancedSearchURLManager = new ManganatoAdvancedSearchURLManager();
readonly url: ManganatoAdvancedSearchURLManager =
new ManganatoAdvancedSearchURLManager();

private constructor() {}

static get Instance() {
if (!this.instance) this.instance = new ManganatoManagerUtils();
if (!ManganatoManagerUtils.instance)
ManganatoManagerUtils.instance = new ManganatoManagerUtils();

return this.instance;
return ManganatoManagerUtils.instance;
}
}
12 changes: 9 additions & 3 deletions src/scraper/sites/manga/manganelo/ManganatoTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const manganatoOrderByOptionsList = ["topview", "newest", "az"] as const;
export type manganatoOrderByOptions = typeof manganatoOrderByOptionsList[number];
export type manganatoOrderByOptions =
(typeof manganatoOrderByOptionsList)[number];

export interface IManganatoFilterParams {
/**
Expand All @@ -13,7 +14,12 @@ export interface IManganatoFilterParams {
genres: string;
/** Results page */
page: number;
};
}

export type ManganatoFilterURLParams =
| keyof Omit<IManganatoFilterParams, "genres">
| "g_i"
| "s";

export const manganatoGenreList = {
action: 2,
Expand Down Expand Up @@ -57,5 +63,5 @@ export const manganatoGenreList = {
manhua: 44,
isekai: 45,
pornographic: 47,
erotica: 48
erotica: 48,
} as const;
43 changes: 30 additions & 13 deletions src/scraper/sites/manga/manganelo/managers/ManganatoURLManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { URLSearchParams } from "url";
import { IManganatoFilterParams, manganatoGenreList, manganatoOrderByOptions, manganatoOrderByOptionsList } from "../ManganatoTypes";
import {
IManganatoFilterParams,
ManganatoFilterURLParams,
manganatoGenreList,
manganatoOrderByOptions,
manganatoOrderByOptionsList,
} from "../ManganatoTypes";
import { ManganatoManager } from "./ManganatoManager";

export class ManganatoAdvancedSearchURLManager extends ManganatoManager {
Expand All @@ -11,9 +17,9 @@ export class ManganatoAdvancedSearchURLManager extends ManganatoManager {
}

private processGenres(genresArray: string[]): number[] {
let arrGenerated: number[] = [];
const arrGenerated: number[] = [];

for (let genre of genresArray) {
for (const genre of genresArray) {
if (manganatoGenreList[genre.toLowerCase()])
arrGenerated.push(manganatoGenreList[genre.toLowerCase()]);
else continue;
Expand All @@ -27,28 +33,39 @@ export class ManganatoAdvancedSearchURLManager extends ManganatoManager {
}

private processStatus(status: unknown) {
return (typeof status === "string" && (status.toLowerCase() === "ongoing" || status.toLowerCase() === "completed"))
return typeof status === "string" &&
(status.toLowerCase() === "ongoing" ||
status.toLowerCase() === "completed")
? status
: "";
}

private processOrderBy(order: unknown) {
return (typeof order === "string" && manganatoOrderByOptionsList.includes(order.toLowerCase() as manganatoOrderByOptions))
return typeof order === "string" &&
manganatoOrderByOptionsList.includes(
order.toLowerCase() as manganatoOrderByOptions,
)
? order
: "";
}

generate(params: IManganatoFilterParams) {
const splitted = this.splitGenresToArray(params.genres);
const processed = this.processGenres(splitted);

const urlParams = new URLSearchParams({
generate(params: Partial<IManganatoFilterParams>) {
const urlParamsObject = {
s: "all",
g_i: processed.length ? this.formatGenres(processed) : "",
g_i: "",
sts: this.processStatus(params.sts),
orby: this.processOrderBy(params.orby),
page: params.page ? params.page.toString() : ""
});
page: params.page ? params.page.toString() : "",
} satisfies Record<ManganatoFilterURLParams, string>;

if (params.genres) {
const splitted = this.splitGenresToArray(params.genres);
const processed = this.processGenres(splitted);

urlParamsObject.g_i = this.formatGenres(processed);
}

const urlParams = new URLSearchParams(urlParamsObject);

return `${this.baseURL}?${urlParams.toString()}`;
}
Expand Down

0 comments on commit 572744d

Please sign in to comment.