Skip to content

Instantly share code, notes, and snippets.

@digitalbase
Created March 9, 2021 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalbase/bb549d7a01b95048cc02c36476b2b10a to your computer and use it in GitHub Desktop.
Save digitalbase/bb549d7a01b95048cc02c36476b2b10a to your computer and use it in GitHub Desktop.
const AirtablePlus = require('airtable-plus');
const fetch = require('node-fetch');
const airtable = new AirtablePlus({
baseID: 'app6ttxB70e3TjmCL',
apiKey: 'AIRTABLE_API_KEY',
tableName: 'Themes',
});
const token = 'AHREF_API_TOKEN';
(async () => {
try {
const themes = await airtable.read({ view: 'Newsroom', filterByFormula: `({UR} = '')`});
for (let theme of themes) {
console.log(`Running report for ${theme.fields.Url}`);
try {
const url = encodeURIComponent(theme.fields.Url);
const urlRating = await fetch(`https://apiv2.ahrefs.com/?token=${token}&from=ahrefs_rank&target=${url}&limit=10&output=json&mode=exact`)
.then(response => response.json())
.then(data => {
return data.pages[0].ahrefs_rank;
});
const domainRating = await fetch(`https://apiv2.ahrefs.com/?token=${token}&from=domain_rating&target=${url}&limit=10&output=json&mode=exact`)
.then(response => response.json())
.then(data => {
// could also use ahrefs_top
return data.domain.domain_rating;
});
const referringDomainsRating = await fetch(`https://apiv2.ahrefs.com/?token=${token}&from=refdomains&target=${url}&limit=10&output=json&mode=exact`)
.then(response => response.json())
.then(data => {
return data.stats.refdomains;
});
const organicTraffic = await fetch(`https://apiv2.ahrefs.com/?token=${token}&from=positions_metrics&target=${url}&limit=10&output=json&mode=exact`)
.then(response => response.json())
.then(data => {
return data.metrics.traffic;
});
const data = {
'UR' : urlRating/100,
'DR' : domainRating/100,
'Referring Domains' : referringDomainsRating,
'Traffic Estimate' : organicTraffic,
}
console.log(data);
await airtable.update(theme.id, data);
} catch (Error) {
console.log(Error);
}
}
} catch (Error) {
console.log(Error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment