Skip to content

Commit

Permalink
fix: ComicK provider (#350)
Browse files Browse the repository at this point in the history
* fix: `ComicK` provider

* remove unused import

* add `User-Agent`
  • Loading branch information
hngngn authored Jul 5, 2023
1 parent 340919c commit 8ce5610
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/providers/manga/comick.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios, { AxiosError } from 'axios';

import { IMangaChapterPage, IMangaInfo, IMangaResult, ISearch, MangaParser, MediaStatus } from '../../models';

class ComicK extends MangaParser {
Expand All @@ -10,20 +9,29 @@ class ComicK extends MangaParser {

private readonly apiUrl = 'https://api.comick.app';

private _axios() {
return axios.create({
baseURL: this.apiUrl,
headers: {
"User-Agent": "Mozilla/5.0",
}
});
}

/**
* @description Fetches info about the manga
* @param mangaId Comic slug
* @returns Promise<IMangaInfo>
*/
override fetchMangaInfo = async (mangaId: string): Promise<IMangaInfo> => {
try {
const req = await axios.get(`${this.apiUrl}/comic/${mangaId}`);
const req = await this._axios().get(`/comic/${mangaId}`);
const data: Comic = req.data.comic;

const links = Object.values(data.links ?? []).filter((link) => link !== null);

const mangaInfo: IMangaInfo = {
id: String(data.id),
id: data.hid,
title: data.title,
altTitles: data.md_titles ? data.md_titles.map((title) => title.title) : [],
description: data.desc,
Expand Down Expand Up @@ -56,13 +64,13 @@ class ComicK extends MangaParser {
};

/**
*
*
* @param chapterId Chapter ID (HID)
* @returns Promise<IMangaChapterPage[]>
*/
override fetchChapterPages = async (chapterId: string): Promise<IMangaChapterPage[]> => {
try {
const { data } = await axios(`${this.apiUrl}/chapter/${chapterId}`);
const { data } = await this._axios().get(`/chapter/${chapterId}`);

const pages: { img: string; page: number }[] = [];

Expand Down Expand Up @@ -94,16 +102,16 @@ class ComicK extends MangaParser {
if (limit * (page - 1) >= 10000) throw new Error('not enough results');

try {
const res = await axios.get(
`${this.apiUrl}/v1.0/search?q=${encodeURIComponent(query)}&limit=${limit}&page=${page}`
const req = await this._axios().get(
`/v1.0/search?q=${encodeURIComponent(query)}&limit=${limit}&page=${page}`
);

const results: ISearch<IMangaResult> = {
currentPage: page,
results: [],
};

const data: SearchResult[] = res.data;
const data: SearchResult[] = await req.data;

for (const manga of data) {
let cover:Cover | string | null = manga.md_covers ? manga.md_covers[0] : null;
Expand Down Expand Up @@ -133,19 +141,19 @@ class ComicK extends MangaParser {
page = 1;
}
const comicId = await this.getComicId(mangaId);
const data = await axios(`${this.apiUrl}/comic/${comicId}/chapter?page=${page}`);
return data.data.chapters;
const req = await this._axios().get(`/comic/${comicId}/chapters?page=${page}`);
return req.data.chapters;
};

/**
* @description Fetches the comic ID from the slug
* @description Fetches the comic HID from the slug
* @param id Comic slug
* @returns Promise<number> -1 if not found
* @returns Promise<string> empty if not found
*/
private async getComicId(id:string): Promise<number> {
const req = await axios(`${this.apiUrl}/comic/${id}`);
private async getComicId(id:string): Promise<string> {
const req = await this._axios().get(`/comic/${id}`);
const data:Comic = req.data["comic"];
return data ? data.id : -1;
return data ? data.hid : '';
}
}

Expand Down Expand Up @@ -186,7 +194,7 @@ interface MDTitle {
}

interface Comic {
id: number;
hid: string;
title: string;
country: string;
status: number;
Expand Down Expand Up @@ -264,7 +272,7 @@ interface ChapterData {
created_at: string;
updated_at: string;
up_count: number;
down_coount: number;
down_count: number;
group_name: string[];
hid: string;
md_groups: string[];
Expand Down

0 comments on commit 8ce5610

Please sign in to comment.