toonviewer.js
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var express = require('express');
var router = express.Router();
var request = require('request');
var cheerio = require('cheerio');
var async = require('async');
function updateLastWebtoon(toon_index, user_id, num,cb){
var sql_query = "UPDATE user_toon_relation SET last=? WHERE user_id=? && toon_index=?";
connection.query(sql_query,[num, user_id,toon_index],function(err){
if(!err){
cb();
}
});
}
function getToonImages(toon_index, num, cb){
var imageList = [];
var toonUrl = "http://comic.naver.com/webtoon/detail.nhn?titleId=" + toon_index + "&no=" + num;
request(toonUrl, function(err, res, html){
if(!err){
var $ = cheerio.load(html);
var p = Promise.resolve();
var eachs = $("div.wt_viewer > img").each(function(index, element) {
p = p.then(function() {
var toonHref = $(element).attr('src');
imageList.push(toonHref);
});
});
p.then(function(){
cb(imageList);
});
}else{
console.log("웹툰 이미지 못가져왔습니다.");
}
});
}
router.get('/:toon_index/:num',function(req,res){
if(!req.isAuthenticated()){
res.redirect('/');
return;
}
updateLastWebtoon(req.params.toon_index, req.user.user_id, req.params.num, function(){
var toonUrl = "http://comic.naver.com/webtoon/detail.nhn?titleId=" + req.params.toon_index + "&no=" + req.params.num;
res.redirect(toonUrl);
getToonImages(req.params.toon_index, req.params.num, function(imageList){
res.render('toonviewer',{
title: "zzz",
images: imageList
});
});
})
});
module.exports = router;