Created
March 28, 2022 12:49
-
-
Save zwithz/b254496838356490e7af315e0939aff6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment