From c87c426a1fc32d075a0b90b46a1f2c44e14e0d99 Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Thu, 25 Feb 2021 12:06:38 +0100 Subject: [PATCH] Set as unique AP for searching handiworks --- src/scripts/search.ts | 97 ++++++++++--------------------------------- 1 file changed, 21 insertions(+), 76 deletions(-) diff --git a/src/scripts/search.ts b/src/scripts/search.ts index 9cd0907..52ef777 100644 --- a/src/scripts/search.ts +++ b/src/scripts/search.ts @@ -1,88 +1,33 @@ "use strict"; -// Public modules from npm -import cheerio from "cheerio"; - // Modules from file -import { fetchHTML } from "./network-helper.js"; -import shared from "./shared.js"; -import { selectors as f95Selector } from "./constants/css-selector.js"; -import { urls as f95urls } from "./constants/url.js"; +import { IBasic } from "./interfaces.js"; +import HandiworkSearchQuery from "./classes/query/handiwork-search-query.js"; +import LatestSearchQuery from "./classes/query/latest-search-query.js"; +import ThreadSearchQuery from "./classes/query/thread-search-query.js"; +import { getPostInformation } from "./scrape-data/scrape-thread.js"; +import executeQuery from "./fetch-data/fetch-query.js"; -//#region Public methods -/** - * Search for a game on F95Zone and return a list of URLs, one for each search result. - * @returns {Promise} URLs of results - */ -export async function searchGame(name: string): Promise { - shared.logger.info(`Searching games with name ${name}`); +export async function search(query: LatestSearchQuery, limit: number): Promise - // Replace the whitespaces with + - const searchName = encodeURIComponent(name.toUpperCase()); - - // Prepare the URL (only title, search in the "Games" section, order by relevance) - const url = `https://f95zone.to/search/83456043/?q="${searchName}"&t=post&c[child_nodes]=1&c[nodes][0]=2&c[title_only]=1&o=relevance`; +export async function search(query: HandiworkSearchQuery, limit: number): Promise - // Fetch and parse the result URLs - return await fetchResultURLs(url); -}; +export async function search(query: ThreadSearchQuery, limit: number): Promise /** - * Search for a mod on F95Zone and return a list of URLs, one for each search result. - * @returns {Promise} URLs of results + * Gets the handiworks that match the passed parameters. + * You *must* be logged. + * @param {Number} limit + * Maximum number of items to get. Default: 30 */ -export async function searchMod(name: string): Promise { - shared.logger.info(`Searching mods with name ${name}`); - - // Replace the whitespaces with + - const searchName = encodeURIComponent(name.toUpperCase()); +export default async function search(query: any, limit: number = 30): Promise { + // Fetch the URLs + const urls: string[] = await executeQuery(query, limit); - // Prepare the URL (only title, search in the "Mods" section, order by relevance) - const url = `https://f95zone.to/search/83459796/?q="${searchName}"&t=post&c[child_nodes]=1&c[nodes][0]=41&c[title_only]=1&o=relevance`; + // Fetch the data + const results = urls.map((url, idx) => { + return getPostInformation(url); + }); - // Fetch and parse the result URLs - return await fetchResultURLs(url); -}; -//#endregion Public methods - -//#region Private methods -/** - * Gets the URLs of the threads resulting from the F95Zone search. - * @return {Promise} List of URLs - */ -async function fetchResultURLs(url: string): Promise { - shared.logger.trace(`Fetching ${url}...`); - - // Fetch HTML and prepare Cheerio - const html = await fetchHTML(url); - const $ = cheerio.load(html); - - // Here we get all the DIV that are the body of the various query results - const results = $("body").find(f95Selector.GS_RESULT_BODY); - - // Than we extract the URLs - const urls = results.map((idx, el) => { - const elementSelector = $(el); - return extractLinkFromResult(elementSelector); - }).get(); - - return urls; + return Promise.all(results); } - -/** - * Look for the URL to the thread referenced by the item. - * @param {cheerio.Cheerio} selector Element to search - * @returns {String} URL to thread - */ -function extractLinkFromResult(selector: cheerio.Cheerio): string { - shared.logger.trace("Extracting thread link from result..."); - - const partialLink = selector - .find(f95Selector.GS_RESULT_THREAD_TITLE) - .attr("href") - .trim(); - - // Compose and return the URL - return new URL(partialLink, f95urls.F95_BASE_URL).toString(); -} -//#endregion Private methods