Skip to content

Commit

Permalink
🎨 Add minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Jun 7, 2024
1 parent f7dd881 commit 8d10356
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/modules/misc/misc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class MiscService{
return response.data as Buffer;
}

async convertThumbnail(url: string){
async convertWebtoonThumbnail(url: string){
const webpImage: Buffer = await this.downloadImage(url);
return await sharp(webpImage).resize(240, 240, {
fit: "cover",
Expand Down
4 changes: 3 additions & 1 deletion src/modules/webtoon/update/update.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Controller, Logger, Post, UseGuards} from "@nestjs/common";
import {UpdateService} from "./update.service";
import {ApiBearerAuth, ApiTags} from "@nestjs/swagger";
import {ApiBearerAuth, ApiResponse, ApiTags} from "@nestjs/swagger";
import {AdminGuard} from "../admin/guard/admin.guard";
import {HttpStatusCode} from "axios";


@Controller("update")
Expand All @@ -17,6 +18,7 @@ export class UpdateController{

@Post("webtoons/thumbnails")
@ApiBearerAuth()
@ApiResponse({status: HttpStatusCode.Created, description: "Thumbnails updated"})
async updateThumbnails(): Promise<void>{
this.updateService.updateThumbnails().then(() => this.logger.log("Thumbnails updated"));
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/webtoon/update/update.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class UpdateService{
for(const webtoon of dbWebtoons){
this.logger.debug(`Updating thumbnail for webtoon ${webtoon.title} (${webtoon.language})`);
const cachedWebtoon: CachedWebtoonModel = this.webtoonParser.findWebtoon(webtoon.title, webtoon.language);
const thumbnail: Buffer = await this.miscService.convertThumbnail(cachedWebtoon.thumbnail);
const thumbnail: Buffer = await this.miscService.convertWebtoonThumbnail(cachedWebtoon.thumbnail);
const sum: string = this.webtoonDatabaseService.saveImage(thumbnail);
// Check if thumbnail already exists
let dbThumbnail = await tx.images.findFirst({
Expand Down
2 changes: 1 addition & 1 deletion src/modules/webtoon/webtoon/webtoon-downloader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class WebtoonDownloaderService{

async downloadWebtoon(webtoon: WebtoonModel): Promise<WebtoonDataModel>{
const downloadPromises: Promise<Buffer>[] = [];
downloadPromises.push(this.miscService.convertThumbnail(webtoon.thumbnail));
downloadPromises.push(this.miscService.convertWebtoonThumbnail(webtoon.thumbnail));
downloadPromises.push(this.miscService.downloadImage(webtoon.banner.background));
downloadPromises.push(this.miscService.downloadImage(webtoon.banner.top));
downloadPromises.push(this.miscService.downloadImage(webtoon.banner.mobile));
Expand Down
9 changes: 0 additions & 9 deletions src/modules/webtoon/webtoon/webtoon-parser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,17 @@ export class WebtoonParserService{

private async getWebtoonsFromLanguage(language: string): Promise<CachedWebtoonModel[]>{
const languageWebtoons: CachedWebtoonModel[] = [];

const promises: Promise<CachedWebtoonModel[]>[] = [];
for (const genre of Object.values(WebtoonGenres))
promises.push(this.getWebtoonsFromGenre(language, genre));
const genreResults = await Promise.all(promises);
for (const webtoons of genreResults)
languageWebtoons.push(...webtoons);

// for (const genre of Object.values(WebtoonGenres))
// languageWebtoons.push(...await this.getWebtoonsFromGenre(language, genre));

return this.removeDuplicateWebtoons(languageWebtoons);
}

private async getWebtoonsFromGenre(language: string, genre: string): Promise<CachedWebtoonModel[]>{
// console.log(language, genre);
const mobileThumbnails = await this.getWebtoonThumbnailFromGenre(language, genre);
// console.log(mobileThumbnails);
const url = `https://www.webtoons.com/${language}/genres/${genre}`;
const response = await this.miscService.getAxiosInstance().get(url);
const document = new JSDOM(response.data).window.document;
Expand Down Expand Up @@ -108,7 +101,6 @@ export class WebtoonParserService{
private async getWebtoonThumbnailFromGenre(language: string, genre: string): Promise<Record<string, string>[]>{
const mobileThumbnails: Record<string, string>[] = [];
const mobileUrl = `https://www.webtoons.com/${language}/genres/${genre}`.replace("www.webtoons", "m.webtoons") + "?webtoon-platform-redirect=true";
// console.log(mobileUrl);
const mobileResponse = await this.miscService.getAxiosInstance().get(mobileUrl, {
headers: {
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
Expand All @@ -117,7 +109,6 @@ export class WebtoonParserService{
const mobileDocument = new JSDOM((mobileResponse).data).window.document;
const className = `genre_${genre.toUpperCase()}_list`;
const wList = mobileDocument.querySelector(`ul.${className}`)?.querySelectorAll("li");
// console.log("wList", wList);
if(!wList) return [];
for(const li of wList){
const webtoonName = li.querySelector("a")?.querySelector("div.info")?.querySelector("p.subj span")?.textContent;
Expand Down

0 comments on commit 8d10356

Please sign in to comment.