Skip to content

Instantly share code, notes, and snippets.

@ayushkamadji
Created September 21, 2019 05:50
Show Gist options
  • Save ayushkamadji/d47e68166cfa42ea3d6ceefde4ac37f1 to your computer and use it in GitHub Desktop.
Save ayushkamadji/d47e68166cfa42ea3d6ceefde4ac37f1 to your computer and use it in GitHub Desktop.
Download Pushbullet Pushes Into Files
// Author: partohap.ayushka@gmail.com
const axios = require("axios")
const fs = require("fs")
const path = require("path")
const URL = "https://api.pushbullet.com/v2/pushes"
const ACCESS_TOKEN = process.env["ACCESS_TOKEN"]
const FILE_PREFIX = "pb_pushes_"
const EXTENSION = ".json"
function main() {
getPushes()
}
function getPushes(cursor = null, count = 0) {
const headers = { "Access-Token": ACCESS_TOKEN }
const params = {}
if (cursor && cursor.length > 0) params.cursor = cursor
return axios.get(URL, { headers, params })
.then( response => writeToFile(response, count) )
.then(logRateLimitRemaining)
.then( response => getNextPushesIfCursor(response, count + 1) )
.catch(logError)
}
function getNextPushesIfCursor(response, count) {
if (response.data.cursor && response.data.cursor.length > 0)
return getPushes(response.data.cursor, count)
}
function logResponse(response) {
console.log(response.data)
return response
}
function writeToFile(response, count) {
fs.writeFile(
generateFileName(count),
stringifyWithIndent(response.data),
logError
)
return response
}
function generateFileName(count) {
const fileName = path.join(__dirname, `${FILE_PREFIX}${count}${EXTENSION}`)
console.log(fileName)
return fileName
}
function stringifyWithIndent(object) {
return JSON.stringify(object, null, 2)
}
function logRateLimitRemaining(response) {
console.log(
`Rate limit remaining: ${response.headers["x-ratelimit-remaining"]}\n`)
return response
}
function logError(error) {
console.error(error)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment