index2.js 3.64 KB
var express = require('express');
var cheerio = require('cheerio');
var request = require('request');
const axios = require('axios');
//axios 추가 설치된거 확인하기
var router = express.Router();
var mysql      = require('mysql');
var client = require('cheerio-httpcli');
var passport = require('passport'),
    KakaoStrategy = require('passport-kakao').Strategy;

passport.use(new KakaoStrategy({
        clientID : 'bd2e610396fb7bbb84cf91a786b3cc72',
        callbackURL :'/auth/login/kakao/callback',
       clientSecret : 'eUtJGtlLoCZJufevp3LKfDP0KOtZUV7R'
    },
    function(accessToken, refreshToken,params, profile, done){
        //사용자 정보는 profile에
        loginByThirdparty(accessToken, refreshToken, profile);

        console.log("(!)로그인 : " + profile._json.id+"("+profile._json.properties.nickname +")");
        //return done(null,profile)
        return done(null, {
            'user_id': profile._json.id,
            'nickname': profile._json.properties.nickname
        });
    }
));

// kakao 로그인
router.get('/auth/login/kakao',
   // passport.authenticate('kakao',{state: "myStateValue"})
   passport.authenticate('kakao')
);

// kakao 로그인 연동 콜백
router.get('/auth/login/kakao/callback',
    passport.authenticate('kakao', {
        //session: false,
        successRedirect: '/mytoons',
        failureRedirect: '/'
    })
);

function loginByThirdparty(accessToken, refreshToken, profile) {
    //예전 코드는 MySQL 버젼이 맞지 않음
  //  var sql = 'INSERT INTO `user`(id) VALUES(?) ON DUPLICATE KEY(PRIMARY) UPDATE id=(?);'
    var sql = "INSERT INTO `user` (id) VALUES (?) ON DUPLICATE KEY UPDATE id=id";
    var kid=[profile._json.id];
    connection.query(sql,kid,function(err,result){
        if (err) {
            console.log("로그인 쿼리중 에러 : " + err);
        } else {
            console.log("로그인 DB처리 완료!");
        }
    });
}

router.get('/auth/logout/kakao',function (req,res) {
    req.logout();
    res.redirect('/');
})


//---------------------------------------------------------------------------------------------------------------------

allWebtoons2 = new Array();

function getAllToons2() {
    allWebtoonList2 = new Array();
//연재 완료 연도별 다음 웹툰
    var daum = 'http://webtoon.daum.net/data/pc/webtoon/list_finished/?genre_id=&timeStamp=1575275921086';
     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 yyyy = (item.finishDate).substring(0,4)
               
                var webtoon = {
                    toon_index : item.id,
                    name: item.title,
                    thum_link: item.pcThumbnailImage.url,
                    webtoon_link: webtoon_link,
                    year : yyyy
               
               }
                allWebtoonList2.push(webtoon);
            });

        });
    allWebtoons2 = allWebtoonList2;
}



getAllToons2();
setInterval(getAllToons2,5*60*1000);


/* GET home page. */

//진아 : list :allWebtoons2.3 해도 되는지??!!
router.get('/',
    function(req,res,next){
        if(req.isAuthenticated()){
            res.redirect('/mytoons');
            console.log("(!)이미 로그인");
        }else{
            console.log("(!)로그인세션 없음");
            res.render('index',{
                title: "니툰내툰",
                list: allWebtoons2
            });
        }
    });

module.exports = router;