Skip to content

Instantly share code, notes, and snippets.

@brianorwhatever
Last active February 12, 2023 10:38
Show Gist options
  • Save brianorwhatever/89470db66868d60b8a25b961d38f57a0 to your computer and use it in GitHub Desktop.
Save brianorwhatever/89470db66868d60b8a25b961d38f57a0 to your computer and use it in GitHub Desktop.
converts ordinal feed.xml to json objects
import convert from 'xml-js';
import cheerio from 'cheerio';
const feed = await (await fetch(`${env.ORD_API}/feed.xml`)).text();
const feedjson = JSON.parse(convert.xml2json(feed, { compact: true, spaces: 4 }))
const inscriptions = feedjson.rss.channel.item;
let inscriptionObjects: any[] = [];
for (let i = 0; i < inscriptions.length; i++) {
const inscription = await (await fetch(`${env.ORD_API}${inscriptions[i].link._text}`)).text()
const $ = cheerio.load(inscription);
const dl = JSON.parse(convert.xml2json('<dl>' + $('dl').html() + '</dl>', { compact: true, spaces: 4 }))
const keys = dl.dl.dt.map((x: any) => x._text)
const obj = dl.dl.dd.map((x: any, i: number) => ([keys[i].replace(' ', '_'), x._text ?? x.a?._attributes.href ?? x.time._text]))
inscriptionObjects = [...inscriptionObjects, Object.fromEntries(obj)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment