-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
96 lines (90 loc) · 1.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const http = require("https")
const fs = require("fs")
const list = fs.createWriteStream("list.txt")
const adapterFor = (function () {
const url = require("url"),
adapters = {
"http:": require("http"),
"https:": require("https"),
}
return function (inputUrl) {
return adapters[url.parse(inputUrl).protocol]
}
})()
url = "https://www.google.com/basepages/producttype/taxonomy-with-ids.en-AU.txt"
let r = adapterFor(url).get(url, (res) => {
res.pipe(list)
let rawData = ""
res.on("data", (chunk) => {
rawData += chunk
})
res.on("end", () => {
try {
const dataset = rawData.trim().split("\n")
dataset.shift()
const go = new gmc2json(dataset)
go.prep()
fs.writeFile(
"output.json",
JSON.stringify(go.exec(), null, 1),
"utf8",
function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File.")
return console.log(err)
}
console.log("JSON file has been saved.")
}
)
} catch (e) {
console.error(e.message)
}
})
})
class gmc2json {
constructor(raw) {
this.raw = raw
this.workload = []
this.payload = {}
}
splitter(s) {
const reg = /([0-9]+) - (.*)/gm
const v = reg.exec(s)
const o = v[2].split(" > ")
return {
id: v[1],
paths: o,
}
}
prep() {
this.raw.forEach((i) => {
this.workload.push(this.splitter(i))
})
return this.workload
}
exec() {
let max = 5000000
let start = 0
this.workload.forEach((i) => {
if (start === max) return
start++
this.payload[i.paths[0]] = this.dive(
this.payload[i.paths[0]],
i.paths,
1,
i.id
)
})
return this.payload
}
dive(o, list, depth, id) {
if (depth < list.length) {
o[list[depth]] = this.dive(o[list[depth]], list, depth + 1, id)
return o
} else {
return {
id: id,
}
}
}
}