Skip to content

Instantly share code, notes, and snippets.

@anselm
Created October 23, 2016 21:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
A bit of code to import some annoying data to parse
const http = require('http');
function pushme(mydata,mytable) {
var postData = JSON.stringify( mydata );
var options = {
hostname: 'localhost',
port: 1337,
path: '/parse/classes/'+mytable,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
'X-Parse-Application-Id':'APPLICATION_ID',
}
};
var req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
req.write(postData);
req.end();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*
// try use request to post
var request = require('request');
var url = "http://localhost:1337/parse/classes/GameScore";
var mydata = { score:1234, playerName:'fuck off and die',cheatMode:false};
var args = {
data: mydata,
headers: { "Content-Type": "application/json", "X-Parse-Application-Id": "APPLICATION_ID" }
};
request.post(url,args, function (error, response, body) {
console.log(body);
console.log(response);
console.log(error);
});
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*
var options = {
host: "http://localhost",
port: 1337,
path: '/parse/classes/GameScore',
method: 'POST',
headers: { "Content-Type": "application/json", "X-Parse-Application-Id": "APPLICATION_ID" },
data: mydata,
};
http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
}).end();
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////
/*
// this does not work - i have to have a separate rest api for some reason - would love to talk to pare-server directly darnit
var Parse = require('node-parse-api').Parse;
var RestParse = new Parse("APPLICATION_ID", "MASTER_KEY");
console.log("attempting to insert something");
RestParse.insert(
'GameScore',
{ score:1234, playerName:"the thing",cheatMode:false},
function (err, response) {
console.log("response was " + response + " " + err );
}
);
// thisneeds to specify the app id... does not work
var Client = require('node-rest-client').Client;
var client = new Client();
client.post(url, args, function (data, response) {
console.log(data);
console.log(response);
});
console.log("done posting");
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Return array of string values, or NULL if CSV string not well formed.
function CSVtoArray(text) {
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;
var re_value = /(?!\s*$)\s*(?:'([^'\\]*(?:\\[\S\s][^'\\]*)*)'|"([^"\\]*(?:\\[\S\s][^"\\]*)*)"|([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*))\s*(?:,|$)/g;
// Return NULL if input string is not well formed CSV string.
if (!re_valid.test(text)) return null;
var a = []; // Initialize array to receive values.
text.replace(re_value, // "Walk" the string using replace with callback.
function(m0, m1, m2, m3) {
// Remove backslash from \' in single quoted values.
if (m1 !== undefined) a.push(m1.replace(/\\'/g, "'"));
// Remove backslash from \" in double quoted values.
else if (m2 !== undefined) a.push(m2.replace(/\\"/g, '"'));
else if (m3 !== undefined) a.push(m3);
return ''; // Return empty string.
});
// Handle special case of empty last value.
if (/,\s*$/.test(text)) a.push('');
return a;
};
//////////////////////////////////////////////////////////////////////////////////////////////////////
// try dump out some tables
fs = require('fs');
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('aa.csv')
});
var badlines = [];
lineReader.on('line', function (line) {
var parts = CSVtoArray(line); // line.split(",");
if(parts == null) {
// if cannot parse then keep building up line
badlines.push(line);
if(badlines.length < 2) {
return;
}
line = badlines.join('');
parts = CSVtoArray(line);
if(parts == null) {
return;
}
badlines = [];
}
if(badlines.length > 0) {
console.log("************** problem with *********** " + badlines.join(""));
badlines = [];
}
// Name,AC,,Type of Entity,Location,City,County,Contact,URL^M
var name = parts[0]; if(!name || typeof name == undefined) return;
var ac = parts[1]; if(!ac || typeof ac == undefined) ac = "none";
var entity = parts[3]; if(!entity || typeof entity == undefined) entity = "error";
var loc = parts[4]; if(!loc || typeof loc == undefined) loc = "none";
var city = parts[5]; if(!city || typeof city == undefined) city = "none";
var county = parts[6]; if(!county || typeof count == undefined) county = "none";
var contact = parts[7]; if(!contact || typeof contact == undefined) contact = "none";
var url = parts[8]; if(!url || typeof url == undefined) url = "none";
var blob = { name:name, entity:entity, address:loc, city:city, contact:contact, url:url };
console.log("...........................................");
console.log("name:"+name);
console.log("address:"+loc);
console.log("contact:"+contact);
console.log("url:"+url);
pushme(blob,"Venue");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment