Last active
November 16, 2020 20:46
-
-
Save leosoto/b5de8e23b7665dda0e17d030c7c7d187 to your computer and use it in GitHub Desktop.
airtable-example-post-to-external-api-integration.js
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
const control = base.getTable("Enqueue Scraping Control"); | |
const controlResult = await control.selectRecordsAsync(); | |
for (let controlRecord of controlResult.records) { | |
let name = controlRecord.getCellValueAsString("Name") | |
console.log("Enqueing " + name); | |
let table = base.getTable(controlRecord.getCellValue("table")); | |
let fieldPresentIfScraped = controlRecord.getCellValueAsString("fieldPresentIfScraped"); | |
let lastEnqueueTime = Date.parse(controlRecord.getCellValue("lastEnqueueTime")); | |
let apiUrl = controlRecord.getCellValue("apiUrl"); | |
let fieldWithUrlToEnqueue = controlRecord.getCellValueAsString("fieldWithUrlToEnqueue"); | |
let newEnqueueTime = new Date().toISOString() | |
let result = await table.selectRecordsAsync(); | |
let urls = ( | |
result.records.filter(record => | |
record.getCellValueAsString(fieldPresentIfScraped) === '' && | |
Date.parse(record.getCellValue('Created time')) > lastEnqueueTime | |
).map(record => { | |
return {url: record.getCellValueAsString(fieldWithUrlToEnqueue)} | |
}) | |
) | |
if (urls.length > 0) { | |
console.log("Adding " + urls.length + " records to scraping sheet:") | |
console.log(urls) | |
let response = await fetch(apiUrl, { | |
method: 'POST', | |
body: JSON.stringify(urls), | |
headers: {'Content-Type': 'application/json'}, | |
}); | |
console.log("Setting new enqueue time to " + newEnqueueTime); | |
control.updateRecordAsync(controlRecord, {"lastEnqueueTime": newEnqueueTime}) | |
} else { | |
console.log("Everything is already scraped or enqueued") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment