tmal
Version:
tuna's myanimelist parser
36 lines (35 loc) • 1.7 kB
JavaScript
const cheerio = require('cheerio');
const _ = require('lodash');
const fetch = require('../utils').fetch;
const parseSidebar = require('../utils').parseSidebar;
const getPersonPics = (href) => {
return fetch(href+'/pictures').then(body => {
const $ = cheerio.load(body);
const images = $('a[href*="images/voiceactors"]');
return _.map(images,(t) => {
return $(t).find('img').attr('src');
});
});
};
module.exports.getPerson = (id,href) => {
return fetch(href).then(body => {
const $ = cheerio.load(body);
const name = $('h1').text().trim();
return getPersonPics(href).then(pictures => {
return {
image_url : $('a[href*="people/'+id+'/'+_.split(href,'/')[5]+'/pictures"]').find('img').attr('src'),
name : (name.indexOf(',') > -1) ? _.split(name,',')[0]+' '+_.split(name,',')[1] : name,
given_name : (parseSidebar($, 'Given name',false)) ? parseSidebar($, 'Given name',false).replace('Given name:' ,'') : null ,
family_name : null, /* TODO: parse this*/
website : $('span:contains("Website:")').next('a').attr('href'),
birthdate : (parseSidebar($, 'Birthday',false)) ? parseSidebar($, 'Birthday',false).replace('Birthday:','').trim() : null,
about : null, /* TODO: parse this also */
alternative_names : (parseSidebar($, 'Alternate names',false)) ? parseSidebar($, 'Alternate names',false).replace('Birthday:','').trim() : null,
pictures : pictures
/*
* TODO: add more info
* */
}
});
});
};