[mod] WW: HTML parsing

This commit is contained in:
2021-01-03 11:52:15 +01:00
parent 566774b89c
commit 1eaf10b83e

View File

@@ -1,24 +1,26 @@
import {Conf, ConfWWMeta} from "../../types/conf";
import {urlWatcher, UrlWatcherResult} from '../abstract/url-watcher'
import {moment} from "duniter/app/lib/common-libs/moment";
function handleLateness(confHead: ConfWWMeta, data: WWMetaJson) {
const diff = Math.round(Date.now()/1000 - data.now)
if (diff >= confHead.maxLate) {
return UrlWatcherResult.ko(`WWMeta.json is late by ${diff}s (>= ${confHead.maxLate})`)
return UrlWatcherResult.ko(`WWData is late by ${diff}s (>= ${confHead.maxLate})`)
}
return UrlWatcherResult.ok()
}
export function jsonWatcher(conf: Conf) {
const URL_PATH = '/WWMeta.json'
const URL_PATH = '/00wwView'
return async (confWWMeta: ConfWWMeta) => {
return urlWatcher(conf, async (data) => {
return urlWatcher(conf, async (html) => {
const data = parseData(html)
return handleLateness(confWWMeta, data)
})({
name: `WWMeta.json watcher ${confWWMeta.address}`,
name: `WWData watcher ${confWWMeta.address}`,
address: confWWMeta.address + URL_PATH,
frequency: confWWMeta.frequency
},
@@ -28,9 +30,19 @@ export function jsonWatcher(conf: Conf) {
}
}
function parseData(html: string): WWMetaJson {
const h3 = html.match(/<h3>([\s\S]*)<\/h3>/)
if (!h3) {
throw `Could not parse H3 from WW`
}
const data = h3[1].match(/Bloc (\d+)\s(.+)\s(.+)/)
if (!data) {
throw `Could not parse datetime from H3`
}
const now = moment(`${data[2]}`, 'DD/MM/YYYY HH:mm:ss')
return { now: now.unix() }
}
interface WWMetaJson {
dossNb: number
block: number
now: number
computation_duration: number
}
}