Created
May 24, 2018 04:00
-
-
Save greyscaled/6df6a87866e9ad37da0be6b208d16228 to your computer and use it in GitHub Desktop.
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
const fs = require('fs') | |
const cheerio = require('cheerio') | |
console.log('Reading File fooTable.html...') | |
const parsedHTML = fs.readFileSync('./footable.html', 'utf8') | |
const $ = cheerio.load(parsedHTML, { | |
normalizeWhitespace: true | |
}) | |
const cardJSON = { | |
'cards': [] | |
} | |
const ASSET_URL = 'http://foo.com/assets/cards' | |
function splitElems (elemStr) { | |
let elems = elemStr.split(' ') | |
return elems.map(e => e.trim()) | |
} | |
function splitClasses (classString) { | |
let classes = classString.split('-') | |
} | |
console.log('Parsing HTML...') | |
$('.card-row').each((i, elem) => { | |
let row = {} | |
// Data Attributes ---------> | |
row.id = elem.attribs['data-id'] | |
row.elems = splitElems(elem.attribs['data-elems']) | |
row.name = elem.attribs['data-name'] | |
row.image = `${ASSET_URL}/${elem.attribs['data-id']}.jpg` | |
// Kingdom And Class ---------> | |
const kingdomNode = $('.ellipsis', elem).next('td') | |
const classNode = $(kingdomNode).next('td') | |
row.kingdom = kingdomNode.text() | |
row.classes = splitClasses(classNode.text()) | |
// add Row to JSON | |
cardJSON.cards.push(row) | |
}) | |
// Lastly - write the damn object to file | |
console.log('writing contents to cards.json') | |
const content = JSON.stringify(cardJSON) | |
fs.writeFile('./cards.json', content, 'utf8', (err) => { | |
if (err) { | |
return console.log(err) | |
} | |
console.log('file saved') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment