Skip to content

Instantly share code, notes, and snippets.

@ludolphus
Last active January 24, 2017 11:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ludolphus/37fccaa32600846e759e1c693feeaf43 to your computer and use it in GitHub Desktop.
Save ludolphus/37fccaa32600846e759e1c693feeaf43 to your computer and use it in GitHub Desktop.
Get all your app.net files
/* ADNFILES.JS
Get all your ADN files and save them to local storage
Created by @ludolphus 2017
Tested with nodeJS v5.5.0, get it at https://nodejs.org/download/release/v5.5.0/
Also install async, get it here: http://caolan.github.io/async/
Before running the script find your access token for app.net and enter it in
this script at line 38
The script will create a base directory adnfiles under the current path and
directories for 2012-2017 under the adnfiles directory
Run the script: node ./adnfiles.js
Depending on the amount and size of your files and your internet connection
this script can run anywhere from minutes to hours.
For me it took just over an hour to download 4000+ files totaling 4GB
*/
const https = require('https');
const fs = require('fs');
var async = require('async');
fs.stat('adnfiles', (err, stats) => {
if (stats === undefined) {
fs.mkdirSync('adnfiles');
fs.mkdirSync('adnfiles/2012');
fs.mkdirSync('adnfiles/2013');
fs.mkdirSync('adnfiles/2014');
fs.mkdirSync('adnfiles/2015');
fs.mkdirSync('adnfiles/2016');
fs.mkdirSync('adnfiles/2017');
}
});
// You can find your access token by going to http://console-app.net/
// or go to http://dev-lite.jonathonduerig.com/
var adnAccessToken = '[ENTER YOUR ACCESSTOKEN HERE]';
var max_id = 0;
var apiroot = 'https://api.app.net/users/me/files/';
var options = {
hostname: 'api.app.net',
port: 443,
path: apiroot,
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Bearer ' + adnAccessToken
}
};
var more = true;
var max_id = 0;
var min_id = 0;
var iterations = 1;
function getFiles(more, max_id, min_id) {
var parameters = '?';
parameters += (min_id != undefined && min_id != 0) ? 'before_id=' + min_id + '&' : '';
parameters += 'count=50&include_incomplete=0&include_html=0&include_file_annotations=0&include_user_annotations=0&include_private=1';
options.path = apiroot + parameters;
console.log('Calling ' + apiroot + parameters);
var req = https.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
var data = '';
res.on('data', (chunck) => {
if (chunck !== undefined) {
data += chunck;
}
});
res.on('end', () => {
var json = JSON.parse(data);
more = json.meta.more;
min_id = json.meta.min_id;
async.eachLimit(json.data, 5, function(file, callback) {
console.log('downloading: ' + file.name.replace(/\/|\'/g, '_'));
var filePath = 'adnfiles/' + file.created_at.substr(0, 4) + '/' + file.source.name.replace(/\s/g, '') + '-' + file.id + '_' + file.name.replace(/\/|\'/g, '_');
var f = file;
var cb = callback;
var request = https.get(file.url, function(response) {
response.on('end', () => {
var timestamp = (new Date(f.created_at)).getTime() / 1000;
fs.utimes(filePath, timestamp, timestamp, function() {
cb();
});
});
if (response.statusCode === 200) {
var fileStream = fs.createWriteStream(filePath);
response.pipe(fileStream);
}
request.setTimeout(15000, function () {
request.abort();
console.log('failed to get: ' + filePath);
callback();
});
});
request.end();
}, function(err, result) {
// iterations--;
if (more && iterations > 0) {
getFiles(more, max_id, min_id);
}
});
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
req.end();
}
getFiles(more, max_id, min_id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment