Guckkasten.js 2.36 KB
var request = require('request');
var cheerio = require('cheerio');

function crawl_guckkasten(callback){
    var n = 0;
    var guckkasten = [];
    crawl(function(json_album,json_year,json_track){
        n++
        if (n == 10) {
            var j;
            var a, y, t;
            for (var i = 1; i < json_year.length; i++) {
                y = json_year[i];
                a = json_album[i];
                t = json_track[i]
                j = i - 1;
                while (j >= 0 && json_year[j] > y) {
                    json_album[j + 1] = json_album[j];
                    json_year[j + 1] = json_year[j];
                    json_track[j + 1] = json_track[j];
                    j--;
                }
                json_album[j + 1] = a;
                json_year[j + 1] = y;
                json_track[j + 1] = t;
            }
            for(var i = 0; i<json_album.length; i++){
                json_album[i] = (i+1)+'. ' +json_album[i]; 
            }
            guckkasten.push({json_album, json_year, json_track});
            callback(guckkasten);
        }
    })
}

function crawl(callback){
    var url;
    let json_album = [], json_year = [], json_track = [];
    var track;
    var year;
    var album;
    for (var i = 1; i <= 13; i++) {
        url = 'http://www.interparkenter.com/guckkasten/Album/' + i;
        if (!(i > 9 && i < 13)) {
            request(url, function (err, res, body) {
                const $ = cheerio.load(body);

                var track_num = 0;
                var album_track = '';
                $('#divDetail > div.disco_left_box > div.track_area > div > ol').find('li').each(function () {
                    track_num++;
                    track = $('#divDetail > div.disco_left_box > div.track_area > div > ol > li:nth-child(' + track_num + ')').text()
                    album_track += track + '\n';
                })
                json_track.push(album_track);

                year = $('#divDetail > div.disco_right_box > div.Introduction > p.txt_release').text();
                year = year.substr(3);
                json_year.push(year);

                album = $('#divDetail > div.disco_right_box > div.Introduction > p.txt_title').text();
                json_album.push(album);
                callback(json_album,json_year,json_track);
            });
        }
    }
}


exports.crawl_guckkasten = crawl_guckkasten;