Get game data from URL

pull/12/head
MillenniumEarl 2020-10-10 11:45:43 +02:00
parent 44d7e126c8
commit 4e2c158a85
2 changed files with 35 additions and 3 deletions

View File

@ -7,7 +7,7 @@ const fs = require("fs");
const shared = require("./scripts/shared.js"); const shared = require("./scripts/shared.js");
const constURLs = require("./scripts/constants/urls.js"); const constURLs = require("./scripts/constants/urls.js");
const selectors = require("./scripts/constants/css-selectors.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 gameScraper = require("./scripts/game-scraper.js");
const { const {
prepareBrowser, prepareBrowser,
@ -238,6 +238,37 @@ module.exports.getGameData = async function (name, includeMods) {
if (shared.isolation) await browser.close(); if (shared.isolation) await browser.close();
return result; 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<GameInfo>} 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 * @public
* Gets the data of the currently logged in user. * Gets the data of the currently logged in user.

View File

@ -17,7 +17,8 @@ const { isStringAValidURL, isF95URL, urlExists } = require("./urls-helper.js");
* Get information from the game's main page. * Get information from the game's main page.
* @param {puppeteer.Browser} browser Browser object used for navigation * @param {puppeteer.Browser} browser Browser object used for navigation
* @param {String} url URL (String) of the game/mod to extract data from * @param {String} url URL (String) of the game/mod to extract data from
* @return {Promise<GameInfo>} Complete information about the game you are looking for * @return {Promise<GameInfo>} Complete information about the game you are
* looking for or null if the URL doesn't exists
*/ */
module.exports.getGameInfo = async function (browser, url) { module.exports.getGameInfo = async function (browser, url) {
if (shared.debug) console.log("Obtaining game info"); 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 // Verify the correctness of the URL
if (!isF95URL(url)) throw url + " is not a valid F95Zone URL"; if (!isF95URL(url)) throw url + " is not a valid F95Zone URL";
let exists = await urlExists(url); let exists = await urlExists(url);
if (!exists) return new GameInfo(); if (!exists) return null;
let page = await preparePage(browser); // Set new isolated page let page = await preparePage(browser); // Set new isolated page
await page.setCookie(...shared.cookies); // Set cookies to avoid login await page.setCookie(...shared.cookies); // Set cookies to avoid login