From c153eca68c7746e315fbf0d8fa6d1cbbda737dcd Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Sat, 20 Mar 2021 12:32:37 +0100 Subject: [PATCH] Update example --- src/example.ts | 71 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/src/example.ts b/src/example.ts index 0bc77aa..00aa735 100644 --- a/src/example.ts +++ b/src/example.ts @@ -28,16 +28,12 @@ import { LatestSearchQuery, Game, searchHandiwork, - HandiworkSearchQuery, - Asset, - getHandiworkFromURL + HandiworkSearchQuery } from "./index"; // Configure the .env reader dotenv.config(); -main(); - /** * Ask the user to enter the OTP code * necessary to authenticate on the server. @@ -56,39 +52,52 @@ async function insert2faCode(): Promise { return answers.code as number; } -async function main() { - // Local variables - const gameList = ["City of broken dreamers", "Seeds of chaos", "MIST"]; - +/** + * Authenticate on the platform. + */ +async function authenticate(): Promise { // Log in the platform console.log("Authenticating..."); const result = await login(process.env.F95_USERNAME, process.env.F95_PASSWORD, insert2faCode); console.log(`Authentication result: ${result.message}\n`); - // Manage failed login - if (!result.success) { - console.log("Failed authentication, impossible to continue"); - return; - } + return result.success; +} - // Get user data +/** + * Fetch and show data of the current logger user. + */ +async function fetchUserData(): Promise { console.log("Fetching user data..."); - const userdata = await getUserData(); - const gameThreads = userdata.watched.filter((e) => e.forum === "Games").length; - console.log( - `${userdata.name} follows ${userdata.watched.length} threads of which ${gameThreads} are games\n` - ); - // Get latest game update + const userdata = await getUserData(); + + const gameThreads = userdata.watched.filter((e) => e.forum === "Games"); + const unread = gameThreads.filter((e) => e.unread).length; + + console.log(`User: ${userdata.name}`); + console.log(`Threads followed: ${userdata.watched.length}`); + console.log(`Games followed: ${gameThreads.length}`); + console.log(`Unread game threads: ${unread}\n`); +} + +/** + * Fetch the data of the latest `3D game` updated. + */ +async function fetchLatestGameInfo(): Promise { const latestQuery: LatestSearchQuery = new LatestSearchQuery(); latestQuery.category = "games"; latestQuery.includedTags = ["3d game"]; const latestUpdates = await getLatestUpdates(latestQuery, 1); console.log(`"${latestUpdates.shift().name}" was the last "3d game" tagged game to be updated\n`); +} - // Get game data - for (const gamename of gameList) { +/** + * Fetch data of the games given theirs names. + */ +async function fetchGameData(games: string[]): Promise { + for (const gamename of games) { console.log(`Searching '${gamename}'...`); // Prepare the query @@ -109,3 +118,19 @@ async function main() { } else console.log(`No data found for '${gamename}'\n`); } } + +async function main() { + if (await authenticate()) { + // Fetch and log user data + await fetchUserData(); + + // Get latest `3D GAME` game updated + await fetchLatestGameInfo(); + + // Get game data + const gameList = ["City of broken dreamers", "Seeds of chaos", "MIST"]; + await fetchGameData(gameList); + } else console.log("Failed authentication, impossible to continue"); +} + +main();