mirror of
https://github.com/wassname/talk.git
synced 2026-06-29 05:18:32 +08:00
f488b0e02d
- Adjusted docs - Fixed issues with process terminating wrong
105 lines
2.2 KiB
JavaScript
Executable File
105 lines
2.2 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Module dependencies.
|
|
*/
|
|
|
|
const program = require('./commander');
|
|
const parseDuration = require('parse-duration');
|
|
const Table = require('cli-table');
|
|
const AssetModel = require('../models/asset');
|
|
const mongoose = require('../services/mongoose');
|
|
const scraper = require('../services/scraper');
|
|
const util = require('./util');
|
|
|
|
// Register the shutdown criteria.
|
|
util.onshutdown([
|
|
() => mongoose.disconnect()
|
|
]);
|
|
|
|
/**
|
|
* Lists all the assets registered in the database.
|
|
*/
|
|
function listAssets() {
|
|
AssetModel
|
|
.find({})
|
|
.sort({'created_at': 1})
|
|
.then((asset) => {
|
|
let table = new Table({
|
|
head: [
|
|
'ID',
|
|
'Title',
|
|
'URL'
|
|
]
|
|
});
|
|
|
|
asset.forEach((asset) => {
|
|
table.push([
|
|
asset.id,
|
|
asset.title ? asset.title : '',
|
|
asset.url ? asset.url : ''
|
|
]);
|
|
});
|
|
|
|
console.log(table.toString());
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
function refreshAssets(ageString) {
|
|
const now = new Date().getTime();
|
|
const ageMs = parseDuration(ageString);
|
|
const age = new Date(now - ageMs);
|
|
|
|
AssetModel.find({
|
|
$or: [
|
|
{
|
|
scraped: {
|
|
$lte: age
|
|
}
|
|
},
|
|
{
|
|
scraped: null
|
|
}
|
|
]
|
|
})
|
|
|
|
// Queue all the assets for scraping.
|
|
.then((assets) => Promise.all(assets.map(scraper.create)))
|
|
|
|
.then(() => {
|
|
console.log('Assets were queued to be scraped');
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
//==============================================================================
|
|
// Setting up the program command line arguments.
|
|
//==============================================================================
|
|
|
|
program
|
|
.command('list')
|
|
.description('list all the assets in the database')
|
|
.action(listAssets);
|
|
|
|
program
|
|
.command('refresh <age>')
|
|
.description('queues the assets that exceed the age requested')
|
|
.action(refreshAssets);
|
|
|
|
program.parse(process.argv);
|
|
|
|
// If there is no command listed, output help.
|
|
if (!process.argv.slice(2).length) {
|
|
program.outputHelp();
|
|
util.shutdown();
|
|
}
|