自动获取favicon图标

This commit is contained in:
luchenqun 2017-02-28 11:14:31 +08:00
parent b7b58e5023
commit 3832b573b5
10 changed files with 121 additions and 51 deletions

1
.gitignore vendored
View File

@ -39,3 +39,4 @@ jspm_packages
# Upload File
uploads
/public/images/snap/
/public/images/favicon/

4
app.js
View File

@ -75,7 +75,7 @@ app.use(function(err, req, res, next) {
error: {}
});
});
// api.checkSnapState();
api.getSnapByTimer();
// api.checkSnapFaviconState();
api.getSnapFaviconByTimer();
module.exports = app;

32
common/download.js Normal file
View File

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

View File

@ -696,7 +696,7 @@ db.getBookmarks = function() {
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 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) {
client.query(sql, (err, result) => {
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;

View File

@ -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);
}
});
}
}
});

View File

@ -30,7 +30,7 @@
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;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>
</div>
</div>
@ -56,7 +56,7 @@
<tbody>
<tr ng-repeat="bookmark in bookmarks" id="{{ bookmark.id }}">
<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;">
{{ bookmark.title }}
</span>

View File

@ -130,7 +130,7 @@
<tbody>
<tr ng-repeat="bookmark in bookmarks" id="{{ bookmark.id }}">
<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;">
{{ bookmark.title }}
</span>

View File

@ -63,7 +63,7 @@
<tbody>
<tr ng-repeat="bookmark in bookmarks" id="{{ bookmark.id }}">
<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;">
{{ bookmark.title }}
</span>

View File

@ -4,39 +4,12 @@ var crypto = require('crypto');
var read = require('node-readability');
var db = require('../database/db.js');
var parseHtml = require('../common/parse_html.js');
var download = require('../common/download.js');
var multer = require('multer');
var webshot = require('webshot');
var fs = require('fs');
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({
destination: function(req, file, cb) {
cb(null, 'uploads/')
@ -574,13 +547,13 @@ api.post('/uploadBookmarkFile', upload.single('bookmark'), function(req, res) {
var tags = [];
item.tags.forEach((tag) => {
allTags.forEach((at) => {
if (at.name == tag) {
tags.push(at.id);
}
allTags.forEach((at) => {
if (at.name == tag) {
tags.push(at.id);
}
})
})
})
// 插入书签
// 插入书签
db.addBookmark(userId, bookmark) // 插入书签
.then((bookmark_id) => {
db.delBookmarkTags(bookmark_id); // 不管3721先删掉旧的分类
@ -797,7 +770,7 @@ api.post('/getArticle', function(req, res) {
});
})
api.checkSnapState = function() {
api.checkSnapFaviconState = function() {
db.getBookmarks()
.then((bookmarks) => {
bookmarks.forEach(bookmark => {
@ -814,8 +787,8 @@ api.checkSnapState = function() {
.catch((err) => console.log('getBookmarks err', err));
}
api.getSnapByTimer = function() {
console.log('getSnapByTimer...........');
api.getSnapFaviconByTimer = function() {
console.log('getSnapFaviconByTimer...........');
var timeout = 5000
setInterval(function() {
var today = new Date().getDate();
@ -823,13 +796,18 @@ api.getSnapByTimer = function() {
.then((bookmarks) => {
if (bookmarks.length == 1) {
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 filePath = './public/images/snap/' + id + '.png';
var faviconPath = './public/images/favicon/' + id + '.ico';
// 获取截图
fs.exists(filePath, function(exists) {
if (exists) {
db.updateBookmarkSnapState(id, -1);
if (snapState != -1) {
db.updateBookmarkSnapState(id, -1);
}
} else {
if (!/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)) {
db.updateBookmarkSnapState(id, today + 31);
@ -846,9 +824,9 @@ api.getSnapByTimer = function() {
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) {
if (snapState == 0 || snapState == 1) {
newSnapState = snapState + 1;
} else if (snapState == 2) {
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));

View File

@ -24,7 +24,8 @@ 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获取截图成功。-2获取ico成功。-3全部获取成功。012获取快照次数。当前天+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`),
KEY `userIdIdx` (`user_id`)
);