From 9157736e454d94b111cc7620b7e01bd0a5f5ac6a Mon Sep 17 00:00:00 2001 From: Ashley //// Date: Fri, 9 Feb 2024 15:26:34 +0000 Subject: [PATCH] fix search --- .../init/pages-channel-and-download.js | 58 ++++++------------- 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/src/libpoketube/init/pages-channel-and-download.js b/src/libpoketube/init/pages-channel-and-download.js index 36fe32e8..1a3e2fff 100644 --- a/src/libpoketube/init/pages-channel-and-download.js +++ b/src/libpoketube/init/pages-channel-and-download.js @@ -167,40 +167,14 @@ module.exports = function (app, config, renderTemplate) { }); app.get("/im-feeling-lucky", function (req, res) { - const query = req.query.query; - - const search = require("google-it"); - - const getRandomLinkAndRedirect = (query, res) => { - search({ query: `${query}` }).then((results) => { - // Check if there are any results - if (results.length > 0) { - // Get a random index - const randomIndex = Math.floor(Math.random() * results.length); - - // Get the random result object - const randomResult = results[randomIndex]; - - // Get the link from the random result - const randomLink = randomResult.link; - - // Redirect to the random link - res.redirect(randomLink); - } else { - // Handle case when no results are found - res.send("No results found."); - } - }); - }; - - getRandomLinkAndRedirect(query, res); + res.send("WIP") }); app.get("/web", async (req, res) => { const query = req.query.query; const tab = req.query.tab; - const search = require("google-it"); + const search = require("duckduckgo-search"); if (req.query.lucky === "true") { res.redirect("/im-feeling-lucky?query=" + query); @@ -239,18 +213,22 @@ module.exports = function (app, config, renderTemplate) { let continuation = req.query.continuation || ""; try { - search({ query: `${req.query.query}` }).then((results) => { - renderTemplate(res, req, "search-web.ejs", { - j: "", - IsOldWindows, - h: "", - tab, - continuation, - isMobile: req.useragent.isMobile, - results: results, - q: query, - summary: "", - }); + const results = []; // Initialize an array to store search results + + for await (const result of search.text(query)) { + results.push(result); // Push each result to the results array + } + + renderTemplate(res, req, "search-web.ejs", { + j: "", + IsOldWindows, + h: "", + tab, + continuation, + isMobile: req.useragent.isMobile, + results: results, + q: query, + summary: "", }); } catch (error) { console.error(`Error while searching for '${query}':`, error);