UNPKG

bedetheque-scraper

Version:

NodeJS script to scrap the entire database of dbgest.com / bedetheque.com (approx. 260.000+ albums)

59 lines (48 loc) 1.66 kB
import {Album} from './album'; import {Utils} from './utils'; export class Serie { serieId: number; serieTitle: string; numberOfAlbums: number; serieCover!: string | null; serieCoverWidth: number | null; serieCoverHeight: number | null; albumsId!: number[]; voteAverage!: number; voteCount!: number; recommendationsId: number[]; constructor($: CheerioStatic) { this.serieId = parseInt($('.idbel').text(), 10); this.serieTitle = $('h1 a').text(); const match = $('.serie-info').text().match(/Tomes? :([0-9]+)/); this.numberOfAlbums = match ? parseInt(match[1], 10) : 0; this.recommendationsId = this.getRecommendationsId($); } public async addAlbumsInfo(albums: Album[]) { this.albumsId = albums.map(album => album.albumId); [this.voteAverage, this.voteCount] = this.getVoteAverage(albums); await Promise.all(albums.map(a => a.getImageDimensions())); if (albums.length == 0) return; this.serieCover = albums[0].imageCover; this.serieCoverWidth = albums[0].imageCoverWidth; this.serieCoverHeight = albums[0].imageCoverHeight; } private getVoteAverage(albums: Album[]) { let voteAverage = 0; let voteCount = 0; albums.forEach((album) => { voteAverage += album.voteAverage * album.voteCount; voteCount += album.voteCount; }); if (voteCount === 0) { return [0, 0]; } return [Math.floor(voteAverage / voteCount * 10) / 10, voteCount]; } private getRecommendationsId($: CheerioStatic) { return $('.alire li a') .map((i, elem) => $(elem).attr('href')) .get() .map(url => Utils.urlToSerieID(url)); } }