mwoffliner
Version:
MediaWiki ZIM scraper
130 lines • 4.18 kB
JavaScript
import { createClient } from 'redis';
import RedisKvs from './util/RedisKvs.js';
import * as logger from './Logger.js';
class RedisStore {
static instance;
constructor() {
this.
}
get client() {
return this.
}
get filesToDownloadXPath() {
return this.
}
get articleDetailXId() {
return this.
}
get redirectsXId() {
return this.
}
get filesQueues() {
return this.
}
static getInstance() {
if (!RedisStore.instance) {
RedisStore.instance = new RedisStore();
}
return RedisStore.instance;
}
setOptions(redisPath, opts) {
if (RedisStore.instance) {
const options = { ...opts };
const quitOnError = !(options.quitOnError === false);
delete options.quitOnError;
if (redisPath.startsWith('/') || redisPath.startsWith('./')) {
options.socket = {
...options.socket,
path: redisPath,
};
}
else {
options.url = redisPath;
}
this.
this.
if (quitOnError) {
logger.error('Redis Client Error', err);
process.exit(3);
}
});
}
else {
throw new Error('Redis store has not been instantiated before setting options');
}
}
async connect(populateStores = true) {
if (this.
return;
}
await this.
if (populateStores) {
await this.checkForExistingStores();
await this.populateStores();
this.
}
}
async close() {
if (this.
logger.log('Flushing Redis DBs');
await Promise.all([this.
}
if (this.
await this.
}
}
async checkForExistingStores() {
const patterns = ['*-media', '*-detail', '*-redirect', '*-files'];
let keys = [];
for (const pattern of patterns) {
keys = keys.concat(await this.
}
keys.forEach(async (key) => {
try {
const length = await this.
const time = new Date(Number(key.slice(0, key.indexOf('-'))));
logger.warn(`Deleting store from previous run from ${time} that was still in Redis: ${key} with length ${length}`);
this.
}
catch {
logger.error(`Key ${key} exists in DB, and is no hash.`);
}
});
}
async populateStores() {
this.
u: 'url',
m: 'mult',
w: 'width',
});
this.
s: 'subCategories',
c: 'categories',
p: 'pages',
h: 'thumbnail',
g: 'coordinates',
t: 'timestamp',
r: 'revisionId',
i: 'internalThumbnailUrl',
m: 'missing',
n: 'title',
});
this.
f: 'fragment',
t: 'targetId',
n: 'title',
});
}
createRedisKvs(...args) {
return new RedisKvs(this.
}
}
const rs = RedisStore.getInstance();
export default rs;
//# sourceMappingURL=RedisStore.js.map