1分钟定时截图

This commit is contained in:
luchenqun 2017-02-21 11:29:03 +08:00
parent e12aefcfb4
commit d736faa17e
6 changed files with 113 additions and 64 deletions

2
.gitignore vendored
View File

@ -38,4 +38,4 @@ jspm_packages
# Upload File # Upload File
uploads uploads
/public/images/shot/ /public/images/snap/

1
app.js
View File

@ -76,5 +76,6 @@ app.use(function(err, req, res, next) {
}); });
}); });
api.getSnapByTimer();
module.exports = app; module.exports = app;

View File

@ -52,15 +52,15 @@ Date.prototype.format = function(fmt) { //author: meizz
// update delete 返回影响的行数 // update delete 返回影响的行数
var db = { var db = {
} }
// var sql = "SELECT * FROM `users` WHERE `username` = 'luchenqun'"; // var sql = "SELECT * FROM `users` WHERE `username` = 'luchenqun1'";
// client.query(sql, (err, result) => { // client.query(sql, (err, result) => {
// if (err) { // if (err) {
// console.log(err); // console.log(err);
// } else { // } else {
// console.log(result); // console.log(result);
// } // }
// }); // });
db.addBookmark = function(user_id, bookmark) { db.addBookmark = function(user_id, bookmark) {
var insertSql = "INSERT INTO `bookmarks` (`user_id`, `title`, `description`, `url`, `public`, `click_count`) VALUES ('" + user_id + "', '" + bookmark.title + "', '" + bookmark.description + "', '" + bookmark.url + "', '" + bookmark.public + "', '1')"; var insertSql = "INSERT INTO `bookmarks` (`user_id`, `title`, `description`, `url`, `public`, `click_count`) VALUES ('" + user_id + "', '" + bookmark.title + "', '" + bookmark.description + "', '" + bookmark.url + "', '" + bookmark.public + "', '1')";
@ -561,4 +561,32 @@ db.getTagsBookmarks = function(bookmark_ids) {
}); });
} }
db.getBookmarkWaitSnap = function(today) {
var todayNotSnap = today + 31;
var sql = "SELECT id, url, snap_state FROM `bookmarks` WHERE `snap_state`>=0 AND `snap_state` <= 64 AND snap_state != " + todayNotSnap + " ORDER BY last_click DESC LIMIT 0, 1";
return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
db.updateBookmarkSnapState = function(id, snapState) {
console.log("updateBookmarkSnapState id = " + id + ", snapState = " + snapState);
var sql = "UPDATE `bookmarks` SET `snap_state`='"+ snapState +"' WHERE (`id`='"+ id +"')";
return new Promise(function(resolve, reject) {
client.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result.affectedRows);
}
});
});
}
module.exports = db; module.exports = db;

View File

@ -95,7 +95,7 @@
<div class="ui five stackable cards"> <div class="ui five stackable cards">
<div class="card link raised" ng-repeat="bookmark in bookmarks"> <div class="card link raised" ng-repeat="bookmark in bookmarks">
<div class="image"> <div class="image">
<img ng-src="./images/shot/{{bookmark.id}}.png" err-src="./images/shot/default.png"/> <img ng-src="./images/snap/{{bookmark.id}}.png" err-src="./images/snap/default.png"/>
</div> </div>
<div class="content" href="{{ bookmark.url }}"> <div class="content" href="{{ bookmark.url }}">
<div class="description bookmarkTitle"> <div class="description bookmarkTitle">

View File

