Set as unique AP for searching handiworks

pull/73/head
MillenniumEarl 2021-02-25 12:06:38 +01:00
parent 3c34b63cb6
commit c87c426a1f
1 changed files with 21 additions and 76 deletions

View File

@ -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<String[]>} URLs of results
*/
export async function searchGame(name: string): Promise<string[]> {
shared.logger.info(`Searching games with name ${name}`);
export async function search<T extends IBasic>(query: LatestSearchQuery, limit: number): Promise<T[]>
// 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<T extends IBasic>(query: HandiworkSearchQuery, limit: number): Promise<T[]>
// Fetch and parse the result URLs
return await fetchResultURLs(url);
};
export async function search<T extends IBasic>(query: ThreadSearchQuery, limit: number): Promise<T[]>
/**
* Search for a mod on F95Zone and return a list of URLs, one for each search result.
* @returns {Promise<String[]>} 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<string[]> {
shared.logger.info(`Searching mods with name ${name}`);
// Replace the whitespaces with +
const searchName = encodeURIComponent(name.toUpperCase());
export default async function search<T extends IBasic>(query: any, limit: number = 30): Promise<T[]> {
// 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<T>(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<String[]>} List of URLs
*/
async function fetchResultURLs(url: string): Promise<string[]> {
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