nim-basic/scrap.nim

106 lines
3.9 KiB
Nim
Raw Normal View History

2024-06-27 11:55:39 +00:00
import std/htmlparser
import std/xmltree
import std/strtabs
2024-07-01 08:14:01 +00:00
import std/strutils
2024-06-27 11:55:39 +00:00
2024-06-28 10:10:21 +00:00
type
2024-06-28 11:19:47 +00:00
Descriptor* = object
name* : string
html_tag* : string
html_context_tag* : string
2024-07-01 11:49:39 +00:00
html_context_attrs* : string
html_context_key* : string
2024-06-28 11:19:47 +00:00
contains_string* : string
aattrs* : string
attrs_key*: string
2024-07-01 09:42:11 +00:00
content* : seq[string]
2024-07-03 12:03:25 +00:00
getInline : bool
2024-06-27 10:02:25 +00:00
type
2024-06-28 11:19:47 +00:00
Entry* = object
2024-07-01 07:55:25 +00:00
desc* : seq[Descriptor]
2024-06-28 10:10:21 +00:00
2024-07-01 09:39:33 +00:00
proc getEntryFromHtml*(entry :var Entry, node : XmlNode) : Entry =
2024-07-01 11:49:39 +00:00
var
context : seq[XmlNode]
tmpContext : seq[XmlNode]
2024-07-01 07:55:25 +00:00
for i,desc in entry.desc:
2024-07-01 11:49:39 +00:00
context = @[] #clear sequences
tmpContext = @[]
2024-07-01 07:55:25 +00:00
echo("descriptor ",i," content: ",entry.desc[i])
2024-07-01 11:49:39 +00:00
#filter context
if entry.desc[i].html_context_attrs == "":
context = node.findAll(entry.desc[i].html_context_tag)
else:
tmpContext = node.findAll(entry.desc[i].html_context_tag)
echo(tmpContext.len)
2024-07-01 11:49:39 +00:00
#filter by attributes
for c in tmpContext:
echo c.len
#echo("c: ",c.attrs, " i3: ", i3, " i: ", i, " len tmpContext: ", "entry: ",entry.desc[i])
if entry.desc[i].html_context_attrs != "":
if c.attrs != nil:
if c.attrs.hasKey(entry.desc[i].html_context_attrs):
echo("c.attrs[key]: ",c.attrs[entry.desc[i].html_context_attrs])
if entry.desc[i].html_context_key != "":
#if entry.desc[i].html_context_key.contains(c.attrs[entry.desc[i].html_context_attrs]):
if c.attrs[entry.desc[i].html_context_attrs].contains(entry.desc[i].html_context_key):
context.add(c)
echo("context_key_ MATCH")
else:
echo("key does NOT match")
else:
context.add(c)
else:
echo("c.attrs does not have the key: ", entry.desc[i].html_context_attrs)
2024-07-01 11:49:39 +00:00
else:
echo("no attributes on this tag")
2024-07-03 12:03:25 +00:00
#echo("----------CONTEXT RESULT")
#echo(context)
if entry.desc[i].html_tag == "":
echo("No html_tag specified, return whole context as string")
for a in context:
entry.desc[i].content.add(a.innerText)
return entry
else:
2024-07-01 11:49:39 +00:00
#end filter context
2024-07-03 12:03:25 +00:00
for a in context:
let subContext = a.findAll(entry.desc[i].html_tag)
for b in subContext:
if entry.desc[i].aattrs != "":
if b.attrs != nil:
if b.attrs.hasKey(entry.desc[i].aattrs):
echo("found key [",entry.desc[i].aattrs,"] in : ",b)
echo("value: ",b.attrs[entry.desc[i].aattrs])
if entry.desc[i].contains_string != "":
var content_txt :string = b.innerText()
echo(" and string(innerText): ",content_txt)
if content_txt.contains(entry.desc[i].contains_string):
echo("ATTRS MATCH")
entry.desc[i].content.add(content_txt)
elif entry.desc[i].attrs_key != "":
#if entry.desc[i].attrs_key.contains(b.attrs[entry.desc[i].aattrs]):
if b.attrs[entry.desc[i].aattrs].contains(entry.desc[i].attrs_key):
echo("KEY_MATCH")
entry.desc[i].content.add(b.innerText())
else:
entry.desc[i].content.add(b.attrs[entry.desc[i].aattrs])
elif entry.desc[i].contains_string != "":
var content_txt = b.innerText()
echo(" string(innerText): ",content_txt)
if content_txt.contains(entry.desc[i].contains_string):
echo("PATTERN MATCH")
entry.desc[i].content.add(content_txt)
2024-07-01 09:39:33 +00:00
return entry
2024-06-27 11:55:39 +00:00
2024-06-27 10:02:25 +00:00
proc test() =
2024-06-27 11:55:39 +00:00
var htmlnode : XmlNode
var str_html : string
2024-06-27 10:02:25 +00:00
var entry: Entry
2024-07-01 07:55:25 +00:00
entry.desc[0] = Descriptor(html_tag : "p", contains_string : "test_container")
2024-06-28 10:10:21 +00:00
echo entry
2024-06-27 10:02:25 +00:00
2024-06-27 11:55:39 +00:00
htmlnode = parseHtml(str_html)
2024-07-01 09:39:33 +00:00
discard entry.getEntryFromHtml(htmlnode)
2024-06-27 10:02:25 +00:00
2024-06-28 11:19:47 +00:00
#test()