Skip to content

Instantly share code, notes, and snippets.

@alexdesousa
Last active February 21, 2020 20:22
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 alexdesousa/a23552ed2995a865895f53a4fd844c6b to your computer and use it in GitHub Desktop.
Save alexdesousa/a23552ed2995a865895f53a4fd844c6b to your computer and use it in GitHub Desktop.
Gathers stats from your DEV articles.
/**
* Gathers statistics from your dev.to articles.
*
* Needs the following colunms:
* - Day
* - Date
* - Time
* - Title
* - Views
* - Reactions
* - Comments
* - Organization (optional).
* - Repository (optional).
* - Stars (before) (optional): If there's a number won't be overwritten.
* - Stars (current) (optional).
*
* Just set the following variables:
*
* - API_KEY: Should be your DEV API key.
* - SHEET_NAME: Should be the name of your Google Sheet sheet.
* - AMOUNT: How many articles it should fetch.
*
* The function DEV_STATS() should be the one to be called.
*/
const API_KEY = "[API KEY]";
const SHEET_NAME = "[SHEET NAME]"
const AMOUNT = 500;
/**
* Formats datetime as day of the week.
* @params timestamp Datetime in ISO8601 format.
*
* @returns Day of the week.
*/
function formatDay(timestamp) {
return (new Date(timestamp).toLocaleString('en-us', { weekday: 'long' }));
}
/**
* Formats datetime as date.
* @params timestamp Datetime in ISO8601 format.
*
* @returns YYYY-MM-DD
*/
function formatDate(timestamp) {
return timestamp.substring(0, 10);
}
/**
* Formats datetime as time.
* @params timestamp Datetime in ISO8601 format.
*
* @returns HH:mm
*/
function formatTime(timestamp) {
return timestamp.substring(11, 16);
}
/**
* Requests stats.
*
* @returns JSON DEV stats.
*/
function getDevStats() {
const API_ENDPOINT = "https://dev.to/api/articles/me/published?per_page=" + AMOUNT;
const HEADERS = {
"headers": {
"access-control-allow-headers": "Content-Type",
"api-key": API_KEY
}
};
try {
const response = UrlFetchApp.fetch(API_ENDPOINT, HEADERS);
return JSON.parse(response.getContentText());
} catch (err) {
throw new Error(err);
}
}
/**
* Fills DEV data.
* @params sheet Sheet.
* @params row Row number.
* @params data JSON DEV data.
*/
function fillDevData(sheet, row, data) {
const day = formatDay(data.published_timestamp);
const date = formatDate(data.published_timestamp);
const time = formatTime(data.published_timestamp);
sheet.getRange(row, 1).setValue(day);
sheet.getRange(row, 2).setValue(date);
sheet.getRange(row, 3).setValue(time);
sheet.getRange(row, 4).setValue(data.title);
sheet.getRange(row, 5).setValue(data.page_views_count);
sheet.getRange(row, 6).setValue(data.positive_reactions_count);
sheet.getRange(row, 7).setValue(data.comments_count);
}
/**
* Gets Github stats of a repository.
* @params organization Organization.
* @params repository Repository.
*
* @return JSON Github stats.
*/
function getGithubStats(sheet, organization, repository) {
const API_ENDPOINT = "https://api.github.com/repos/" + organization + "/" + repository;
const HEADERS = {
"headers": {
"Accept": "application/vnd.github.v3+json"
}
};
try {
const response = UrlFetchApp.fetch(API_ENDPOINT, HEADERS);
return JSON.parse(response.getContentText());
} catch (err) {
throw new Error(err);
}
}
/**
* Fills Github stats.
* @params sheet Sheet.
* @params row Row number.
*/
function fillGithubData(sheet, row) {
const organization = sheet.getRange(row, 8).getValue();
const repository = sheet.getRange(row, 9).getValue();
if (organization === "" || repository === "")
return;
const github = getGithubStats(sheet, organization, repository);
const stars = github.stargazers_count;
if (sheet.getRange(row, 10).getValue() === "")
sheet.getRange(row, 10).setValue(stars);
sheet.getRange(row, 11).setValue(stars);
}
/**
* Fills sheet.
*/
function DEV_STATS() {
let dev = getDevStats();
let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME);
for(var index = dev.length - 1; index >= 0; --index) {
let row = dev.length - index + 1;
let data = dev[index];
fillDevData(sheet, row, data);
fillGithubData(sheet, row);
}
}
@alexdesousa
Copy link
Author

This Gist was inspired by this article

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment