File size: 3,191 Bytes
3c9a18e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { run } = require('shannz-playwright');

async function iask(query) {
 const code = `const { chromium } = require('playwright');

 async function iask(query) {
 const browser = await chromium.launch();
 const page = await browser.newPage();

 try {
 await page.goto(\`https://iask.ai/?mode=question&q=\${query}\`);
 await page.waitForSelector('.mt-6.md\\\\:mt-4.w-full.p-px.relative.self-center.flex.flex-col.items-center.results-followup', { timeout: 0 });
 
 const outputDiv = await page.$('#output');

 if (!outputDiv) {
 return { image: [], answer: null, sources: [], videoSource: [], webSearch: [] };
 }

 const answerElement = await outputDiv.$('#text');
 const answerText = await answerElement.evaluate(el => el.innerText);
 const [answer, sourcesText] = answerText.split('Top 3 Authoritative Sources Used in Answering this Question');
 const cleanedAnswer = answer.replace(/According to Ask AI & Question AI www\\.iAsk\\.ai:\\s*/, '').trim();
 const sources = sourcesText ? sourcesText.split('\\n').filter(source => source.trim() !== '') : [];
 
 const imageElements = await outputDiv.$$('img');
 const images = await Promise.all(imageElements.map(async (img) => {
 return await img.evaluate(img => img.src);
 }));

 const videoSourceDiv = await page.$('#related-videos');
 const videoSources = [];
 if (videoSourceDiv) {
 const videoElements = await videoSourceDiv.$$('a');
 for (const videoElement of videoElements) {
 const videoLink = await videoElement.evaluate(el => el.href);
 const videoTitle = await videoElement.$eval('h3', el => el.innerText).catch(() => 'No title found');
 const videoThumbnail = await videoElement.$eval('img', el => el.src).catch(() => 'No thumbnail found');

 if (videoTitle !== 'No title found' && videoThumbnail !== 'No thumbnail found') {
 videoSources.push({ title: videoTitle, link: videoLink, thumbnail: videoThumbnail });
 }
 }
 }

 const webSearchDiv = await page.$('#related-links');
 const webSearchResults = [];
 if (webSearchDiv) {
 const linkElements = await webSearchDiv.$$('a');
 for (const linkElement of linkElements) {
 const linkUrl = await linkElement.evaluate(el => el.href);
 const linkTitle = await linkElement.evaluate(el => el.innerText);
 const linkImage = await linkElement.$eval('img', el => el.src).catch(() => 'No image found');
 const linkDescription = await linkElement.evaluate(el => el.nextElementSibling.innerText).catch(() => 'No description found');

 if (linkTitle && linkUrl) {
 webSearchResults.push({
 title: linkTitle,
 link: linkUrl,
 image: linkImage,
 description: linkDescription
 });
 }
 }
 }
 
 const src = sources.map(source => source.trim());
 const result = { image: images, answer: cleanedAnswer, sources: src, videoSource: videoSources, webSearch: webSearchResults };
 return JSON.stringify(result, null, 2);

 } catch (error) {
 console.error('Error fetching data:', error);
 return { image: [], answer: null, sources: [], videoSource: [], webSearch: [] };
 } finally {
 await browser.close();
 }
 }

 iask(\`${query}\`).then(a => console.log(a));`;

 const start = await run('javascript', code);
 const string = start.result.output;
 return JSON.parse(string);
}

module.exports = { iask };