nim-basic/main.nim

115 lines
3.9 KiB
Nim
Raw Normal View History

2024-06-27 10:02:25 +00:00
import std/httpclient
import std/htmlparser
import std/xmltree
import std/strtabs
import std/os
2024-06-28 11:19:47 +00:00
import scrap
2024-07-03 12:03:25 +00:00
import std/json
import std/strutils
2024-07-08 11:43:25 +00:00
import std/tables
import std/macros
2024-07-09 09:22:15 +00:00
import std/enumerate
2024-07-08 11:43:25 +00:00
2024-06-27 10:02:25 +00:00
var client = newHttpClient()
var html: string
2024-07-08 11:43:25 +00:00
#
# main_loop <------------
# | | |
# normal_loop array_loop |
# (var node) (var array[]) |
# | |
# ----->-------------------
#
proc dbg_dl(level : int)=
for a in 0..level:
stdout.write " "
stdout.write level
stdout.write("[do_loop]")
proc dbg_ml(level : int)=
for a in 0..level:
stdout.write " "
stdout.write level
stdout.write("[main_loop]")
proc dbg_al(level : int)=
for a in 0..level:
stdout.write " "
stdout.write level
stdout.write("[array_loop]")
2024-06-27 10:02:25 +00:00
2024-07-09 09:22:15 +00:00
proc main_loop[T]( node : T , level : int, searchString : string = "",path :string = "")
2024-07-04 12:02:46 +00:00
2024-07-05 11:08:22 +00:00
proc do_loop[T](node: T,level : int, searchString : string = "", path: string = "", key : string) =
if node.kind == JObject:
2024-07-09 07:56:36 +00:00
for y in node.keys:
2024-07-09 09:22:15 +00:00
if searchString == "" or path.contains(searchString) or y.contains(searchString):
2024-07-08 11:43:25 +00:00
dbg_dl(level)
2024-07-09 09:22:15 +00:00
stdout.write path & "][" & key & "][" & y," [", node[y].kind,"] {" ,key,"}"
if node[y].kind == Jint:
stdout.write "(val: ", node[y],")\n"
else:
stdout.write "\n"
main_loop(node[y],level+1,searchString,path & "][" & key & "][" & y)
2024-07-03 12:03:25 +00:00
2024-07-05 11:16:33 +00:00
proc do_array_loop(node : JsonNode, level : int, searchString : string = "", path: string = "",key :string = "") =
2024-07-09 09:22:15 +00:00
for i,b in enumerate(node.getElems):
if searchString == "" or path.contains(searchString) or b.getStr().contains(searchString):
2024-07-09 07:56:36 +00:00
dbg_al(level)
2024-07-09 09:22:15 +00:00
echo "node : ", "array_node_len: ", b.len()," ,subkind: " ,b.kind, " mainkind: ", node.kind, "seq[",i,"]", " path:",path," key:",key
main_loop(b,level+1,searchString,path & "][" & key)
2024-07-05 11:38:16 +00:00
2024-07-03 12:03:25 +00:00
2024-07-09 09:22:15 +00:00
proc main_loop[T]( node : T , level : int, searchString : string = "",path :string = "") =
2024-07-05 11:08:22 +00:00
if node.len() > 0:
if node.kind == JObject:
2024-07-09 09:22:15 +00:00
for i,b in enumerate(node.pairs):
2024-07-09 07:56:36 +00:00
if searchString == "" or path.contains(searchString) or b.key.contains(searchString):
dbg_ml(level)
2024-07-09 09:22:15 +00:00
echo "element:seq[",i,"]",b, " path: ",path
2024-07-05 11:08:22 +00:00
for a in node.keys:
if node[a].kind == JObject:
do_loop(node[a],level,searchString,path, a)
elif node[a].kind == JArray:
2024-07-08 11:43:25 +00:00
do_array_loop(node[a],level+1,searchString,path,a)
2024-07-05 11:08:22 +00:00
elif node.kind == JArray:
2024-07-09 09:22:15 +00:00
do_array_loop(node,level+1,searchString,path)
2024-07-05 11:08:22 +00:00
2024-06-27 10:02:25 +00:00
echo "URL:"
var url: string = "https://www.comparis.ch/immobilien/marktplatz/lenzburg/mieten?page=2"#readLine(stdin)
2024-06-27 10:02:25 +00:00
echo "given url is: ",url
try:
2024-06-28 11:19:47 +00:00
html = client.getContent(url)
let node = parseHtml(html)
var htmlnode: XmlNode
var entry : Entry
2024-06-28 11:58:36 +00:00
add(entry.desc,Descriptor(name : "test", html_context_tag : "div",html_context_attrs : "class",html_context_key : "css-19re50j", html_tag : "p",aattrs : "class",attrs_key : "css-svet6u"))
2024-07-03 12:03:25 +00:00
#comparis stores part of the data in a script tag as a json-string, so we get that part:
add(entry.desc,Descriptor(name : "script-json", html_context_tag : "script",html_context_attrs : "type",html_context_key : r"application/json", html_tag : "",aattrs : "",attrs_key : ""))
2024-07-01 07:55:25 +00:00
2024-07-01 11:49:39 +00:00
echo("###########")
2024-07-03 12:03:25 +00:00
discard entry.getEntryFromHtml(node)
#echo entry.desc[1].content[0]
let jsonString = replaceWord(entry.desc[1].content[0],sub = r"\"",by = r""")
let jsonNode = parseJson(jsonString)
#echo "jsonString: ",jsonString
#echo "JSON Node:", jsonNode
2024-07-09 07:56:36 +00:00
let field = jsonNode.getFields()
2024-07-09 09:22:15 +00:00
main_loop(jsonNode,0,searchString = "resultItems")
2024-07-09 07:56:36 +00:00
#-props-pageProps-initialResultData-resultItems
2024-07-09 09:22:15 +00:00
echo "node fields: ",jsonNode["props"]["pageProps"]["initialResultData"]["resultItems"][1]["Title"]
echo "node fields: ",jsonNode["props"]["pageProps"]["initialResultData"]["adIds"][0]
2024-06-27 10:02:25 +00:00
finally:
client.close()