From 4e2c158a85e80beb739d2d7a82c9c6acd92ca45e Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Sat, 10 Oct 2020 11:45:43 +0200 Subject: [PATCH] Get game data from URL --- app/index.js | 33 ++++++++++++++++++++++++++++++++- app/scripts/game-scraper.js | 5 +++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/index.js b/app/index.js index e5dce9f..15e84dc 100644 --- a/app/index.js +++ b/app/index.js @@ -7,7 +7,7 @@ const fs = require("fs"); const shared = require("./scripts/shared.js"); const constURLs = require("./scripts/constants/urls.js"); const selectors = require("./scripts/constants/css-selectors.js"); -const { isStringAValidURL, urlExists } = require("./scripts/urls-helper.js"); +const { isStringAValidURL, urlExists, isF95URL } = require("./scripts/urls-helper.js"); const gameScraper = require("./scripts/game-scraper.js"); const { prepareBrowser, @@ -238,6 +238,37 @@ module.exports.getGameData = async function (name, includeMods) { if (shared.isolation) await browser.close(); return result; }; +/** + * @public + * Starting from the url, it gets all the information about the game you are looking for. + * You **must** be logged in to the portal before calling this method. + * @param {String} url URL of the game to obtain information of + * @returns {Promise} Information about the game. If no game was found, null is returned + */ +module.exports.getGameDataFromURL = async function (url) { + if (!shared.isLogged) { + console.warn("user not authenticated, unable to continue"); + return null; + } + + // Check URL + if(!urlExists(url)) return null; + if (!isF95URL(url)) throw url + " is not a valid F95Zone URL"; + + // Gets the search results of the game being searched for + let browser = null; + if (shared.isolation) browser = await prepareBrowser(); + else { + if (_browser === null) _browser = await prepareBrowser(); + browser = _browser; + } + + // Get game data + let result = await gameScraper.getGameInfo(browser, url); + + if (shared.isolation) await browser.close(); + return result; +}; /** * @public * Gets the data of the currently logged in user. diff --git a/app/scripts/game-scraper.js b/app/scripts/game-scraper.js index d12081e..da01a47 100644 --- a/app/scripts/game-scraper.js +++ b/app/scripts/game-scraper.js @@ -17,7 +17,8 @@ const { isStringAValidURL, isF95URL, urlExists } = require("./urls-helper.js"); * Get information from the game's main page. * @param {puppeteer.Browser} browser Browser object used for navigation * @param {String} url URL (String) of the game/mod to extract data from - * @return {Promise} Complete information about the game you are looking for + * @return {Promise} Complete information about the game you are + * looking for or null if the URL doesn't exists */ module.exports.getGameInfo = async function (browser, url) { if (shared.debug) console.log("Obtaining game info"); @@ -25,7 +26,7 @@ module.exports.getGameInfo = async function (browser, url) { // Verify the correctness of the URL if (!isF95URL(url)) throw url + " is not a valid F95Zone URL"; let exists = await urlExists(url); - if (!exists) return new GameInfo(); + if (!exists) return null; let page = await preparePage(browser); // Set new isolated page await page.setCookie(...shared.cookies); // Set cookies to avoid login