From 34d065f84c8a0dce7e9b27ab09e9168f09d08027 Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Thu, 3 Dec 2020 11:55:06 +0100 Subject: [PATCH] Added prefix parser --- app/index.js | 12 ++-- app/scripts/classes/prefix-parser.js | 72 ++++++++++++++++++++++ app/scripts/classes/tag-parser.js | 92 ---------------------------- 3 files changed, 77 insertions(+), 99 deletions(-) create mode 100644 app/scripts/classes/prefix-parser.js delete mode 100644 app/scripts/classes/tag-parser.js diff --git a/app/index.js b/app/index.js index 73023d0..2054df0 100644 --- a/app/index.js +++ b/app/index.js @@ -13,13 +13,13 @@ const Credentials = require("./scripts/classes/credentials.js"); const GameInfo = require("./scripts/classes/game-info.js"); const LoginResult = require("./scripts/classes/login-result.js"); const UserData = require("./scripts/classes/user-data.js"); -const TagParser = require("./scripts/classes/tag-parser.js"); +const PrefixParser = require("./scripts/classes/prefix-parser.js"); //#region Export classes module.exports.GameInfo = GameInfo; module.exports.LoginResult = LoginResult; module.exports.UserData = UserData; -module.exports.TagParser = TagParser; +module.exports.PrefixParser = PrefixParser; //#endregion Export classes //#region Export properties @@ -187,7 +187,6 @@ module.exports.getUserData = async function () { * Use `0` to select no time limit. * @param {String[]} [args.prefixes] * Prefixes to be included in the search. - * **Currently not supported** * @param {String} [args.sorting] * Method of sorting the results between (default: `date`): * `date`, `likes`, `views`, `name`, `rating` @@ -199,8 +198,7 @@ module.exports.getLatestUpdates = async function(args, limit) { if(limit <= 0) throw new Error("limit must be greater than 0"); // Prepare the parser - const tp = new TagParser(); - tp.fetch(); + const parser = new PrefixParser(); // Get the closest date limit let filterDate = 0; @@ -216,8 +214,8 @@ module.exports.getLatestUpdates = async function(args, limit) { // Fetch the games const query = { - tags: args.tags ? tp.tagsToIDs(args.tags) : [], - prefixes: [], // TODO: Add prefix parser + tags: args.tags ? parser.prefixesToIDs(args.tags) : [], + prefixes: args.prefixes ? parser.prefixesToIDs(args.prefixes) : [], sort: args.sorting ? args.sorting : "date", date: filterDate, }; diff --git a/app/scripts/classes/prefix-parser.js b/app/scripts/classes/prefix-parser.js new file mode 100644 index 0000000..ce04bb6 --- /dev/null +++ b/app/scripts/classes/prefix-parser.js @@ -0,0 +1,72 @@ +"use strict"; + +// Modules from file +const shared = require("../shared.js"); + +/** + * Convert prefixes and platform tags from string to ID and vice versa. + */ +class PrefixParser { + constructor() { + } + + //#region Private methods + /** + * @private + * Gets the key associated with a given value from a dictionary. + * @param {Object} object Dictionary to search + * @param {Any} value Value associated with the key + * @returns {String|undefined} Key found or undefined + */ + _getKeyByValue(object, value) { + return Object.keys(object).find(key => object[key] === value); + } + //#endregion Private methods + + /** + * @public + * Convert a list of prefixes to their respective IDs. + * @param {String[]} prefixes + */ + prefixesToIDs(prefixes) { + const ids = []; + for(const p of prefixes) { + // Check what dict contains the value + let dict = null; + if(Object.values(shared.statuses).includes(p)) dict = shared.statuses; + else if (Object.values(shared.engines).includes(p)) dict = shared.engines; + else if (Object.values(shared.tags).includes(p)) dict = shared.tags; + else if (Object.values(shared.others).includes(p)) dict = shared.others; + else continue; + + // Extract the key from the dict + const key = this._getKeyByValue(dict, p); + if(key) ids.push(parseInt(key)); + } + return ids.sort((a, b) => a - b); // JS sort alphabetically, same old problem + } + + /** + * @public + * It converts a list of IDs into their respective prefixes. + * @param {number[]} ids + */ + idsToPrefixes(ids) { + const prefixes = []; + for(const id of ids) { + // Check what dict contains the key + let dict = null; + if (Object.keys(shared.statuses).includes(id)) dict = shared.statuses; + else if (Object.keys(shared.engines).includes(id)) dict = shared.engines; + else if (Object.keys(shared.tags).includes(id)) dict = shared.tags; + else if (Object.keys(shared.others).includes(id)) dict = shared.others; + else continue; + + // Check if the key exists in the dict + if (id in dict) prefixes.push(dict[id]); + } + return prefixes.sort(); + } +} + +module.exports = PrefixParser; \ No newline at end of file diff --git a/app/scripts/classes/tag-parser.js b/app/scripts/classes/tag-parser.js deleted file mode 100644 index a77da58..0000000 --- a/app/scripts/classes/tag-parser.js +++ /dev/null @@ -1,92 +0,0 @@ -"use strict"; - -// Public modules from npm -const cheerio = require("cheerio"); - -// Modules from file -const { fetchHTML } = require("../network-helper.js"); -const f95Selector = require("../constants/css-selector.js"); -const { F95_LATEST_UPDATES } = require("../constants/url.js"); - -class TagParser { - constructor() { - /** - * Dictionary mapping the keys to the F95API game tags. - * @type Object. - */ - this._tagsDict = {}; - } - - //#region Private methods - /** - * @private - * Gets the key associated with a given value from a dictionary. - * @param {Object} object Dictionary to search - * @param {Any} value Value associated with the key - * @returns {String|undefined} Key found or undefined - */ - _getKeyByValue(object, value) { - return Object.keys(object).find(key => object[key] === value); - } - //#endregion Private methods - - /** - * @public - * Gets all tags, with their IDs, from F95Zone. - */ - async fetch() { - // Clean dictionary - this._tagsDict = {}; - - // Load the HTML - const html = await fetchHTML(F95_LATEST_UPDATES); - const $ = cheerio.load(html); - - // Search for the tags - const unparsedText = $(f95Selector.LU_TAGS_SCRIPT).html().trim(); - const startIndex = unparsedText.indexOf("{"); - const endIndex = unparsedText.lastIndexOf("}"); - const parsedText = unparsedText.substring(startIndex, endIndex + 1); - const data = JSON.parse(parsedText); - - // Extract only the data we need - this._tagsDict = data.tags; - } - - /** - * @public - * Convert a list of tags to their respective IDs. - * @param {String[]} tags - */ - tagsToIDs(tags) { - const ids = []; - for(const tag of tags) { - // Extract the key from the value - const key = this._getKeyByValue(this._tagsDict, tag); - /* istanbul ignore next */ - if(key) ids.push(parseInt(key)); - } - return ids.sort((a, b) => a - b); // JS sort alphabetically, same old problem - } - - /** - * @public - * It converts a list of IDs into their respective tags. - * @param {number[]} ids - */ - idsToTags(ids) { - const tags = []; - for(const id of ids) { - // Check if the key exists in the dict - const exist = id in this._tagsDict; - /* istanbul ignore next */ - if (!exist) continue; - - // Save the value - tags.push(this._tagsDict[id]); - } - return tags.sort(); - } -} - -module.exports = TagParser; \ No newline at end of file