Better and cleaner game search

pull/41/head
samuele.berlusconi 2020-10-24 20:52:29 +02:00
parent 63a7a9a3a0
commit d24593a04a
5 changed files with 19 additions and 36 deletions

View File

@ -13,7 +13,8 @@ module.exports = Object.freeze({
ONLY_GAMES_THREAD_OPTION: 'select[name="nodes[]"] > option[value="2"]',
PASSWORD_INPUT: 'input[name="password"]',
SEARCH_BUTTON: "form.block > * button.button--icon--search",
SEARCH_FORM_TEXTBOX: 'input[name="keywords"]',
SEARCH_FORM_TEXTBOX: 'input[name="keywords"][type="search"]',
SEARCH_ONLY_GAMES_OPTION: 'select[name="c[nodes][]"] > option[value="1"]',
STATUS_ID_SELECTOR: 'div[id^="btn-prefix_4_"]>span',
THREAD_POSTS:
"article.message-body:first-child > div.bbWrapper:first-of-type",

View File

@ -1,6 +1,6 @@
module.exports = Object.freeze({
F95_BASE_URL: "https://f95zone.to",
F95_SEARCH_URL: "https://f95zone.to/search",
F95_SEARCH_URL: "https://f95zone.to/search/?type=post",
F95_LATEST_UPDATES: "https://f95zone.to/latest",
F95_LOGIN_URL: "https://f95zone.to/login",
F95_WATCHED_THREADS: "https://f95zone.to/watched/threads",

View File

@ -25,9 +25,9 @@ module.exports.getGameInfo = async function (browser, url) {
// Verify the correctness of the URL
const exists = await urlHelper.urlExists(url);
if (!exists) throw new URIError(url + " is not a valid URL");
if (!exists) throw new URIError(`${url} is not a valid URL`);
if (!urlHelper.isF95URL(url))
throw new Error(url + " is not a valid F95Zone URL");
throw new Error(`${url} is not a valid F95Zone URL`);
const page = await preparePage(browser); // Set new isolated page
await page.setCookie(...shared.cookies); // Set cookies to avoid login
@ -45,7 +45,6 @@ module.exports.getGameInfo = async function (browser, url) {
info = await parsePrefixes(page, info); // Fill status/engines/isMod
const structuredText = await getMainPostStructuredText(page);
const overview = getOverview(structuredText, info.isMod);
const parsedInfos = parseConversationPage(structuredText);
const previewSource = getGamePreviewSource(page);
const changelog = getLastChangelog(page);
@ -283,10 +282,8 @@ async function getGameTags(page) {
* @returns {Promise<GameInfo>} GameInfo object passed in to which the identified information has been added
*/
async function parsePrefixes(page, info) {
const MOD_PREFIX = "MOD";
// The 'Ongoing' status is not specified, only 'Abandoned'/'OnHold'/'Complete'
info.status = "Ongoing";
info.status = "ONGOING";
for (const handle of await page.$$(selectorK.GAME_TITLE_PREFIXES)) {
const value = await page.evaluate(
/* istanbul ignore next */
@ -298,10 +295,10 @@ async function parsePrefixes(page, info) {
const prefix = value.toUpperCase().replace("[", "").replace("]", "").trim();
// Getting infos...
if (shared.statuses.includes(prefix)) info.status = capitalize(prefix);
else if (shared.engines.includes(prefix)) info.engine = capitalize(prefix);
if (shared.statuses.includes(prefix)) info.status = prefix;
else if (shared.engines.includes(prefix)) info.engine = prefix;
// This is not a game but a mod
else if (prefix === MOD_PREFIX) info.isMod = true;
else if (prefix === "MOD" || prefix === "CHEAT MOD") info.isMod = true;
}
return info;
}
@ -467,11 +464,4 @@ function extractGameHostingData(platform, text) {
return downloadData;
}
/**
* Capitalize a string
* @param {String} string
*/
function capitalize(string) {
return string.toLowerCase().charAt(0).toUpperCase() + string.slice(1);
}
//#endregion Private methods

View File

@ -18,7 +18,7 @@ const { isF95URL } = require("./url-helper.js");
* @returns {Promise<String[]>} List of URL of possible games obtained from the preliminary research on the F95 portal
*/
module.exports.getSearchGameResults = async function (browser, gamename) {
shared.logger.info("Searching " + gamename + " on F95Zone");
shared.logger.info(`Searching ${gamename} on F95Zone`);
const page = await preparePage(browser); // Set new isolated page
await page.setCookie(...shared.cookies); // Set cookies to avoid login
@ -30,11 +30,13 @@ module.exports.getSearchGameResults = async function (browser, gamename) {
await Promise.all([
page.waitForSelector(selectorK.SEARCH_FORM_TEXTBOX),
page.waitForSelector(selectorK.TITLE_ONLY_CHECKBOX),
page.waitForSelector(selectorK.SEARCH_ONLY_GAMES_OPTION),
page.waitForSelector(selectorK.SEARCH_BUTTON),
]);
await page.type(selectorK.SEARCH_FORM_TEXTBOX, gamename); // Type the game we desire
await page.click(selectorK.TITLE_ONLY_CHECKBOX); // Select only the thread with the game in the titles
await page.click(selectorK.SEARCH_ONLY_GAMES_OPTION); // Search only games and mod
await Promise.all([
page.click(selectorK.SEARCH_BUTTON), // Execute search
page.waitForNavigation({
@ -52,7 +54,7 @@ module.exports.getSearchGameResults = async function (browser, gamename) {
const gameUrl = await getOnlyGameThreads(page, element);
if (gameUrl !== null) results.push(gameUrl);
}
shared.logger.info("Find " + results.length + " conversations");
shared.logger.info(`Find ${results.length} conversations`);
await page.close(); // Close the page
return results;

View File

@ -1,25 +1,15 @@
const {
debug,
login,
getGameData,
loadF95BaseData,
getUserData,
logout,
} = require("../app/index.js");
const F95API = require("../app/index.js");
debug(true);
F95API.debug(true);
main();
async function main() {
const loginResult = await login("MillenniumEarl", "f9vTcRNuvxj4YpK");
const loginResult = await F95API.login("MillenniumEarl", "f9vTcRNuvxj4YpK");
if (loginResult.success) {
await loadF95BaseData();
const gameData = await getGameData("kingdom of deception", false);
await F95API.loadF95BaseData();
const gameData = await F95API.getGameData("a struggle with sin", false);
console.log(gameData);
// let userData = await getUserData();
// console.log(userData);
}
logout();
F95API.logout();
}