Skip to content

Instantly share code, notes, and snippets.

@vool
Created March 2, 2019 11:26
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 vool/182546f27cb055b2394afa02cd9d77de to your computer and use it in GitHub Desktop.
Save vool/182546f27cb055b2394afa02cd9d77de to your computer and use it in GitHub Desktop.
Script to fetch the content from the kerrywayfolkloreandheritage.com
#!/usr/bin/env node
'use strict';
/*
* Requires
*/
var fs = require('fs'),
http = require('http');
var domain = "http://www.kerrywayfolkloreandheritage.com";
var url = domain + '/api/section/1';
http.get(url, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var json = JSON.parse(data);
for (var i = 0; i < json.pois.length; i++) {
console.log(json.pois[i].title);
/*
// images
if (json.pois[i].image && json.pois[i].image.file) {
fetch(json.pois[i].image.file, 'images');
}
*/
//audion
if (json.pois[i].audio && json.pois[i].audio.file) {
fetch(json.pois[i].audio.file, 'audio');
}
}
});
}).on('error', function(e) {
console.log("Got error: ", e);
});
function fetch(filename, type) {
var file_url = domain + "/content/" + type + "/" + filename;
console.log("fetch -> " + file_url);
http.get(file_url, function(res) {
res.setEncoding('binary');
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
// save file
fs.writeFile("content/" + type + "/" + filename, data, 'binary', function(err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
});
});
}
{
"name": "fetch_kerryway_content",
"version": "1.0.0",
"description": "Script to fetch the content from the kerrywayfolkloreandheritage.com via the API",
"main": "app.js",
"dependencies": {
"fs": "^0.0.2",
"http": "^0.0.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment