이진아

파일분리!

1 +var express = require('express');
2 +var cheerio = require('cheerio');
3 +var request = require('request');
4 +const axios = require('axios');
5 +//axios 추가 설치된거 확인하기
6 +var router = express.Router();
7 +var mysql = require('mysql');
8 +var client = require('cheerio-httpcli');
9 +var passport = require('passport'),
10 + KakaoStrategy = require('passport-kakao').Strategy;
11 +
12 +passport.use(new KakaoStrategy({
13 + clientID : 'bd2e610396fb7bbb84cf91a786b3cc72',
14 + callbackURL :'/auth/login/kakao/callback',
15 + clientSecret : 'eUtJGtlLoCZJufevp3LKfDP0KOtZUV7R'
16 + },
17 + function(accessToken, refreshToken,params, profile, done){
18 + //사용자 정보는 profile에
19 + loginByThirdparty(accessToken, refreshToken, profile);
20 +
21 + console.log("(!)로그인 : " + profile._json.id+"("+profile._json.properties.nickname +")");
22 + //return done(null,profile)
23 + return done(null, {
24 + 'user_id': profile._json.id,
25 + 'nickname': profile._json.properties.nickname
26 + });
27 + }
28 +));
29 +
30 +// kakao 로그인
31 +router.get('/auth/login/kakao',
32 + // passport.authenticate('kakao',{state: "myStateValue"})
33 + passport.authenticate('kakao')
34 +);
35 +
36 +// kakao 로그인 연동 콜백
37 +router.get('/auth/login/kakao/callback',
38 + passport.authenticate('kakao', {
39 + //session: false,
40 + successRedirect: '/mytoons',
41 + failureRedirect: '/'
42 + })
43 +);
44 +
45 +function loginByThirdparty(accessToken, refreshToken, profile) {
46 + //예전 코드는 MySQL 버젼이 맞지 않음
47 + // var sql = 'INSERT INTO `user`(id) VALUES(?) ON DUPLICATE KEY(PRIMARY) UPDATE id=(?);'
48 + var sql = "INSERT INTO `user` (id) VALUES (?) ON DUPLICATE KEY UPDATE id=id";
49 + var kid=[profile._json.id];
50 + connection.query(sql,kid,function(err,result){
51 + if (err) {
52 + console.log("로그인 쿼리중 에러 : " + err);
53 + } else {
54 + console.log("로그인 DB처리 완료!");
55 + }
56 + });
57 +}
58 +
59 +router.get('/auth/logout/kakao',function (req,res) {
60 + req.logout();
61 + res.redirect('/');
62 +})
63 +
64 +
65 +//---------------------------------------------------------------------------------------------------------------------
66 +
67 +allWebtoons2 = new Array();
68 +
69 +function getAllToons2() {
70 + allWebtoonList2 = new Array();
71 +//연재 완료 연도별 다음 웹툰
72 + var daum = 'http://webtoon.daum.net/data/pc/webtoon/list_finished/?genre_id=&timeStamp=1575275921086';
73 + var site = 'daum';
74 + client.fetch(daum, {}, function (err, $, res, body) {
75 + var data = JSON.parse(body);
76 + var list = data["data"];
77 + list.forEach(function (item, idx) {
78 + var webtoon_link = 'http://webtoon.daum.net/webtoon/view/' + item.nickname.toString();
79 + var yyyy = (item.finishDate).substring(0,4)
80 +
81 + var webtoon = {
82 + name: item.title,
83 + thum_link: item.pcThumbnailImage.url,
84 + webtoon_link: webtoon_link,
85 + year : yyyy
86 +
87 + }
88 + allWebtoonList2.push(webtoon);
89 + });
90 +
91 + });
92 + allWebtoons2 = allWebtoonList2;
93 +}
94 +
95 +
96 +
97 +getAllToons2();
98 +setInterval(getAllToons2,5*60*1000);
99 +
100 +
101 +/* GET home page. */
102 +
103 +//진아 : list :allWebtoons2.3 해도 되는지??!!
104 +router.get('/',
105 + function(req,res,next){
106 + if(req.isAuthenticated()){
107 + res.redirect('/mytoons');
108 + console.log("(!)이미 로그인");
109 + }else{
110 + console.log("(!)로그인세션 없음");
111 + res.render('index',{
112 + title: "니툰내툰",
113 + list: allWebtoons2
114 + });
115 + }
116 + });
117 +
118 +module.exports = router;