raawaa / jav-scrapy

批量抓取AV磁链或封面的苦劳力
1.05k stars 211 forks source link

添加下载视频截图的方法 #24

Closed ghost closed 7 years ago

ghost commented 7 years ago

小封面下载的方法已经除掉,方法可见#23,在 getItemPagegetItemMagnet(link, meta, callback);后面添加了下载当前番号的视频截图的功能:

// 所有截图link
var snapshots = []
$('a.sample-box').each(function (i, e) {
    let $e = $(e);

    snapshots.push($e.attr("href"))
})
getSnapshots(link, snapshots);

getSnapshots 方法:

function getSnapshots(link, snapshots) {
    // https://pics.dmm.co.jp/digital/video/118abp00454/118abp00454jp-1.jpg
    for (var i = 0; i < snapshots.length; i++){
        getSnapshot(link, snapshots[i])
    }
}

getSnapshot 方法:

function getSnapshot(link, snahpshotLink) {
    let fanhao = link.split('/').pop();
    let itemOutput = output + "/" + fanhao
    mkdirp.sync(itemOutput);

    let snapshotName = snahpshotLink.split('/').pop();
    let fileFullPath = path.join(itemOutput, snapshotName)
    fs.access(fileFullPath, fs.F_OK, function (err) {
        if (err) {
            var snapshotFileStream = fs.createWriteStream(fileFullPath + '.part');
            var finished = false;
            request.get(snahpshotLink)
                .on('end', function () {
                    if (!finished) {
                        fs.renameSync(fileFullPath + '.part', fileFullPath);
                        finished = true;
                        // console.error(('[' + fanhao + ']').green.bold.inverse + '[截图]'.yellow.inverse, fileFullPath);
                    }
                })
                .on('error', function (err) {
                    if (!finished) {
                        finished = true;
                        // console.error(('[' + fanhao + ']').red.bold.inverse + '[截图]'.yellow.inverse, err.message.red);
                        errorCount++;
                    }
                })
                .pipe(snapshotFileStream);
        } else {
            // console.log(('[' + fanhao + ']').green.bold.inverse + '[截图]'.yellow.inverse, 'file already exists, skip!'.yellow);
        }
    })
}

由于本人对nodejs不熟,所以添加的方法是在作者原来的方法上改的。

raawaa commented 7 years ago

欢迎提交Pull request. 💋

raawaa commented 7 years ago

已在 commit 527343638673280534544cc649fbf2db75eb8062 中实现