From c88c9720a274097319d169354c736da367a59a30 Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Mon, 2 Nov 2020 10:21:36 +0100 Subject: [PATCH] Bugfix --- app/index.js | 2 +- app/scripts/scraper.js | 3 +- test/user-test.js | 72 ++++++++++++++++++------------------------ 3 files changed, 33 insertions(+), 44 deletions(-) diff --git a/app/index.js b/app/index.js index d1b16d8..4ceb06e 100644 --- a/app/index.js +++ b/app/index.js @@ -122,7 +122,7 @@ module.exports.getGameData = async function (name, mod) { const results = []; for (const url of urls) { // Start looking for information - const info = scraper.getGameInfo(url); + const info = await scraper.getGameInfo(url); results.push(info); } return results; diff --git a/app/scripts/scraper.js b/app/scripts/scraper.js index e5cd538..129c90c 100644 --- a/app/scripts/scraper.js +++ b/app/scripts/scraper.js @@ -305,10 +305,11 @@ function extractStructuredData(body) { * Different processing depending on whether the game is a mod or not. * @param {String} text Structured text extracted from the game's web page * @param {Boolean} mod Specify if it is a game or a mod - * @returns {Promise} Game description + * @returns {String} Game description */ function getOverview(text, mod) { shared.logger.trace("Extracting game overview..."); + // Get overview (different parsing for game and mod) const overviewEndIndex = mod ? text.indexOf("Updated") : text.indexOf("Thread Updated"); return text.substring(0, overviewEndIndex).replace("Overview:\n", "").trim(); diff --git a/test/user-test.js b/test/user-test.js index 5ec4fbc..804fa2d 100644 --- a/test/user-test.js +++ b/test/user-test.js @@ -4,58 +4,46 @@ const dotenv = require("dotenv"); // Modules from file -const searcher = require("../app/scripts/searcher.js"); -const scraper = require("../app/scripts/scraper.js"); -const Credentials = require("../app/scripts/classes/credentials.js"); -const networkHelper = require("../app/scripts/network-helper.js"); -const uScraper = require("../app/scripts/user-scraper.js"); +const F95API = require("../app/index.js"); // Configure the .env reader dotenv.config(); -// Login -auth().then(async function searchGames(result) { - if(!result) return; +main(); + +async function main() { + // Local variables + const gameList = [ + "kingdom of deception", + "perverted education", + "corrupted kingdoms", + "summertime saga", + "brothel king" + ]; + + // Log in the platform + console.log("Authenticating..."); + const result = await F95API.login(process.env.F95_USERNAME, process.env.F95_PASSWORD); + console.log(`Authentication result: ${result.message}`); + + // Get user data console.log("Fetching user data..."); - const userdata = await uScraper.getUserData(); + const userdata = await F95API.getUserData(); console.log(`${userdata.username} follows ${userdata.watchedThreads.length} threads`); - // Search for Kingdom Of Deception data - await search("kingdom of deception"); + for(const gamename of gameList) { + console.log(`Searching '${gamename}'...`); + const found = await F95API.getGameData(gamename, false); - // Search for Perverted Education data - await search("perverted education"); + // If no game is found + if (found.length === 0) { + console.log(`No data found for '${gamename}'`); + continue; + } - // Search for Corrupted Kingdoms data - await search("corrupted kingdoms"); - - // Search for Summertime Saga data - await search("summertime saga"); -}); - -async function auth() { - console.log("Token fetch..."); - const creds = new Credentials(process.env.F95_USERNAME, process.env.F95_PASSWORD); - await creds.fetchToken(); - console.log(`Token obtained: ${creds.token}`); - - console.log("Authenticating..."); - const result = await networkHelper.autenticate(creds); - console.log(`Authentication result: ${result.message}`); - - return result.success; -} - -async function search(gamename) { - console.log(`Searching '${gamename}'...`); - const urls = await searcher.searchGame(gamename); - console.log(`Found: ${urls}`); - - console.log("Scraping data..."); - for (const url of urls) { - const gamedata = await scraper.getGameInfo(url); + // Extract first game + const gamedata = found[0]; console.log(`Found ${gamedata.name} (${gamedata.version}) by ${gamedata.author}`); } - console.log("Scraping completed!"); }