Created
May 5, 2021 06:01
-
-
Save tayyebi/80f0fe0a94bd43c243181135da2e7c81 to your computer and use it in GitHub Desktop.
HackerNews Api Client Based on Google Apps Script
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
/** | |
* | |
* This program simply creats an Google Spreadsheet | |
* document stored in Google Drive, and checks for | |
* new items in hacker news, then stores them inside | |
* the document file. | |
* | |
* Please run the `SyncDataSheet()` method in order | |
* to make it done. Or add it to cron triggers. | |
* | |
* @author Mohammad R. Tayyebi | |
* @email tayyebimohammadreza@gmail.com | |
* | |
*/ | |
function fetchNewStories() { | |
var url = "https://hacker-news.firebaseio.com/v0/newstories.json"; | |
var newStoriesReponse = UrlFetchApp.fetch(url, {'muteHttpExceptions': true}); | |
var newStoriesIds = JSON.parse(newStoriesReponse.getContentText()); | |
return newStoriesIds; | |
} | |
function fetchStoryDetails(storyId) { | |
var url = `https://hacker-news.firebaseio.com/v0/item/${storyId}.json?print=pretty`; | |
var itemResponse = UrlFetchApp.fetch(url, {'muteHttpExceptions': true}); | |
var itemDetails = JSON.parse(itemResponse.getContentText()); | |
return itemDetails; | |
} | |
function CreateOrOpenSpreadSheet() { | |
var fileName = "Sariab_HackerNews"; | |
var iterator = DriveApp.getFilesByName(fileName); | |
while (iterator.hasNext()) | |
{ | |
var file = iterator.next(); | |
if (file.getName() == fileName) | |
{ | |
var spreadsheet = SpreadsheetApp.open(file); | |
var activeSheet = spreadsheet.getActiveSheet(); | |
activeSheet.appendRow([-1, 'Init.']); | |
return activeSheet; | |
} | |
} | |
var spreadsheet = SpreadsheetApp.create(fileName); | |
var url = spreadsheet.getUrl(); | |
Logger.log(`Opened Datasheet: ${url}`); | |
var activeSheet = spreadsheet.getActiveSheet(); | |
return activeSheet; | |
} | |
function getLastId(sheet) { | |
// var lastRow = sheet.getLastRow(); | |
// var firstCellOfLastRow = sheet.getRange(lastRow, 1); | |
// var lastId = firstCellOfLastRow.getValue(); | |
// return lastId; | |
var colArray = sheet.getRange(2, 1, sheet.getLastRow()).getValues(); | |
var maxInColumn = colArray.sort(function(a,b){return b-a})[0][0]; | |
return maxInColumn; | |
} | |
function SyncDataSheet() { | |
// Fetch new stories form HackerNews | |
var newStoriesIds = fetchNewStories(); | |
// Open Sheet for Data I/O | |
var sheet = CreateOrOpenSpreadSheet(); | |
// get lastId to prevent duplicate | |
var lastId = getLastId(sheet); | |
// Prune array | |
var prunedStoryIds = newStoriesIds.filter(function(x) { | |
return x > lastId ; | |
}); | |
// Iterate items | |
prunedStoryIds.forEach(function (value) { | |
// TODO: Run it as async task | |
// Async.call('fetchStoryDetailsAndAdd', value) | |
var itemDetails = fetchStoryDetails(value); | |
Logger.log(itemDetails); | |
// Append to sheet | |
sheet.appendRow([value, itemDetails.title, itemDetails.url, itemDetails.by, itemDetails.text]); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment