index.js
8.85 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
var express = require('express');
var cheerio = require('cheerio');
var request = require('request');
var mysql = require('mysql');
var client = require('cheerio-httpcli');
var passport = require('passport');
var KakaoStrategy = require('passport-kakao').Strategy;
var NaverStrategy = require('passport-naver').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var router = express.Router();
// kakao login API
var kakaoKey = {
clientID : '5634a5f8ca5c9a5eb378d6b6e6e869a0',
callbackURL : '/auth/login/kakao/callback'
};
// naver login API
var naverKey = {
clientID : 'OHmCrpQuVFnIEB4GkmF_',
callbackURL : '/auth/login/naver/callback',
clientSecret : 'BBgcRdnj0M'
};
// google login API
var googleKey = {
clientID : '924802195853-ir977i2ohnddaaninlqlbeg6sov629hq.apps.googleusercontent.com',
callbackURL : '/auth/login/google/callback',
clientSecret : 'NaWeoJouUzYq4VfBesTkdUfA'
};
// kakao passport
passport.use(new KakaoStrategy(kakaoKey,
function(accessToken, refreshToken,params, profile, done) {
console.log(profile);
loginByThirdparty({
'auth_type' : 'kakao',
'auth_id' : profile._json.id,
'auth_name' : profile._json.properties.nickname
}, done);
}
));
// naver passport
passport.use(new NaverStrategy(naverKey,
function (accessToken, refreshToken, profile, done) {
console.log(profile);
loginByThirdparty({
'auth_type': 'naver',
'auth_id': profile._json.id,
'auth_name': profile._json.nickName
}, done);
}
));
// google passport
passport.use(new GoogleStrategy(googleKey,
function (accessToken, refreshToken, profile, done) {
console.log(profile);
loginByThirdparty({
'auth_type': 'naver',
'auth_id': profile._json.sub,
'auth_name': profile._json.email
}, done);
}
));
// kakao 로그인, 로그인 콜백
router.get('/auth/login/kakao', passport.authenticate('kakao'));
router.get('/auth/login/kakao/callback', passport.authenticate('kakao', {
successRedirect: '/mytoons',
failureRedirect: '/'
})
);
// naver 로그인, 로그인 콜백
router.get('/auth/login/naver', passport.authenticate('naver'));
router.get('/auth/login/naver/callback', passport.authenticate('naver', {
successRedirect: '/mytoons',
failureRedirect: '/'
})
);
// facebook 로그인, 로그인 콜백
router.get('/auth/login/google', passport.authenticate('google', { scope: ['email profile'] }));
router.get('/auth/login/google/callback', passport.authenticate('google', {
successRedirect: '/mytoons',
failureRedirect: '/'
})
);
// 로그인 처리
function loginByThirdparty(info, done) {
var stmt_duplicated = "select *from `user` where id = id";
//'select *from `user` where `id` = ?
//"INSERT INTO `user` (id) VALUES (?) ON DUPLICATE KEY UPDATE id=id"
connection.query(stmt_duplicated, info.auth_id, function (err, result) {
if (err) {
return done(err);
} else {
if (result.length === 0) {
// 신규 유저는 회원 가입 이후 로그인 처리
var stmt_thridparty_signup = 'insert into `user` set `user_id`= ?, `nickname`= ?';
//
connection.query(stmt_thridparty_signup, [info.auth_id, info.auth_name], function (err, result) {
if(err){
return done(err);
} else {
done(null, {
'user_id': info.auth_id,
'nickname': info.auth_name
});
}
});
} else {
//기존유저 로그인 처리
console.log('Old User');
done(null, {
'user_id': info.auth_id,
'nickname': info.auth_name
});
}
}
});
}
router.get('/login', function(req,res) {
res.render('login_page')
})
// 라우터 설정, 카카오
router.get('/auth/logout/kakao',function (req,res) {
req.logout();
res.redirect('/');
})
// 라우터 설정, 페이스북
router.get('/auth/logout/naver',function (req,res) {
req.logout();
res.redirect('/');
})
// 라우터 설정, 구글
router.get('/auth/logout/google',function (req,res) {
req.logout();
res.redirect('/');
})
//
function getLatestToon(titleid, day ,cb) {
}
var daumCount = 0;
// Shows Top-10 webtoons
function gettop(){
var daum = 'http://webtoon.daum.net/data/pc/webtoon/list_daily_ranking/serialized?timeStamp=1559440437553';
var site = 'daum';
client.fetch(daum, {}, function (err, $, res, body){
var data = JSON.parse(body);
var list = data['data'];
list.forEach(function(item, idx){
var webtoon_link = 'http://webtoon.daum.net/webtoon/view/' + item.nickname.toString();
var webtoon = {
toon_index : item.id,
name : item.title,
thum_link : item.pcThumbnailImage.url,
webtoon_link : webtoon_link,
week : "TOP-10",
site : site,
lates : item.latestWebtoonEpisode.episode
}
allWebtoonList.push(webtoon);
});
});
};
//_day요일의 다음웹툰 불러오기
function getDaumToons(_day) {
var day = _day;
var day_name = day;
var daum = `http://webtoon.daum.net/data/pc/webtoon/list_serialized/${day}?timeStamp=1515819276574`;
var site = 'daum';
client.fetch(daum, {}, function (err, $, res, body){
var data = JSON.parse(body);
var list = data["data"];
if(err) {
console.log("다음웹툰 DB 에러 : " + err);
} else {
list.forEach(function (item, idx) {
var webtoon_link = 'http://webtoon.daum.net/webtoon/view/' + item.nickname.toString();
var webtoon = {
toon_index : item.id,
name : item.title,
thum_link : item.pcThumbnailImage.url,
webtoon_link : webtoon_link,
week : day_name,
site : site,
latest : item.latestWebtoonEpisode.episode,
averageScore : item.webtoonServices.averageScore
};
daumCount++;
allWebtoonList.push(webtoon);
});
}
});
}
// 네이버 전체 웹툰 불러오기
function getNaverToons() {
var allWeeklyToonsUrl = "http://comic.naver.com/webtoon/weekday.nhn";
// 네이버 웹툰 개수
var naverCount = 0;
var webtoonCount = 0;
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 site = 'naver';
var webtoon= {
toon_index: titleid,
name : name,
thum_link : thumb_link,
webtoon_link : webtoon_link,
week : week,
site : site,
latest : 0,
averageScore : 0
};
naverCount++;
allWebtoonList.push(webtoon);
});
p.then(function() {
i = 0;
allWebtoonList.forEach(function (webtoon) {
var sql= "INSERT INTO `toon` (toon_index, name, thum_link, webtoon_link, week, site, latest) VALUES(?) ON DUPLICATE KEY UPDATE latest=latest";
var values=[webtoon.toon_index, webtoon.name, webtoon.thum_link, webtoon.webtoon_link,webtoon.week, webtoon.site, webtoon.latest];
connection.query(sql,[values],function(err,result){
if(err) {
console.log("웹툰 DB 에러 : " + err);
} else {
webtoonCount++;
console.log(`웹툰 ${webtoonCount}개 DB처리 완료!`);
}
});
})
});
}
console.log(`다음웹툰 ${daumCount}개, 네이버웹툰 ${naverCount}개`)
});
}
// 모든 웹툰을 담고있는 배열
allWebtoons = new Array();
// 설명
function getAllToons() {
allWebtoonList = new Array();
//월요일 다음 웹툰
getDaumToons('mon');
//화요일 다음 웹툰
getDaumToons('tue');
//수요일 다음 웹툰
getDaumToons('wed');
//목요일 다음 웹툰
getDaumToons('thu');
//금요일 다음 웹툰
getDaumToons('fri');
//토요일 다음 웹툰
getDaumToons('sat');
//일요일 다음 웹툰
getDaumToons('sun');
//Top10
gettop()
//네이버 웹툰
getNaverToons();
allWebtoons = allWebtoonList;
};
//처음 한번 수행
getAllToons();
//5분에 한번 수행
setInterval(getAllToons, 5*60*1000);
/* GET home page. */
router.get('/', function(req,res,next) {
if(req.isAuthenticated()) {
res.redirect('/mytoons');
console.log("(!)Already logined");
} else {
console.log("(!)로그인세션 없음");
res.render('index',{
title: "니툰내툰",
list: allWebtoons
});
}
});
module.exports = router;