@ -13,6 +13,7 @@ var webshotOptions = {
width: 320, width: 320,
height: 320 height: 320
}, },
timeout: 50000,
}; };
var storage = multer.diskStorage({ var storage = multer.diskStorage({
@ -522,36 +523,38 @@ api.post('/uploadBookmarkFile', upload.single('bookmark'), function(req, res) {
.then((allTags) => { .then((allTags) => {
bookmarks.forEach((item, index) => { bookmarks.forEach((item, index) => {
var count = 0; var count = 0;
if (/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(item.url)) {
var bookmark = {};
bookmark.title = item.name;
bookmark.description = "";
bookmark.url = item.url;
bookmark.public = '1';
if (item.tags.length == 0) {
item.tags.push("未分类")
}
var bookmark = {}; var tags = [];
bookmark.title = item.name; item.tags.forEach((tag) => {
bookmark.description = ""; allTags.forEach((at) => {
bookmark.url = item.url; if (at.name == tag) {
bookmark.public = '1'; tags.push(at.id);
if (item.tags.length == 0) { }
item.tags.push("未分类") })
})
// 插入书签
db.addBookmark(userId, bookmark) // 插入书签
.then((bookmark_id) => {
db.delBookmarkTags(bookmark_id); // 不管3721先删掉旧的分类
return bookmark_id;
}) // 将之前所有的书签分类信息删掉
.then((bookmark_id) => db.addTagsBookmarks(tags, bookmark_id)) // 插入分类
.then(() => db.updateLastUseTags(userId, tags)) // 更新最新使用的分类
.then(() => {
count++
}) // 运气不错
.catch((err) => console.log('uploadBookmarkFile addBookmark err', err)); // oops!
} }
var tags = [];
item.tags.forEach((tag) => {
allTags.forEach((at) => {
if (at.name == tag) {
tags.push(at.id);
}
})
})
// 插入书签
db.addBookmark(userId, bookmark) // 插入书签
.then((bookmark_id) => {
db.delBookmarkTags(bookmark_id); // 不管3721先删掉旧的分类
return bookmark_id;
}) // 将之前所有的书签分类信息删掉
.then((bookmark_id) => db.addTagsBookmarks(tags, bookmark_id)) // 插入分类
.then(() => db.updateLastUseTags(userId, tags)) // 更新最新使用的分类
.then(() => {
count++
}) // 运气不错
.catch((err) => console.log('uploadBookmarkFile addBookmark err', err)); // oops!
if ((index + 1) == bookmarks.length) { if ((index + 1) == bookmarks.length) {
// 通知前台 // 通知前台
} }
@ -575,10 +578,7 @@ api.post('/addBookmark', function(req, res) {
db.delBookmarkTags(bookmark_id); // 不管3721先删掉旧的分类 db.delBookmarkTags(bookmark_id); // 不管3721先删掉旧的分类
return bookmark_id; return bookmark_id;
}) // 将之前所有的书签分类信息删掉 }) // 将之前所有的书签分类信息删掉
.then((bookmark_id) => { .then((bookmark_id) => db.addTagsBookmarks(tags, bookmark_id)) // 插入分类
getWebshot(bookmark_id, bookmark.url);
return db.addTagsBookmarks(tags, bookmark_id)
}) // 插入分类
.then(() => db.updateLastUseTags(userId, tags)) // 更新最新使用的分类 .then(() => db.updateLastUseTags(userId, tags)) // 更新最新使用的分类
.then(() => res.json({})) // 运气不错 .then(() => res.json({})) // 运气不错
.catch((err) => console.log('addBookmark err', err)); // oops! .catch((err) => console.log('addBookmark err', err)); // oops!
@ -625,28 +625,47 @@ api.post('/getTitle', function(req, response) {
}); });
}) })
function md5(str) { api.getSnapByTimer = function() {
return crypto console.log('getSnapByTimer...........');
.createHash('md5') setInterval(function() {
.update(str) var today = new Date().getDate();
.digest('hex'); db.getBookmarkWaitSnap(today)
}; .then((bookmarks) => {
if (bookmarks.length == 1) {
var cnt = 1; var id = bookmarks[0].id;
var snap_state = bookmarks[0].snap_state;
function getWebshot(id, url) { var url = bookmarks[0].url;
var finePath = './public/images/shot/' + id + '.png' var finePath = './public/images/snap/' + id + '.png'
fs.exists(finePath, function(exists) { fs.exists(finePath, function(exists) {
if (!exists) { if (exists) {
setTimeout(function() { db.updateBookmarkSnapState(id, -1);
webshot(url, finePath, webshotOptions, function(err) { } else {
if (err) { if (!/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)) {
console.log(id + " webshot fail", err); db.updateBookmarkSnapState(id, today + 31);
} return;
}); }
}, 10000 * cnt++); webshot(url, finePath, webshotOptions, function(err) {
} var newSnapState = -1;
}); if (err) {
console.log("boomarkid = " + id + ", webshot over", err)
if (snap_state == 0 || snap_state == 1) {
newSnapState = snap_state + 1;
} else if (snap_state == 2) {
newSnapState = today + 31;
}
}
db.updateBookmarkSnapState(id, newSnapState);
});
}
});
}
})
.catch((err) => console.log('getBookmarkWaitSnap err', err));
}, 60000);
} }
function md5(str) {
return crypto.createHash('md5').update(str).digest('hex');
};
module.exports = api; module.exports = api;

View File

@ -24,6 +24,7 @@ CREATE TABLE `bookmarks` (
`click_count` smallint DEFAULT 1, -- 总共点击次数 `click_count` smallint DEFAULT 1, -- 总共点击次数
`created_at` datetime DEFAULT now(), -- 创建时间 `created_at` datetime DEFAULT now(), -- 创建时间
`last_click` datetime DEFAULT now(), -- 最后一次点击时间 `last_click` datetime DEFAULT now(), -- 最后一次点击时间
`snap_state` tinyint(8) DEFAULT '0', -- -1获取成功。012获取快照次数。当前天+31今天不再获取该网页快照
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `userIdIdx` (`user_id`) KEY `userIdIdx` (`user_id`)
); );