processed commited on
Commit
3c9a18e
·
verified ·
1 Parent(s): 03650a3

Create iask.js

Browse files
Files changed (1) hide show
  1. iask.js +86 -0
iask.js ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const { run } = require('shannz-playwright');
2
+
3
+ async function iask(query) {
4
+ const code = `const { chromium } = require('playwright');
5
+
6
+ async function iask(query) {
7
+ const browser = await chromium.launch();
8
+ const page = await browser.newPage();
9
+
10
+ try {
11
+ await page.goto(\`https://iask.ai/?mode=question&q=\${query}\`);
12
+ await page.waitForSelector('.mt-6.md\\\\:mt-4.w-full.p-px.relative.self-center.flex.flex-col.items-center.results-followup', { timeout: 0 });
13
+
14
+ const outputDiv = await page.$('#output');
15
+
16
+ if (!outputDiv) {
17
+ return { image: [], answer: null, sources: [], videoSource: [], webSearch: [] };
18
+ }
19
+
20
+ const answerElement = await outputDiv.$('#text');
21
+ const answerText = await answerElement.evaluate(el => el.innerText);
22
+ const [answer, sourcesText] = answerText.split('Top 3 Authoritative Sources Used in Answering this Question');
23
+ const cleanedAnswer = answer.replace(/According to Ask AI & Question AI www\\.iAsk\\.ai:\\s*/, '').trim();
24
+ const sources = sourcesText ? sourcesText.split('\\n').filter(source => source.trim() !== '') : [];
25
+
26
+ const imageElements = await outputDiv.$$('img');
27
+ const images = await Promise.all(imageElements.map(async (img) => {
28
+ return await img.evaluate(img => img.src);
29
+ }));
30
+
31
+ const videoSourceDiv = await page.$('#related-videos');
32
+ const videoSources = [];
33
+ if (videoSourceDiv) {
34
+ const videoElements = await videoSourceDiv.$$('a');
35
+ for (const videoElement of videoElements) {
36
+ const videoLink = await videoElement.evaluate(el => el.href);
37
+ const videoTitle = await videoElement.$eval('h3', el => el.innerText).catch(() => 'No title found');
38
+ const videoThumbnail = await videoElement.$eval('img', el => el.src).catch(() => 'No thumbnail found');
39
+
40
+ if (videoTitle !== 'No title found' && videoThumbnail !== 'No thumbnail found') {
41
+ videoSources.push({ title: videoTitle, link: videoLink, thumbnail: videoThumbnail });
42
+ }
43
+ }
44
+ }
45
+
46
+ const webSearchDiv = await page.$('#related-links');
47
+ const webSearchResults = [];
48
+ if (webSearchDiv) {
49
+ const linkElements = await webSearchDiv.$$('a');
50
+ for (const linkElement of linkElements) {
51
+ const linkUrl = await linkElement.evaluate(el => el.href);
52
+ const linkTitle = await linkElement.evaluate(el => el.innerText);
53
+ const linkImage = await linkElement.$eval('img', el => el.src).catch(() => 'No image found');
54
+ const linkDescription = await linkElement.evaluate(el => el.nextElementSibling.innerText).catch(() => 'No description found');
55
+
56
+ if (linkTitle && linkUrl) {
57
+ webSearchResults.push({
58
+ title: linkTitle,
59
+ link: linkUrl,
60
+ image: linkImage,
61
+ description: linkDescription
62
+ });
63
+ }
64
+ }
65
+ }
66
+
67
+ const src = sources.map(source => source.trim());
68
+ const result = { image: images, answer: cleanedAnswer, sources: src, videoSource: videoSources, webSearch: webSearchResults };
69
+ return JSON.stringify(result, null, 2);
70
+
71
+ } catch (error) {
72
+ console.error('Error fetching data:', error);
73
+ return { image: [], answer: null, sources: [], videoSource: [], webSearch: [] };
74
+ } finally {
75
+ await browser.close();
76
+ }
77
+ }
78
+
79
+ iask(\`${query}\`).then(a => console.log(a));`;
80
+
81
+ const start = await run('javascript', code);
82
+ const string = start.result.output;
83
+ return JSON.parse(string);
84
+ }
85
+
86
+ module.exports = { iask };