Yorusika.js 1.95 KB
var request = require('request');
var cheerio = require('cheerio');

function crawl_yorusika(url, callback){
    let json_album = [], json_year = [], json_track = [], json_image = [];
    let yorusika = [];
    var track;
    var year;
    request(url, function (err, res, body) {
        const $ = cheerio.load(body);
        var index;
        var album_num = 0;
        console.log($('.discography_contents').length);
        for (var i = $('.discography_contents').length; i >=1; i--) {
            year = $('body > article > section > div > div:nth-child('+i+') > div.discography_text > div.discography_date').text();
            year = year.substr(0,year.length-7);
            json_year.push(year);

            if(i == 3){
                track = $('body > article > section > div > div:nth-child('+i+') > div.discography_text > div.discography_description').text();
                track = track.substr(6);
                track = track.substr(0,track.length-22);
            }
            else{
                track = $('body > article > section > div > div:nth-child('+i+') > div.discography_text > div.discography_description').text();
                track = track.substr(6);
                track = track.substr(0,track.length-7);
            }
            json_track.push(track);

            var check = false
            album_num++;
            var album_name = '';
            album = $('body > article > section > div > div:nth-child('+i+') > div.discography_text > div.discography_title').text();
            for(var j = 0; j<album.length;j++){
                if(album[j] == '「')
                    check = true;
                if(check == true)
                    album_name += album[j];
            }
            album_name = album_num + '. ' + album_name
            json_album.push(album_name);
           
        }      

        yorusika.push({ json_album, json_year, json_track});
        callback(yorusika);
    });
}

exports.crawl_yorusika = crawl_yorusika;