let collection = ZoteroPane.getSelectedCollection();
let itemIDs = collection.getChildItems(true);
const fieldName = 'extra'
const fieldID = Zotero.ItemFields.getID(fieldName);
function extractCodeURL(data) {
if (!data || data.length == 0) return "";
if (data[0].repository && data[0].repository.url) return data[0].repository.url;
return "";
}
async function getCodeURL(title) {
const url = 'https://paperswithcode.com/api/v1/search/?q=' + title
return fetch(url)
.then(res => res.json())
.then(data => data.results)
.then(results => results.filter(
item => item.paper.title === title
))
.then(filterdResults => extractCodeURL(filterdResults))
}
let results = []
await Zotero.DB.executeTransaction(async function () {
for (let id of itemIDs) {
let item = await Zotero.Items.getAsync(id);
let codeURL = await getCodeURL(item.getField('title'))
if (codeURL) {
results.push(codeURL);
// Add `Code/YES` tag
item.addTag('Code/YES', 0);
}
let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
item.setField(mappedFieldID ? mappedFieldID : fieldID, codeURL);
await item.save({
skipDateModifiedUpdate: true
});
}
});
return results;