Created
October 31, 2017 14:00
-
-
Save anonymous/586f246455c3b3392cc2b7f72c34c1a1 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) | |
$('.fw-b.fl-l').each(function(i, elem){ | |
// here i want to create elements | |
var newElement = new Parent.Anime($(this).attr('href')).then(function(value){ | |
results.push(value) | |
console.log(results) // returns what it should | |
}) | |
}) | |
resolve(results) // returns [] | |
}) | |
}) | |
} | |
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