Compare commits

..

No commits in common. "7a4d9072df7f7f71bc96fbab33a60cca780738ae" and "66d8e890fb5f1b4644144ffea454ef06769a3311" have entirely different histories.

1 changed files with 150 additions and 147 deletions

297
jrpg.ts
View File

@ -1,147 +1,150 @@
// deno // deno
import { DOMParser } from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts' import { DOMParser } from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts'
function getdompareser(html) function getdompareser(html)
{ {
const parser = new DOMParser() const parser = new DOMParser()
return parser.parseFromString(html, "text/html") return parser.parseFromString(html, "text/html")
} }
function gettables(doc) function gettables(doc)
{ {
return [...doc.getElementsByTagName("h2")].map(h2 => h2.id) return [...doc.getElementsByTagName("h2")].map(h2 => h2.id)
} }
function gettable(doc, table, notes) function gettable(doc, table, notes)
{ {
const result = [] const result = []
const items = [...doc.getElementById(table).nextSibling.nextSibling.nextSibling.firstChild.children[0].children] const items = [...doc.getElementById(table).nextSibling.nextSibling.nextSibling.firstChild.children[0].children]
const header = [...items.shift().children].map(h => h.textContent) const header = [...items.shift().children].map(h => h.textContent)
items.forEach(elt => { items.forEach(elt => {
const item = {} const item = {}
const columns = [...elt.children] const columns = [...elt.children]
header.forEach( (h,i) => header.forEach( (h,i) =>
{ {
item[h] = columns[i].textContent item[h] = columns[i].textContent
}) })
if (item.Name) if (item.Name)
{ {
const sup = parseInt(item.Name.split(" ").pop()) const sup = parseInt(item.Name.split(" ").pop())
if (!isNaN(sup)) if (!isNaN(sup))
{ {
item.Notes = notes[sup - 1] item.Notes = notes[sup - 1]
item.Name = item.Name.substr(0, item.Name.length - 2) item.Name = item.Name.substr(0, item.Name.length - 2)
} }
} }
result.push(item) result.push(item)
}) })
return result return result
} }
function getnotes(doc) function getnotes(doc)
{ {
const lastsup = [...doc.getElementsByTagName("sup")].pop() const lastsup = [...doc.getElementsByTagName("sup")].pop()
if (!lastsup) if (!lastsup)
{ {
return [] return []
} }
return lastsup.parentElement.textContent.split("\n") return lastsup.parentElement.textContent.split("\n")
.filter(i => i.length) .filter(i => i.length)
.map(i => i.substr(3).trim()) .map(i => i.substr(3).trim())
} }
async function getpage(url) async function getpage(url)
{ {
const result = {} const result = {}
const response = await fetch(url) const response = await fetch(url)
const html = await response.text() const html = await response.text()
const doc = getdompareser(html) const doc = getdompareser(html)
const notes = getnotes(doc) const notes = getnotes(doc)
const tables = gettables(doc) const tables = gettables(doc)
tables.forEach(table => tables.forEach(table =>
{ {
result[table] = gettable(doc, table, notes) result[table] = gettable(doc, table, notes)
}) })
return result return result
} }
function format(equipment, exclude) function format(equipment, exclude)
{ {
const result = [] const result = []
// todo: notes as footers instead of column
Object.keys(equipment).forEach(table => { // todo: invert available X mark
const columnsizes = {} // todo: remove a column if always empty
result.push(table.toUpperCase())
const items = equipment[table] Object.keys(equipment).forEach(table => {
const columnsizes = {}
// exclude result.push(table.toUpperCase())
items.forEach(item => { const items = equipment[table]
exclude.forEach(name => {
delete item[name] // exclude
}) items.forEach(item => {
}) exclude.forEach(name => {
delete item[name]
// init sizes })
items.forEach(item => })
{
Object.keys(item).forEach(columnname => { // init sizes
columnsizes[columnname] = columnsizes[columnname] || columnname.length items.forEach(item =>
columnsizes[columnname] = Math.max(columnsizes[columnname], (item[columnname] || "").length) {
}) Object.keys(item).forEach(columnname => {
}) columnsizes[columnname] = columnsizes[columnname] || columnname.length
columnsizes[columnname] = Math.max(columnsizes[columnname], (item[columnname] || "").length)
// write header })
let line = "" })
Object.keys(columnsizes).forEach(columnname => {
const size = columnsizes[columnname] // write header
line += columnname + " ".repeat((Math.max(0, size - columnname.length))) + "|" let line = ""
}) Object.keys(columnsizes).forEach(columnname => {
result.push(line) const size = columnsizes[columnname]
line += columnname + " ".repeat((Math.max(0, size - columnname.length))) + "|"
// write lines })
items.forEach(item => { result.push(line)
let line = ""
Object.keys(columnsizes).forEach(columnname => { // write lines
const size = columnsizes[columnname] items.forEach(item => {
const value = (item[columnname] || "").replace("\n", " ") let line = ""
line += value + " ".repeat(Math.max(0, size - value.length)) + "|" Object.keys(columnsizes).forEach(columnname => {
}) const size = columnsizes[columnname]
result.push(line) const value = (item[columnname] || "").replace("\n", " ")
}) line += value + " ".repeat(Math.max(0, size - value.length)) + "|"
result.push(" ") })
}) result.push(line)
})
return result.join("\n") result.push(" ")
} })
async function getdata(configname) return result.join("\n")
{ }
const pages = configs[configname].pages
let result = {} async function getdata(configname)
const root = "https://mikesrpgcenter.com" {
for (const index in pages) const pages = configs[configname].pages
{ let result = {}
const page = pages[index] const root = "https://mikesrpgcenter.com"
const pagedata = await getpage([root, configname, page].join("/") + ".html") for (const index in pages)
result = {...result, ...pagedata} {
} const page = pages[index]
return result const pagedata = await getpage([root, configname, page].join("/") + ".html")
} result = {...result, ...pagedata}
}
function output(stringresult, configname) return result
{ }
//console.log(stringresult)
Deno.writeTextFile(configname + ".txt", stringresult) function output(stringresult, configname)
} {
//console.log(stringresult)
const configs = { Deno.writeTextFile(configname + ".txt", stringresult)
ffantasy: { pages: ["weapons", "armor", "blackmagic", "whitemagic"], exclude: ["Found", "Sold", "Available", "Notes", "Cost", "Ni", "WM", "WW", "BM", "BW", "Th", "BB", "Ma"] }, }
dw3: { pages: ["armor", "weapons"], exclude: [] },
} const configs = {
ffantasy: { pages: ["weapons", "armor", "blackmagic", "whitemagic"], exclude: ["Found", "Sold", "Available", "Notes", "Cost", "Ni", "WM", "WW", "BM", "BW", "Th", "BB", "Ma"] },
for (const configname in configs) { dw3: { pages: ["armor", "weapons"], exclude: [] },
const data = await getdata(configname) }
const stringresult = format(data, configs[configname].exclude)
output(stringresult, configname) for (const configname in configs) {
} const data = await getdata(configname)
const stringresult = format(data, configs[configname].exclude)
output(stringresult, configname)
}