윤혜원

내툰리스트 추가하기,삭제하기 성공!

This diff is collapsed. Click to expand it.
......@@ -62,36 +62,6 @@ router.get('/auth/logout/kakao',function (req,res) {
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() {
......@@ -287,10 +257,6 @@ function getAllToons() {
p.then(function() {
i = 0;
allWebtoonList.forEach(function (webtoon) {
// getLatestToon(webtoon.toon_index, webtoon.week, function (latest_toon) {
// webtoon.latest = latest_toon.latest;
//i++;
//console.log(i + " = " + webtoon.name + " : " + webtoon.latest);
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){
......@@ -299,11 +265,6 @@ function getAllToons() {
} else {
console.log("웹툰 DB처리 완료!");
}
//[webtoon,webtoon.latest], function () {
// if (err) {
// console.log("웹툰 갱신중 에러!");
//}
// }
});
//});
})
......
var express = require('express');
var cheerio = require('cheerio');
var request = require('request');
var async = require('async');
var router = express.Router();
function getUpdatedToons(cb){
var allToons = new Array();
var allToonsUrl = "http://comic.naver.com/webtoon/weekday.nhn";
request(allToonsUrl, function(err, res, html){
if(!err){
var $ = cheerio.load(html);
var p = Promise.resolve();
var eachs = $(".thumb").has('.ico_updt').next().each(function() {
p = p.then(function() {
var link = $(this);
var toonName = link.text();
var toonHref = link.attr('href');
allToons[toonName] = toonHref;
});
});
p.then(function(){
cb(allToons);
});
}else{
console.log("업데이트 웹툰 못가져왔습니다.");
throw err;
}
});
}
function getMyToons(id,cb){
//var sql = "INSERT INTO `user` (id) VALUES (?)";
//var kid=[profile._json.id];
//connection.query(sql,kid,function(err,result){
var sqlquery = "SELECT name, thum_link, webtoon_link, week, last, t.toon_index AS toon_index FROM user u, user_toon_relation ur, toon t WHERE u.id=? && u.id=ur.user_id && t.toon_index=ur.toon_index;";
//var sqlquery = "SELECT name, thum_link, webtoon_link, week, last, latest, t.toon_index AS toon_index FROM user u, user_toon_relation ur, toon t WHERE u.id=? && u.id=ur.user_id && t.toon_index=ur.toon_index;";
//id 값을 가진 user가 저장한 웹툰들 가져오기
var sqlquery = "SELECT t.toon_index, t.name, t.thum_link, t.webtoon_link, t.week, t.site FROM user u, user_toon_relation ur, toon t WHERE u.id = '"+id+"' && u.id=ur.user_id && t.toon_index=ur.toon_index;";
var mylist = new Array();
connection.query(sqlquery,id,function(err,rows,result){
if(!err){
mylist=rows;
cb(mylist);
console.log(mylist);
}else{
console.log("내 웹툰 리스트 가져오는데 실패했습니다!");
//throw err;
......
......@@ -3,35 +3,34 @@ var router = express.Router();
function addMyToons(id,index,cb){
connection.query("INSERT INTO user_toon_relation SET ?;",
{user_id : id,toon_index : index, last : 1},function (err) {
{user_id : id,toon_index : index},function (err) {
if(err) {
throw err;
console.log("내 웹툰 추가중 에러!")
} else{
//alert("추가되었습니다.")
// alert("추가되었습니다.")
cb();
}
});
}
//내툰 수정하기
router.post('/toggle_toon',function(req,res,next){
var index = req.body.toon_index;
var id = req.user.user_id;
connection.query("SELECT COUNT(*) FROM user_toon_relation WHERE user_id=? && toon_index=?",
connection.query("SELECT COUNT(*) FROM user_toon_relation WHERE user_id='"+id+"' && toon_index='"+index+"'",
[id,index], function (err, rows,result) {
if(err){
console.log("내웹툰 등록중 에러!");
}else{
var count = rows[0]["COUNT(*)"];
if(count>0){//이미 등록되어 있는 것이라면,
connection.query("DELETE FROM user_toon_relation WHERE user_id=? && toon_index=?;",
connection.query("DELETE FROM user_toon_relation WHERE user_id='"+id+"' && toon_index='"+index+"';",
[id, index],function (err, rows, result) {
if(err) {
console.log("내 웹툰 제거중 에러!");
throw err;
}else{
//alert("제거되었습니다.")
// alert("제거되었습니다.")
res.redirect('/setting');
}
});
......@@ -45,13 +44,15 @@ router.post('/toggle_toon',function(req,res,next){
});
function getMyToons(id,cb){
var sqlquery = 'SELECT name, thum_link, webtoon_link, week, last FROM user u, user_toon_relation ur, toon t WHERE u.id=? && u.id=ur.user_id && t.toon_index=ur.toon_index;';
//mysql5.7 syntax에 맞게 수정 => 로그인한 유저의
var sqlquery = "SELECT t.toon_index, t.name, t.thum_link, t.webtoon_link, t.week, t.site FROM user u, user_toon_relation ur, toon t WHERE u.id = '"+id+"' && u.id=ur.user_id && t.toon_index=ur.toon_index;";
var mylist = new Array();
connection.query(sqlquery,id,function(err,rows,result){
if(!err){
mylist=rows;
cb(mylist);
}else{
router.get('/')
console.log("내 웹툰 리스트 가져오는데 실패했습니다!");
//throw err;
}
......
......@@ -4,15 +4,6 @@ var request = require('request');
var cheerio = require('cheerio');
var async = require('async');
function updateLastWebtoon(toon_index, user_id, num,cb){
var sql_query = "UPDATE user_toon_relation SET last=? WHERE user_id=? && toon_index=?";
connection.query(sql_query,[num, user_id,toon_index],function(err){
if(!err){
cb();
}
});
}
function getToonImages(toon_index, num, cb){
var imageList = [];
var toonUrl = "http://comic.naver.com/webtoon/detail.nhn?titleId=" + toon_index + "&no=" + num;
......@@ -41,10 +32,6 @@ router.get('/:toon_index/:num',function(req,res){
res.redirect('/');
return;
}
updateLastWebtoon(req.params.toon_index, req.user.user_id, req.params.num, function(){
var toonUrl = "http://comic.naver.com/webtoon/detail.nhn?titleId=" + req.params.toon_index + "&no=" + req.params.num;
res.redirect(toonUrl);
getToonImages(req.params.toon_index, req.params.num, function(imageList){
res.render('toonviewer',{
title: "zzz",
......@@ -52,7 +39,7 @@ router.get('/:toon_index/:num',function(req,res){
});
});
})
// })
});
module.exports = router;
\ No newline at end of file
......
......@@ -4,7 +4,7 @@
<title>내툰</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<style>
.toon_latest,.toon_next,.toon_last,.toon_name,.toon_thumbnail{
.toon_name,.toon_thumbnail{
width:10%;
}
......@@ -28,24 +28,30 @@
<h1>내툰</h1>
<p>추가한 리스트</p>
<a href="/auth/logout/kakao">로그아웃</a>
<a href="/setting/">세팅</a>
<a href="/setting/">수정하기</a>
<table>
<tr>
<th>썸네일</th>
<th>웹툰명</th>
<th>최근에 본 화</th>
<th>다음화</th>
<th>최신화</th>
<th>요일</th>
<th>사이트</th>
<th>바로가기</th>
</tr>
<!--로그인한 회원이 내툰에 웹툰이 없을시-->
<%if (mytoons.length==0){
%>
<th>내툰리스트에 웹툰이 없습니다! 수정하기 버튼을 눌러서 추가하세요!</th>
<%}%>
<% for(i=0;i<mytoons.length; i++){
%><tr>
<td class="toon_thumbnail"><image src="<%= mytoons[i].thum_link%>"/></td>
<td class="toon_name"><%= mytoons[i].name %></td>
<td class="toon_last"><a href="/toonviewer/<%= mytoons[i].toon_index %>/<%= mytoons[i].last %>"><%= mytoons[i].last %>화</a></td>
<td class="toon_next"><% if(mytoons[i].last+1 <= mytoons[i].latest){ %> <a href="/toonviewer/<%= mytoons[i].toon_index %>/<%= mytoons[i].last+1 %>"><%= mytoons[i].last +1 %>화</a><% } %></td>
<td class="toon_latest"><a href="/toonviewer/<%= mytoons[i].toon_index %>/<%= mytoons[i].latest %>"><%= mytoons[i].latest %>화</a></td>
<td class="week"><%= mytoons[i].week.toLowerCase() %></td>
<td class="site"><%= mytoons[i].site %></td>
<td class="webtoon_link"><a href="<%=mytoons[i].webtoon_link%>">바로가기</a></td>
</tr>
<% } %>
</table>
</br>
......