강현태

toggle_toon 추가

This diff is collapsed. Click to expand it.
......@@ -54,16 +54,49 @@ router.get('/auth/logout/kakao',function (req,res) {
res.redirect('/');
})
var allWebtoons;
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() {
var allWeeklyToonsUrl = "http://comic.naver.com/webtoon/weekday.nhn";
//connection.connect();
allWebtoonJSONList = new Array();
allWebtoonList = new Array();
request(allWeeklyToonsUrl,function (err, res, html) {
if(!err){
var $ = cheerio.load(html);
$(".thumb").each(function (i) {
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');
......@@ -75,25 +108,37 @@ function getAllToons() {
name : name,
thum_link : thumb_link,
webtoon_link : webtoon_link,
week : week
week : week,
latest : 0
};
webtoon_string = JSON.stringify(webtoon);
connection.query("INSERT INTO toon SET ? ON DUPLICATE KEY UPDATE toon_index=toon_index",
webtoon);
//JSON으로 만든당.
allWebtoonJSONList.push(webtoon_string);
allWebtoonList.push(webtoon);
});
p.then(function() {
i = 0;
allWebtoonList.forEach(function (webtoon) {
getLatestToon(webtoon.toon_index, webtoon.week, function (latest_toon) {
webtoon.latest = latest_toon.latest;
console.log(i + " = " + webtoon.name + " : " + webtoon.latest)
i++;
connection.query("INSERT INTO toon SET ? ON DUPLICATE KEY UPDATE latest=?",
[webtoon,webtoon.latest], function () {
if(err){
console.log("웹툰 갱신중 에러!");
}
});
});
})
});
}
//connection.end();
});
allWebtoons = allWebtoonJSONList;
allWebtoons = allWebtoonList;
}
getAllToons();
setInterval(getAllToons,5000);
//5초에 한번 수행
//처음 한번 수행
setInterval(getAllToons,5*60*1000);
//5분에 한번 수행
/* GET home page. */
router.get('/',
......
......@@ -7,6 +7,7 @@ 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);
......@@ -18,7 +19,6 @@ function getUpdatedToons(cb){
var toonHref = link.attr('href');
allToons[toonName] = toonHref;
//connection.query("");
});
});
p.then(function(){
......@@ -32,8 +32,8 @@ function getUpdatedToons(cb){
}
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;';
var mylist;
var sqlquery = 'SELECT name, thum_link, webtoon_link, week, last, latest FROM user u, user_toon_relation ur, toon t WHERE u.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;
......@@ -48,32 +48,24 @@ function getMyToons(id,cb){
/* GET home page. */
router.get('/', function(req, res, next) {
if(!req.isAuthenticated()){
res.redirect('/');
}else{
async.series(
[
function(callback){
getMyToons(req.user.user_id, function (mytoon_list) {
callback(null,mytoon_list);
});
},
function(callback){
getUpdatedToons(function (updated_list) {
callback(null,updated_list);
});
}
],
function(err, results){
console.log(results);
res.render('mytoons', {
mytoons: results[0],
updatedtoons: results[1]
mytoons: results[0]
});
}
);
}
});
module.exports = router;
\ No newline at end of file
......
var express = require('express');
var router = express.Router();
require('../routes/mytoons');
function addMyToons(id,index,cb){
connection.query("INSERT INTO user_toon_relation SET ?;",
{user_id : id,toon_index : index, last : 1},function (err) {
if(err) {
throw err;
console.log("내 웹툰 추가중 에러!")
} else{
//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=?",
[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=?;",
[id, index],function (err, rows, result) {
if(err) {
console.log("내 웹툰 제거중 에러!");
throw err;
}else{
//alert("제거되었습니다.")
res.redirect('/setting');
}
});
}else{
addMyToons(id,index,function(){
res.redirect('/setting');
});
}
}
});
});
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;';
var mylist = new Array();
connection.query(sqlquery,id,function(err,rows,result){
if(!err){
mylist=rows;
cb(mylist);
}else{
console.log("내 웹툰 리스트 가져오는데 실패했습니다!");
//throw err;
}
});
}
router.get('/', function(req, res, next) {
getMyToons(req.user.user_id,function (mytoons) {
res.render('setting',{
list : updatedToons
alltoons : allWebtoons,
mytoons : mytoons
});
})
});
module.exports = router;
\ No newline at end of file
......
......@@ -14,25 +14,24 @@
<table>
<%
var current = "";
for(jsonString in list){
var A=JSON.parse(list[jsonString]);
if(current!=A.week){
for(webtoon in list){
if(current!=list[webtoon].week){
if(current!=""){
%>
</tr>
<% } %>
<tr>
<th><%= A.week %></th>
<th><%= list[webtoon].week %></th>
<% } %>
<td>
<a href="<%= A.webtoon_link %>">
<img alt="img" width="83" height="90" src="<%= A.thum_link %>"/>
<a href="<%= list[webtoon].webtoon_link %>">
<img alt="img" width="83" height="90" src="<%= list[webtoon].thum_link %>"/>
</a>
<%= A.name %>
<%= list[webtoon].name %>
</td>
<%
if(current!=A.week) {
current = A.week;
if(current!=list[webtoon].week) {
current = list[webtoon].week;
%>
<%
......
......@@ -3,8 +3,27 @@
<head>
<title>내툰</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<script src="http://developers.kakao.com/sdk/js/kakao.min.js"></script>
<style>
.toon_latest,.toon_next,.toon_last,.toon_name,.toon_thumbnail{
width:10%;
}
td{
vertical-align: middle;
border-bottom: 1px solid #bcbcbc;
border-left: 1px solid #bcbcbc;
border-right: 1px solid #bcbcbc;
margin:0;
Text-align:center;
}
table{
border-collapse:collapse;
border: 2px solid #474747;
}
th{
border-bottom:2px solid #474747;
}
</style>
</head>
<h1>내툰</h1>
<p>추가한 리스트</p>
......@@ -14,15 +33,23 @@
</br>
<table>
<%
<tr>
<th>썸네일</th>
<th>웹툰명</th>
<th>최근에 본 화</th>
<th>다음화</th>
<th>최신화</th>
</tr><%
for(i=0;i<mytoons.length; i++){
%>
<%= mytoons[i].name + " : " + mytoons[i].week %><br>
<%
}
%>
%><tr>
<td class="toon_thumbnail"><image src="<%= mytoons[i].thum_link%>"/></td>
<td class="toon_name"><%= mytoons[i].name %></td>
<td class="toon_last"><%= mytoons[i].last %>화</td>
<td class="toon_next"><%= mytoons[i].last +1 %>화</td>
<td class="toon_latest"><%= mytoons[i].latest %>화</td>
</tr>
<br>
<% } %>
</table>
</br>
......
......@@ -3,23 +3,54 @@
<head>
<meta charset="UTF-8">
<title>설정</title>
<style>
.registered{
background-color: #AAAAAA;
}
</style>
</head>
<h1>내툰</h1>
<p>추가하거나 제거할 웹툰을 눌러주세요.</p>
<p>추가하거나 제거할 웹툰을 선택해주세요.</p>
<table>
<%
var current = "";
for(toonName in mytoons) {
var A = mytoons[toonName];
for(webtoon in alltoons){
if(current!=alltoons[webtoon].week){
if(current!=""){
%>
</tr>
<% } %>
<tr>
<th><%= alltoons[webtoon].week %></th>
<% } %>
<td class="<%
for(i=0;i<mytoons.length;i++){
if(mytoons[i].name == alltoons[webtoon].name){
%>registered<%
break;
}
}
%>">
<form method="post" action="/setting/toggle_toon">
<input type="hidden" name="toon_index" value="<%= alltoons[webtoon].toon_index %>">
<input TYPE="IMAGE" src="<%= alltoons[webtoon].thum_link %>" name="Submit" value="Submit" align="absmiddle">
</form>
<%= alltoons[webtoon].name; %>
</td>
<%
if(current!=alltoons[webtoon].week) {
current = alltoons[webtoon].week;
%>
<%= A %></br>
<%
}
}
%>
</tr>
</table>
</form>
</br>
<body>
......