zotero-categorise
Version:
A command-line tool to manage Zotero collections by placing items into specific collections based on their title, description, and tags.
96 lines (95 loc) • 4.64 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.generate = void 0;
const zotero_lib_1 = __importDefault(require("zotero-lib"));
const fs_1 = __importDefault(require("fs"));
function generate(commanderOptions) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let zotero;
const groupid = commanderOptions.group;
if (!groupid) {
zotero = new zotero_lib_1.default({ verbose: false });
}
else {
zotero = new zotero_lib_1.default({ verbose: false, 'group-id': groupid });
}
if (!commanderOptions.collection) {
console.log('You must provide at least one collection');
return;
}
const list = {};
list.library = groupid;
list.source_collections = [];
for (const collectionId of commanderOptions.collection) {
const collectionList = {};
collectionList.source_collection_name = '';
collectionList.source_collection = collectionId;
collectionList.collections = [];
const options = {
top: false,
key: [collectionId],
verbose: false,
recursive: commanderOptions.recursive,
};
// fetch the collection
const result = yield zotero.collection(options);
// check if the collection exists
if (!result || !result.data) {
console.log(`There is no collection with this key ${collectionId}`);
continue;
}
collectionList.source_collection_name = result.data.name;
if (list.library === undefined)
list.library = result.library.id;
// fetch the subcollections
const results = yield zotero.collections(options);
// fs.writeFileSync('collection.json', JSON.stringify(results));
function addChildren(collection, parent) {
return __awaiter(this, void 0, void 0, function* () {
for (const child of parent.children) {
// console.log('child name', child.data.name);
collection.push({
collection_name: child.data.name,
collection: `zotero://select/groups/${list.library}/collections/${child.key}`,
terms: [{ term: child.data.name, description: 'main', type: 'word' }],
});
if (child.children.length > 0) {
addChildren(collection, child);
}
}
});
}
// make a list of the subcollections with the collection name, the collection key and the terms
for (const collectionkey of results) {
collectionList.collections.push({
collection_name: collectionkey.data.name,
collection: `zotero://select/groups/${list.library}/collections/${collectionkey.key}`,
terms: [{ term: collectionkey.data.name, description: 'main', type: 'word' }],
});
if (commanderOptions.recursive && ((_a = collectionkey.children) === null || _a === void 0 ? void 0 : _a.length) > 0) {
yield addChildren(collectionList.collections, collectionkey);
}
console.log(collectionkey.data);
}
list.source_collections.push(collectionList);
}
list.ignoretag = [];
list.addtag = [];
const filename = commanderOptions.name;
fs_1.default.writeFileSync(filename, JSON.stringify(list));
});
}
exports.generate = generate;