41 lines
1.2 KiB
Nim
41 lines
1.2 KiB
Nim
|
import std/os
|
||
|
import std/strutils
|
||
|
import std/htmlparser
|
||
|
import std/xmltree
|
||
|
import std/json
|
||
|
import std/strtabs # To access XmlAttributes
|
||
|
|
||
|
# extract iSpindel JSON from little bock HTML files
|
||
|
|
||
|
proc jsparse(data: string): JsonNode =
|
||
|
# get JSON tree
|
||
|
var tree: JsonNode = parseJson(data)
|
||
|
if tree.kind == JObject and tree.hasKey("series"):
|
||
|
tree = tree["series"]
|
||
|
for branch in tree:
|
||
|
for unwanted in ["color","opacity","yAxis","dashStyle"]:
|
||
|
if branch.hasKey(unwanted): branch.delete(unwanted)
|
||
|
return tree
|
||
|
|
||
|
proc hparse(hfile: string): string =
|
||
|
# parse a named HTML document
|
||
|
let html: XmlNode = loadHtml(hfile)
|
||
|
for hdiv in html.findAll("div"):
|
||
|
if hdiv.attrs.hasKey("id"):
|
||
|
if hdiv.attrs["id"] == "fermentation_log_chart":
|
||
|
let data: string = hdiv.attrs["data-chart-options"]
|
||
|
return pretty(jsparse(data))
|
||
|
|
||
|
proc jsextract(hfile: string): string =
|
||
|
# run the parsers
|
||
|
result = "None"
|
||
|
var js: string = hparse(hfile)
|
||
|
if not isEmptyOrWhitespace(js):
|
||
|
let jsfile: string = hfile.changeFileExt("json")
|
||
|
result = jsfile
|
||
|
writeFile(jsfile, js)
|
||
|
|
||
|
for hfile in walkFiles("*.html"):
|
||
|
echo hfile & " >> " & jsextract(hfile)
|
||
|
|