将获取faicon的分离出来,重新开个timer
This commit is contained in:
parent
5591426a92
commit
75b4d1d9f1
3
app.js
3
app.js
|
|
@ -76,6 +76,7 @@ app.use(function(err, req, res, next) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// api.checkSnapFaviconState();
|
// api.checkSnapFaviconState();
|
||||||
api.getSnapFaviconByTimer();
|
api.getSnapByTimer();
|
||||||
|
api.getFaviconByTimer();
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
||||||
|
|
|
||||||
|
|
@ -719,7 +719,21 @@ 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, 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";
|
var sql = "SELECT id, url, snap_state, favicon_state FROM `bookmarks` WHERE `snap_state`>=0 AND `snap_state` <= 64 AND snap_state != " + todayNotSnap + " ORDER BY created_at DESC LIMIT 0, 1";
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
client.query(sql, (err, result) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
db.getBookmarkWaitFavicon = function(today) {
|
||||||
|
var todayNotSnap = today + 31;
|
||||||
|
var sql = "SELECT id, url, snap_state, favicon_state FROM `bookmarks` WHERE `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) {
|
||||||
|
|
|
||||||
|
|
@ -877,9 +877,9 @@ api.checkSnapFaviconState = function() {
|
||||||
.catch((err) => console.log('getBookmarks err', err));
|
.catch((err) => console.log('getBookmarks err', err));
|
||||||
}
|
}
|
||||||
|
|
||||||
api.getSnapFaviconByTimer = function() {
|
api.getSnapByTimer = function() {
|
||||||
console.log('getSnapFaviconByTimer...........');
|
console.log('getSnapByTimer...........');
|
||||||
var timeout = 5000
|
var timeout = 30000
|
||||||
setInterval(function() {
|
setInterval(function() {
|
||||||
var today = new Date().getDate();
|
var today = new Date().getDate();
|
||||||
db.getBookmarkWaitSnap(today)
|
db.getBookmarkWaitSnap(today)
|
||||||
|
|
@ -887,11 +887,8 @@ api.getSnapFaviconByTimer = function() {
|
||||||
if (bookmarks.length == 1) {
|
if (bookmarks.length == 1) {
|
||||||
var id = bookmarks[0].id;
|
var id = bookmarks[0].id;
|
||||||
var snapState = bookmarks[0].snap_state;
|
var snapState = bookmarks[0].snap_state;
|
||||||
var faviconState = bookmarks[0].favicon_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) {
|
||||||
|
|
@ -924,19 +921,32 @@ api.getSnapFaviconByTimer = function() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err) => console.log('getBookmarkWaitSnap err', err));
|
||||||
|
}, timeout + 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
api.getFaviconByTimer = function() {
|
||||||
|
console.log('getFaviconByTimer...........');
|
||||||
|
var timeout = 3000
|
||||||
|
setInterval(function() {
|
||||||
|
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 = 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)) {
|
if (!/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)) {
|
||||||
fs.exists(faviconPath, function(exists) {
|
copyFile(defaultFile, faviconPath);
|
||||||
if (!exists) {
|
|
||||||
var sourceFile = './public/images/favicon/default.ico';
|
|
||||||
var readStream = fs.createReadStream(sourceFile);
|
|
||||||
var writeStream = fs.createWriteStream(faviconPath);
|
|
||||||
readStream.pipe(writeStream);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
db.updateBookmarkFaviconState(id, today + 31);
|
db.updateBookmarkFaviconState(id, today + 31);
|
||||||
}else{
|
}else{
|
||||||
var faviconUrl = "http://g.soz.im/" + url + "/cdn.ico"
|
var faviconUrl = "http://g.soz.im/" + url;
|
||||||
download(faviconUrl).then(data => {
|
download(faviconUrl).then(data => {
|
||||||
fs.writeFileSync(faviconPath, data);
|
fs.writeFileSync(faviconPath, data);
|
||||||
db.updateBookmarkFaviconState(id, -1);
|
db.updateBookmarkFaviconState(id, -1);
|
||||||
|
|
@ -947,18 +957,29 @@ api.getSnapFaviconByTimer = function() {
|
||||||
newFaviconState = faviconState + 1;
|
newFaviconState = faviconState + 1;
|
||||||
} else if (faviconState == 2) {
|
} else if (faviconState == 2) {
|
||||||
newFaviconState = today + 31;
|
newFaviconState = today + 31;
|
||||||
|
copyFile(defaultFile, faviconPath);
|
||||||
}
|
}
|
||||||
db.updateBookmarkFaviconState(id, newFaviconState);
|
db.updateBookmarkFaviconState(id, newFaviconState);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => console.log('getBookmarkWaitSnap err', err));
|
.catch((err) => console.log('getFaviconByTimer err', err));
|
||||||
}, timeout + 1000);
|
}, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
function md5(str) {
|
function md5(str) {
|
||||||
return crypto.createHash('md5').update(str).digest('hex');
|
return crypto.createHash('md5').update(str).digest('hex');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function copyFile(sourceFile, destFile){
|
||||||
|
fs.exists(sourceFile, function(exists) {
|
||||||
|
if (exists) {
|
||||||
|
var readStream = fs.createReadStream(sourceFile);
|
||||||
|
var writeStream = fs.createWriteStream(destFile);
|
||||||
|
readStream.pipe(writeStream);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = api;
|
module.exports = api;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue