-
Notifications
You must be signed in to change notification settings - Fork 1
/
business-logic.js
243 lines (214 loc) · 8.53 KB
/
business-logic.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const async = require('async');
const q = require('q');
const FinderWords = require('./services/finder-words');
const GoogleWeb = require('./services/google.web');
const { Table } = require('console-table-printer');
const table = require('table').table;
const fs = require('fs');;
class BusinessLogic {
constructor() {
this.finderWords = new FinderWords();
}
async process({ domainNames, tlds, consoleLog, prefix, postfix, statusPrefix, statusPostfix }) {
let defer = q.defer()
let initDomains;
if (domainNames.length == 1){
initDomains = await this.finderWords.find(domainNames[0])
if(!initDomains.includes(domainNames[0].toUpperCase())){
initDomains.push(domainNames[0].toUpperCase())
}
}
else {
initDomains = domainNames
}
let parsedDomain = this.setPostfixPrefixDomain({ domains: initDomains, prefix, postfix, statusPrefix, statusPostfix })
const reversedParsedDomain = parsedDomain.reverse()
let domainsChunk = [];
const size = 20;
let result = []
let allParsedUrls;
while (reversedParsedDomain.length > 0)
domainsChunk.push(reversedParsedDomain.splice(0, size));
async.eachOfSeries(domainsChunk, async (domains, index) => {
try {
let urls = await this.getUrlsFromGoogle({ domains: domains, prefix, postfix, statusPrefix, statusPostfix, counter: index, size })
allParsedUrls = await this.saveUrls(urls, tlds, consoleLog);
if (allParsedUrls.length > 0) console.log(`Please check ${allParsedUrls.fileName}.txt`);
result.push(allParsedUrls)
} catch (error) {
console.log(error.message);
}
}, () => {
defer.resolve(allParsedUrls)
})
return defer.promise;
}
setPostfixPrefixDomain({ domains, prefix, postfix, statusPrefix, statusPostfix }) {
let result = []
if (statusPrefix == 5 && statusPostfix == 5) {
domains.forEach(item => {
if (`${prefix}${item}`.length > 3)
result.push(`${prefix}${item}`)
if (item.length > 3)
result.push(`${item}`)
if (`${item}${postfix}`.length > 3)
result.push(`${item}${postfix}`)
if (`${prefix}${item}${postfix}`.length > 3)
result.push(`${prefix}${item}${postfix}`)
})
}
if (prefix) {
if (statusPrefix == 0) {
domains.forEach(item => {
if (item.length > 3)
result.push(`${item}`)
})
}
if (statusPrefix == 1) {
domains.forEach(item => {
result.push(`${prefix}${item}`)
})
}
if (statusPrefix == 2) {
domains.forEach(item => {
if (item.length > 3)
result.push(`${item}`)
result.push(`${prefix}${item}`)
})
}
}
if (postfix) {
if (statusPostfix == 0) {
domains.forEach(item => {
if (item.length > 3)
result.push(`${item}`)
})
}
if (statusPostfix == 1) {
domains.forEach(item => {
result.push(`${item}${postfix}`)
})
}
if (statusPostfix == 2) {
domains.forEach(item => {
if (item.length > 3)
result.push(`${item}`)
result.push(`${item}${postfix}`)
})
}
}
let uniqeDomain = [...new Set(result)]
return uniqeDomain
}
async getUrlsFromGoogle({ domains, prefix, postfix, statusPrefix, statusPostfix, counter, size }) {
let defer = q.defer()
let googleUrls = []
console.log('Domain names list:', domains)
async.eachOfSeries(domains, async (domain, index) => {
try {
// console.log(`${index + (counter) * size}:${domain.toLowerCase()}`);
const { error, result } = await GoogleWeb.run(domain);
if (error) {
console.log(error.message)
}
if (result) {
let obj = { domainName: domain, urls: result }
googleUrls.push(obj)
}
} catch (error) {
console.log(error.message)
}
}, () => {
defer.resolve(googleUrls)
})
return defer.promise;
}f
async saveUrls(allUrls, tlds, consoleLog) {
let fileName = +new Date()
let validInvalidUrls = []
for (let domains of allUrls) {
let { domainName, urls } = domains
let { validResult, invalidResult } = this.extractValidAndInvalidUrls({ urls, tlds })
if (consoleLog)
this.logUrlsInTerminal({ domainName, validResult, invalidResult })
await this.saveUrlsToFile({ validResult, invalidResult, fileName, domainName })
validInvalidUrls.push({ validResult, invalidResult })
}
return { validInvalidUrls, fileName }
}
extractValidAndInvalidUrls({ urls, tlds }) {
let validResult = [];
let invalidResult = [];
try {
for (let i = 0; i < urls.length; i++) {
if (tlds.includes(urls[i].domainName.tld)) {
if (urls[i].supportedResultInfo.availabilityInfo.availability.includes("AVAILABILITY_UNAVAILABLE")) {
if (!invalidResult[urls[i].domainName.tld]) invalidResult[urls[i].domainName.tld] = []
let fullDomain = `${urls[i].domainName.sld}.${urls[i].domainName.tld}`
invalidResult[urls[i].domainName.tld].push(fullDomain);
}
else {
if (!validResult[urls[i].domainName.tld]) validResult[urls[i].domainName.tld] = []
let fullDomain = `${urls[i].domainName.sld}.${urls[i].domainName.tld}`
validResult[urls[i].domainName.tld].push(fullDomain);
}
}
}
return { validResult: validResult, invalidResult: invalidResult }
} catch (error) {
console.log(error)
}
}
logUrlsInTerminal({ domainName, validResult, invalidResult }) {
const p = new Table();
let count = 1;
for (let key in validResult) {
validResult[key].forEach(item => {
p.addRow({ index: count, domain: item }, { color: 'green' });
count += 1
})
}
for (let key in invalidResult) {
invalidResult[key].forEach(item => {
p.addRow({ index: count, domain: item }, { color: 'red' });
count += 1
})
}
console.log('\x1b[36m%s\x1b[0m', domainName)
p.printTable();
}
async saveUrlsToFile({ validResult, invalidResult, fileName, domainName }) {
try {
let dataValid;
let dataInvalid;
let outputValid;
let outputInvalid;
dataValid = this.processValidInvalidUrls(validResult);
dataInvalid = this.processValidInvalidUrls(invalidResult);
fs.appendFileSync(`${fileName}.txt`, `${domainName}:\r\n\n`);
fs.appendFileSync(`${fileName}.txt`, `Available\r\n`);
if (dataValid.length != 0) {
outputValid = table([dataValid]);
fs.appendFileSync(`${fileName}.txt`, outputValid)
}
fs.appendFileSync(`${fileName}.txt`, `Unvailable\r\n`)
if (dataInvalid.length != 0) {
outputInvalid = table([dataInvalid]);
fs.appendFileSync(`${fileName}.txt`, `${outputInvalid}`)
}
fs.appendFileSync(`${fileName}.txt`, `--------------------------------------------------------------------------------------------\n`)
} catch (error) {
console.log("saveUrlsToFile:", error.message);
}
}
processValidInvalidUrls(urls) {
let result = []
for (let key in urls) {
urls[key].forEach(item => {
result.push(item)
})
}
return result
}
}
module.exports = new BusinessLogic()