F95API/src/example.ts

80 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-11-02 14:46:07 +00:00
/*
to use this example, create an .env file
in the project root with the following values:
F95_USERNAME = YOUR_USERNAME
F95_PASSWORD = YOUR_PASSWORD
*/
2020-10-31 15:00:26 +00:00
"use strict";
2020-09-29 15:11:43 +00:00
// Public modules from npm
2021-02-15 21:26:18 +00:00
import dotenv from "dotenv";
2020-10-31 15:00:26 +00:00
// Modules from file
2021-03-03 19:18:14 +00:00
import { login,
getUserData,
getLatestUpdates,
LatestSearchQuery,
Game,
searchHandiwork,
HandiworkSearchQuery
} from "./index.js";
// Configure the .env reader
dotenv.config();
2020-09-29 15:11:43 +00:00
2020-11-02 09:21:36 +00:00
main();
2020-11-02 09:21:36 +00:00
async function main() {
// Local variables
const gameList = [
2020-11-09 16:53:29 +00:00
"Four Elements Trainer",
2020-11-02 09:21:36 +00:00
"corrupted kingdoms",
2020-11-09 16:53:29 +00:00
"summertime saga"
2020-11-02 09:21:36 +00:00
];
2020-11-02 09:21:36 +00:00
// Log in the platform
console.log("Authenticating...");
2021-02-15 21:26:18 +00:00
const result = await login(process.env.F95_USERNAME, process.env.F95_PASSWORD);
2020-11-30 09:40:35 +00:00
console.log(`Authentication result: ${result.message}\n`);
2020-11-02 09:21:36 +00:00
// Get user data
console.log("Fetching user data...");
2021-02-15 21:26:18 +00:00
const userdata = await getUserData();
2021-03-03 19:18:14 +00:00
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`);
2020-11-02 09:21:36 +00:00
2020-11-30 09:40:35 +00:00
// Get latest game update
2021-03-03 19:18:14 +00:00
const latestQuery: LatestSearchQuery = new LatestSearchQuery();
latestQuery.category = "games";
latestQuery.includedTags = ["3d game"];
const latestUpdates = await getLatestUpdates<Game>(latestQuery, 1);
console.log(`"${latestUpdates.shift().name}" was the last "3d game" tagged game to be updated\n`);
2020-11-30 09:40:35 +00:00
// Get game data
2020-11-02 09:21:36 +00:00
for(const gamename of gameList) {
console.log(`Searching '${gamename}'...`);
2021-03-03 19:18:14 +00:00
// Prepare the query
const query: HandiworkSearchQuery = new HandiworkSearchQuery();
2021-03-03 19:28:22 +00:00
query.category = "games";
2021-03-03 19:18:14 +00:00
query.keywords = gamename;
// Fetch the first result
const result = await searchHandiwork<Game>(query, 1);
// No game found
if (result.length === 0) {
2020-11-02 09:21:36 +00:00
console.log(`No data found for '${gamename}'`);
continue;
}
2020-10-31 15:00:26 +00:00
2020-11-02 09:21:36 +00:00
// Extract first game
2021-03-03 19:18:14 +00:00
const gamedata = result.shift();
const authors = gamedata.authors.map((a, idx) => a.name).join(", ");
console.log(`Found: ${gamedata.name} (${gamedata.version}) by ${authors}\n`);
2020-10-29 21:14:40 +00:00
}
}