Skip to content

Instantly share code, notes, and snippets.

@Jytesh
Last active September 19, 2023 09:32
Show Gist options
  • Save Jytesh/b8d9b8328a1df44f79947b727f2e5f21 to your computer and use it in GitHub Desktop.
Save Jytesh/b8d9b8328a1df44f79947b727f2e5f21 to your computer and use it in GitHub Desktop.
Uploads fac_dat.csv into firestore
const fs = require("fs");
const { parse } = require("csv-parse");
const { finished } = require("stream/promises");
const admin = require("firebase-admin");
const serviceAccount = require("./ig-rmp-firebase-adminsdk-bxw6w-9d61f4b78a.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
const firestore = admin.firestore();
firestore.settings({ ignoreUndefinedProperties: true });
const processFile = async () => {
const records = [];
const parser = fs.createReadStream(`./faculty_data.csv`).pipe(parse());
parser.on("readable", function () {
let record;
while ((record = parser.read()) !== null) {
// Work with each record
records.push(record);
}
});
await finished(parser);
return records;
};
(async () => {
let data = await processFile();
data = data.map((x) => {
// console.log(x[0].match(/(a-zA-Z.)*/gi))
x[0] = x[0].trim().replaceAll(/(Prof. )?(Dr. )?/gi, "");
let ph_number_sanitized = x[3].replaceAll(/[\,a-z@\.\-\[\]\:]/gi, "");
l = x[3].replaceAll(/(\s*\[(?:dot|\.)\]\s*)/gi, "."); // replace some weird ahh [ dot ] with .
x[3] = l.replaceAll(/\s*\[\s*a(t)?\s*\]\s*/gi, "@"); // replace [a] and [at] with @
x[4] = ph_number_sanitized.match(/[0-9]{10}/g); // Need some more manual editing in the csv, remove +91 manually or find some regex
x[3] = x[3].match(/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/gi); // match emails
return {
name: x[0],
department: x[1],
role: x[2],
email: x[3]?.at(0),
phone_numbers: x[4],
ratings: 0,
courses: [],
comments: [],
};
});
console.log(data);
const promises = data.map((x) => firestore.collection("professors").add(x));
console.log(await Promise.all(promises));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment