-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (107 loc) · 3.39 KB
/
index.js
File metadata and controls
137 lines (107 loc) · 3.39 KB
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
const puppeteer = require("puppeteer");
const fs = require("fs");
const shortid = require("shortid");
const FOLDER_PATH = "/answers/";
if (process.argv.length !== 3) {
console.error("Expected a query argument");
process.exit(1);
}
const args = process.argv;
const query = args[2];
const createDir = async (dirPath) => {
fs.access(process.cwd() + dirPath, (error) => {
if (error) {
fs.mkdirSync(process.cwd() + dirPath, { recursive: true }, (error) => {
if (error) {
console.error("Error creating dir", error);
} else {
console.log("Directory created");
}
});
}
});
};
const getAnswerFromQuestion = async (website, query, page) => {
console.log("Website", website);
await page.goto(website, ["load", "domcontentloaded", "networkidle0"]);
const popUp = (await page.$x("//button[@title='Dismiss']"))[0];
if (popUp) await popUp.click();
const generateId = shortid.generate();
const screenshotPath = `${generateId}.png`;
const acceptedAnswer = await page.$(".accepted-answer");
if (!acceptedAnswer) return;
const pathToSaveScreenshot = `/answers/${query}/`;
await createDir(pathToSaveScreenshot);
await acceptedAnswer.screenshot({
path: `.${pathToSaveScreenshot}${screenshotPath}`,
});
};
(async () => {
try {
const browser = await puppeteer.launch({
headless: false,
});
const page = await browser.newPage();
page.on("console", (msg) => {
for (let i = 0; i < msg._args.length; ++i)
console.log(`${i}: ${msg._args[i]}`);
});
const queryWordArray = query.split(" ");
const queryFolderId = query.replace(/ /g, "");
const queryUrl = `${query.replace(/ /g, "%20")}`;
const googleUrl = `https://www.google.com/search?q=${queryUrl}+site%3Astackoverflow.com`;
await page.goto(googleUrl, ["load", "domcontentloaded", "networkidle0"]);
const validUrls = await page.evaluate((queryUrl) => {
const hrefElementsList = Array.from(
document.querySelectorAll(
`div[data-async-context='query:${queryUrl}%20site%3Astackoverflow.com'] a[href]`
)
);
const filterElementsList = hrefElementsList.filter((elem) =>
elem
.getAttribute("href")
.startsWith("https://stackoverflow.com/questions")
);
const stackOverflowLinks = filterElementsList.map((elem) =>
elem.getAttribute("href")
);
return stackOverflowLinks;
}, queryUrl);
const keywordLikeability = [];
validUrls.forEach((url) => {
let wordCounter = 0;
queryWordArray.forEach((word) => {
if (url.indexOf(word) > -1) {
wordCounter = wordCounter + 1;
}
});
if (queryWordArray.length / 2 < wordCounter) {
keywordLikeability.push({
keywordMatch: wordCounter,
url: url,
});
}
});
/*
Order by number of matched words
keywordLikeability.sort((a, b) =>
b.keywordMatch > a.keywordMatch ? 1 : -1
);
*/
await createDir(FOLDER_PATH);
await (async function () {
for (var i = 0; i < keywordLikeability.length; i++) {
if (i < 4) {
await getAnswerFromQuestion(
keywordLikeability[i].url,
queryFolderId,
page
);
}
}
await browser.close();
})();
} catch (error) {
console.log("Error " + error.toString());
}
})();