zotero-categorise
Version:
A command-line tool to manage Zotero collections by placing items into specific collections based on their title, description, and tags.
114 lines (113 loc) • 5.14 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.generateByJSon = void 0;
const fs_1 = __importDefault(require("fs"));
const zotero_lib_1 = __importDefault(require("zotero-lib"));
const addItemToCollection_1 = require("./addItemToCollection");
// this function is used to categorize items from a JSON file into Zotero collections
function generateByJSon(commanderOptions) {
return __awaiter(this, void 0, void 0, function* () {
// check if the user provided a json file
if (!commanderOptions.json) {
console.log('Please provide a json file');
return;
}
const { itemsfromcollection, itemswithtag, itemswithouttag, itemsfromlibrary } = commanderOptions;
const itemOptions = [commanderOptions.item, 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 = commanderOptions.item;
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 jsonFile = commanderOptions.json;
const data = fs_1.default.readFileSync(jsonFile, 'utf8');
const json = JSON.parse(data);
if (!json.source_collections) {
console.log('Please provide a json file with collections');
return;
}
if (commanderOptions.ignoretag) {
json.ignoretag = [...(json.ignoretag || []), ...commanderOptions.ignoretag];
}
if (commanderOptions.addtag) {
json.addtag = [...(json.addtag || []), ...commanderOptions.addtag];
}
const testmode = commanderOptions.test;
const resultcollcections = [];
// get the collections from the json file
// make a list of collections with their terms,name and situation
for (const element of json.source_collections) {
if (!element.collections)
continue;
for (const collle of element.collections) {
const termsList = [];
for (const term of collle.terms) {
termsList.push({ term: term.term, type: term.type });
}
resultcollcections.push({
terms: termsList,
collection: collle.collection,
collection_name: collle.collection_name,
situation: 'nothing',
});
}
}
let FinalOutput = '';
// add the items to the collections for each item
for (const item of items) {
const id = typeof item === 'string' ? item : item.key;
FinalOutput += '\n\nItem ' + id + ' :\n';
FinalOutput = yield (0, addItemToCollection_1.addItemToCollection)(item, zotero, resultcollcections, testmode, FinalOutput, json.ignoretag, json.addtag);
for (const element of resultcollcections) {
element.situation = 'nothing';
}
}
console.log(FinalOutput);
});
}
exports.generateByJSon = generateByJSon;