-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.js
155 lines (134 loc) · 5.19 KB
/
search.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
const searchQ = require('../dbmodels/queryDb.js');
const { AIData } = require('../dbmodels/aiDb.js');
const { GoogleGenerativeAI } = require("@google/generative-ai");
const axios = require('axios');
const googleTrends = require('google-trends-api');
async function getTrendingTopics() {
const apiKey = process.env.T_API_KEY;
const url = `https://newsapi.org/v2/top-headlines?country=in&apiKey=${apiKey}`;
try {
const response = await axios.get(url);
const articles = response.data.articles;
// console.log(`Trending Topics: ${articles}`);
return articles;
} catch (error) {
console.error('Error fetching trending topics:', error);
}
}
// Replace with your actual API key and Search Engine ID
async function searchRelatedQueries(query) {
const url = `http://suggestqueries.google.com/complete/search?client=firefox&q=${encodeURIComponent(query)}`;
try {
const response = await axios.get(url);
const suggestions = response.data[1]; // The second element of the response contains the suggestions
console.log(suggestions);
return suggestions;
} catch (error) {
console.error('Error fetching search suggestions:');
}
}
// Example usage
async function imageGetter(see,q) {
let cardImage = null;
if (see && see.result && see.result.data && see.result.data.items && see.result.data.items.length > 0) {
let yeah = false;
see.result.data.items.forEach(function (sees) {
if (sees.pagemap.hcard) {
yeah = true;
}
});
if (yeah) {
console.log('Card Images Added');
return cardImage = await getreq(q);
}
}
}
async function getreq(q) {
try {
let subscriptionKey = process.env.KEY;
let host = 'api.bing.microsoft.com';
let path = '/v7.0/images/search';
let term = q;
const response = await axios.get(`https://${host}${path}?q=${encodeURIComponent(term)}`, {
headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey,
}
});
return response.data;
} catch (error) {
console.error('Error:', error.response.data);
throw new Error('Failed to fetch data from the API.');
}
}
module.exports.index = async (req, res) => {
const trends = await getTrendingTopics();
res.render('main/index.ejs', { trends });
};
module.exports.searchIndex = async (req, res) => {
let { q } = req.body.search;
console.log(q);
if (req.file) {
let url = req.file.path;
let filename = req.file.filename;
console.log(url, filename);
}
q = q.toLowerCase();
console.log(q);
let see = await searchQ.findOne({ "result.query": q }); // Use findOne instead of find
console.log(see);
if (see && see.result && see.result.query) {
const condition = see.result.query;
console.log(condition);
if (q == condition) {
console.log("Condition 1.1 Triggered");
let suggestions = await searchRelatedQueries(q);
//AI Code to be written in searchResult.ejs
const prompt = q;
let cardImage = await imageGetter(see,q);
// res.send(see);
res.render('main/searchresult.ejs', { cardImage, suggestions, see, q });
// Update the database with fresh data from API for future searches
const apiKey = process.env.SEARCH_API_KEY;
const cx = process.env.SEARCH_ID;
const apiUrl = `https://www.googleapis.com/customsearch/v1?key=${apiKey}&cx=${cx}&q=${encodeURIComponent(q)}&safe=active`;
const searchResponse = await axios.get(apiUrl);
const ros = searchResponse.data;
console.log(see);
// Update existing document with new data
see.result.data = ros;
await see.save();
console.log("Database updated with fresh data for query:", q);
}
} else {
const see = new searchQ({
query: q,
result: {
query: q
},
});
console.log(see);
const apiKey = process.env.SEARCH_API_KEY;
const cx = process.env.SEARCH_ID;
const apiUrl = `https://www.googleapis.com/customsearch/v1?key=${apiKey}&cx=${cx}&q=${encodeURIComponent(q)}&safe=active`;
const searchResponse = await axios.get(apiUrl);
const ros = searchResponse.data;
see.result.data = ros;
let suggestions = await searchRelatedQueries(q);
let cardImage = await imageGetter(see,q);
await see.save();
console.log("Condition 2 Triggered");
res.render('main/searchresult.ejs', { cardImage, suggestions, see, q });
}
};
module.exports.imageSearch = async (req, res) => {
let { q } = req.body;
console.log("Search query:", q); // Debugging statement
if (req.file) {
let url = req.file.path;
let filename = req.file.filename;
console.log("File uploaded:", filename); // Debugging statement
}
q = q.toLowerCase();
const images = await getreq(q); // assuming getreq is an asynchronous function
res.render('main/imagesearch.ejs', { images, q });
}