feat: abstract classes and destroy subscriptions

This commit is contained in:
2024-07-08 23:24:03 +03:00
parent 9d2373a298
commit 6833105604
13 changed files with 191 additions and 100 deletions

View File

@@ -0,0 +1,19 @@
export function ParserDecorator({
site_name,
url,
nsfw = false,
site_id = 1,
}: {
site_name: string;
url: string;
nsfw?: boolean;
site_id?: number;
}) {
// eslint-disable-next-line @typescript-eslint/ban-types
return function (constructor: Function) {
constructor.prototype.site_name = site_name;
constructor.prototype.nsfw = nsfw;
constructor.prototype.api_url = url;
constructor.prototype.site_id = site_id;
};
}

View File

@@ -0,0 +1,18 @@
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
@Injectable({ providedIn: "root" })
export abstract class Parser {
constructor(protected http: HttpClient) {}
protected abstract url: string;
abstract searchManga(query: string): Observable<object>;
abstract getDetails(slug_url: string): Observable<object>;
abstract getChapters(url: string): Observable<object>;
abstract getChapter(url: string, chapter: string, volume: string): Observable<object>;
}

View File

@@ -1,7 +1,6 @@
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable, catchError, map, throwError } from "rxjs";
import { ESiteUrls } from "../urls";
import { Parser } from "../parser";
import { IRulibChapterResult } from "./rulib.chapter.dto";
import { IRulibChaptersResult } from "./rulib.chapters.dto";
import { IRulibDetailResult } from "./rulib.detail.dto";
@@ -11,17 +10,20 @@ import { IRulibSearchResult } from "./rulib.search.dto";
@Injectable({
providedIn: "root",
})
export class LibSocialParserService {
private readonly url = ESiteUrls.LIB_SOCIAL;
constructor(private readonly http: HttpClient) {}
export class LibSocialParserService extends Parser {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected url = (this as any).api_url;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private site_id = (this as any).site_id;
get imageServer() {
return "https://img33.imgslib.link";
}
searchManga(query: string): Observable<IRulibSearchResult> {
return this.http
.get(`${this.url}/api/manga?fields[]=rate_avg&fields[]=rate&q=${query}&site_id[]=1`)
.get(
`${this.url}/api/manga?fields[]=rate_avg&fields[]=rate&q=${query}&site_id[]=${this.site_id}`,
)
.pipe(
map((data: object) => {
return data as IRulibSearchResult;

View File

@@ -0,0 +1,10 @@
import { Injectable } from "@angular/core";
import { ParserDecorator } from "../decorators/parser.decorator";
import { ESiteUrls } from "../urls";
import { LibSocialParserService } from "./lib.social.parser.service";
@Injectable({
providedIn: "root",
})
@ParserDecorator({ site_name: "MangaLib", url: ESiteUrls.MANGALIB, site_id: 1 })
export class MangalibParserService extends LibSocialParserService {}

View File

@@ -1,3 +1,4 @@
export enum ESiteUrls {
LIB_SOCIAL = "https://api.lib.social",
MANGALIB = "https://api.mangalib.me",
}

View File

@@ -1,25 +1,27 @@
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { BehaviorSubject, Observable, map } from "rxjs";
import { LibSocialParserService } from "./parsers/rulib/lib.social.parser.service";
import { MangalibParserService } from "./parsers/rulib/mangalib.parser.service";
import { IRulibChapterResult } from "./parsers/rulib/rulib.chapter.dto";
import { IRulibChaptersResult } from "./parsers/rulib/rulib.chapters.dto";
import { IRulibDetailResult } from "./parsers/rulib/rulib.detail.dto";
import { Datum } from "./parsers/rulib/rulib.search.dto";
import { HttpClient } from "@angular/common/http";
import { Datum, IRulibSearchResult } from "./parsers/rulib/rulib.search.dto";
@Injectable({ providedIn: "root" })
export class SearchService {
private itemsTerm = new BehaviorSubject<Datum[]>([]);
currentItemsTerm = this.itemsTerm.asObservable();
constructor(
private parser: LibSocialParserService,
private parser: MangalibParserService,
private http: HttpClient,
) {}
search(query: string) {
this.parser.searchManga(query).subscribe((data) => {
this.itemsTerm.next(data.data);
});
search(query: string): Observable<IRulibSearchResult> {
return this.parser.searchManga(query).pipe(
map((data) => {
return data;
}),
);
}
getDetails(slug_url: string): Observable<IRulibDetailResult> {
@@ -50,12 +52,11 @@ export class SearchService {
return this.parser.imageServer;
}
getImageData(imageUrl: string): Observable<Uint8Array>{
return this.http.get(imageUrl, {responseType: 'arraybuffer'}).pipe(
getImageData(imageUrl: string): Observable<Uint8Array> {
return this.http.get(imageUrl, { responseType: "arraybuffer" }).pipe(
map((arrayBuffer: ArrayBuffer) => {
return new Uint8Array(arrayBuffer);
})
}),
);
}
}