You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/scripts/commands/database/clear.js

48 lines
1.1 KiB
JavaScript

2 years ago
const { logger, parser, db, date } = require('../../core')
const { program } = require('commander')
const options = program
.option(
'-t, --threshold <threshold>',
'Number of days after which the stream should be deleted',
parser.parseNumber,
7
)
.option('--input-dir <input-dir>', 'Set path to input directory', 'streams')
.parse(process.argv)
.opts()
async function main() {
await db.streams.load()
const streams = await db.streams.all()
2 years ago
let buffer = {}
let removed = 0
2 years ago
logger.info('searching...')
2 years ago
for (const stream of streams) {
if (
stream.status === 'error' &&
date.utc().diff(stream.updated_at, 'day') >= options.threshold
) {
2 years ago
logger.info(`${stream.url} (offline)`)
removed += await db.streams.remove({ url: stream.url }, { multi: true })
}
2 years ago
2 years ago
const key = stream.url.toLowerCase()
if (buffer[key]) {
logger.info(`${stream.url} (duplicate)`)
await db.streams.remove({ _id: stream._id })
removed++
} else {
buffer[key] = true
2 years ago
}
}
await db.streams.compact()
2 years ago
logger.info(`removed ${removed} streams`)
2 years ago
}
main()