zotero-categorise
Version:
A command-line tool to manage Zotero collections by placing items into specific collections based on their title, description, and tags.
144 lines (143 loc) • 6.9 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.collection = void 0;
const zotero_lib_1 = __importDefault(require("zotero-lib"));
const addItemToCollection_1 = require("./addItemToCollection");
function collection(commanderOptions) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
let FinalOutput = '';
const itemId = commanderOptions.item;
const collectionId = commanderOptions.collection;
const testmode = commanderOptions.test;
const ignoretag = commanderOptions.ignoretag || [];
const addtag = commanderOptions.addtag || [];
if (!collectionId) {
console.log('Please provide a collection');
return;
}
const { itemsfromcollection, itemswithtag, itemswithouttag, itemsfromlibrary } = commanderOptions;
const itemOptions = [itemId, itemsfromcollection, itemswithtag, itemswithouttag, itemsfromlibrary];
if (itemOptions.filter(Boolean).length > 1) {
console.log('Only one of these options should be used at a time:');
console.log('--item');
console.log('--itemsfromcollection');
console.log('--itemswithtag');
console.log('--itemswithouttag');
console.log('--itemsfromlibrary');
return;
}
const groupid = commanderOptions.group;
let zotero;
if (groupid) {
zotero = new zotero_lib_1.default({ verbose: false, 'group-id': groupid });
}
else {
zotero = new zotero_lib_1.default({ verbose: false });
}
let items = itemId;
let fetched = [];
if (itemsfromcollection) {
fetched = yield zotero.items({ collection: itemsfromcollection });
}
else if (itemswithtag) {
fetched = yield zotero.items({ filter: { tag: itemswithtag } });
}
else if (itemswithouttag) {
fetched = yield zotero.items({ filter: { tag: `-${itemswithouttag}` } });
}
else if (itemsfromlibrary) {
fetched = yield zotero.items({});
}
if (fetched.length) {
items = fetched.map((item) => item.data);
}
if (!items || !items.length) {
console.log('No items to process');
return;
}
const options = {
top: false,
key: [collectionId[0]],
verbose: false,
recursive: commanderOptions.recursive,
};
const listCollections = [];
let result;
try {
// fetch the collection
result = yield zotero.collection(options);
FinalOutput += 'Number of subcollections for ' + JSON.stringify(collectionId[0]) + ' : ';
if (!result) {
throw new Error(`There is no collection with this key ${collectionId[0]}`);
}
FinalOutput += ((_a = result.meta) === null || _a === void 0 ? void 0 : _a.numCollections) + '\n';
console.log('Number of subcollections for ' + JSON.stringify(collectionId[0]) + ' : ' + ((_b = result.meta) === null || _b === void 0 ? void 0 : _b.numCollections));
// check if there is sub collections
if (((_c = result.meta) === null || _c === void 0 ? void 0 : _c.numCollections) == 0)
throw new Error(`There is no sub collection in this collection ${collectionId[0]} please add a sub collection`);
function addChildren(listCollections, parent) {
return __awaiter(this, void 0, void 0, function* () {
for (const child of parent.children) {
listCollections.push({
terms: [{ term: child.data.name, type: 'word' }],
collection: child.data.key,
collection_name: child.data.name,
situation: 'nothing',
});
if (child.children.length > 0) {
yield addChildren(listCollections, child);
}
}
// console.log('child', parent.children[0].data.name);
});
}
// fetch the sub collections
const results = yield zotero.collections(options);
results.forEach((collectionkey) => __awaiter(this, void 0, void 0, function* () {
var _d;
listCollections.push({
terms: [{ term: collectionkey.data.name, type: 'word' }],
collection: collectionkey.data.key,
collection_name: collectionkey.data.name,
situation: 'nothing',
});
if (options.recursive && ((_d = collectionkey.children) === null || _d === void 0 ? void 0 : _d.length) > 0) {
yield addChildren(listCollections, collectionkey);
}
}));
// fs.writeFileSync('listCollections.json', JSON.stringify(listCollections));
const listCollectionsForOutput = listCollections.map((item) => [item.collection_name, item.collection]);
FinalOutput += 'Subcollections :' + JSON.stringify(listCollectionsForOutput) + '\n';
console.log('Subcollections :' + JSON.stringify(listCollectionsForOutput));
}
catch (error) {
console.log(error.message);
}
if (!listCollections.length)
return;
for (const item of items) {
// add item to collection
FinalOutput = yield (0, addItemToCollection_1.addItemToCollection)(item, zotero, listCollections, testmode, FinalOutput, ignoretag, addtag);
for (const element of listCollections) {
element.situation = 'nothing';
}
}
if (testmode) {
console.log('\n\nOutput:\n' + FinalOutput);
}
});
}
exports.collection = collection;