이진아

파일 분리!

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 +allWebtoons4 = new Array();
68 +
69 +function getAllToons4() {
70 +
71 + allWebtoonList4 = new Array();
72 + //현재 상영작 좋아요 순
73 + const currentlikemovie = 'https://movie.naver.com/movie/running/current.nhn?view=list&tab=normal&order=likeCount';
74 +
75 + axios.get(currentlikemovie).then(res => {
76 + if (res.status === 200) {
77 + let currentLikeMovie = [];
78 + const $ = cheerio.load(res.data);
79 + const $movieList = $('div.lst_wrap ul.lst_detail_t1').children('li');
80 +
81 + $movieList.each(function (i) {
82 + currentLikeMovie[i] = {
83 + title: $(this).find('dt.tit a').text(),
84 + img: $(this).find('div.thumb img').attr('src')
85 + //개봉예정작에선 star가 없음
86 + //star: $(this).find('em.num_likeit').text().replace(/\t/gi, '').replace(/\n/gi,'')
87 + };
88 + });
89 +
90 + const data = currentLikeMovie.filter(m => m.title);
91 + allWebtoonList4.push(data);
92 + //console.log(data);
93 + }
94 + }, (error) => console.log(error));
95 +
96 + //현재 상영작 예매 순
97 + const runningreserve = 'https://movie.naver.com/movie/running/current.nhn?view=list&tab=normal&order=reserve';
98 +
99 + axios.get(runningreserve).then(res => {
100 + if (res.status === 200) {
101 + let crawledMovie = [];
102 + const $ = cheerio.load(res.data);
103 + const $movieList = $('div.lst_wrap ul.lst_detail_t1').children('li');
104 +
105 + $movieList.each(function (i) {
106 + crawledMovie[i] = {
107 + title: $(this).find('dt.tit a').text(),
108 + img: $(this).find('div.thumb img').attr('src')
109 + };
110 + });
111 +
112 + const data = crawledMovie.filter(m => m.title);
113 + allWebtoonList4.push(data);
114 + //console.log(data);
115 + }
116 + }, (error) => console.log(error));
117 +
118 + //개봉 예정영화 기대지수순
119 + const premovieinterest = 'https://movie.naver.com/movie/running/premovie.nhn?order=interestRate';
120 +
121 + axios.get(premovieinterest).then(res => {
122 + if (res.status === 200) {
123 + let crawledMovie = [];
124 + const $ = cheerio.load(res.data);
125 + const $movieList = $('div.lst_wrap ul.lst_detail_t1').children('li');
126 +
127 + $movieList.each(function (i) {
128 + crawledMovie[i] = {
129 + title: $(this).find('dt.tit a').text(),
130 + img: $(this).find('div.thumb img').attr('src')
131 + };
132 + });
133 +
134 + const data = crawledMovie.filter(m => m.title);
135 + allWebtoonList4.push(data);
136 + //console.log(data);
137 + }
138 + }, (error) => console.log(error));
139 +
140 + //계봉 예정영화 예매순
141 + const prereserve = 'https://movie.naver.com/movie/running/premovie.nhn?order=reserve';
142 +
143 + axios.get(prereserve).then(res => {
144 + if (res.status === 200) {
145 + let crawledMovie = [];
146 + const $ = cheerio.load(res.data);
147 + const $movieList = $('div.lst_wrap ul.lst_detail_t1').children('li');
148 +
149 + $movieList.each(function (i) {
150 + crawledMovie[i] = {
151 + title: $(this).find('dt.tit a').text(),
152 + img: $(this).find('div.thumb img').attr('src')
153 + };
154 + });
155 +
156 + const data = crawledMovie.filter(m => m.title);
157 + allWebtoonList4.push(data);
158 + //console.log(data);
159 + }
160 + }, (error) => console.log(error));
161 +
162 + allWebtoons4 = allWebtoonList4;
163 +}
164 +
165 +
166 +
167 +getAllToons4();
168 +setInterval(getAllToons4,5*60*1000);
169 +
170 +/* GET home page. */
171 +
172 +//진아 : list :allWebtoons2.3 해도 되는지??!!
173 +router.get('/',
174 + function(req,res,next){
175 + if(req.isAuthenticated()){
176 + res.redirect('/mytoons');
177 + console.log("(!)이미 로그인");
178 + }else{
179 + console.log("(!)로그인세션 없음");
180 + res.render('index',{
181 + title: "니툰내툰",
182 + list: allWebtoons4
183 + });
184 + }
185 + });
186 +
187 +module.exports = router;