优化下载favicon, 30分钟下载一次
This commit is contained in:
parent
cc6f1985c9
commit
57fe592d02
|
|
@ -1015,7 +1015,7 @@ db.getBookmarkWaitSnap = function(today) {
|
||||||
|
|
||||||
db.getBookmarkWaitFavicon = function(today) {
|
db.getBookmarkWaitFavicon = function(today) {
|
||||||
var todayNotSnap = today + 31;
|
var todayNotSnap = today + 31;
|
||||||
var sql = "SELECT id, url, favicon_state FROM `bookmarks` WHERE `favicon_state`>=0 AND `favicon_state` <= 64 AND favicon_state != " + todayNotSnap + " ORDER BY created_at DESC LIMIT 0, 1";
|
var sql = "SELECT id, url, favicon_state FROM `bookmarks` WHERE `favicon_state`>=0 AND `favicon_state` <= 64 AND favicon_state != " + todayNotSnap + " ORDER BY created_at DESC";
|
||||||
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) {
|
||||||
|
|
|
||||||
|
|
@ -1219,35 +1219,23 @@ api.getSnapByTimer = function() {
|
||||||
|
|
||||||
api.getFaviconByTimer = function() {
|
api.getFaviconByTimer = function() {
|
||||||
console.log('getFaviconByTimer...........');
|
console.log('getFaviconByTimer...........');
|
||||||
var timeout = 30000;
|
let timeout = 1000 * 60 * 30; // 半小时更新一次
|
||||||
var busy = false;
|
let busy = false;
|
||||||
setInterval(function() {
|
|
||||||
if (busy) {
|
|
||||||
console.log('getFaviconByTimer is busy')
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
busy = true;
|
|
||||||
var today = new Date().getDate();
|
|
||||||
db.getBookmarkWaitFavicon(today)
|
|
||||||
.then((bookmarks) => {
|
|
||||||
if (bookmarks.length == 1) {
|
|
||||||
var id = bookmarks[0].id;
|
|
||||||
var faviconState = bookmarks[0].favicon_state;
|
|
||||||
var url = encodeURI(bookmarks[0].url);
|
|
||||||
var faviconPath = './public/images/favicon/' + id + '.ico';
|
|
||||||
var defaultFile = './public/images/favicon/default.ico';
|
|
||||||
|
|
||||||
if (!/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)) {
|
let downloadFavicon = async () => {
|
||||||
copyFile(defaultFile, faviconPath);
|
if(busy) return;
|
||||||
db.updateBookmarkFaviconState(id, today + 31)
|
var today = new Date().getDate();
|
||||||
.then((affectedRows) => {
|
try {
|
||||||
busy = false
|
busy = true;
|
||||||
})
|
let bookmarks = await db.getBookmarkWaitFavicon(today);
|
||||||
.catch((err) => {
|
for (let bookmark of bookmarks) {
|
||||||
console.log('updateBookmarkFaviconState err', err);
|
let id = bookmark.id;
|
||||||
busy = false
|
let faviconState = bookmark.favicon_state;
|
||||||
});
|
let url = encodeURI(bookmark.url);
|
||||||
} else {
|
let faviconPath = './public/images/favicon/' + id + '.ico';
|
||||||
|
let defaultFile = './public/images/favicon/default.ico';
|
||||||
|
|
||||||
|
if (/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)) {
|
||||||
// http://www.cnblogs.com/zhangwei595806165/p/4984912.html 各种方法都试一遍
|
// http://www.cnblogs.com/zhangwei595806165/p/4984912.html 各种方法都试一遍
|
||||||
var faviconUrl = "http://47.75.89.228:3000/?url=" + url; // 默认地址
|
var faviconUrl = "http://47.75.89.228:3000/?url=" + url; // 默认地址
|
||||||
if (faviconState == 1) {
|
if (faviconState == 1) {
|
||||||
|
|
@ -1255,44 +1243,35 @@ api.getFaviconByTimer = function() {
|
||||||
} else if (faviconState == 2) {
|
} else if (faviconState == 2) {
|
||||||
faviconUrl = "http://www.google.com/s2/favicons?domain=" + url;
|
faviconUrl = "http://www.google.com/s2/favicons?domain=" + url;
|
||||||
}
|
}
|
||||||
download(faviconUrl).then(data => {
|
|
||||||
|
try {
|
||||||
|
let data = await download(faviconUrl);
|
||||||
fs.writeFileSync(faviconPath, data);
|
fs.writeFileSync(faviconPath, data);
|
||||||
db.updateBookmarkFaviconState(id, -1)
|
faviconState = -1;
|
||||||
.then((affectedRows) => {
|
} catch (error) {
|
||||||
busy = false;
|
console.log("boomarkid = " + id + ", url = " + url + ", download error")
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.log('updateBookmarkFaviconState err', err);
|
|
||||||
busy = false;
|
|
||||||
});
|
|
||||||
}).catch((err) => {
|
|
||||||
var newFaviconState = -1;
|
|
||||||
console.log("boomarkid = " + id + ", url = " + url + ", download over")
|
|
||||||
if (faviconState == 0 || faviconState == 1) {
|
if (faviconState == 0 || faviconState == 1) {
|
||||||
newFaviconState = faviconState + 1;
|
faviconState = faviconState + 1;
|
||||||
} else if (faviconState == 2) {
|
} else if (faviconState == 2) {
|
||||||
newFaviconState = today + 31;
|
faviconState = -1;
|
||||||
copyFile(defaultFile, faviconPath);
|
copyFile(defaultFile, faviconPath);
|
||||||
}
|
}
|
||||||
db.updateBookmarkFaviconState(id, newFaviconState)
|
|
||||||
.then((affectedRows) => {
|
|
||||||
busy = false;
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.log('updateBookmarkFaviconState err', err);
|
|
||||||
busy = false;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
console.log("error http url" + url);
|
||||||
|
faviconState = -1;
|
||||||
|
copyFile(defaultFile, faviconPath);
|
||||||
|
}
|
||||||
|
await db.updateBookmarkFaviconState(id, faviconState);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log('getFaviconByTimer err', err);
|
||||||
|
}
|
||||||
busy = false;
|
busy = false;
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch((err) => {
|
downloadFavicon(); // 一进来先调用一次
|
||||||
console.log('getFaviconByTimer err', err);
|
setInterval(downloadFavicon, timeout);
|
||||||
busy = false;
|
|
||||||
});
|
|
||||||
}, timeout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
api.getHotBookmarksByTimer = function() {
|
api.getHotBookmarksByTimer = function() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue