-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·148 lines (122 loc) · 3.74 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
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
require("dotenv").config();
const axios = require("axios");
const RAPID_API_KEY = process.env.RAPID_API_KEY;
async function checkBrand(brand) {
console.log("checking:", brand);
const results = {
brand: brand,
twitter: { available: null },
github: { available: null },
dotcom: { available: null },
dotio: { available: null },
dotco: { available: null },
urban_dictionary: { has_matches: null, matches: null },
};
if (!brand) {
return results;
}
try {
const twitter = await axios.get(`https://twitter.com/${brand}`);
results.twitter.available = twitter.status !== 200;
} catch (error) {
if (error.response) {
results.twitter.available = error.response.status === 404;
} else {
console.error(`unknown error for twitter:${brand}`);
}
}
try {
const github = await axios.get(`https://github.com/${brand}`);
results.github.available = github.status !== 200;
} catch (error) {
if (error.response) {
results.github.available = error.response.status === 404;
} else {
console.error(`unknown error for twitter:${brand}`);
}
}
try {
const dotcom = await domainr(brand, "com");
console.log(dotcom);
results.dotcom.available = dotcom.status === "undelegated inactive";
results.dotcom.status = dotcom.status;
} catch (error) {
console.error(error);
console.error("unexpected error getting .com status");
}
try {
const dotio = await domainr(brand, "io");
results.dotio.available = dotio.status === "undelegated inactive";
results.dotio.status = dotio.status;
} catch (error) {
console.error(error);
console.error("unexpected error getting .io status");
}
try {
const dotco = await domainr(brand, "co");
results.dotco.available = dotco.status === "undelegated inactive";
results.dotco.status = dotco.status;
} catch (error) {
console.error(error);
console.error("unexpected error getting .co status");
}
try {
const urban = await axios.get(
`http://api.urbandictionary.com/v0/define?term=${brand}`
);
results.urban_dictionary.has_matches = urban.data.list.length > 0;
results.urban_dictionary.matches = urban.data.list;
} catch (error) {
console.error(`error for Urban Dictionary:${brand}`);
console.error(error);
}
console.log("---------------------------");
console.log(JSON.stringify(results, null, 2));
return results;
}
async function domainr(brand, tld) {
const result = await axios({
method: "GET",
url: "https://domainr.p.rapidapi.com/v2/status",
headers: {
"content-type": "application/octet-stream",
"x-rapidapi-host": "domainr.p.rapidapi.com",
"x-rapidapi-key": RAPID_API_KEY,
},
params: {
domain: `${brand}.${tld}`,
"mashape-key": RAPID_API_KEY,
},
});
return result.data.status[0];
}
const express = require("express");
const app = express();
app.set("view engine", "ejs");
app.use(express.static("public"));
app.get("/check", handleCheck);
app.get("/check/:brand", handleCheck);
async function handleCheck(request, response) {
console.log(request);
const brand = (request.params.brand || request.query.brand || "")
.trim()
.replace(/\s+/, "");
const checkResult = await checkBrand(brand);
if (request.accepts("html")) {
response.render("pages/index", { results: checkResult });
} else {
response.json(checkResult);
}
}
app.get("/", (request, response) => {
response.json({ hello: "world" });
});
const brand = process.argv[2];
if (brand) {
console.log("performing brand checks for", brand);
checkBrand(brand);
} else {
const listener = app.listen(process.env.PORT || 8080, () => {
console.log("Your app is listening on port " + listener.address().port);
});
}