-
-
Save Dragory/51892a302c38f2c8c7df21b556193d8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const fs = require('fs') | |
const cheerio = require('cheerio') | |
const request = require('request') | |
var Parent = this | |
Parent.Anime = function Anime (url) { | |
return new Promise((resolve, reject) => { | |
request(url, (err, res, body) => { // Send HTTP request to ask for | |
if (err) { reject(err); return } // Error handling | |
let $ = cheerio.load(body) | |
let element = {} | |
element.cover = $('.js-scrollfix-bottom > div > a').first().children('img').attr('src') | |
element.name = $('span[itemprop="name"]').first().text() | |
element.type = $('.dark_text:contains("Type")').first().siblings('a').text() | |
element.episodes = $('.dark_text:contains("Episode")').parent().text().replace(/\D+/g, '') | |
element.status = $('.dark_text:contains("Status")').parent().children().remove().end().text().trim() | |
element.aired = $('.dark_text:contains("Aired")').parent().children().remove().end().text().trim() | |
element.premiered = $('.dark_text:contains("Premiered")').first().siblings('a').text() | |
element.broadcast = $('.dark_text:contains("Broadcast")').parent().children().remove().end().text().trim() | |
element.producers = [] | |
$('.dark_text:contains("Producers")').siblings('a').each(function(i, elem){ | |
element.producers.push($(this).text()) | |
}) | |
element.licensors = $('.dark_text:contains("Licensors")').first().siblings('a').text() | |
element.studios = $('.dark_text:contains("Studios")').first().siblings('a').text() | |
element.source = $('.dark_text:contains("Source")').parent().children().remove().end().text().trim() | |
element.genres = [] | |
$('.dark_text:contains("Genres")').first().siblings('a').each(function(elem, i) { | |
element.genres.push($(this).text()) | |
}) | |
element.duration = $('.dark_text:contains("Duration")').parent().children().remove().end().text().trim() | |
element.rating = $('.dark_text:contains("Rating")').parent().children().remove().end().text().trim() | |
element.score = $('.score').text().trim() | |
element.ranked = $('.dark_text:contains("Ranked")').parent().children().remove().end().text().trim() | |
element.popularity = $('.dark_text:contains("Popularity")').parent().children().remove().end().text().trim() | |
element.related = [] | |
$('.anime_detail_related_anime > tbody > tr > td').children('a').each(function(elem, i) { | |
element.related.push($(this).text()) | |
}) | |
resolve(element) | |
}) | |
}) | |
} | |
Parent.search = function search(query) { | |
var results = [] | |
return new Promise((resolve, reject) => { | |
request(encodeURI('https://myanimelist.net/anime.php?q=' + query), (err, res, body) => { | |
if(err){reject(err); return} | |
let $ = cheerio.load(body) | |
const promises = $('.fw-b.fl-l') | |
.map(function(i, elem) { | |
return new Parent.Anime($(this).attr('href')); | |
}) | |
.get(); | |
resolve(Promise.all(promises)); | |
}) | |
}) | |
} | |
this.search('some search query').then(function(val){ | |
console.log(JSON.stringify(val, null, 2)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment