优化爬虫,增加缓存,线程池,保存浏览器用户信息
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* 按 sessionKey 解析持久 Chrome Profile 目录
|
||||
*/
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const { RUNTIME_DIR, PERSIST_BROWSER_PROFILE, PROFILE_MAX_AGE_MS } = require('./constants');
|
||||
|
||||
const PROFILES_DIR = path.join(RUNTIME_DIR, 'profiles');
|
||||
|
||||
/**
|
||||
* @param {string} sessionKey
|
||||
* @param {'standard'|'real'} profile
|
||||
* @returns {string|null}
|
||||
*/
|
||||
function resolveProfileDir(sessionKey, profile = 'standard') {
|
||||
if (!sessionKey || !PERSIST_BROWSER_PROFILE) {
|
||||
return null;
|
||||
}
|
||||
const hash = crypto.createHash('md5').update(String(sessionKey)).digest('hex');
|
||||
const dir = path.join(PROFILES_DIR, `${hash}-${profile}`);
|
||||
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
||||
return dir;
|
||||
}
|
||||
|
||||
function profileExists(sessionKey, profile = 'standard') {
|
||||
const dir = resolveProfileDir(sessionKey, profile);
|
||||
if (!dir) return false;
|
||||
return fs.existsSync(dir);
|
||||
}
|
||||
|
||||
function pruneStaleProfiles() {
|
||||
if (!PERSIST_BROWSER_PROFILE || PROFILE_MAX_AGE_MS <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if (!fs.existsSync(PROFILES_DIR)) {
|
||||
return 0;
|
||||
}
|
||||
const now = Date.now();
|
||||
let removed = 0;
|
||||
for (const name of fs.readdirSync(PROFILES_DIR)) {
|
||||
const full = path.join(PROFILES_DIR, name);
|
||||
try {
|
||||
const stat = fs.statSync(full);
|
||||
if (!stat.isDirectory()) continue;
|
||||
if (now - stat.mtimeMs > PROFILE_MAX_AGE_MS) {
|
||||
fs.rmSync(full, { recursive: true, force: true });
|
||||
removed++;
|
||||
}
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
function getProfileStats() {
|
||||
if (!fs.existsSync(PROFILES_DIR)) {
|
||||
return { profilesDir: PROFILES_DIR, count: 0 };
|
||||
}
|
||||
const count = fs.readdirSync(PROFILES_DIR).filter((name) => {
|
||||
try {
|
||||
return fs.statSync(path.join(PROFILES_DIR, name)).isDirectory();
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}).length;
|
||||
return { profilesDir: PROFILES_DIR, count };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
PROFILES_DIR,
|
||||
resolveProfileDir,
|
||||
profileExists,
|
||||
pruneStaleProfiles,
|
||||
getProfileStats,
|
||||
};
|
||||
Reference in New Issue
Block a user