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
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;

View File

@ -53,7 +53,7 @@ Date.prototype.format = function(fmt) { //author: meizz
var db = {
}
// var sql = "SELECT * FROM `users` WHERE `username` = 'luchenqun'";
// var sql = "SELECT * FROM `users` WHERE `username` = 'luchenqun1'";
// client.query(sql, (err, result) => {
// if (err) {
// console.log(err);
@ -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;

View File

@ -95,7 +95,7 @@
<div class="ui five stackable cards">
<div class="card link raised" ng-repeat="bookmark in bookmarks">
<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 class="content" href="{{ bookmark.url }}">
<div class="description bookmarkTitle">

View File

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

View File

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