Last active
May 28, 2021 17:40
-
-
Save rviscomi/bea98941f05462c752bd5498f3cb5c70 to your computer and use it in GitHub Desktop.
Apps Script code for syncing MDN content popularity stats to Google Sheets to be visualized in the content dashboard: bit.ly/mdn-content-dash
This file contains hidden or 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
// Written by Rick Viscomi (@rick_Viscomi) | |
const MDN_JSON = 'https://raw.githubusercontent.com/mdn/content/main/files/popularities.json'; | |
const MDN_JSON_COMMITS = 'https://api.github.com/repos/mdn/content/commits?path=files/popularities.json'; | |
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Raw data') | |
function run() { | |
const lastCommit = getLastCommit(); | |
const lastCommitDate = new Date(lastCommit.author.date).toLocaleDateString(); | |
const lastEntryDate = new Date(sheet.getRange(sheet.getLastRow(), 1).getValue()).toLocaleDateString(); | |
if (lastCommitDate == lastEntryDate) { | |
console.log('No recent changes. Done.'); | |
return; | |
} | |
const popularities = fetchData(); | |
const lastCommitMessage = lastCommit.message.split('/n')[0]; | |
const data = Object.entries(popularities).map(([page, popularity]) => { | |
return [lastCommitDate, page, popularity, lastCommitMessage]; | |
}); | |
sheet.getRange(sheet.getLastRow() + 1, 1, data.length, data[0].length).setValues(data); | |
} | |
function fetchData() { | |
const response = UrlFetchApp.fetch(MDN_JSON).getContentText(); | |
return JSON.parse(response); | |
} | |
function getLastCommit() { | |
const response = UrlFetchApp.fetch(MDN_JSON_COMMITS).getContentText(); | |
const commits = JSON.parse(response); | |
return commits[0].commit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment