自动获取favicon图标
This commit is contained in:
parent
b7b58e5023
commit
3832b573b5
|
|
@ -39,3 +39,4 @@ jspm_packages
|
||||||
# Upload File
|
# Upload File
|
||||||
uploads
|
uploads
|
||||||
/public/images/snap/
|
/public/images/snap/
|
||||||
|
/public/images/favicon/
|
||||||
4
app.js
4
app.js
|
|
@ -75,7 +75,7 @@ app.use(function(err, req, res, next) {
|
||||||
error: {}
|
error: {}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// api.checkSnapState();
|
// api.checkSnapFaviconState();
|
||||||
api.getSnapByTimer();
|
api.getSnapFaviconByTimer();
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
var request = require('request');
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
|
var download = function(url, dest, cb) {
|
||||||
|
var file = fs.createWriteStream(dest);
|
||||||
|
var sendReq = request.get(url);
|
||||||
|
|
||||||
|
var error = null;
|
||||||
|
sendReq.on('response', function(response) {
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
|
error = 'Response status was ' + response.statusCode;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
sendReq.on('error', function(err) {
|
||||||
|
fs.unlink(dest);
|
||||||
|
error = err
|
||||||
|
});
|
||||||
|
|
||||||
|
sendReq.pipe(file);
|
||||||
|
|
||||||
|
file.on('finish', function() {
|
||||||
|
file.close(cb(error));
|
||||||
|
});
|
||||||
|
|
||||||
|
file.on('error', function(err) {
|
||||||
|
fs.unlink(dest);
|
||||||
|
error = err.message;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = download;
|
||||||
|
|
@ -696,7 +696,7 @@ db.getBookmarks = function() {
|
||||||
|
|
||||||
db.getBookmarkWaitSnap = function(today) {
|
db.getBookmarkWaitSnap = function(today) {
|
||||||
var todayNotSnap = today + 31;
|
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 created_at DESC LIMIT 0, 1";
|
var sql = "SELECT id, url, snap_state, favicon_state FROM `bookmarks` WHERE (`snap_state`>=0 AND `snap_state` <= 64 AND snap_state != " + todayNotSnap + ") OR (`favicon_state`>=0 AND `favicon_state` <= 64 AND favicon_state != " + todayNotSnap + ") ORDER BY created_at DESC LIMIT 0, 1";
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
client.query(sql, (err, result) => {
|
client.query(sql, (err, result) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
@ -722,4 +722,18 @@ db.updateBookmarkSnapState = function(id, snapState) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.updateBookmarkFaviconState = function(id, faviconState) {
|
||||||
|
console.log("updateBookmarkFaviconState id = " + id + ", faviconState = " + faviconState);
|
||||||
|
var sql = "UPDATE `bookmarks` SET `favicon_state`='" + faviconState + "' 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;
|
||||||
|
|
|
||||||
|
|
@ -174,3 +174,15 @@ app.directive('errSrc', function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.directive('faviconErr', function() {
|
||||||
|
return {
|
||||||
|
link: function(scope, element, attrs) {
|
||||||
|
element.bind('error', function() {
|
||||||
|
if (attrs.src != attrs.faviconErr) {
|
||||||
|
attrs.$set('src', attrs.faviconErr);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@
|
||||||
id="{{bookmark.id}}">
|
id="{{bookmark.id}}">
|
||||||
<img class="ui ui middle aligned tiny image bookmarkOperaterHover" style="width:16px;height:16px" ng-src="./images/{{ bookmarkEditHover ? 'delete-hover' : 'delete'}}.png" ng-if="edit" ng-click="delBookmark(bookmark)">
|
<img class="ui ui middle aligned tiny image bookmarkOperaterHover" style="width:16px;height:16px" ng-src="./images/{{ bookmarkEditHover ? 'delete-hover' : 'delete'}}.png" ng-if="edit" ng-click="delBookmark(bookmark)">
|
||||||
<img class="ui ui middle aligned tiny image bookmarkOperaterHover" style="width:16px;height:16px;float:right;" ng-src="./images/{{ bookmarkEditHover ? 'edit-bookmark-hover' : 'edit-bookmark'}}.png" ng-if="edit" ng-click="editBookmark(bookmark.id)">
|
<img class="ui ui middle aligned tiny image bookmarkOperaterHover" style="width:16px;height:16px;float:right;" ng-src="./images/{{ bookmarkEditHover ? 'edit-bookmark-hover' : 'edit-bookmark'}}.png" ng-if="edit" ng-click="editBookmark(bookmark.id)">
|
||||||
<img class="ui ui middle aligned tiny image" ng-src=" http://favicon.byi.pw/?url={{bookmark.url}}" style="width:16px;height:16px;cursor:default;" ng-click="detailBookmark(bookmark);$event.stopPropagation()" ng-if="!edit">
|
<img class="ui ui middle aligned tiny image" ng-src="./images/favicon/{{bookmark.id}}.ico" style="width:16px;height:16px;cursor:default;" ng-click="detailBookmark(bookmark);$event.stopPropagation()" ng-if="!edit" favicon-err="http://g.soz.im/{{bookmark.url}}">
|
||||||
<span>{{ bookmark.title}}</span>
|
<span>{{ bookmark.title}}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -56,7 +56,7 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr ng-repeat="bookmark in bookmarks" id="{{ bookmark.id }}">
|
<tr ng-repeat="bookmark in bookmarks" id="{{ bookmark.id }}">
|
||||||
<td>
|
<td>
|
||||||
<img class="ui ui middle aligned tiny image" src="http://favicon.byi.pw/?url={{bookmark.url}}" style="width:16px;height:16px;cursor:pointer;" ng-click="jumpToUrl(bookmark.url, bookmark.id)">
|
<img class="ui ui middle aligned tiny image" ng-src="./images/favicon/{{bookmark.id}}.ico" style="width:16px;height:16px;cursor:pointer;" ng-click="jumpToUrl(bookmark.url, bookmark.id)" favicon-err="http://g.soz.im/{{bookmark.url}}">
|
||||||
<span ng-click="jumpToUrl(bookmark.url, bookmark.id)" title="{{bookmark.title}}" style="cursor:pointer;">
|
<span ng-click="jumpToUrl(bookmark.url, bookmark.id)" title="{{bookmark.title}}" style="cursor:pointer;">
|
||||||
{{ bookmark.title }}
|
{{ bookmark.title }}
|
||||||
</span>
|
</span>
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr ng-repeat="bookmark in bookmarks" id="{{ bookmark.id }}">
|
<tr ng-repeat="bookmark in bookmarks" id="{{ bookmark.id }}">
|
||||||
<td>
|
<td>
|
||||||
<img class="ui ui middle aligned tiny image" src=" http://favicon.byi.pw/?url={{bookmark.url}}" style="width:16px;height:16px;cursor:pointer;" ng-click="jumpToUrl(bookmark.url, bookmark.id)">
|
<img class="ui ui middle aligned tiny image" src=" ./images/favicon/{{bookmark.id}}.ico" style="width:16px;height:16px;cursor:pointer;" ng-click="jumpToUrl(bookmark.url, bookmark.id)" favicon-err="http://g.soz.im/{{bookmark.url}}">
|
||||||
<span ng-click="jumpToUrl(bookmark.url, bookmark.id)" title="{{bookmark.title}}" style="cursor:pointer;">
|
<span ng-click="jumpToUrl(bookmark.url, bookmark.id)" title="{{bookmark.title}}" style="cursor:pointer;">
|
||||||
{{ bookmark.title }}
|
{{ bookmark.title }}
|
||||||
</span>
|
</span>
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr ng-repeat="bookmark in bookmarks" id="{{ bookmark.id }}">
|
<tr ng-repeat="bookmark in bookmarks" id="{{ bookmark.id }}">
|
||||||
<td>
|
<td>
|
||||||
<img class="ui ui middle aligned tiny image" src=" http://favicon.byi.pw/?url={{bookmark.url}}" style="width:16px;height:16px;cursor:pointer;" ng-click="jumpToUrl(bookmark.url, bookmark.id)">
|
<img class="ui ui middle aligned tiny image" src=" ./images/favicon/{{bookmark.id}}.ico" style="width:16px;height:16px;cursor:pointer;" ng-click="jumpToUrl(bookmark.url, bookmark.id)" favicon-err="http://g.soz.im/{{bookmark.url}}">
|
||||||
<span ng-click="jumpToUrl(bookmark.url, bookmark.id)" title="{{bookmark.title}}" style="cursor:pointer;">
|
<span ng-click="jumpToUrl(bookmark.url, bookmark.id)" title="{{bookmark.title}}" style="cursor:pointer;">
|
||||||
{{ bookmark.title }}
|
{{ bookmark.title }}
|
||||||
</span>
|
</span>
|
||||||
|
|
|
||||||
|
|
@ -4,39 +4,12 @@ var crypto = require('crypto');
|
||||||
var read = require('node-readability');
|
var read = require('node-readability');
|
||||||
var db = require('../database/db.js');
|
var db = require('../database/db.js');
|
||||||
var parseHtml = require('../common/parse_html.js');
|
var parseHtml = require('../common/parse_html.js');
|
||||||
|
var download = require('../common/download.js');
|
||||||
var multer = require('multer');
|
var multer = require('multer');
|
||||||
var webshot = require('webshot');
|
var webshot = require('webshot');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var favicon = require('favicon');
|
var favicon = require('favicon');
|
||||||
|
|
||||||
var download = function(url, dest, cb) {
|
|
||||||
var file = fs.createWriteStream(dest);
|
|
||||||
var sendReq = request.get(url);
|
|
||||||
|
|
||||||
var error = null;
|
|
||||||
sendReq.on('response', function(response) {
|
|
||||||
if (response.statusCode !== 200) {
|
|
||||||
error = 'Response status was ' + response.statusCode;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
sendReq.on('error', function(err) {
|
|
||||||
fs.unlink(dest);
|
|
||||||
error = err
|
|
||||||
});
|
|
||||||
|
|
||||||
sendReq.pipe(file);
|
|
||||||
|
|
||||||
file.on('finish', function() {
|
|
||||||
file.close(cb(error));
|
|
||||||
});
|
|
||||||
|
|
||||||
file.on('error', function(err) {
|
|
||||||
fs.unlink(dest);
|
|
||||||
error = err.message;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var storage = multer.diskStorage({
|
var storage = multer.diskStorage({
|
||||||
destination: function(req, file, cb) {
|
destination: function(req, file, cb) {
|
||||||
cb(null, 'uploads/')
|
cb(null, 'uploads/')
|
||||||
|
|
@ -574,13 +547,13 @@ api.post('/uploadBookmarkFile', upload.single('bookmark'), function(req, res) {
|
||||||
|
|
||||||
var tags = [];
|
var tags = [];
|
||||||
item.tags.forEach((tag) => {
|
item.tags.forEach((tag) => {
|
||||||
allTags.forEach((at) => {
|
allTags.forEach((at) => {
|
||||||
if (at.name == tag) {
|
if (at.name == tag) {
|
||||||
tags.push(at.id);
|
tags.push(at.id);
|
||||||
}
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
// 插入书签
|
||||||
// 插入书签
|
|
||||||
db.addBookmark(userId, bookmark) // 插入书签
|
db.addBookmark(userId, bookmark) // 插入书签
|
||||||
.then((bookmark_id) => {
|
.then((bookmark_id) => {
|
||||||
db.delBookmarkTags(bookmark_id); // 不管3721,先删掉旧的分类
|
db.delBookmarkTags(bookmark_id); // 不管3721,先删掉旧的分类
|
||||||
|
|
@ -797,7 +770,7 @@ api.post('/getArticle', function(req, res) {
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
api.checkSnapState = function() {
|
api.checkSnapFaviconState = function() {
|
||||||
db.getBookmarks()
|
db.getBookmarks()
|
||||||
.then((bookmarks) => {
|
.then((bookmarks) => {
|
||||||
bookmarks.forEach(bookmark => {
|
bookmarks.forEach(bookmark => {
|
||||||
|
|
@ -814,8 +787,8 @@ api.checkSnapState = function() {
|
||||||
.catch((err) => console.log('getBookmarks err', err));
|
.catch((err) => console.log('getBookmarks err', err));
|
||||||
}
|
}
|
||||||
|
|
||||||
api.getSnapByTimer = function() {
|
api.getSnapFaviconByTimer = function() {
|
||||||
console.log('getSnapByTimer...........');
|
console.log('getSnapFaviconByTimer...........');
|
||||||
var timeout = 5000
|
var timeout = 5000
|
||||||
setInterval(function() {
|
setInterval(function() {
|
||||||
var today = new Date().getDate();
|
var today = new Date().getDate();
|
||||||
|
|
@ -823,13 +796,18 @@ api.getSnapByTimer = function() {
|
||||||
.then((bookmarks) => {
|
.then((bookmarks) => {
|
||||||
if (bookmarks.length == 1) {
|
if (bookmarks.length == 1) {
|
||||||
var id = bookmarks[0].id;
|
var id = bookmarks[0].id;
|
||||||
var snap_state = bookmarks[0].snap_state;
|
var snapState = bookmarks[0].snap_state;
|
||||||
|
var faviconState = bookmarks[0].snap_state;
|
||||||
var url = bookmarks[0].url;
|
var url = bookmarks[0].url;
|
||||||
var filePath = './public/images/snap/' + id + '.png';
|
var filePath = './public/images/snap/' + id + '.png';
|
||||||
|
var faviconPath = './public/images/favicon/' + id + '.ico';
|
||||||
|
|
||||||
|
// 获取截图
|
||||||
fs.exists(filePath, function(exists) {
|
fs.exists(filePath, function(exists) {
|
||||||
if (exists) {
|
if (exists) {
|
||||||
db.updateBookmarkSnapState(id, -1);
|
if (snapState != -1) {
|
||||||
|
db.updateBookmarkSnapState(id, -1);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)) {
|
if (!/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)) {
|
||||||
db.updateBookmarkSnapState(id, today + 31);
|
db.updateBookmarkSnapState(id, today + 31);
|
||||||
|
|
@ -846,9 +824,9 @@ api.getSnapByTimer = function() {
|
||||||
var newSnapState = -1;
|
var newSnapState = -1;
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log("boomarkid = " + id + ", webshot over", err)
|
console.log("boomarkid = " + id + ", webshot over", err)
|
||||||
if (snap_state == 0 || snap_state == 1) {
|
if (snapState == 0 || snapState == 1) {
|
||||||
newSnapState = snap_state + 1;
|
newSnapState = snapState + 1;
|
||||||
} else if (snap_state == 2) {
|
} else if (snapState == 2) {
|
||||||
newSnapState = today + 31;
|
newSnapState = today + 31;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -856,6 +834,38 @@ api.getSnapByTimer = function() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 获取favicon
|
||||||
|
fs.exists(faviconPath, function(exists) {
|
||||||
|
if (exists) {
|
||||||
|
if (faviconState != -1) {
|
||||||
|
db.updateBookmarkFaviconState(id, -1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)) {
|
||||||
|
db.updateBookmarkFaviconState(id, today + 31);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
favicon(url, function(err, faviconUrl) {
|
||||||
|
if (faviconUrl) {
|
||||||
|
download(faviconUrl, faviconPath, function(err) {
|
||||||
|
var newFaviconState = -1;
|
||||||
|
if (err) {
|
||||||
|
console.log("boomarkid = " + id + ", download over", err)
|
||||||
|
if (faviconState == 0 || faviconState == 1) {
|
||||||
|
newFaviconState = faviconState + 1;
|
||||||
|
} else if (faviconState == 2) {
|
||||||
|
newFaviconState = today + 31;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
db.updateBookmarkFaviconState(id, newFaviconState);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
db.updateBookmarkFaviconState(id, today + 31);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => console.log('getBookmarkWaitSnap err', err));
|
.catch((err) => console.log('getBookmarkWaitSnap err', err));
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,8 @@ 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:获取截图成功。-2:获取ico成功。-3:全部获取成功。0,1,2:获取快照次数。当前天+31:今天不再获取该网页快照
|
`snap_state` tinyint(8) DEFAULT 0, -- -1:获取截图成功。0~2:获取快照次数。当前天+31:今天不再获取该网页快照
|
||||||
|
`favicon_state` tinyint(8) DEFAULT 0, -- -1:获取ico成功。0~2:获取快照次数。当前天+31:今天不再获取该网页快照
|
||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
KEY `userIdIdx` (`user_id`)
|
KEY `userIdIdx` (`user_id`)
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue