index.js
5.37 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var express = require('express');
var cheerio = require('cheerio');
var request = require('request');
var router = express.Router();
var mysql = require('mysql');
var passport = require('passport'),
KakaoStrategy = require('passport-kakao').Strategy;
passport.use(new KakaoStrategy({
clientID : '5cebedb504cf4d1aeeadff0b55f1589d',
callbackURL :'http://localhost:3000/auth/login/kakao/callback',
clientSecret : 'tUL5KhahqDPD8fvlSlbVdRY3C4WyblNq'
},
function(accessToken, refreshToken,params, profile, done){
//사용자 정보는 profile에
loginByThirdparty(accessToken, refreshToken, profile);
console.log("(!)로그인 : " + profile._json.id+"("+profile._json.properties.nickname +")");
return done(null, {
'user_id': profile._json.id,
'nickname': profile._json.properties.nickname
});
}
));
// kakao 로그인
router.get('/auth/login/kakao',
passport.authenticate('kakao',{state: "myStateValue"})
);
// kakao 로그인 연동 콜백
router.get('/auth/login/kakao/callback',
passport.authenticate('kakao', {
//session: false,
successRedirect: '/mytoons',
failureRedirect: '/'
})
);
function loginByThirdparty(accessToken, refreshToken, profile) {
var stmt_duplicated = 'INSERT INTO user(id) VALUES(?) ON DUPLICATE KEY UPDATE id=?;'
connection.query(stmt_duplicated, [profile._json.id,profile._json.id] , function (err, result) {
if (err) {
console.log("로그인 쿼리중 에러 : " + err);
} else {
console.log("로그인 DB처리 완료!");
}
});
}
router.get('/auth/logout/kakao',function (req,res) {
req.logout();
res.redirect('/');
})
allWebtoons = new Array();
function getLatestToon(titleid, day ,cb) {
var url = "http://comic.naver.com/webtoon/list.nhn?titleId=" + titleid+ "&weekday="+day;
console.log(url);
request(url, function (err, res, html) {
if (!err) {
var $ = cheerio.load(html);
var latestLink = 'http://comic.naver.com';
var latest;
var t = $('#content > table > tbody > tr > td.title > a').first().each(function () {
latestLink += $(this).attr('href');
setTimeout(function () {},100);
latest = latestLink.split('?')[1].split('&')[1].split('=')[1];
});
var latestImage;
$('#content > table > tbody > tr > td > a > img').first().each(function () {
latestImage = $(this).attr('src');
setTimeout(function () {},100);
});
cb({
latest: latest,
latestLink: latestLink,
latestImage: latestImage
});
} else {
console.log("최신화 못가져왔습니다.");
//throw err;
}
});
}
function getAllToons() {
var allWeeklyToonsUrl = "http://comic.naver.com/webtoon/weekday.nhn";
allWebtoonList = new Array();
request(allWeeklyToonsUrl,function (err, res, html) {
if(!err){
var $ = cheerio.load(html);
var p = Promise.resolve();
var eachs = $(".thumb").each(function (i) {
var week = $(this).parent().parent().prev().attr('class');
var webtoon_link = "http://comic.naver.com" + $(this).children().first().attr('href');
var thumb_link = $(this).children().first().children().first().attr('src');
var name = $(this).next().text();
var titleid = webtoon_link.split('?')[1].split('&')[0].split('=')[1];
var webtoon= {
toon_index: titleid,
name : name,
thum_link : thumb_link,
webtoon_link : webtoon_link,
week : week,
latest : 0
};
allWebtoonList.push(webtoon);
});
p.then(function() {
i = 0;
allWebtoonList.forEach(function (webtoon) {
getLatestToon(webtoon.toon_index, webtoon.week, function (latest_toon) {
webtoon.latest = latest_toon.latest;
console.log(i + " = " + webtoon.name + " : " + webtoon.latest)
i++;
connection.query("INSERT INTO toon SET ? ON DUPLICATE KEY UPDATE latest=?",
[webtoon,webtoon.latest], function () {
if(err){
console.log("웹툰 갱신중 에러!");
}
});
});
})
});
}
});
allWebtoons = allWebtoonList;
}
getAllToons();
//처음 한번 수행
setInterval(getAllToons,5*60*1000);
//5분에 한번 수행
/* GET home page. */
router.get('/',
function(req,res,next){
if(req.isAuthenticated()){
res.redirect('/mytoons');
console.log("(!)이미 로그인");
}else{
console.log("(!)로그인세션 없음");
res.render('index',{
title: "니툰내툰",
list: allWebtoons
});
}
});
module.exports = router;