Created
April 10, 2023 19:15
-
-
Save timcash/8e61fe6cd091a22df19ad572e2b76c9a to your computer and use it in GitHub Desktop.
xml.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jsonxml from "jsontoxml"; | |
import { parseString as xmlParse } from "xml2js"; | |
import crypto from "node:crypto"; | |
import { pd } from "pretty-data"; | |
import moment from "moment"; | |
function toJson(xml) { | |
return new Promise(function (resolve, reject) { | |
xmlParse(xml, { trim: true }, function (err, result) { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(result); | |
} | |
}); | |
}); | |
} | |
async function feedXmlToJSON( | |
feed_xml, | |
{ sourceName, authorization, replyEmail } | |
) { | |
const r1 = await toJson(feed_xml); | |
if (r1?.jobs?.job === undefined) return []; | |
let results = []; | |
for (let job of r1.jobs.job) { | |
const id = crypto.randomUUID(); | |
const converted = toJobJson(job, sourceName, authorization, replyEmail); | |
results.push(converted); | |
} | |
return results; | |
} | |
function toJobJson(a, sourceName, authorization, replyEmail) { | |
// console.log(a); | |
let b = {}; | |
let path_safe_id; | |
try { | |
try { | |
path_safe_id = a.unique_ID[0].replace(/\//g, "-"); | |
b.cust_unique_ID = path_safe_id; | |
} catch (e) { | |
throw new Error(`${sourceName} - ${a.title?.[0]} has no unique_ID`); | |
} | |
b.authorization = authorization; | |
b.craigslist = {}; | |
b.craigslist.category = a.category?.[0]; | |
b.craigslist.area = a.area?.[0]; | |
b.consultant = a.consultant?.[0]; | |
b.company = a.company?.[0]; | |
b.cost_center = a.cost_center?.[0]; | |
b.location = {}; | |
b.location.city = a.city?.[0]; | |
b.location.lat = a.lat?.[0]; | |
b.location.long = a.long?.[0]; | |
b.location.state = a.state?.[0]; | |
b.location.neighborhood = a.neighborhood?.[0]; | |
b.posting = {}; | |
b.posting.PONumber = path_safe_id //a.ID?.[0]; | |
b.posting.compensation = a.compensation?.[0]; | |
b.posting.custom1 = a.custom1?.[0]; | |
b.posting.description = a.description.join(""); | |
b.posting.employment_type = a.employment_type?.[0]; | |
b.posting.phoneCallsOK = "0"; | |
b.posting.privacy = a.privacy?.[0] ?? "A"; | |
b.posting.replyEmail = replyEmail; | |
b.posting.title = a.title?.[0]; | |
b.guid = a.unique_ID?.[0]; | |
b.sourceName = sourceName; | |
b.errors = false; | |
b.date = dataFormated(); | |
return b; | |
} catch (error) { | |
console.error(error); | |
console.error(`JOB: ${JSON.stringify(a)}`); | |
b = a; | |
b.error = true; | |
b.errorMessages = error.message; | |
return b; | |
} | |
} | |
function dataFormated() { | |
const time = moment().format("YYYY-MM-DD HH:mm:ss"); | |
return time; | |
} | |
function jsonToCraigsListXML(listOfJobs) { | |
const cdataJobs = listOfJobs.map((j) => { | |
delete j.guid; | |
delete j.sourceName; | |
return { job: addCdata(j) }; | |
}); | |
const xml = jsonxml({ jobs: cdataJobs }, { xmlHeader: true }); | |
return pd.xml(xml); | |
} | |
function toReciept(jobs) { | |
const cdataReciepts = jobs.map((j) => { | |
let r = {}; | |
r.client = j.company | |
r.id = j.posting.PONumber | |
r.cust_unique_ID = j.cust_unique_ID | |
r.title = j.posting.title | |
r.date = j.date | |
r.craigslist_id = j.posting.clpostingid | |
r.media_fee = `$${parseFloat(j.posting.cost).toFixed(2)}` | |
r.service_fee = `$${parseFloat(j.posting.servicefee).toFixed(2)}` | |
r.status = j.posting.success === "TRUE" ? "POSTED" : j.posting.success; | |
r.area = j.posting.hostname; | |
r.category = j.posting.category; | |
r.link = j.posting.postingurl; | |
r.application_url = j.posting?.custom1; | |
r.cost_center = j.cost_center ? j.cost_center : "0" | |
r.consultant = j.consultant | |
r.company_name = j.company | |
r.state = j.location.state | |
return { reciept: addCdata(r) }; | |
}); | |
const xml = jsonxml({ grp: cdataReciepts }, { xmlHeader: true }); | |
return pd.xml(xml); | |
} | |
function addCdata(obj) { | |
for (let prop in obj) { | |
if (typeof obj[prop] === "object") { | |
addCdata(obj[prop]); | |
} else { | |
obj[prop] = jsonxml.cdata(obj[prop]); | |
} | |
} | |
return obj; | |
} | |
export { toJson, feedXmlToJSON, jsonToCraigsListXML, toReciept }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment