Skip to content

Instantly share code, notes, and snippets.

@wallabyway
Last active February 25, 2022 00:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallabyway/51a7bf50f5fe2ec19657278494fcd076 to your computer and use it in GitHub Desktop.
Save wallabyway/51a7bf50f5fe2ec19657278494fcd076 to your computer and use it in GitHub Desktop.
download JSON Properties files
// download properties database JSON files
// add your KEY, SECRET and URN
// npm install node-fetch@2 fs-extra
// node propertiesdownload.js
const fetch = require('node-fetch');
const fs = require('fs-extra');
const atob = a => Buffer.from(a, 'base64').toString('binary');
const opts = token => ({ compress: true, headers: { 'Authorization': 'Bearer ' + token }});
const TOKEN = "1234"
const URN = "dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Y29uc29saWRhdGVkL3JtZV9hZHZhbmNlZF9zYW1wbGVfcHJvamVjdC5ydnQ";
const KEY=`Ni9I5iHS....geus8WduUC`;
const SECRET=`A1s.....ws3O`;
const FORGEURL = 'https://developer.api.autodesk.com';
async function getToken() {
const url = `${FORGEURL}/authentication/v1/authenticate`;
const header = { 'Content-Type': 'application/x-www-form-urlencoded' }
const body = `grant_type=client_credentials&client_id=${KEY}&client_secret=${SECRET}&scope=data:read`;
let token = await fetch( url, { method: 'POST', headers: header, body: body });
token = await token.json();
return token.access_token;
}
async function _fetch(url, file, token, outdir) {
const buff = await ( await fetch(`${url}${file}`, opts(token) ) ).buffer();
await fs.outputFile(`${outdir}${file}`, buff);
console.log(`downloaded (${buff.length}) - ${file}`)
}
const getPath = (jsn, sz) => { aa=JSON.stringify(jsn).split(sz)[0].split('/');return aa.slice(aa.lastIndexOf('output'),-1).join('/')};
async function getPathFromManifest(urn, token) {
const jsn = await (await fetch(`${FORGEURL}/modelderivative/v2/designdata/${urn}/manifest`, opts(token))).json();
const dbpath = getPath(jsn, '.db');
const svfpath = getPath(jsn, '.svf');
return {dbpath:`urn:adsk.viewing:fs.file:${urn}/${dbpath}`, svfpath:`urn:adsk.viewing:fs.file:${urn}/${svfpath}`};
}
async function downloadProperties(urn, path, token, outfolder = ".") {
const files = [`${path.svfpath}/FragmentList.pack`, `${path.dbpath}/objects_attrs.json.gz`, `${path.dbpath}/objects_vals.json.gz`, `${path.dbpath}/objects_offs.json.gz`, `${path.dbpath}/objects_ids.json.gz`, `${path.dbpath}/objects_avs.json.gz`];
const url = `${FORGEURL}/derivativeservice/v2/derivatives/`;
await Promise.all( files.map ( file =>
_fetch(url, file, token, outfolder) ));
}
async function main() {
const token = TOKEN; //await getToken();
const path = await getPathFromManifest(URN, token);
const outfolder = `${atob(URN)}/`;
console.log(`writing files to ${outfolder}`)
await downloadProperties(URN, path, token, outfolder);
console.log('done')
}
main();
// Alternative to using Manifest to find path
// const path = ["Resource","0","0/1","1","1",""][['.rvt','.nwd','.f3d','.zip','.iam',".dw",].findIndex((i) => outfolder.indexOf(i) > 0)];
// const url = `${FORGEURL}/derivativeservice/v2/derivatives/urn:adsk.viewing:fs.file:${urn}/output/${path}/`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment