Merge branch 'master' into deepsource-fix-fd03307c

pull/29/head
Millennium Earl 2020-10-16 09:49:02 +02:00 committed by GitHub
commit 0f5c82751e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 39 deletions

View File

@ -268,7 +268,7 @@ module.exports.getGameDataFromURL = async function (url) {
// Check URL
if (!urlExists(url)) return null;
if (!isF95URL(url)) throw url + " is not a valid F95Zone URL";
if (!isF95URL(url)) throw new Error(url + " is not a valid F95Zone URL");
// Gets the search results of the game being searched for
if (_browser === null && !shared.isolation) _browser = await prepareBrowser();
@ -508,9 +508,8 @@ async function loginF95(browser, username, password) {
const c = await page.cookies();
fs.writeFileSync(shared.cookiesCachePath, JSON.stringify(c));
result.message = "Authentication successful";
}
// Obtain the error message
else if (
} else if (
// Obtain the error message
await page.evaluate(
/* istanbul ignore next */ (selector) =>
document.querySelector(selector) !== null,

View File

@ -4,17 +4,17 @@
* Object obtained in response to an attempt to login to the portal.
*/
class LoginResult {
constructor() {
/**
* Result of the login operation
* @type Boolean
*/
this.success = false;
/**
* Login response message
* @type String
*/
this.message = '';
}
constructor() {
/**
* Result of the login operation
* @type Boolean
*/
this.success = false;
/**
* Login response message
* @type String
*/
this.message = '';
}
}
module.exports = LoginResult;

View File

@ -1,7 +1,7 @@
module.exports = Object.freeze({
F95_BASE_URL: 'https://f95zone.to',
F95_SEARCH_URL: 'https://f95zone.to/search',
F95_LATEST_UPDATES: 'https://f95zone.to/latest',
F95_LOGIN_URL: 'https://f95zone.to/login',
F95_WATCHED_THREADS: 'https://f95zone.to/watched/threads',
F95_BASE_URL: 'https://f95zone.to',
F95_SEARCH_URL: 'https://f95zone.to/search',
F95_LATEST_UPDATES: 'https://f95zone.to/latest',
F95_LOGIN_URL: 'https://f95zone.to/login',
F95_WATCHED_THREADS: 'https://f95zone.to/watched/threads',
});

View File

@ -2,15 +2,21 @@
// Public modules from npm
const HTMLParser = require("node-html-parser");
const puppeteer = require("puppeteer");
const puppeteer = require("puppeteer"); // skipcq: JS-0128
// Modules from file
const shared = require("./shared.js");
const selectors = require("./constants/css-selectors.js");
const { preparePage } = require("./puppeteer-helper.js");
const {
preparePage
} = require("./puppeteer-helper.js");
const GameDownload = require("./classes/game-download.js");
const GameInfo = require("./classes/game-info.js");
const { isStringAValidURL, isF95URL, urlExists } = require("./urls-helper.js");
const {
isStringAValidURL,
isF95URL,
urlExists
} = require("./urls-helper.js");
/**
* @protected
@ -24,7 +30,7 @@ module.exports.getGameInfo = async function (browser, url) {
if (shared.debug) console.log("Obtaining game info");
// Verify the correctness of the URL
if (!isF95URL(url)) throw url + " is not a valid F95Zone URL";
if (!isF95URL(url)) throw new Error(url + " is not a valid F95Zone URL");
const exists = await urlExists(url);
if (!exists) return null;
@ -130,7 +136,8 @@ async function getMainPostStructuredText(page) {
// The info are plain text so we need to parse the HTML code
const bodyHTML = await page.evaluate(
/* istanbul ignore next */ (mainPost) => mainPost.innerHTML,
/* istanbul ignore next */
(mainPost) => mainPost.innerHTML,
post
);
return HTMLParser.parse(bodyHTML).structuredText;
@ -145,8 +152,9 @@ async function getMainPostStructuredText(page) {
async function getGameAuthor(page) {
// Get the game/mod name (without square brackets)
const titleHTML = await page.evaluate(
/* istanbul ignore next */ (selector) =>
document.querySelector(selector).innerHTML,
/* istanbul ignore next */
(selector) =>
document.querySelector(selector).innerHTML,
selectors.GAME_TITLE
);
const structuredTitle = HTMLParser.parse(titleHTML);
@ -194,7 +202,8 @@ function parseConversationPage(text) {
*/
async function getGamePreviewSource(page) {
const src = await page.evaluate(
/* istanbul ignore next */ (selector) => {
/* istanbul ignore next */
(selector) => {
// Get the firs image available
const img = document.querySelector(selector);
@ -217,8 +226,9 @@ async function getGamePreviewSource(page) {
async function getGameTitle(page) {
// Get the game/mod name (without square brackets)
const titleHTML = await page.evaluate(
/* istanbul ignore next */ (selector) =>
document.querySelector(selector).innerHTML,
/* istanbul ignore next */
(selector) =>
document.querySelector(selector).innerHTML,
selectors.GAME_TITLE
);
const structuredTitle = HTMLParser.parse(titleHTML);
@ -241,7 +251,8 @@ async function getGameTags(page) {
// Get the game tags
for (const handle of await page.$$(selectors.GAME_TAGS)) {
const tag = await page.evaluate(
/* istanbul ignore next */ (element) => element.innerText,
/* istanbul ignore next */
(element) => element.innerText,
handle
);
tags.push(tag.toUpperCase());
@ -264,7 +275,8 @@ async function parsePrefixes(page, info) {
info.status = "Ongoing";
for (const handle of await page.$$(selectors.GAME_TITLE_PREFIXES)) {
const value = await page.evaluate(
/* istanbul ignore next */ (element) => element.innerText,
/* istanbul ignore next */
(element) => element.innerText,
handle
);
@ -308,6 +320,7 @@ async function getLastChangelog(page) {
* @param {puppeteer.Page} page Page containing the links to be extrapolated
* @returns {Promise<GameDownload[]>} List of objects used for game download
*/
// skipcq: JS-0128
async function getGameDownloadLink(page) {
// Most used hosting platforms
const hostingPlatforms = [
@ -335,7 +348,8 @@ async function getGameDownloadLink(page) {
if (container !== null) break;
const upperText = (
await page.evaluate(
/* istanbul ignore next */ (e) => e.innerText,
/* istanbul ignore next */
(e) => e.innerText,
candidate
)
).toUpperCase();
@ -353,7 +367,8 @@ async function getGameDownloadLink(page) {
// Extract the HTML text from the container
const searchText = (
await page.evaluate(
/* istanbul ignore next */ (e) => e.innerHTML,
/* istanbul ignore next */
(e) => e.innerHTML,
container
)
).toLowerCase();
@ -396,8 +411,8 @@ function extractGameHostingData(platform, text) {
// Find the end of the container
if (endIndex === -1)
endIndex =
text.indexOf(CONTAINER_SPAN_CLOSE, startIndex) +
CONTAINER_SPAN_CLOSE.length;
text.indexOf(CONTAINER_SPAN_CLOSE, startIndex) +
CONTAINER_SPAN_CLOSE.length;
text = text.substring(startIndex, endIndex);

View File

@ -1,7 +1,7 @@
"use strict";
// Public modules from npm
const puppeteer = require("puppeteer");
const puppeteer = require("puppeteer"); // skipcq: JS-0128
// Modules from file
const shared = require("./shared.js");

View File

@ -27,7 +27,7 @@ module.exports.isF95URL = function (url) {
*/
module.exports.isStringAValidURL = function (url) {
try {
new URL(url);
new URL(url); // skipcq: JS-0078
return true;
} catch (err) {
return false;